Banco de Dados¶
Database architecture¶
The SDK uses Android Room Persistence Library as its database layer. Room provides compile-time verification of SQL queries, reduces boilerplate code, and offers better integration with Android architecture components.
The database implementation is internal to the SDK and apps should interact with data through the provided repositories and services. Direct database access is not recommended as the internal structure may change between versions.
Database scope¶
O SDK mantém um par de [servidor, utilizador] numa base de dados isolada. Assim como na versão 1.6.0, o SDK suporta múltiplas contas(pares de [servidor, utilizador]) e informação de cada conta é armazenada numa base de dados isolada. A base de dados apenas é eliminada quando elimina-se a conta.. As base de dados são criadas automaticamente quando um login é feito com sucesso.
Encryption¶
As of SDK version 1.1.0, it is possible to store the data in an encrypted database. The encryption key is generated randomly by the SDK and kept secure. The encryption status (if the database is encrypted or not) can be configured at server level in the android-settings-app. The default status is false: If the app is not installed, the database won't be encrypted.
Durante o primeiro login para um determinado servidor e usuário, o status da criptografia será baixado da API e um banco de dados do tipo fornecido será criado.
Em logins posteriores ou sincronizações de metadados, o SDK baixará novamente o status de criptografia do servidor e, se alterado, criptografará ou descriptografará o banco de dados atual sem perda de dados.
Desempenho de criptografia¶
- Tamanho do banco de dados: o tamanho do banco de dados é aproximadamente o mesmo, independentemente de ser criptografado ou não.
- Velocidade: leituras e gravações são em média 5 a 10% mais lentas usando um banco de dados criptografado.
Import / export¶
The database can be exported and imported in a different device.
One of the main use cases of this functionality is debugging: sometimes it is hard to know the reason for a sync problem or a bug, and it is very useful to replicate the issue in an emulator or a different device.
// Export database
val database = d2.maintenanceModule().databaseImportExport().exportLoggedUserDatabase()
// Import database
val metadata = d2.maintenanceModule().databaseImportExport().importDatabase(database)
// The metadata object contains information about the database (serverUrl, username,...)
// Once the database is imported, it is possible to login as usual
d2.userModule().login("username", "password", "serverUrl")
The export process encrypts the database using ZIP encryption, so the database file can't be read unless the right user credentials are provided.
Things to consider: - The exported file only contains the database, it does not contain file resources (images, icons, files,...). - The receiver device must run an SDK version that is equal or higher than the SDK version used to export the database.