Package org.nuxeo.runtime.kv
Class AbstractKeyValueStoreProvider
- java.lang.Object
-
- org.nuxeo.runtime.kv.AbstractKeyValueStoreProvider
-
- All Implemented Interfaces:
KeyValueStore
,KeyValueStoreProvider
- Direct Known Subclasses:
MemKeyValueStore
,MongoDBKeyValueStore
,RedisKeyValueStore
,SQLKeyValueStore
public abstract class AbstractKeyValueStoreProvider extends Object implements KeyValueStoreProvider
Key/Value Store common methods.- Since:
- 9.3
-
-
Field Summary
Fields Modifier and Type Field Description protected String
name
protected static ThreadLocal<CharsetDecoder>
UTF_8_DECODERS
-
Constructor Summary
Constructors Constructor Description AbstractKeyValueStoreProvider()
-
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 static Long
bytesToLong(byte[] bytes)
Converts UTF-8 bytes to a Long, or throws if malformed.protected static String
bytesToString(byte[] bytes)
Converts UTF-8 bytes to a String, or throws if malformed.boolean
compareAndSet(String key, byte[] expected, byte[] value)
Atomically sets the value associated to the key to the given value if the current value is the expected value.boolean
compareAndSet(String key, String expected, String value)
Atomically sets the value associated to the key to the given value if the current value is the expected value.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.Map<String,byte[]>
get(Collection<String> keys)
Retrieves the key/value map associated with the keys.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.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.protected static byte[]
longToBytes(Long value)
Converts a long to UTF-8 bytes.void
put(String key, byte[] value)
Sets the value associated to the key.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.void
put(String key, String value)
Sets the value associated to the key.void
put(String key, String value, long ttl)
Sets the value associated to the key, and a TTL.protected static byte[]
stringToBytes(String string)
Converts a String to UTF-8 bytes.String
toString()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.nuxeo.runtime.kv.KeyValueStore
compareAndSet, get, put, setTTL
-
Methods inherited from interface org.nuxeo.runtime.kv.KeyValueStoreProvider
clear, close, keyStream, keyStream
-
-
-
-
Field Detail
-
name
protected String name
-
UTF_8_DECODERS
protected static final ThreadLocal<CharsetDecoder> UTF_8_DECODERS
-
-
Method Detail
-
initialize
public void initialize(KeyValueStoreDescriptor descriptor)
Description copied from interface:KeyValueStoreProvider
Initializes this Key/Value store provider.- Specified by:
initialize
in interfaceKeyValueStoreProvider
- Parameters:
descriptor
- the store provider descriptor
-
bytesToString
protected static String bytesToString(byte[] bytes) throws CharacterCodingException
Converts UTF-8 bytes to a String, or throws if malformed.- Throws:
CharacterCodingException
-
stringToBytes
protected static byte[] stringToBytes(String string)
Converts a String to UTF-8 bytes.
-
bytesToLong
protected static Long bytesToLong(byte[] bytes) throws NumberFormatException
Converts UTF-8 bytes to a Long, or throws if malformed.- Throws:
NumberFormatException
-
longToBytes
protected static byte[] longToBytes(Long value)
Converts a long to UTF-8 bytes.
-
put
public void put(String key, byte[] value)
Description copied from interface:KeyValueStore
Sets the value associated to the key.- Specified by:
put
in interfaceKeyValueStore
- Parameters:
key
- the keyvalue
- the value, which may benull
-
put
public void put(String key, String value)
Description copied from interface:KeyValueStore
Sets the value associated to the key.- Specified by:
put
in interfaceKeyValueStore
- Parameters:
key
- the keyvalue
- the value, which may benull
-
put
public void put(String key, String value, long ttl)
Description copied from interface:KeyValueStore
Sets the value associated to the key, and a TTL.- Specified by:
put
in interfaceKeyValueStore
- Parameters:
key
- the keyvalue
- 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
- 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
- Parameters:
key
- the keyvalue
- the value, which may benull
ttl
- the TTL, in seconds (0 for infinite)
-
getString
public String getString(String key)
Description copied from interface:KeyValueStore
Retrieves the value associated to the key.- Specified by:
getString
in interfaceKeyValueStore
- 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
- 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
- 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
- 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
- Parameters:
keys
- the keys- Returns:
- the key/value map
- Throws:
NumberFormatException
- if one of the values cannot be returned as aLong
-
compareAndSet
public boolean compareAndSet(String key, byte[] expected, byte[] value)
Description copied from interface:KeyValueStore
Atomically sets the value associated to the key to the given value if the current value is the expected value.Note value comparison is done by value and not by reference.
- Specified by:
compareAndSet
in interfaceKeyValueStore
- Parameters:
key
- the keyexpected
- the expected value, which may benull
value
- the updated value, which may benull
- 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)
Description copied from interface:KeyValueStore
Atomically sets the value associated to the key to the given value if the current value is the expected value.Note value comparison is done by value and not by reference.
- Specified by:
compareAndSet
in interfaceKeyValueStore
- Parameters:
key
- the keyexpected
- the expected value, which may benull
value
- the updated value, which may benull
- 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
- 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
- Parameters:
key
- the keydelta
- the delta to add- Returns:
- the new value
- Throws:
NumberFormatException
- if the existing value cannot be interpreted as along
-
-