Interface DocumentStore

    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      <T extends Document>
      boolean
      create​(Collection<T> collection, java.util.List<UpdateOp> updateOps)
      Try to create a list of documents.
      <T extends Document>
      java.util.List<T>
      createOrUpdate​(Collection<T> collection, java.util.List<UpdateOp> updateOps)
      Create or unconditionally update a number of documents.
      <T extends Document>
      T
      createOrUpdate​(Collection<T> collection, UpdateOp update)
      Atomically checks if the document exists and updates it, otherwise the document is created (aka "upsert"), unless the update operation requires the document to be present (see UpdateOp.isNew()).
      long determineServerTimeDifferenceMillis()  
      void dispose()
      Dispose this instance.
      <T extends Document>
      T
      find​(Collection<T> collection, java.lang.String key)
      Get the document with the given key.
      <T extends Document>
      T
      find​(Collection<T> collection, java.lang.String key, int maxCacheAge)
      Get the document with the key.
      default <T extends Document>
      @NotNull java.util.List<T>
      findAndUpdate​(@NotNull Collection<T> collection, @NotNull java.util.List<UpdateOp> updateOps)
      Performs a conditional update (e.g.
      <T extends Document>
      T
      findAndUpdate​(Collection<T> collection, UpdateOp update)
      Performs a conditional update (e.g.
      @Nullable java.lang.Iterable<CacheStats> getCacheStats()  
      <T extends Document>
      T
      getIfCached​(Collection<T> collection, java.lang.String key)
      Fetches the cached document.
      java.util.Map<java.lang.String,​java.lang.String> getMetadata()  
      default int getNodeNameLimit()
      Return the size limit for node name based on the document store implementation
      @NotNull java.util.Map<java.lang.String,​java.lang.String> getStats()
      Returns statistics about the underlying storage.
      @Nullable CacheInvalidationStats invalidateCache()
      Invalidate the document cache.
      @Nullable CacheInvalidationStats invalidateCache​(java.lang.Iterable<java.lang.String> keys)
      Invalidate the document cache but only with entries that match one of the keys provided.
      <T extends Document>
      void
      invalidateCache​(Collection<T> collection, java.lang.String key)
      Invalidate the document cache for the given key.
      default <T extends Document>
      void
      prefetch​(Collection<T> collection, java.lang.Iterable<java.lang.String> keys)
      optional method indicating the DocumentStore should prefetch, in the most optimal way eg by batching, a bunch of keys as they might soon be needed.
      <T extends Document>
      @NotNull java.util.List<T>
      query​(Collection<T> collection, java.lang.String fromKey, java.lang.String toKey, int limit)
      Get a list of documents where the key is greater than a start value and less than an end value.
      <T extends Document>
      @NotNull java.util.List<T>
      query​(Collection<T> collection, java.lang.String fromKey, java.lang.String toKey, java.lang.String indexedProperty, long startValue, int limit)
      Get a list of documents where the key is greater than a start value and less than an end value and the given "indexed property" is greater or equals the specified value.
      default <T extends Document>
      @NotNull java.util.List<T>
      query​(Collection<T> collection, java.lang.String fromKey, java.lang.String toKey, java.lang.String indexedProperty, long startValue, int limit, java.util.List<java.lang.String> projection)
      Get a list of documents with only projected fields (as mentioned in projections param) along with "_id" field and where the key is greater than a start value and less than an end value and the given "indexed property" is greater or equals the specified value.
      <T extends Document>
      void
      remove​(Collection<T> collection, java.lang.String key)
      Remove a document.
      <T extends Document>
      int
      remove​(Collection<T> collection, java.lang.String indexedProperty, long startValue, long endValue)
      Batch remove documents where the given "indexed property" is within the given range (exclusive) - (startValue, endValue).
      <T extends Document>
      void
      remove​(Collection<T> collection, java.util.List<java.lang.String> keys)
      Batch remove documents with given keys.
      <T extends Document>
      int
      remove​(Collection<T> collection, java.util.Map<java.lang.String,​java.lang.Long> toRemove)
      Batch remove documents with given keys and corresponding equal conditions on NodeDocument.MODIFIED_IN_SECS values.
      void setReadWriteMode​(java.lang.String readWriteMode)
      Set the level of guarantee for read and write operations, if supported by this backend.
      default Throttler throttler()
      Return the Throttler for the underlying store Default is no throttling
    • Method Detail

      • find

        @Nullable
        <T extends Document> T find​(Collection<T> collection,
                                    java.lang.String key)
                             throws DocumentStoreException
        Get the document with the given key. This is a convenience method and equivalent to find(Collection, String, int) with a maxCacheAge of Integer.MAX_VALUE.

        The returned document is immutable.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        key - the key
        Returns:
        the document, or null if not found
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • find

        @Nullable
        <T extends Document> T find​(Collection<T> collection,
                                    java.lang.String key,
                                    int maxCacheAge)
                             throws DocumentStoreException
        Get the document with the key. The implementation may serve the document from a cache, but the cached document must not be older than the given maxCacheAge in milliseconds. An implementation must invalidate a cached document when it detects it is outdated. That is, a subsequent call to find(Collection, String) must return the newer version of the document.

        The returned document is immutable.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        key - the key
        maxCacheAge - the maximum age of the cached document (in ms)
        Returns:
        the document, or null if not found
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • query

        @NotNull
        <T extends Document> @NotNull java.util.List<T> query​(Collection<T> collection,
                                                              java.lang.String fromKey,
                                                              java.lang.String toKey,
                                                              int limit)
                                                       throws DocumentStoreException
        Get a list of documents where the key is greater than a start value and less than an end value.

        The returned documents are sorted by key and are immutable.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        fromKey - the start value (excluding)
        toKey - the end value (excluding)
        limit - the maximum number of entries to return (starting with the lowest key)
        Returns:
        the list (possibly empty)
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • query

        @NotNull
        <T extends Document> @NotNull java.util.List<T> query​(Collection<T> collection,
                                                              java.lang.String fromKey,
                                                              java.lang.String toKey,
                                                              java.lang.String indexedProperty,
                                                              long startValue,
                                                              int limit)
                                                       throws DocumentStoreException
        Get a list of documents where the key is greater than a start value and less than an end value and the given "indexed property" is greater or equals the specified value.

        The indexed property can either be a Long value, in which case numeric comparison applies, or a Boolean value, in which case "false" is mapped to "0" and "true" is mapped to "1".

        The returned documents are sorted by key and are immutable.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        fromKey - the start value (excluding)
        toKey - the end value (excluding)
        indexedProperty - the name of the indexed property (optional)
        startValue - the minimum value of the indexed property
        limit - the maximum number of entries to return
        Returns:
        the list (possibly empty)
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • remove

        <T extends Document> void remove​(Collection<T> collection,
                                         java.lang.String key)
                                  throws DocumentStoreException
        Remove a document. This method does nothing if there is no document with the given key.

        In case of a DocumentStoreException, the document with the given key may or may not have been removed from the store. It is the responsibility of the caller to check whether it still exists. The implementation however ensures that the result of the operation is properly reflected in the document cache. That is, an implementation could simply evict the document with the given key.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        key - the key
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • remove

        <T extends Document> void remove​(Collection<T> collection,
                                         java.util.List<java.lang.String> keys)
                                  throws DocumentStoreException
        Batch remove documents with given keys. Keys for documents that do not exist are simply ignored. If this method fails with an exception, then only some of the documents identified by keys may have been removed.

        In case of a DocumentStoreException, the documents with the given keys may or may not have been removed from the store. It may also be possible that only some have been removed from the store. It is the responsibility of the caller to check which documents still exist. The implementation however ensures that the result of the operation is properly reflected in the document cache. That is, an implementation could simply evict documents with the given keys from the cache.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        keys - list of keys
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • remove

        <T extends Document> int remove​(Collection<T> collection,
                                        java.util.Map<java.lang.String,​java.lang.Long> toRemove)
                                 throws DocumentStoreException
        Batch remove documents with given keys and corresponding equal conditions on NodeDocument.MODIFIED_IN_SECS values. Keys for documents that do not exist are simply ignored. A document is only removed if the corresponding condition is met.

        In case of a DocumentStoreException, the documents with the given keys may or may not have been removed from the store. It may also be possible that only some have been removed from the store. It is the responsibility of the caller to check which documents still exist. The implementation however ensures that the result of the operation is properly reflected in the document cache. That is, an implementation could simply evict documents with the given keys from the cache.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection.
        toRemove - the keys of the documents to remove with the corresponding timestamps.
        Returns:
        the number of removed documents.
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • remove

        <T extends Document> int remove​(Collection<T> collection,
                                        java.lang.String indexedProperty,
                                        long startValue,
                                        long endValue)
                                 throws DocumentStoreException
        Batch remove documents where the given "indexed property" is within the given range (exclusive) - (startValue, endValue).

        The indexed property is a Long value and numeric comparison applies.

        In case of a DocumentStoreException, the documents with the given keys may or may not have been removed from the store. It may also be possible that only some have been removed from the store. It is the responsibility of the caller to check which documents still exist. The implementation however ensures that the result of the operation is properly reflected in the document cache. That is, an implementation could simply evict documents with the given keys from the cache.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection.
        indexedProperty - the name of the indexed property
        startValue - the minimum value of the indexed property (exclusive)
        endValue - the maximum value of the indexed property (exclusive)
        Returns:
        the number of removed documents.
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • create

        <T extends Document> boolean create​(Collection<T> collection,
                                            java.util.List<UpdateOp> updateOps)
                                     throws java.lang.IllegalArgumentException,
                                            DocumentStoreException
        Try to create a list of documents. This method returns true iff none of the documents existed before and the create was successful. This method will return false if one of the documents already exists in the store. Some documents may still have been created in the store. An implementation does not have to guarantee an atomic create of all the documents described in the updateOps. It is the responsibility of the caller to check, which documents were created and take appropriate action. The same is true when this method throws DocumentStoreException (e.g. when a communication error occurs). In this case only some documents may have been created.
        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        updateOps - the list of documents to add (where UpdateOp.Conditions are not allowed)
        Returns:
        true if this worked (if none of the documents already existed)
        Throws:
        java.lang.IllegalArgumentException - when at least one of the UpdateOps is conditional
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • createOrUpdate

        @Nullable
        <T extends Document> T createOrUpdate​(Collection<T> collection,
                                              UpdateOp update)
                                       throws java.lang.IllegalArgumentException,
                                              DocumentStoreException
        Atomically checks if the document exists and updates it, otherwise the document is created (aka "upsert"), unless the update operation requires the document to be present (see UpdateOp.isNew()). The returned document is immutable.

        If this method fails with a DocumentStoreException, then the document may or may not have been created or updated. It is the responsibility of the caller to check the result e.g. by calling find(Collection, String). The implementation however ensures that the result of the operation is properly reflected in the document cache. That is, an implementation could simply evict documents with the given keys from the cache.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        update - the update operation (where UpdateOp.Conditions are not allowed)
        Returns:
        the old document or null if it either didn't exist before, or the UpdateOp required the document to be present but UpdateOp.isNew() was false.
        Throws:
        java.lang.IllegalArgumentException - when the UpdateOp is conditional
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • createOrUpdate

        <T extends Document> java.util.List<T> createOrUpdate​(Collection<T> collection,
                                                              java.util.List<UpdateOp> updateOps)
                                                       throws DocumentStoreException
        Create or unconditionally update a number of documents. An implementation does not have to guarantee that all changes are applied atomically, together.

        In case of a DocumentStoreException (e.g. when a communication error occurs) only some changes may have been applied. In this case it is the responsibility of the caller to check which UpdateOps were applied and take appropriate action. The implementation however ensures that the result of the operations are properly reflected in the document cache. That is, an implementation could simply evict documents related to the given update operations from the cache.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        updateOps - the update operation list
        Returns:
        the list containing old documents or null values if they didn't exist before (see createOrUpdate(Collection, UpdateOp)), where the order reflects the order in the "updateOps" parameter
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • findAndUpdate

        @Nullable
        <T extends Document> T findAndUpdate​(Collection<T> collection,
                                             UpdateOp update)
                                      throws DocumentStoreException
        Performs a conditional update (e.g. using UpdateOp.Condition.Type.EXISTS and only updates the document if the condition is true. The returned document is immutable.

        In case of a DocumentStoreException (e.g. when a communication error occurs) the update may or may not have been applied. In this case it is the responsibility of the caller to check whether the update was applied and take appropriate action. The implementation however ensures that the result of the operation is properly reflected in the document cache. That is, an implementation could simply evict the document related to the given update operation from the cache.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        update - the update operation with the condition
        Returns:
        the old document or null if the condition is not met or if the document wasn't found
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
        See Also:
        createOrUpdate(Collection, List)
      • invalidateCache

        @Nullable
        @Nullable CacheInvalidationStats invalidateCache()
        Invalidate the document cache. Calling this method instructs the implementation to invalidate each document from the cache, which is not up to date with the underlying storage at the time this method is called. A document is considered in the cache if getIfCached(Collection, String) returns a non-null value for a key.

        An implementation is allowed to perform lazy invalidation and only check whether a document is up-to-date when it is accessed after this method is called. However, this also includes a call to getIfCached(Collection, String), which must only return the document if it was up-to-date at the time this method was called. Similarly, a call to find(Collection, String) must guarantee the returned document reflects all the changes done up to when invalidateCache() was called.

        In some implementations this method can be a NOP because documents can only be modified through a single instance of a DocumentStore.

        Returns:
        cache invalidation statistics or null if none are available.
      • invalidateCache

        @Nullable
        @Nullable CacheInvalidationStats invalidateCache​(java.lang.Iterable<java.lang.String> keys)
        Invalidate the document cache but only with entries that match one of the keys provided. See invalidateCache() for the general contract of cache invalidation.
        Parameters:
        keys - the keys of the documents to invalidate.
        Returns:
        cache invalidation statistics or null if none are available.
      • invalidateCache

        <T extends Document> void invalidateCache​(Collection<T> collection,
                                                  java.lang.String key)
        Invalidate the document cache for the given key. See invalidateCache() for the general contract of cache invalidation.
        Parameters:
        collection - the collection
        key - the key
      • dispose

        void dispose()
        Dispose this instance.
      • getIfCached

        @Nullable
        <T extends Document> T getIfCached​(Collection<T> collection,
                                           java.lang.String key)
        Fetches the cached document. If the document is not present in the cache null will be returned. This method is consistent with other find methods that may return cached documents and will return null even when the implementation has a negative cache for documents that do not exist. This method will never return NodeDocument.NULL.
        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        key - the key
        Returns:
        cached document if present. Otherwise null.
      • setReadWriteMode

        void setReadWriteMode​(java.lang.String readWriteMode)
        Set the level of guarantee for read and write operations, if supported by this backend.
        Parameters:
        readWriteMode - the read/write mode
      • getCacheStats

        @Nullable
        @Nullable java.lang.Iterable<CacheStats> getCacheStats()
        Returns:
        status information about the cache
      • getMetadata

        java.util.Map<java.lang.String,​java.lang.String> getMetadata()
        Returns:
        description of the underlying storage.
      • getStats

        @NotNull
        @NotNull java.util.Map<java.lang.String,​java.lang.String> getStats()
        Returns statistics about the underlying storage. The information and keys returned by this method are implementation specific, may change between releases or may even depend on deployment aspects. E.g. depending on access rights, the method may return more or less information from the underlying store. This method should only be used for informational or debug purposes.
        Returns:
        statistics about this document store.
      • determineServerTimeDifferenceMillis

        long determineServerTimeDifferenceMillis()
                                          throws java.lang.UnsupportedOperationException,
                                                 DocumentStoreException
        Returns:
        the estimated time difference in milliseconds between the local instance and the (typically common, shared) document server system. The value can be zero if the times are estimated to be equal, positive when the local instance is ahead of the remote server and negative when the local instance is behind the remote server. An invocation is not cached and typically requires a round-trip to the server (but that is not a requirement).
        Throws:
        java.lang.UnsupportedOperationException - if this DocumentStore does not support this method
        DocumentStoreException - if an I/O error occurs.
      • prefetch

        default <T extends Document> void prefetch​(Collection<T> collection,
                                                   java.lang.Iterable<java.lang.String> keys)
        optional method indicating the DocumentStore should prefetch, in the most optimal way eg by batching, a bunch of keys as they might soon be needed.
      • getNodeNameLimit

        default int getNodeNameLimit()
        Return the size limit for node name based on the document store implementation
        Returns:
        node name size limit
      • throttler

        default Throttler throttler()
        Return the Throttler for the underlying store Default is no throttling
        Returns:
        throttler for document store
      • query

        @NotNull
        default <T extends Document> @NotNull java.util.List<T> query​(Collection<T> collection,
                                                                      java.lang.String fromKey,
                                                                      java.lang.String toKey,
                                                                      java.lang.String indexedProperty,
                                                                      long startValue,
                                                                      int limit,
                                                                      java.util.List<java.lang.String> projection)
                                                               throws DocumentStoreException
        Get a list of documents with only projected fields (as mentioned in projections param) along with "_id" field and where the key is greater than a start value and less than an end value and the given "indexed property" is greater or equals the specified value.

        The indexed property can either be a Long value, in which case numeric comparison applies, or a Boolean value, in which case "false" is mapped to "0" and "true" is mapped to "1".

        The returned documents are sorted by key and are immutable.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        fromKey - the start value (excluding)
        toKey - the end value (excluding)
        indexedProperty - the name of the indexed property (optional)
        startValue - the minimum value of the indexed property
        limit - the maximum number of entries to return
        projection - List of projected keys (optional). Keep this empty to fetch all fields on document.
        Returns:
        the list (possibly empty)
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.
      • findAndUpdate

        @NotNull
        default <T extends Document> @NotNull java.util.List<T> findAndUpdate​(@NotNull
                                                                              @NotNull Collection<T> collection,
                                                                              @NotNull
                                                                              @NotNull java.util.List<UpdateOp> updateOps)
                                                                       throws DocumentStoreException
        Performs a conditional update (e.g. using UpdateOp.Condition.Type.EXISTS and only update the document if the condition is true. The returned documents are immutable.

        In case of a DocumentStoreException (e.g. when a communication error occurs) only some updates may have been applied. In this case it is the responsibility of the caller to check which UpdateOps were applied and take appropriate action. The implementation however ensures that the result of the operations are properly reflected in the document cache. That is, an implementation could simply evict the documents related to the given update operations from the cache.

        Type Parameters:
        T - the document type
        Parameters:
        collection - the collection
        updateOps - the update operation List
        Returns:
        the list containing old documents or null if the condition is not met or if the document wasn't found. The order in the result list reflects the order in the updateOps parameter
        Throws:
        DocumentStoreException - if the operation failed. E.g. because of an I/O error.