Modifier and Type | Field and Description |
---|---|
static java.lang.String |
GET_ALL_LEAFS |
static java.lang.String |
GET_NON_DELETED_LEAFS |
static java.lang.String |
SQL_CHANGE_IDS_SINCE_LIMIT |
static int |
SQLITE_QUERY_PLACEHOLDERS_LIMIT |
SEQUENCE_NUMBER_START
Constructor and Description |
---|
DatastoreImpl(java.lang.String dir,
java.lang.String name) |
DatastoreImpl(java.lang.String dir,
java.lang.String name,
KeyProvider provider)
Constructor for single thread SQLCipher-based datastore.
|
Modifier and Type | Method and Description |
---|---|
java.util.List<? extends Attachment> |
attachmentsForRevision(DocumentRevision rev)
Returns all attachments for the revision.
|
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 docId)
Returns whether this datastore contains any revisions of a document.
|
boolean |
containsDocument(java.lang.String docId,
java.lang.String revId)
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.
|
void |
deleteLocalDocument(java.lang.String docId)
Deletes a local document.
|
java.lang.String |
extensionDataFolder(java.lang.String extensionName) |
void |
forceInsert(DocumentRevision rev,
java.util.List<java.lang.String> revisionHistory,
java.util.Map<java.lang.String,java.lang.Object> attachments,
java.util.Map<java.lang.String[],java.util.List<PreparedAttachment>> preparedAttachments,
boolean pullAttachmentsInline)
Deprecated.
|
void |
forceInsert(DocumentRevision rev,
java.lang.String... revisionHistory)
Inserts a revision of a document with an existing revision ID
|
void |
forceInsert(java.util.List<ForceInsertItem> items)
Inserts one or more revisions of a document into the database.
|
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.
|
DocumentRevisionTree |
getAllRevisionsOfDocument(java.lang.String docId)
Returns
DocumentRevisionTree of a document. |
Attachment |
getAttachment(java.lang.String id,
java.lang.String rev,
java.lang.String attachmentName)
Returns attachment
attachmentName for the revision. |
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 id)
Returns the current winning revision of a document.
|
DocumentRevision |
getDocument(java.lang.String id,
java.lang.String rev)
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> docIds)
Returns the current winning revisions for a set of documents.
|
EventBus |
getEventBus()
Returns the EventBus which this Datastore posts
Document Notification Events to. |
KeyProvider |
getKeyProvider() |
long |
getLastSequence()
Retrieves the datastore's current sequence number.
|
LocalDocument |
getLocalDocument(java.lang.String docId)
Returns the current winning revision of a local document.
|
java.util.List<java.lang.String> |
getPossibleAncestorRevisionIDs(java.lang.String docId,
java.lang.String revId,
int limit) |
java.lang.String |
getPublicIdentifier()
Returns the datastore's unique identifier.
|
LocalDocument |
insertLocalDocument(java.lang.String docId,
DocumentBody body)
Inserts a local document with an ID and body.
|
PreparedAttachment |
prepareAttachment(Attachment att,
long length,
long encodedLength)
Read attachment stream to a temporary location and calculate sha1,
prior to being added to the datastore.
|
void |
resolveConflictsForDocument(java.lang.String docId,
ConflictResolver resolver)
Resolve conflicts for specified Document using the
given
ConflictResolver |
java.util.Map<java.lang.String,java.util.Collection<java.lang.String>> |
revsDiff(com.google.common.collect.Multimap<java.lang.String,java.lang.String> revisions)
Returns the subset of given the document id/revisions that are not stored in the database.
|
DocumentRevision |
updateDocumentFromRevision(DocumentRevision rev)
Updates a document that exists in the datastore with with body and attachments
from
rev . |
public static final java.lang.String SQL_CHANGE_IDS_SINCE_LIMIT
public static final java.lang.String GET_NON_DELETED_LEAFS
public static final java.lang.String GET_ALL_LEAFS
public static final int SQLITE_QUERY_PLACEHOLDERS_LIMIT
public DatastoreImpl(java.lang.String dir, java.lang.String name) throws java.sql.SQLException, java.io.IOException, DatastoreException
java.sql.SQLException
java.io.IOException
DatastoreException
public DatastoreImpl(java.lang.String dir, java.lang.String name, KeyProvider provider) throws java.sql.SQLException, java.io.IOException, DatastoreException
dir
- The directory where the datastore will be createdname
- The user-defined name of the datastoreprovider
- The key provider object that contains the user-defined SQLCipher keyjava.sql.SQLException
java.io.IOException
DatastoreException
public java.lang.String getDatastoreName()
Datastore
Returns the name of this datastore.
getDatastoreName
in interface Datastore
public KeyProvider getKeyProvider()
public long getLastSequence()
Datastore
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.
getLastSequence
in interface Datastore
public int getDocumentCount()
Datastore
Return the number of documents in the datastore
getDocumentCount
in interface Datastore
public boolean containsDocument(java.lang.String docId, java.lang.String revId)
Datastore
Returns whether this datastore contains a particular revision of a document.
true
will still be returned if the document is deleted.
containsDocument
in interface Datastore
docId
- id of the documentrevId
- revision of the documenttrue
if specified document's particular revision exists
in the datastore, false
otherwise.public boolean containsDocument(java.lang.String docId)
Datastore
Returns whether this datastore contains any revisions of a document.
true
will still be returned if the document is deleted.
containsDocument
in interface Datastore
docId
- id of the documenttrue
if specified document exists
in the datastore, false
otherwise.public DocumentRevision getDocument(java.lang.String id) throws DocumentNotFoundException
Datastore
Returns the current winning revision of a document.
Previously deleted documents can be retrieved
(via tombstones, see Datastore.deleteDocumentFromRevision(DocumentRevision)
)
getDocument
in interface Datastore
id
- ID of document to retrieve.DocumentRevision
of the document or null if it doesn't exist.DocumentNotFoundException
- When the document specified was not foundpublic DocumentRevision getDocument(java.lang.String id, java.lang.String rev) throws DocumentNotFoundException
Datastore
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 Datastore.deleteDocumentFromRevision(DocumentRevision)
)
getDocument
in interface Datastore
id
- ID of the documentrev
- Revision of the documentDocumentRevision
of the document or null if it doesn't exist.DocumentNotFoundException
- if the document at the specified revision was not foundpublic DocumentRevisionTree getAllRevisionsOfDocument(java.lang.String docId)
Returns DocumentRevisionTree
of a document.
The tree contains the complete revision history of the document, including branches for conflicts and deleted leaves.
docId
- id of the documentDocumentRevisionTree
of the specified documentpublic Changes changes(long since, int limit)
Datastore
Returns a list of changed documents, from since
to
since + limit
, inclusive.
changes
in interface Datastore
since
- the lower bound (exclusive) of the change set
sequence numberlimit
- since + limit
is the upper bound (inclusive) of the
change set sequence numberpublic java.util.List<DocumentRevision> getAllDocuments(int offset, int limit, boolean descending)
Datastore
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.
getAllDocuments
in interface Datastore
offset
- start positionlimit
- maximum number of documents to returndescending
- whether the documents are read in ascending or
descending order.DBObjects
, maximum length limit
.public java.util.List<java.lang.String> getAllDocumentIds()
Datastore
Enumerates the current winning revision for all documents in the datastore and return a list of their document identifiers.
getAllDocumentIds
in interface Datastore
String
.public java.util.List<DocumentRevision> getDocumentsWithIds(java.util.List<java.lang.String> docIds) throws DocumentException
Datastore
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.
getDocumentsWithIds
in interface Datastore
docIds
- list of document idDocumentRevision
objects.DocumentException
- if there was an error retrieving the
documents.public java.util.List<java.lang.String> getPossibleAncestorRevisionIDs(java.lang.String docId, java.lang.String revId, int limit)
public LocalDocument getLocalDocument(java.lang.String docId) throws DocumentNotFoundException
Returns the current winning revision of a local document.
docId
- id of the local documentLocalDocument
of the documentDocumentNotFoundException
- if the document ID doesn't existpublic LocalDocument insertLocalDocument(java.lang.String docId, DocumentBody body) throws DocumentException
Inserts a local document with an ID and body. Replacing the current local document of the same id if one is present.
Local documents are not replicated between datastores.
docId
- The document id for the documentbody
- JSON body for the documentDocumentRevision
of the newly created documentDocumentException
- if there is an error inserting the local document into the databasepublic void deleteLocalDocument(java.lang.String docId) throws DocumentNotFoundException
Deletes a local document.
docId
- documentId of the document to be deletedDocumentNotFoundException
- if the document ID doesn't existpublic java.lang.String getPublicIdentifier() throws DatastoreException
Returns the datastore's unique identifier.
This is used for the checkpoint document in a remote datastore during replication.
DatastoreException
- if there was an error retrieving the unique identifier from the
database@Deprecated public void forceInsert(DocumentRevision rev, java.util.List<java.lang.String> revisionHistory, java.util.Map<java.lang.String,java.lang.Object> attachments, java.util.Map<java.lang.String[],java.util.List<PreparedAttachment>> preparedAttachments, boolean pullAttachmentsInline) throws DocumentException
DocumentException
forceInsert(List)
public void forceInsert(java.util.List<ForceInsertItem> items) throws DocumentException
Inserts one or more revisions of a document into the database. For efficiency, this is performed as one database transaction.
Each revision is inserted at a point in the tree expressed by the path described in the
revisionHistory
field. If any non-leaf revisions do not exist locally, then they are
created as "stub" revisions.
This method should only be called by the replicator. It is designed to allow revisions from remote databases to be added to this database during the replication process: the documents in the remote database already have revision IDs that need to be preserved for the two databases to be in sync (otherwise it would not be possible to tell that the two represent the same revision). This is analogous to using the _new_edits false option in CouchDB (see the CouchDB wiki for more detail).
If the document was successfully inserted, a
DocumentCreated
,
DocumentModified
, or
DocumentDeleted
event is posted on the event bus. The event will depend on the nature
of the update made.
items
- one or more revisions to insert. Each ForceInsertItem
consists of:
DocumentRevision
containing the information for a revision
from a remote datastore.
rev
. This list
needs to be sorted in ascending order
pullAttachmentsInline
true
pullAttachmentsInline
false.
Attachments that have already been prepared, this is a
Map of String[docId,revId] → list of attachments
attachments
metadata and data directly
from received JSON to add new attachments for this revision.
Else use preparedAttachments
which were previously
downloaded and prepared by processOneChangesBatch in
BasicPullStrategy
DocumentException
- if there was an error inserting the revision or its attachments
into the databaseDatastore.getEventBus()
public void forceInsert(DocumentRevision rev, java.lang.String... revisionHistory) throws DocumentException
Inserts a revision of a document with an existing revision ID
Equivalent to:
forceInsert(rev, Arrays.asList(revisionHistory), null, null, false);
rev
- A DocumentRevision
containing the information for a revision
from a remote datastore.revisionHistory
- The history of the revision being inserted,
including the rev ID of rev
. This list
needs to be sorted in ascending orderDocumentException
- if there was an error inserting the revision into the databaseforceInsert(DocumentRevision, java.util.List,java.util.Map, java.util.Map, boolean)
public void compact()
Datastore
public void close()
Datastore
public java.util.Map<java.lang.String,java.util.Collection<java.lang.String>> revsDiff(com.google.common.collect.Multimap<java.lang.String,java.lang.String> revisions)
revisions
- a Multimap of document id → revision idpublic java.lang.String extensionDataFolder(java.lang.String extensionName)
public java.util.Iterator<java.lang.String> getConflictedDocumentIds()
Datastore
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.
getConflictedDocumentIds
in interface Datastore
public void resolveConflictsForDocument(java.lang.String docId, ConflictResolver resolver) throws ConflictException
Datastore
Resolve conflicts for specified Document using the
given ConflictResolver
resolveConflictsForDocument
in interface Datastore
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
public PreparedAttachment prepareAttachment(Attachment att, long length, long encodedLength) throws AttachmentException
Read attachment stream to a temporary location and calculate sha1, prior to being added to the datastore.
Used by replicator when receiving new/updated attachments
att
- Attachment to be prepared, providing data either from a file or a streamlength
- Size in bytes of attachment as signalled by "length" metadata propertyencodedLength
- Size in bytes of attachment, after encoding, as signalled by
"encoded_length" metadata propertyAttachmentException
- if there was an error preparing the attachment, e.g., reading
attachment data.public Attachment getAttachment(java.lang.String id, java.lang.String rev, java.lang.String attachmentName)
Returns attachment attachmentName
for the revision.
Used by replicator when pushing attachments
id
- The revision ID with which the attachment is associatedrev
- The document ID with which the attachment is associatedattachmentName
- Name of the attachmentAttachment
or null if there is no attachment with that name.public java.util.List<? extends Attachment> attachmentsForRevision(DocumentRevision rev) throws AttachmentException
Returns all attachments for the revision.
Used by replicator when pulling attachments
rev
- The revision with which the attachments are associatedAttachment
AttachmentException
- if there was an error reading the attachment metadata from the
databasepublic EventBus getEventBus()
Datastore
Returns the EventBus which this Datastore posts
Document Notification Events
to.
getEventBus
in interface Datastore
public DocumentRevision createDocumentFromRevision(DocumentRevision rev) throws DocumentException
Datastore
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.
createDocumentFromRevision
in interface Datastore
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 documentDatastore.getEventBus()
public DocumentRevision updateDocumentFromRevision(DocumentRevision rev) throws DocumentException
Datastore
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.
updateDocumentFromRevision
in interface Datastore
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 documentDatastore.getEventBus()
public DocumentRevision deleteDocumentFromRevision(DocumentRevision rev) throws ConflictException
Datastore
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. Datastore.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.
deleteDocumentFromRevision
in interface Datastore
rev
- the DocumentRevision
to be deletedDocumentRevision
- the deleted or "tombstone" documentConflictException
- if the sourceRevisionId
is not the current revisionDatastore.getEventBus()
,
Datastore.resolveConflictsForDocument(java.lang.String, com.cloudant.sync.datastore.ConflictResolver)
public java.util.List<DocumentRevision> deleteDocument(java.lang.String id) throws DocumentException
Datastore
Delete all leaf revisions for the document
This is equivalent to calling
deleteDocumentFromRevision
on all leaf revisions
deleteDocument
in interface Datastore
id
- the ID of the document to delete leaf nodes forDocumentRevision
s - the deleted or "tombstone" documentsDocumentException
- if there was an error deleting the documentDatastore.getEventBus()
,
Datastore.deleteDocumentFromRevision(DocumentRevision)