Class SQLKeyValueStore
- java.lang.Object
-
- org.nuxeo.runtime.kv.AbstractKeyValueStoreProvider
-
- org.nuxeo.ecm.core.storage.sql.kv.SQLKeyValueStore
-
- All Implemented Interfaces:
KeyValueStore
,KeyValueStoreProvider
public class SQLKeyValueStore extends AbstractKeyValueStoreProvider
SQL implementation of a Key/Value Store Provider.The following configuration properties are available:
- datasource: the datasource to use.
- table: the table to use. The default is the Store name.
This implementation uses a table with a KEY column (unique and not NULL), and for the value one of these three columns is used: LONG, STRING, BYTES. If possible LONG is used, then STRING, otherwise BYTES.
The TTL is stored as an expiration time (seconds since epoch) in its own column. Expiration is done by a thread running a cleanup DELETE query every 60 seconds.
- Since:
- 10.10
-
-
Field Summary
Fields Modifier and Type Field Description static String
BYTES_COL
Bytes column, or NULL if the value is representable as a Long or String.protected Column
bytesCol
protected String
bytesColName
static String
DATASOURCE_PROP
Datasource configuration property.protected String
dataSourceName
protected String
deleteAllSQL
protected String
deleteIfBytesSQL
protected String
deleteIfLongSQL
protected String
deleteIfStringSQL
protected String
deleteSQL
protected Dialect
dialect
protected String
existsSQL
protected String
expireSQL
protected String
getLongSQL
protected String
getMultiSQL
protected String
getSQL
protected String
insertLongSQL
protected String
insertSQL
static String
KEY_COL
Key column, a short string.protected Column
keyCol
protected String
keyColName
protected String
keyStreamPrefixSQL
protected String
keyStreamSQL
protected JDBCLogger
logger
static String
LONG_COL
Long column, or NULL if the value is not representable as a Long.protected Column
longCol
protected String
longColName
protected static int
MAX_RETRY
protected String
setTTLSQL
static String
STRING_COL
String column, or NULL if the value is representable as a Long or not representable as a String.protected Column
stringCol
protected String
stringColName
protected TableImpl
table
static String
TABLE_PROP
Table configuration property.protected String
tableName
static String
TTL_COL
TTL column, holding expiration date in seconds since epoch, or NULL if there is no expiration.protected static int
TTL_EXPIRATION_FREQUENCY_MS
protected Column
ttlCol
protected String
ttlColName
protected Thread
ttlThread
protected String
updateLongSQL
protected String
updateReturningOracleSql
protected String
updateReturningPostgreSQLSql
protected String
updateReturningSQLServerSql
-
Fields inherited from class org.nuxeo.runtime.kv.AbstractKeyValueStoreProvider
name, UTF_8_DECODERS
-
-
Constructor Summary
Constructors Constructor Description SQLKeyValueStore()
-
Method Summary
All 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 Column
addColumn(String columnName, ColumnType type)
protected void
checkColumns(Connection connection)
Checks that columns have expected JDBC types.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
createTable(Connection connection)
protected String
escapeLike(String prefix)
protected void
expireTTLOnce()
protected void
expireTTLThread()
Runs in a thread to do TTL expiration.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 Connection
getConnection()
protected String
getDatabaseSchemaName(Connection connection)
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)
protected void
getObjects(Collection<String> keys, BiConsumer<String,Object> consumer)
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.protected void
getTable(Connection connection, String tbl)
protected Long
getTTLValue(long ttl)
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.protected Stream<String>
keyStream(Connection connection, String prefix)
protected String
nParams(int n)
protected void
prepareSQL()
void
put(String key, byte[] bytes)
Sets the value associated to the key.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.protected void
setToPreparedStatement(String sql, PreparedStatement ps, List<Column> columns, List<? extends Serializable> values)
protected void
setToPreparedStatement(String sql, PreparedStatement ps, Column column, Serializable value)
protected void
setToPreparedStatement(String sql, PreparedStatement ps, Column column1, Serializable value1, Column column2, Serializable value2)
protected void
setToPreparedStatement(String sql, PreparedStatement ps, Column column1, Serializable value1, Column column2, Serializable value2, Column column3, Serializable value3)
boolean
setTTL(String key, long ttl)
Sets the TTL for an existing key.protected void
sleepBeforeRetry()
protected void
startTTLThread()
protected void
stopTTLThread()
protected boolean
tableExists(Connection connection)
protected byte[]
toBytes(Object value)
protected Long
toLong(Object value)
protected Object
toStorage(Object value)
Canonicalizes value for the database: use a String or a Long if possible.String
toString()
protected String
toString(Object value)
protected Long
ttlToStorage(long ttl)
-
Methods inherited from class org.nuxeo.runtime.kv.AbstractKeyValueStoreProvider
bytesToLong, bytesToString, compareAndSet, compareAndSet, longToBytes, stringToBytes
-
-
-
-
Field Detail
-
DATASOURCE_PROP
public static final String DATASOURCE_PROP
Datasource configuration property.- See Also:
- Constant Field Values
-
TABLE_PROP
public static final String TABLE_PROP
Table configuration property. Default is the store name. The namespace is also used for disambiguation.- See Also:
- Constant Field Values
-
KEY_COL
public static final String KEY_COL
Key column, a short string.- See Also:
- Constant Field Values
-
LONG_COL
public static final String LONG_COL
Long column, or NULL if the value is not representable as a Long.- See Also:
- Constant Field Values
-
STRING_COL
public static final String STRING_COL
String column, or NULL if the value is representable as a Long or not representable as a String.- See Also:
- Constant Field Values
-
BYTES_COL
public static final String BYTES_COL
Bytes column, or NULL if the value is representable as a Long or String.- See Also:
- Constant Field Values
-
TTL_COL
public static final String TTL_COL
TTL column, holding expiration date in seconds since epoch, or NULL if there is no expiration.- See Also:
- Constant Field Values
-
TTL_EXPIRATION_FREQUENCY_MS
protected static final int TTL_EXPIRATION_FREQUENCY_MS
- See Also:
- Constant Field Values
-
MAX_RETRY
protected static final int MAX_RETRY
- See Also:
- Constant Field Values
-
logger
protected JDBCLogger logger
-
dataSourceName
protected String dataSourceName
-
dialect
protected Dialect dialect
-
table
protected TableImpl table
-
keyCol
protected Column keyCol
-
longCol
protected Column longCol
-
stringCol
protected Column stringCol
-
bytesCol
protected Column bytesCol
-
ttlCol
protected Column ttlCol
-
tableName
protected String tableName
-
keyColName
protected String keyColName
-
longColName
protected String longColName
-
stringColName
protected String stringColName
-
bytesColName
protected String bytesColName
-
ttlColName
protected String ttlColName
-
ttlThread
protected Thread ttlThread
-
getSQL
protected String getSQL
-
getMultiSQL
protected String getMultiSQL
-
getLongSQL
protected String getLongSQL
-
deleteAllSQL
protected String deleteAllSQL
-
deleteSQL
protected String deleteSQL
-
deleteIfLongSQL
protected String deleteIfLongSQL
-
deleteIfStringSQL
protected String deleteIfStringSQL
-
deleteIfBytesSQL
protected String deleteIfBytesSQL
-
expireSQL
protected String expireSQL
-
keyStreamSQL
protected String keyStreamSQL
-
keyStreamPrefixSQL
protected String keyStreamPrefixSQL
-
setTTLSQL
protected String setTTLSQL
-
existsSQL
protected String existsSQL
-
insertSQL
protected String insertSQL
-
insertLongSQL
protected String insertLongSQL
-
updateLongSQL
protected String updateLongSQL
-
updateReturningPostgreSQLSql
protected String updateReturningPostgreSQLSql
-
updateReturningOracleSql
protected String updateReturningOracleSql
-
updateReturningSQLServerSql
protected String updateReturningSQLServerSql
-
-
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
-
close
public void close()
Description copied from interface:KeyValueStoreProvider
Closes this Key/Value store provider.
-
getTable
protected void getTable(Connection connection, String tbl) throws SQLException
- Throws:
SQLException
-
addColumn
protected Column addColumn(String columnName, ColumnType type)
-
prepareSQL
protected void prepareSQL()
-
startTTLThread
protected void startTTLThread()
-
stopTTLThread
protected void stopTTLThread()
-
expireTTLThread
protected void expireTTLThread()
Runs in a thread to do TTL expiration.
-
toStorage
protected Object toStorage(Object value)
Canonicalizes value for the database: use a String or a Long if possible.
-
toBytes
protected byte[] toBytes(Object value)
-
toLong
protected Long toLong(Object value) throws NumberFormatException
- Throws:
NumberFormatException
-
getConnection
protected Connection getConnection() throws SQLException
- Throws:
SQLException
-
setToPreparedStatement
protected void setToPreparedStatement(String sql, PreparedStatement ps, Column column, Serializable value) throws SQLException
- Throws:
SQLException
-
setToPreparedStatement
protected void setToPreparedStatement(String sql, PreparedStatement ps, Column column1, Serializable value1, Column column2, Serializable value2) throws SQLException
- Throws:
SQLException
-
setToPreparedStatement
protected void setToPreparedStatement(String sql, PreparedStatement ps, Column column1, Serializable value1, Column column2, Serializable value2, Column column3, Serializable value3) throws SQLException
- Throws:
SQLException
-
setToPreparedStatement
protected void setToPreparedStatement(String sql, PreparedStatement ps, List<Column> columns, List<? extends Serializable> values) throws SQLException
- Throws:
SQLException
-
tableExists
protected boolean tableExists(Connection connection) throws SQLException
- Throws:
SQLException
-
createTable
protected void createTable(Connection connection) throws SQLException
- Throws:
SQLException
-
checkColumns
protected void checkColumns(Connection connection) throws SQLException
Checks that columns have expected JDBC types.- Throws:
SQLException
-
getDatabaseSchemaName
protected String getDatabaseSchemaName(Connection connection) throws SQLException
- Throws:
SQLException
-
expireTTLOnce
protected void expireTTLOnce()
-
clear
public void clear()
Description copied from interface:KeyValueStoreProvider
Clears the content of this Key/Value store provider.
-
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
-
keyStream
protected Stream<String> keyStream(Connection connection, String prefix) throws SQLException
- Throws:
SQLException
-
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
-
getObjects
protected void getObjects(Collection<String> keys, BiConsumer<String,Object> consumer)
-
nParams
protected String nParams(int n)
-
ttlToStorage
protected Long ttlToStorage(long ttl)
-
getTTLValue
protected Long getTTLValue(long ttl)
-
put
public void put(String key, byte[] bytes)
Description copied from interface:KeyValueStore
Sets the value associated to the key.- Specified by:
put
in interfaceKeyValueStore
- Overrides:
put
in classAbstractKeyValueStoreProvider
- Parameters:
key
- the keybytes
- the value, which may benull
-
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)
-
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
-
sleepBeforeRetry
protected void sleepBeforeRetry()
-
addAndGetGeneric
protected long addAndGetGeneric(String key, long delta) throws NumberFormatException
- Throws:
NumberFormatException
-
toString
public String toString()
- Overrides:
toString
in classAbstractKeyValueStoreProvider
-
-