Class MongoDBKeyValueStore

java.lang.Object
org.nuxeo.runtime.kv.AbstractKeyValueStoreProvider
org.nuxeo.ecm.core.mongodb.kv.MongoDBKeyValueStore
All Implemented Interfaces:
KeyValueStore, KeyValueStoreProvider

public class MongoDBKeyValueStore extends AbstractKeyValueStoreProvider
MongoDB implementation of a Key/Value Store Provider.

The following configuration properties are available:

  • collection: the MongoDB collection prefix to use, the default is "kv". This will be followed by the Store name.
Since:
9.3
  • Field Details

  • Constructor Details

    • MongoDBKeyValueStore

      public MongoDBKeyValueStore()
  • Method Details

    • initialize

      public void initialize(KeyValueStoreDescriptor descriptor)
      Description copied from interface: KeyValueStoreProvider
      Initializes this Key/Value store provider.
      Specified by:
      initialize in interface KeyValueStoreProvider
      Overrides:
      initialize in class AbstractKeyValueStoreProvider
      Parameters:
      descriptor - the store provider descriptor
    • keyStream

      public Stream<String> keyStream()
      Description copied from interface: KeyValueStoreProvider
      Returns a Stream of the keys contained in this Key/Value store provider.

      This operation may be slow and should only be used for management or debug purposes.

      Returns:
      the stream of keys
    • keyStream

      public Stream<String> keyStream(String prefix)
      Description copied from interface: KeyValueStoreProvider
      Returns a Stream of the keys with the given prefix contained in this Key/Value store provider.

      This operation may be slow and should only be used for management or debug purposes.

      Returns:
      the stream of keys
    • close

      public void close()
      Description copied from interface: KeyValueStoreProvider
      Closes this Key/Value store provider.
    • clear

      public void clear()
      Description copied from interface: KeyValueStoreProvider
      Clears the content of this Key/Value store provider.
    • toStorage

      protected static Object toStorage(byte[] bytes)
    • toBytes

      protected byte[] toBytes(Object value)
    • toString

      protected String toString(Object value)
    • toLong

      protected Long toLong(Object value) throws NumberFormatException
      Throws:
      NumberFormatException
    • get

      public byte[] get(String key)
      Description copied from interface: KeyValueStore
      Retrieves the value associated to the key.
      Parameters:
      key - the key
      Returns:
      the value, or null if there is no value
    • getString

      public String getString(String key)
      Description copied from interface: KeyValueStore
      Retrieves the value associated to the key.
      Specified by:
      getString in interface KeyValueStore
      Overrides:
      getString in class AbstractKeyValueStoreProvider
      Parameters:
      key - the key
      Returns:
      the value, or null if there is no value
    • getLong

      public Long getLong(String key) throws NumberFormatException
      Description copied from interface: KeyValueStore
      Retrieves the value associated to the key.
      Specified by:
      getLong in interface KeyValueStore
      Overrides:
      getLong in class AbstractKeyValueStoreProvider
      Parameters:
      key - the key
      Returns:
      the value, or null if there is no value
      Throws:
      NumberFormatException - if the value cannot be returned as a Long
    • getObject

      protected Object getObject(String key)
    • remove

      public byte[] remove(String key)
      Description copied from interface: KeyValueStore
      Retrieves and removes the value associated to the key.
      Parameters:
      key - the key
      Returns:
      the value, or null if there is no value
    • removeString

      public String removeString(String key)
      Description copied from interface: KeyValueStore
      Retrieves and removes the value associated to the key.
      Parameters:
      key - the key
      Returns:
      the value, or null if there is no value
    • removeLong

      public Long removeLong(String key) throws NumberFormatException
      Description copied from interface: KeyValueStore
      Retrieves and removes the value associated to the key.
      Parameters:
      key - the key
      Returns:
      the value, or null if there is no value
      Throws:
      NumberFormatException - if the value cannot be returned as a Long
    • removeObject

      protected Object removeObject(String key)
    • get

      public Map<String,byte[]> get(Collection<String> keys)
      Description copied from interface: KeyValueStore
      Retrieves the key/value map associated with the keys.
      Specified by:
      get in interface KeyValueStore
      Overrides:
      get in class AbstractKeyValueStoreProvider
      Parameters:
      keys - the keys
      Returns:
      the key/value map
    • getStrings

      public Map<String,String> getStrings(Collection<String> keys)
      Description copied from interface: KeyValueStore
      Retrieves the key/value map associated with the keys.
      Specified by:
      getStrings in interface KeyValueStore
      Overrides:
      getStrings in class AbstractKeyValueStoreProvider
      Parameters:
      keys - the keys
      Returns:
      the key/value map
    • getLongs

      public Map<String,Long> getLongs(Collection<String> keys) throws NumberFormatException
      Description copied from interface: KeyValueStore
      Retrieves the key/value map associated with the keys.
      Specified by:
      getLongs in interface KeyValueStore
      Overrides:
      getLongs in class AbstractKeyValueStoreProvider
      Parameters:
      keys - the keys
      Returns:
      the key/value map
      Throws:
      NumberFormatException - if one of the values cannot be returned as a Long
    • findByKeys

      protected void findByKeys(Collection<String> keys, Consumer<org.bson.Document> block)
      Since:
      9.10
    • getDateFromTTL

      protected Date getDateFromTTL(long ttl)
    • put

      public void put(String key, byte[] bytes, long ttl)
      Description copied from interface: KeyValueStore
      Sets the value associated to the key, and a TTL.
      Parameters:
      key - the key
      bytes - the value, which may be null
      ttl - the TTL, in seconds (0 for infinite)
    • put

      public void put(String key, String string)
      Description copied from interface: KeyValueStore
      Sets the value associated to the key.
      Specified by:
      put in interface KeyValueStore
      Overrides:
      put in class AbstractKeyValueStoreProvider
      Parameters:
      key - the key
      string - the value, which may be null
    • put

      public void put(String key, String string, long ttl)
      Description copied from interface: KeyValueStore
      Sets the value associated to the key, and a TTL.
      Specified by:
      put in interface KeyValueStore
      Overrides:
      put in class AbstractKeyValueStoreProvider
      Parameters:
      key - the key
      string - the value, which may be null
      ttl - the TTL, in seconds (0 for infinite)
    • put

      public void put(String key, Long value)
      Description copied from interface: KeyValueStore
      Sets the value associated to the key.
      Specified by:
      put in interface KeyValueStore
      Overrides:
      put in class AbstractKeyValueStoreProvider
      Parameters:
      key - the key
      value - the value, which may be null
    • put

      public void put(String key, Long value, long ttl)
      Description copied from interface: KeyValueStore
      Sets the value associated to the key.
      Specified by:
      put in interface KeyValueStore
      Overrides:
      put in class AbstractKeyValueStoreProvider
      Parameters:
      key - the key
      value - the value, which may be null
      ttl - the TTL, in seconds (0 for infinite)
    • put

      protected void put(String key, Object value, long ttl)
    • addTTL

      protected void addTTL(org.bson.Document doc, long ttl)
    • setTTL

      public boolean setTTL(String key, long ttl)
      Description copied from interface: KeyValueStore
      Sets the TTL for an existing key.
      Parameters:
      key - the key
      ttl - the TTL, in seconds (0 for infinite)
      Returns:
      true if the TTL has been set, or false if the key does not exist
    • compareAndSet

      public boolean compareAndSet(String key, byte[] expected, byte[] value, long ttl)
      Description copied from interface: KeyValueStore
      Atomically sets the value associated to the key to the given value, with the given TTL, if the current value is the expected value.

      Note value comparison is done by value and not by reference.

      Parameters:
      key - the key
      expected - the expected value, which may be null
      value - the updated value, which may be null
      ttl - the TTL, in seconds (0 for infinite)
      Returns:
      true if the value was updated, or false if not (the expected value was not found)
    • compareAndSet

      public boolean compareAndSet(String key, String expected, String value, long ttl)
      Description copied from interface: KeyValueStore
      Atomically sets the value associated to the key to the given value, with the given TTL, if the current value is the expected value.

      Note value comparison is done by value and not by reference.

      Specified by:
      compareAndSet in interface KeyValueStore
      Overrides:
      compareAndSet in class AbstractKeyValueStoreProvider
      Parameters:
      key - the key
      expected - the expected value, which may be null
      value - the updated value, which may be null
      ttl - the TTL, in seconds (0 for infinite)
      Returns:
      true if the value was updated, or false if not (the expected value was not found)
    • compareAndSet

      protected boolean compareAndSet(String key, Object expected, Object value, long ttl)
    • addAndGet

      public long addAndGet(String key, long delta) throws NumberFormatException
      Description copied from interface: KeyValueStore
      Atomically adds the delta to the value associated to the key, interpreted as a long represented as a string.

      If the value does not exist (if KeyValueStore.get(java.lang.String) would return null), it is interpreted as 0.

      Specified by:
      addAndGet in interface KeyValueStore
      Overrides:
      addAndGet in class AbstractKeyValueStoreProvider
      Parameters:
      key - the key
      delta - the delta to add
      Returns:
      the new value
      Throws:
      NumberFormatException - if the existing value cannot be interpreted as a long
    • addAndGetGeneric

      protected long addAndGetGeneric(String key, long delta) throws NumberFormatException
      Throws:
      NumberFormatException