Databases created on Couchbase Lite can share the same hierarchical structure as Capella databases. This makes it easier to sync data between mobile applications and applications built using Capella.
Although the terminology is different, the structure can be mapped to relational database terms:
This structure gives you plenty of choices when it comes to partitioning your data. The most basic structure is to use the single default scope with a single default collection; or you could opt for a structure that allow you to split your collections into logical scopes.
Your first step in using the API must be to call its initializer. An exception is raised if any other API method is invoked before the initializer.
// Initialize the Couchbase Lite system CouchbaseLite.init();
Things to watch for include:
If the named database does not exist in the specified, or default, location then a new one is created
Best Practice is to always specify the path to the database explicitly.
Typically, the default location for Java is the application sandbox .
See also Finding a Database File.
DatabaseConfiguration config = new DatabaseConfiguration(); config.setDirectory(customDir); Database database = new Database(DB_NAME, config);
You are advised to incorporate the closing of all open databases into your application workflow.
Closing a database soon after starting a replication involving it can cause an exception as the asynchronous
Safely Closing a Database pre 2.8Before closing, check that any attached listeners (query/replication/change) indicate they are at least at
|This is an Enterprise Edition feature.|
Couchbase Lite on Java includes the ability to encrypt Couchbase Lite databases. This allows mobile applications to secure the data at rest, when it is being stored on the device. The algorithm used to encrypt the database is 256-bit AES.
DatabaseConfiguration config = new DatabaseConfiguration(); config.setEncryptionKey(new EncryptionKey("PASSWORD")); Database database = new Database(DB_NAME, config);
Couchbase Lite does not persist the key. It is the application’s responsibility to manage the key and store it in a platform-specific secure store such Android’s Keystore.
An encrypted database can only be opened with the same language SDK that was used to encrypt it in the first place. So a database encrypted using the Java SDK, and then exported, is readable only by the Java SDK.
To change an existing encryption key, open the database using its existing encryption-key and use Database.changeEncryptionKey() to set the required new encryption-key value.
To remove encryption, open the database using its existing encryption-key and use Database.changeEncryptionKey() with a null value as the encryption key.
To upgrade an encrypted database see: Upgrade 1.x databases
By default a Couchbase Lite on Java database is created in a directory at the current location called
This location is set by the DatabaseConfiguration method. You should always use it to explicitly set the database location. See the following example for how to do this:
DatabaseConfiguration thisConfig = new DatabaseConfiguration(); thisConfig.setDirectory("yourDBpath"); Database thisDB = new Database("db", thisConfig);
From time to time it may be necessary to perform certain maintenance activities on your database, for example to compact the database file, removing unused documents and blobs no longer referenced by any documents.
This is a resource intensive operation and is not performed automatically. It should be run on-demand using the API. If in doubt, consult Couchbase support.
You should use console logs as your first source of diagnostic information. If the information in the default logging level is insufficient you can focus it on database errors and generate more verbose messages — see: Example 5.
For more on using Couchbase logs — see: Using Logs.