Package com.couchbase.lite
Class Database
- java.lang.Object
-
- com.couchbase.lite.BaseDatabase
-
- com.couchbase.lite.Database
-
public final class Database extends BaseDatabase
A Couchbase Lite database.
-
-
Field Summary
Fields Modifier and Type Field Description static Log
log
Gets the logging controller for the Couchbase Lite library to configure the logging settings and add custom logging.static Prediction
prediction
ENTERPRISE EDITION API
-
Constructor Summary
Constructors Constructor Description Database(java.lang.String name)
Construct a Database with a given name and the default config.Database(java.lang.String name, DatabaseConfiguration config)
Construct a AbstractDatabase with a given name and database config.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ListenerToken
addChangeListener(DatabaseChangeListener listener)
Adds a change listener for the changes that occur in the database.ListenerToken
addChangeListener(java.util.concurrent.Executor executor, DatabaseChangeListener listener)
Adds a change listener for the changes that occur in the database with an executor on which the changes will be posted to the listener.ListenerToken
addDocumentChangeListener(java.lang.String id, DocumentChangeListener listener)
Adds a change listener for the changes that occur to the specified document.ListenerToken
addDocumentChangeListener(java.lang.String id, java.util.concurrent.Executor executor, DocumentChangeListener listener)
Adds a change listener for the changes that occur to the specified document with an executor on which the changes will be posted to the listener.void
changeEncryptionKey(EncryptionKey encryptionKey)
ENTERPRISE EDITION APIvoid
close()
Closes a database.static void
copy(java.io.File path, java.lang.String name, DatabaseConfiguration config)
Make a copy of a database in a new location.void
createIndex(java.lang.String name, Index index)
void
createIndex(java.lang.String name, com.couchbase.lite.IndexConfiguration config)
Query
createQuery(java.lang.String query)
void
delete()
Deletes a database.void
delete(Document document)
Deletes a document from the database.boolean
delete(Document document, ConcurrencyControl concurrencyControl)
Deletes a document from the database.static void
delete(java.lang.String name, java.io.File directory)
Deletes a database of the given name in the given directory.void
deleteIndex(java.lang.String name)
static boolean
exists(java.lang.String name, java.io.File directory)
Checks whether a database of the given name exists in the given directory or not.Blob
getBlob(java.util.Map<java.lang.String,java.lang.Object> props)
DatabaseConfiguration
getConfig()
Returns a copy of the database configuration.long
getCount()
The number of documents in the database.Document
getDocument(java.lang.String id)
Gets an existing Document object with the given ID.java.util.Date
getDocumentExpiration(java.lang.String id)
Returns the expiration time of the document.java.util.List<java.lang.String>
getIndexes()
java.lang.String
getName()
Return the database namejava.lang.String
getPath()
The database's absolute path<T extends java.lang.Exception>
voidinBatch(UnitOfWork<T> work)
Runs a group of database operations in a batch.boolean
performMaintenance(MaintenanceType type)
void
purge(Document document)
Purges the given document from the database.void
purge(java.lang.String id)
Purges the given document id for the document in database.void
removeChangeListener(ListenerToken token)
Removes the change listener added to the database.void
save(MutableDocument document)
Saves a document to the database.boolean
save(MutableDocument document, ConcurrencyControl concurrencyControl)
Saves a document to the database.boolean
save(MutableDocument document, ConflictHandler conflictHandler)
Saves a document to the database.void
saveBlob(Blob blob)
void
setDocumentExpiration(java.lang.String id, java.util.Date expiration)
Sets an expiration date on a document.java.lang.String
toString()
-
-
-
Field Detail
-
prediction
@NonNull public static final Prediction prediction
ENTERPRISE EDITION API
The predictive model manager for registering and unregistering predictive models. This is part of the Public API.
-
log
@NonNull public static final Log log
Gets the logging controller for the Couchbase Lite library to configure the logging settings and add custom logging.
-
-
Constructor Detail
-
Database
public Database(@NonNull java.lang.String name) throws CouchbaseLiteException
Construct a Database with a given name and the default config. If the database does not yet exist it will be created.- Parameters:
name
- The name of the database: May NOT contain capital letters!- Throws:
CouchbaseLiteException
- if any error occurs during the open operation.
-
Database
public Database(@NonNull java.lang.String name, @NonNull DatabaseConfiguration config) throws CouchbaseLiteException
Construct a AbstractDatabase with a given name and database config. If the database does not yet exist, it will be created, unless the `readOnly` option is used.- Parameters:
name
- The name of the database: May NOT contain capital letters!config
- The database config.- Throws:
CouchbaseLiteException
- Throws an exception if any error occurs during the open operation.
-
-
Method Detail
-
copy
public static void copy(@NonNull java.io.File path, @NonNull java.lang.String name, @NonNull DatabaseConfiguration config) throws CouchbaseLiteException
Make a copy of a database in a new location. NOTE: This method will copy the database without changing the encryption key of the original database: the encryption key specified in the given config is the encryption key used for both the original and copied database. To change or add the encryption key for the copied database, call Database.changeEncryptionKey(key) for the copy.- Parameters:
path
- path to the existing db filename
- the name of the new DBconfig
- a config with the new location- Throws:
CouchbaseLiteException
- on copy failure
-
changeEncryptionKey
public void changeEncryptionKey(EncryptionKey encryptionKey) throws CouchbaseLiteException
ENTERPRISE EDITION API
Changes the database's encryption key, or removes encryption if the new key is null.
- Parameters:
encryptionKey
- The encryption key- Throws:
CouchbaseLiteException
- on error
-
delete
public static void delete(@NonNull java.lang.String name, @Nullable java.io.File directory) throws CouchbaseLiteException
Deletes a database of the given name in the given directory.- Parameters:
name
- the database's namedirectory
- the directory containing the database: the database's parent directory.- Throws:
CouchbaseLiteException
- Throws an exception if any error occurs during the operation.
-
exists
public static boolean exists(@NonNull java.lang.String name, @NonNull java.io.File directory)
Checks whether a database of the given name exists in the given directory or not.- Parameters:
name
- the database's namedirectory
- the path where the database is located.- Returns:
- true if exists, false otherwise.
-
getName
@NonNull public java.lang.String getName()
Return the database name- Returns:
- the database's name
-
getPath
@Nullable public java.lang.String getPath()
The database's absolute path- Returns:
- the database's path or null if the database is closed.
-
getCount
public long getCount()
The number of documents in the database.- Returns:
- the number of documents in the database, 0 if database is closed.
-
getConfig
@NonNull public DatabaseConfiguration getConfig()
Returns a copy of the database configuration.- Returns:
- the READONLY copied config object
-
getDocument
@Nullable public Document getDocument(@NonNull java.lang.String id)
Gets an existing Document object with the given ID. If the document with the given ID doesn't exist in the database, the value returned will be null.- Parameters:
id
- the document ID- Returns:
- the Document object
-
save
public void save(@NonNull MutableDocument document) throws CouchbaseLiteException
Saves a document to the database. When write operations are executed concurrently, the last writer will overwrite all other written values. Calling this method is the same as calling the ave(MutableDocument, ConcurrencyControl) method with LAST_WRITE_WINS concurrency control.- Parameters:
document
- The document.- Throws:
CouchbaseLiteException
- on error
-
save
public boolean save(@NonNull MutableDocument document, @NonNull ConcurrencyControl concurrencyControl) throws CouchbaseLiteException
Saves a document to the database. When used with LAST_WRITE_WINS concurrency control, the last write operation will win if there is a conflict. When used with FAIL_ON_CONFLICT concurrency control, save will fail with false value- Parameters:
document
- The document.concurrencyControl
- The concurrency control.- Returns:
- true if successful. false if the FAIL_ON_CONFLICT concurrency
- Throws:
CouchbaseLiteException
- on error
-
save
public boolean save(@NonNull MutableDocument document, @NonNull ConflictHandler conflictHandler) throws CouchbaseLiteException
Saves a document to the database. Conflicts will be resolved by the passed ConflictHandler- Parameters:
document
- The document.conflictHandler
- A conflict handler.- Returns:
- true if successful. false if the FAIL_ON_CONFLICT concurrency
- Throws:
CouchbaseLiteException
- on error
-
delete
public void delete(@NonNull Document document) throws CouchbaseLiteException
Deletes a document from the database. When write operations are executed concurrently, the last writer will overwrite all other written values. Calling this function is the same as calling the delete(Document, ConcurrencyControl) function with LAST_WRITE_WINS concurrency control.- Parameters:
document
- The document.- Throws:
CouchbaseLiteException
- on error
-
delete
public boolean delete(@NonNull Document document, @NonNull ConcurrencyControl concurrencyControl) throws CouchbaseLiteException
Deletes a document from the database. When used with lastWriteWins concurrency control, the last write operation will win if there is a conflict. When used with FAIL_ON_CONFLICT concurrency control, delete will fail with 'false' value returned.- Parameters:
document
- The document.concurrencyControl
- The concurrency control.- Throws:
CouchbaseLiteException
- on error
-
purge
public void purge(@NonNull Document document) throws CouchbaseLiteException
Purges the given document from the database. This is more drastic than delete(Document), it removes all traces of the document. The purge will NOT be replicated to other databases.- Parameters:
document
- the document to be purged.- Throws:
CouchbaseLiteException
-
purge
public void purge(@NonNull java.lang.String id) throws CouchbaseLiteException
Purges the given document id for the document in database. This is more drastic than delete(Document), it removes all traces of the document. The purge will NOT be replicated to other databases.- Parameters:
id
- the document ID- Throws:
CouchbaseLiteException
-
setDocumentExpiration
public void setDocumentExpiration(@NonNull java.lang.String id, @Nullable java.util.Date expiration) throws CouchbaseLiteException
Sets an expiration date on a document. After this time, the document will be purged from the database.- Parameters:
id
- The ID of the Documentexpiration
- Nullable expiration timestamp as a Date, set timestamp to null to remove expiration date time from doc.- Throws:
CouchbaseLiteException
- Throws an exception if any error occurs during the operation.
-
getDocumentExpiration
@Nullable public java.util.Date getDocumentExpiration(@NonNull java.lang.String id) throws CouchbaseLiteException
Returns the expiration time of the document. null will be returned if there is no expiration time set- Parameters:
id
- The ID of the Document- Returns:
- Date a nullable expiration timestamp of the document or null if time not set.
- Throws:
CouchbaseLiteException
- Throws an exception if any error occurs during the operation.
-
inBatch
public <T extends java.lang.Exception> void inBatch(@NonNull UnitOfWork<T> work) throws CouchbaseLiteException, T extends java.lang.Exception
Runs a group of database operations in a batch. Use this when performing bulk write operations like multiple inserts/updates; it saves the overhead of multiple database commits, greatly improving performance.- Parameters:
work
- a unit of work that may terminate abruptly (with an exception)- Throws:
CouchbaseLiteException
- Throws an exception if any error occurs during the operation.T extends java.lang.Exception
-
addChangeListener
@NonNull public ListenerToken addChangeListener(@NonNull DatabaseChangeListener listener)
Adds a change listener for the changes that occur in the database. The changes will be delivered on the UI thread for the Android platform and on an arbitrary thread for the Java platform. When developing a Java Desktop application using Swing or JavaFX that needs to update the UI after receiving the changes, make sure to schedule the UI update on the UI thread by using SwingUtilities.invokeLater(Runnable) or Platform.runLater(Runnable) respectively.- Parameters:
listener
- callback
-
addChangeListener
@NonNull public ListenerToken addChangeListener(@Nullable java.util.concurrent.Executor executor, @NonNull DatabaseChangeListener listener)
Adds a change listener for the changes that occur in the database with an executor on which the changes will be posted to the listener. If the executor is not specified, the changes will be delivered on the UI thread for the Android platform and on an arbitrary thread for the Java platform.- Parameters:
listener
- callback
-
removeChangeListener
public void removeChangeListener(@NonNull ListenerToken token)
Removes the change listener added to the database.- Parameters:
token
- returned by a previous call to addChangeListener or addDocumentListener.
-
addDocumentChangeListener
@NonNull public ListenerToken addDocumentChangeListener(@NonNull java.lang.String id, @NonNull DocumentChangeListener listener)
Adds a change listener for the changes that occur to the specified document. The changes will be delivered on the UI thread for the Android platform and on an arbitrary thread for the Java platform. When developing a Java Desktop application using Swing or JavaFX that needs to update the UI after receiving the changes, make sure to schedule the UI update on the UI thread by using SwingUtilities.invokeLater(Runnable) or Platform.runLater(Runnable) respectively.
-
addDocumentChangeListener
@NonNull public ListenerToken addDocumentChangeListener(@NonNull java.lang.String id, @Nullable java.util.concurrent.Executor executor, @NonNull DocumentChangeListener listener)
Adds a change listener for the changes that occur to the specified document with an executor on which the changes will be posted to the listener. If the executor is not specified, the changes will be delivered on the UI thread for the Android platform and on an arbitrary thread for the Java platform.
-
close
public void close() throws CouchbaseLiteException
Closes a database. Closing a database will stop all replicators, live queries and all listeners attached to it.- Throws:
CouchbaseLiteException
- Throws an exception if any error occurs during the operation.
-
delete
public void delete() throws CouchbaseLiteException
Deletes a database. Deleting a database will stop all replicators, live queries and all listeners attached to it. Although attempting to close a closed database is not an error, attempting to delete a closed database is.- Throws:
CouchbaseLiteException
- Throws an exception if any error occurs during the operation.
-
createQuery
@NonNull public Query createQuery(@NonNull java.lang.String query)
-
getIndexes
@NonNull public java.util.List<java.lang.String> getIndexes() throws CouchbaseLiteException
- Throws:
CouchbaseLiteException
-
createIndex
public void createIndex(@NonNull java.lang.String name, @NonNull Index index) throws CouchbaseLiteException
- Throws:
CouchbaseLiteException
-
createIndex
public void createIndex(@NonNull java.lang.String name, @NonNull com.couchbase.lite.IndexConfiguration config) throws CouchbaseLiteException
- Throws:
CouchbaseLiteException
-
deleteIndex
public void deleteIndex(@NonNull java.lang.String name) throws CouchbaseLiteException
- Throws:
CouchbaseLiteException
-
performMaintenance
public boolean performMaintenance(MaintenanceType type) throws CouchbaseLiteException
- Throws:
CouchbaseLiteException
-
saveBlob
@Internal("This method is not part of the public API: it is for internal use only") public void saveBlob(@NonNull Blob blob)
-
getBlob
@Internal("This method is not part of the public API: it is for internal use only") @Nullable public Blob getBlob(@NonNull java.util.Map<java.lang.String,java.lang.Object> props)
-
toString
@NonNull public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-