public interface Datastore
The Datastore is the core interaction point for create, delete and update operations (CRUD) for within Cloudant Sync.
The Datastore can be viewed as a pool of heterogeneous JSON documents. One datastore can hold many different types of document, unlike tables within a relational model. The datastore exposes a simple key-value model where the key is the document ID combined with a (sometimes optional) revision ID.
For a more advanced way of querying the Datastore, see the
IndexManager
class
Each document consists of a set of revisions, hence most methods within
this class operating on DocumentRevision
objects, which carry both a
document ID and a revision ID. This forms the basis of the MVCC data model,
used to ensure safe peer-to-peer replication is possible.
Each document is formed of a tree of revisions. Replication can create
branches in this tree when changes have been made in two or more places to
the same document in-between replications. MVCC exposes these branches as
conflicted documents. These conflicts should be resolved by user-code, by
marking all but one of the leaf nodes of the branches as "deleted", using
the deleteDocumentFromRevision(DocumentRevision)
method. When the
datastore is next replicated with a remote datastore, this fix will be
propagated, thereby resolving the conflicted document across the set of
peers.
DocumentRevision
,
IndexManager
Modifier and Type | Field and Description |
---|---|
static long |
SEQUENCE_NUMBER_START
The sequence number of the datastore when no updates have been made,
-1L.
|
Modifier and Type | Method and Description |
---|---|
Changes |
changes(long since,
int limit)
Returns a list of changed documents, from
since to
since + limit , inclusive. |
void |
close()
Close the datastore
|
void |
compact()
Compacts the sqlDatabase storage by removing the bodies and attachments of obsolete revisions.
|
boolean |
containsDocument(java.lang.String documentId)
Returns whether this datastore contains any revisions of a document.
|
boolean |
containsDocument(java.lang.String documentId,
java.lang.String revisionId)
Returns whether this datastore contains a particular revision of
a document.
|
DocumentRevision |
createDocumentFromRevision(DocumentRevision rev)
Adds a new document with body and attachments from
rev . |
java.util.List<DocumentRevision> |
deleteDocument(java.lang.String id)
Delete all leaf revisions for the document
|
DocumentRevision |
deleteDocumentFromRevision(DocumentRevision rev)
Deletes a document from the datastore.
|
java.util.List<java.lang.String> |
getAllDocumentIds()
Enumerates the current winning revision for all documents in the
datastore and return a list of their document identifiers.
|
java.util.List<DocumentRevision> |
getAllDocuments(int offset,
int limit,
boolean descending)
Enumerates the current winning revision for all documents in the
datastore.
|
java.util.Iterator<java.lang.String> |
getConflictedDocumentIds()
Return
@Iterable<String> over ids to all the Documents with
conflicted revisions. |
java.lang.String |
getDatastoreName()
Returns the name of this datastore.
|
DocumentRevision |
getDocument(java.lang.String documentId)
Returns the current winning revision of a document.
|
DocumentRevision |
getDocument(java.lang.String documentId,
java.lang.String revisionId)
Retrieves a given revision of a document.
|
int |
getDocumentCount()
Return the number of documents in the datastore
|
java.util.List<DocumentRevision> |
getDocumentsWithIds(java.util.List<java.lang.String> documentIds)
Returns the current winning revisions for a set of documents.
|
EventBus |
getEventBus()
Returns the EventBus which this Datastore posts
Document Notification Events to. |
long |
getLastSequence()
Retrieves the datastore's current sequence number.
|
void |
resolveConflictsForDocument(java.lang.String docId,
ConflictResolver resolver)
Resolve conflicts for specified Document using the
given
ConflictResolver |
DocumentRevision |
updateDocumentFromRevision(DocumentRevision rev)
Updates a document that exists in the datastore with with body and attachments
from
rev . |
static final long SEQUENCE_NUMBER_START
java.lang.String getDatastoreName()
Returns the name of this datastore.
DocumentRevision getDocument(java.lang.String documentId) throws DocumentNotFoundException
Returns the current winning revision of a document.
Previously deleted documents can be retrieved
(via tombstones, see deleteDocumentFromRevision(DocumentRevision)
)
documentId
- ID of document to retrieve.DocumentRevision
of the document or null if it doesn't exist.DocumentNotFoundException
- When the document specified was not foundDocumentRevision getDocument(java.lang.String documentId, java.lang.String revisionId) throws DocumentNotFoundException
Retrieves a given revision of a document.
This method gets the revision of a document with a given ID. As the datastore prunes the content of old revisions to conserve space, this revision may contain the metadata but not content of the revision.
Previously deleted documents can be retrieved
(via tombstones, see deleteDocumentFromRevision(DocumentRevision)
)
documentId
- ID of the documentrevisionId
- Revision of the documentDocumentRevision
of the document or null if it doesn't exist.DocumentNotFoundException
- if the document at the specified revision was not foundboolean containsDocument(java.lang.String documentId, java.lang.String revisionId)
Returns whether this datastore contains a particular revision of a document.
true
will still be returned if the document is deleted.
documentId
- id of the documentrevisionId
- revision of the documenttrue
if specified document's particular revision exists
in the datastore, false
otherwise.boolean containsDocument(java.lang.String documentId)
Returns whether this datastore contains any revisions of a document.
true
will still be returned if the document is deleted.
documentId
- id of the documenttrue
if specified document exists
in the datastore, false
otherwise.java.util.List<DocumentRevision> getAllDocuments(int offset, int limit, boolean descending)
Enumerates the current winning revision for all documents in the datastore.
Logically, this method takes all the documents in either ascending
or descending order, skips all documents up to offset
then
returns up to limit
document revisions, stopping either
at limit
or when the list of document is exhausted.
offset
- start positionlimit
- maximum number of documents to returndescending
- whether the documents are read in ascending or
descending order.DBObjects
, maximum length limit
.java.util.List<java.lang.String> getAllDocumentIds()
Enumerates the current winning revision for all documents in the datastore and return a list of their document identifiers.
String
.java.util.List<DocumentRevision> getDocumentsWithIds(java.util.List<java.lang.String> documentIds) throws DocumentException
Returns the current winning revisions for a set of documents.
If the documentIds
list contains document IDs not present
in the datastore, they will be skipped and there will be no entry for
them in the returned list.
documentIds
- list of document idDocumentRevision
objects.DocumentException
- if there was an error retrieving the
documents.long getLastSequence()
Retrieves the datastore's current sequence number.
The datastore's sequence number is incremented every time the content of the datastore is changed. Each document revision within the datastore has an associated sequence number, describing when the change took place.
The sequence number is particularly useful to find out the changes to the database since a given time. For example, replication uses the sequence number so only the changes since the last replication are sent. Indexing could also use this in a similar manner.
int getDocumentCount()
Return the number of documents in the datastore
Changes changes(long since, int limit)
Returns a list of changed documents, from since
to
since + limit
, inclusive.
since
- the lower bound (exclusive) of the change set
sequence numberlimit
- since + limit
is the upper bound (inclusive) of the
change set sequence numberEventBus getEventBus()
Returns the EventBus which this Datastore posts
Document Notification Events
to.
java.util.Iterator<java.lang.String> getConflictedDocumentIds()
Return @Iterable<String>
over ids to all the Documents with
conflicted revisions.
Document is modeled as a tree. If a document has at least two leaf revisions that not deleted, those leaf revisions considered conflicted revisions of the document.
void resolveConflictsForDocument(java.lang.String docId, ConflictResolver resolver) throws ConflictException
Resolve conflicts for specified Document using the
given ConflictResolver
docId
- id of Document to resolve conflictsresolver
- the ConflictResolver used to resolve
conflictsConflictException
- Found new conflicts while
resolving the existing conflicted revision.
This is very likely caused by new conflicted
revision are added while the resolver is
running.ConflictResolver
void close()
DocumentRevision createDocumentFromRevision(DocumentRevision rev) throws DocumentException
Adds a new document with body and attachments from rev
.
If the ID in rev
is null, the document's ID will be auto-generated,
and can be found by inspecting the returned DocumentRevision
.
If the document is successfully created, a
DocumentCreated
event is posted on the event bus.
rev
- the DocumentRevision
to be createdDocumentRevision
- the newly created documentAttachmentException
- if there was an error saving any new attachmentsDocumentException
- if there was an error creating the documentgetEventBus()
DocumentRevision updateDocumentFromRevision(DocumentRevision rev) throws DocumentException
Updates a document that exists in the datastore with with body and attachments
from rev
.
rev
must be a current revision for this document.
If the document is successfully updated, a
DocumentUpdated
event is posted on the event bus.
rev
- the DocumentRevision
to be updatedDocumentRevision
- the updated documentConflictException
- rev
is not a current revision for this documentAttachmentException
- if there was an error saving the attachmentsDocumentException
- if there was an error updating the documentgetEventBus()
DocumentRevision deleteDocumentFromRevision(DocumentRevision rev) throws ConflictException
Deletes a document from the datastore.
This operation leaves a "tombstone" for the deleted document, so that future replication operations can successfully replicate the deletion.
If the document is successfully deleted, a
DocumentDeleted
event is posted on the event bus.
If the input revision is already deleted, nothing will be changed.
When resolving conflicts, this method can be used to delete any non-deleted
leaf revision of a document. resolveConflictsForDocument(java.lang.String, com.cloudant.sync.datastore.ConflictResolver)
handles this
deletion step during conflict resolution, so it's not usually necessary to call this
method in this way from client code. See the doc/conflicts.md document for
more details.
rev
- the DocumentRevision
to be deletedDocumentRevision
- the deleted or "tombstone" documentConflictException
- if the sourceRevisionId
is not the current revisiongetEventBus()
,
resolveConflictsForDocument(java.lang.String, com.cloudant.sync.datastore.ConflictResolver)
java.util.List<DocumentRevision> deleteDocument(java.lang.String id) throws DocumentException
Delete all leaf revisions for the document
This is equivalent to calling
deleteDocumentFromRevision
on all leaf revisions
id
- the ID of the document to delete leaf nodes forDocumentRevision
s - the deleted or "tombstone" documentsDocumentException
- if there was an error deleting the documentgetEventBus()
,
deleteDocumentFromRevision(DocumentRevision)
void compact()