Package org.nuxeo.ecm.core.mongodb.kv
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 Summary
Fields Modifier and Type Field Description protected com.mongodb.client.MongoCollection<org.bson.Document>
coll
static String
COLLECTION_DEFAULT
static String
COLLECTION_PROP
static String
ID_KEY
static String
KEYVALUE_CONNECTION_ID
static Double
ONE
static String
TTL_KEY
static String
VALUE_KEY
-
Fields inherited from class org.nuxeo.runtime.kv.AbstractKeyValueStoreProvider
name, UTF_8_DECODERS
-
-
Constructor Summary
Constructors Constructor Description MongoDBKeyValueStore()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description long
addAndGet(String key, long delta)
Atomically adds the delta to the value associated to the key, interpreted as a long represented as a string.protected long
addAndGetGeneric(String key, long delta)
protected void
addTTL(org.bson.Document doc, long ttl)
void
clear()
Clears the content of this Key/Value store provider.void
close()
Closes this Key/Value store provider.boolean
compareAndSet(String key, byte[] expected, byte[] value, long ttl)
Atomically sets the value associated to the key to the given value, with the given TTL, if the current value is the expected value.protected boolean
compareAndSet(String key, Object expected, Object value, long ttl)
boolean
compareAndSet(String key, String expected, String value, long ttl)
Atomically sets the value associated to the key to the given value, with the given TTL, if the current value is the expected value.protected void
findByKeys(Collection<String> keys, Consumer<org.bson.Document> block)
byte[]
get(String key)
Retrieves the value associated to the key.Map<String,byte[]>
get(Collection<String> keys)
Retrieves the key/value map associated with the keys.protected Date
getDateFromTTL(long ttl)
Long
getLong(String key)
Retrieves the value associated to the key.Map<String,Long>
getLongs(Collection<String> keys)
Retrieves the key/value map associated with the keys.protected Object
getObject(String key)
String
getString(String key)
Retrieves the value associated to the key.Map<String,String>
getStrings(Collection<String> keys)
Retrieves the key/value map associated with the keys.void
initialize(KeyValueStoreDescriptor descriptor)
Initializes this Key/Value store provider.Stream<String>
keyStream()
Returns aStream
of the keys contained in this Key/Value store provider.Stream<String>
keyStream(String prefix)
Returns aStream
of the keys with the given prefix contained in this Key/Value store provider.void
put(String key, byte[] bytes, long ttl)
Sets the value associated to the key, and a TTL.void
put(String key, Long value)
Sets the value associated to the key.void
put(String key, Long value, long ttl)
Sets the value associated to the key.protected void
put(String key, Object value, long ttl)
void
put(String key, String string)
Sets the value associated to the key.void
put(String key, String string, long ttl)
Sets the value associated to the key, and a TTL.boolean
setTTL(String key, long ttl)
Sets the TTL for an existing key.protected byte[]
toBytes(Object value)
protected Long
toLong(Object value)
protected static Object
toStorage(byte[] bytes)
protected String
toString(Object value)
-
Methods inherited from class org.nuxeo.runtime.kv.AbstractKeyValueStoreProvider
bytesToLong, bytesToString, compareAndSet, compareAndSet, longToBytes, put, stringToBytes, toString
-
-
-
-
Field Detail
-
KEYVALUE_CONNECTION_ID
public static final String KEYVALUE_CONNECTION_ID
- See Also:
- Constant Field Values
-
COLLECTION_PROP
public static final String COLLECTION_PROP
- See Also:
- Constant Field Values
-
COLLECTION_DEFAULT
public static final String COLLECTION_DEFAULT
- See Also:
- Constant Field Values
-
ID_KEY
public static final String ID_KEY
- See Also:
- Constant Field Values
-
VALUE_KEY
public static final String VALUE_KEY
- See Also:
- Constant Field Values
-
TTL_KEY
public static final String TTL_KEY
- See Also:
- Constant Field Values
-
ONE
public static final Double ONE
-
coll
protected com.mongodb.client.MongoCollection<org.bson.Document> coll
-
-
Method Detail
-
initialize
public void initialize(KeyValueStoreDescriptor descriptor)
Description copied from interface:KeyValueStoreProvider
Initializes this Key/Value store provider.- Specified by:
initialize
in interfaceKeyValueStoreProvider
- Overrides:
initialize
in classAbstractKeyValueStoreProvider
- Parameters:
descriptor
- the store provider descriptor
-
keyStream
public Stream<String> keyStream()
Description copied from interface:KeyValueStoreProvider
Returns aStream
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 aStream
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)
-
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 interfaceKeyValueStore
- Overrides:
getString
in classAbstractKeyValueStoreProvider
- 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 interfaceKeyValueStore
- Overrides:
getLong
in classAbstractKeyValueStoreProvider
- Parameters:
key
- the key- Returns:
- the value, or
null
if there is no value - Throws:
NumberFormatException
- if the value cannot be returned as aLong
-
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 interfaceKeyValueStore
- Overrides:
get
in classAbstractKeyValueStoreProvider
- 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 interfaceKeyValueStore
- Overrides:
getStrings
in classAbstractKeyValueStoreProvider
- 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 interfaceKeyValueStore
- Overrides:
getLongs
in classAbstractKeyValueStoreProvider
- Parameters:
keys
- the keys- Returns:
- the key/value map
- Throws:
NumberFormatException
- if one of the values cannot be returned as aLong
-
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 keybytes
- the value, which may benull
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 interfaceKeyValueStore
- Overrides:
put
in classAbstractKeyValueStoreProvider
- Parameters:
key
- the keystring
- the value, which may benull
-
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 interfaceKeyValueStore
- Overrides:
put
in classAbstractKeyValueStoreProvider
- Parameters:
key
- the keystring
- the value, which may benull
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 interfaceKeyValueStore
- Overrides:
put
in classAbstractKeyValueStoreProvider
- Parameters:
key
- the keyvalue
- the value, which may benull
-
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 interfaceKeyValueStore
- Overrides:
put
in classAbstractKeyValueStoreProvider
- Parameters:
key
- the keyvalue
- the value, which may benull
ttl
- the TTL, in seconds (0 for infinite)
-
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 keyttl
- the TTL, in seconds (0 for infinite)- Returns:
true
if the TTL has been set, orfalse
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 keyexpected
- the expected value, which may benull
value
- the updated value, which may benull
ttl
- the TTL, in seconds (0 for infinite)- Returns:
true
if the value was updated, orfalse
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 interfaceKeyValueStore
- Overrides:
compareAndSet
in classAbstractKeyValueStoreProvider
- Parameters:
key
- the keyexpected
- the expected value, which may benull
value
- the updated value, which may benull
ttl
- the TTL, in seconds (0 for infinite)- Returns:
true
if the value was updated, orfalse
if not (the expected value was not found)
-
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 returnnull
), it is interpreted as0
.- Specified by:
addAndGet
in interfaceKeyValueStore
- Overrides:
addAndGet
in classAbstractKeyValueStoreProvider
- Parameters:
key
- the keydelta
- the delta to add- Returns:
- the new value
- Throws:
NumberFormatException
- if the existing value cannot be interpreted as along
-
addAndGetGeneric
protected long addAndGetGeneric(String key, long delta) throws NumberFormatException
- Throws:
NumberFormatException
-
-