public class SQLKeyValueStore extends AbstractKeyValueStoreProvider
The following configuration properties are available:
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.
Modifier and Type | Class and Description |
---|---|
protected static interface |
SQLKeyValueStore.SQLConsumer<T>
A
Consumer that can throw SQLException . |
protected static interface |
SQLKeyValueStore.SQLFunction<T,R>
A
Function that can throw SQLException . |
Modifier and Type | Field and 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 |
name, UTF_8_DECODERS
Constructor and Description |
---|
SQLKeyValueStore() |
Modifier and Type | Method and 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.
|
Map<String,byte[]> |
get(Collection<String> keys)
Retrieves the key/value map associated with the keys.
|
byte[] |
get(String key)
Retrieves the value associated to the key.
|
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 a
Stream of the keys contained in this Key/Value store provider. |
protected Stream<String> |
keyStream(Connection connection,
String prefix) |
Stream<String> |
keyStream(String prefix)
Returns a
Stream of the keys with the given prefix contained in this Key/Value store provider. |
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 |
runWithConnection(SQLKeyValueStore.SQLConsumer<Connection> consumer) |
protected <R> R |
runWithConnection(SQLKeyValueStore.SQLFunction<Connection,R> function) |
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) |
protected void |
setToPreparedStatement(String sql,
PreparedStatement ps,
List<Column> columns,
List<? extends Serializable> values) |
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) |
bytesToLong, bytesToString, compareAndSet, compareAndSet, longToBytes, stringToBytes
public static final String DATASOURCE_PROP
public static final String TABLE_PROP
public static final String KEY_COL
public static final String LONG_COL
public static final String STRING_COL
public static final String BYTES_COL
public static final String TTL_COL
protected static final int TTL_EXPIRATION_FREQUENCY_MS
protected static final int MAX_RETRY
protected JDBCLogger logger
protected String dataSourceName
protected String keyColName
protected String longColName
protected String stringColName
protected String bytesColName
protected String ttlColName
protected String getMultiSQL
protected String getLongSQL
protected String deleteAllSQL
protected String deleteIfLongSQL
protected String deleteIfStringSQL
protected String deleteIfBytesSQL
protected String keyStreamSQL
protected String keyStreamPrefixSQL
protected String insertLongSQL
protected String updateLongSQL
protected String updateReturningPostgreSQLSql
protected String updateReturningOracleSql
protected String updateReturningSQLServerSql
public SQLKeyValueStore()
public void initialize(KeyValueStoreDescriptor descriptor)
KeyValueStoreProvider
initialize
in interface KeyValueStoreProvider
initialize
in class AbstractKeyValueStoreProvider
descriptor
- the store provider descriptorpublic void close()
KeyValueStoreProvider
protected void getTable(Connection connection, String tbl) throws SQLException
SQLException
protected Column addColumn(String columnName, ColumnType type)
protected void prepareSQL()
protected void startTTLThread()
protected void stopTTLThread()
protected void expireTTLThread()
protected String escapeLike(String prefix)
protected Object toStorage(Object value)
protected Long toLong(Object value) throws NumberFormatException
NumberFormatException
protected void runWithConnection(SQLKeyValueStore.SQLConsumer<Connection> consumer)
protected <R> R runWithConnection(SQLKeyValueStore.SQLFunction<Connection,R> function)
protected Connection getConnection() throws SQLException
SQLException
protected void setToPreparedStatement(String sql, PreparedStatement ps, Column column, Serializable value) throws SQLException
SQLException
protected void setToPreparedStatement(String sql, PreparedStatement ps, Column column1, Serializable value1, Column column2, Serializable value2) throws SQLException
SQLException
protected void setToPreparedStatement(String sql, PreparedStatement ps, Column column1, Serializable value1, Column column2, Serializable value2, Column column3, Serializable value3) throws SQLException
SQLException
protected void setToPreparedStatement(String sql, PreparedStatement ps, List<Column> columns, List<? extends Serializable> values) throws SQLException
SQLException
protected boolean tableExists(Connection connection) throws SQLException
SQLException
protected void createTable(Connection connection) throws SQLException
SQLException
protected void checkColumns(Connection connection) throws SQLException
SQLException
protected String getDatabaseSchemaName(Connection connection) throws SQLException
SQLException
protected void expireTTLOnce()
public void clear()
KeyValueStoreProvider
public Stream<String> keyStream()
KeyValueStoreProvider
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.
public Stream<String> keyStream(String prefix)
KeyValueStoreProvider
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.
protected Stream<String> keyStream(Connection connection, String prefix) throws SQLException
SQLException
public byte[] get(String key)
KeyValueStore
key
- the keynull
if there is no valuepublic String getString(String key)
KeyValueStore
getString
in interface KeyValueStore
getString
in class AbstractKeyValueStoreProvider
key
- the keynull
if there is no valuepublic Long getLong(String key) throws NumberFormatException
KeyValueStore
getLong
in interface KeyValueStore
getLong
in class AbstractKeyValueStoreProvider
key
- the keynull
if there is no valueNumberFormatException
- if the value cannot be returned as a Long
public Map<String,byte[]> get(Collection<String> keys)
KeyValueStore
get
in interface KeyValueStore
get
in class AbstractKeyValueStoreProvider
keys
- the keyspublic Map<String,String> getStrings(Collection<String> keys)
KeyValueStore
getStrings
in interface KeyValueStore
getStrings
in class AbstractKeyValueStoreProvider
keys
- the keyspublic Map<String,Long> getLongs(Collection<String> keys) throws NumberFormatException
KeyValueStore
getLongs
in interface KeyValueStore
getLongs
in class AbstractKeyValueStoreProvider
keys
- the keysNumberFormatException
- if one of the values cannot be returned as a Long
protected void getObjects(Collection<String> keys, BiConsumer<String,Object> consumer)
protected Long ttlToStorage(long ttl)
protected Long getTTLValue(long ttl)
public void put(String key, byte[] bytes)
KeyValueStore
put
in interface KeyValueStore
put
in class AbstractKeyValueStoreProvider
key
- the keybytes
- the value, which may be null
public void put(String key, byte[] bytes, long ttl)
KeyValueStore
key
- the keybytes
- the value, which may be null
ttl
- the TTL, in seconds (0 for infinite)public void put(String key, String string)
KeyValueStore
put
in interface KeyValueStore
put
in class AbstractKeyValueStoreProvider
key
- the keystring
- the value, which may be null
public void put(String key, String string, long ttl)
KeyValueStore
put
in interface KeyValueStore
put
in class AbstractKeyValueStoreProvider
key
- the keystring
- the value, which may be null
ttl
- the TTL, in seconds (0 for infinite)public void put(String key, Long value)
KeyValueStore
put
in interface KeyValueStore
put
in class AbstractKeyValueStoreProvider
key
- the keyvalue
- the value, which may be null
public void put(String key, Long value, long ttl)
KeyValueStore
put
in interface KeyValueStore
put
in class AbstractKeyValueStoreProvider
key
- the keyvalue
- the value, which may be null
ttl
- the TTL, in seconds (0 for infinite)public boolean setTTL(String key, long ttl)
KeyValueStore
key
- the keyttl
- the TTL, in seconds (0 for infinite)true
if the TTL has been set, or false
if the key does not existpublic boolean compareAndSet(String key, byte[] expected, byte[] value, long ttl)
KeyValueStore
Note value comparison is done by value and not by reference.
key
- the keyexpected
- the expected value, which may be null
value
- the updated value, which may be null
ttl
- the TTL, in seconds (0 for infinite)true
if the value was updated, or false
if not (the expected value was not found)public boolean compareAndSet(String key, String expected, String value, long ttl)
KeyValueStore
Note value comparison is done by value and not by reference.
compareAndSet
in interface KeyValueStore
compareAndSet
in class AbstractKeyValueStoreProvider
key
- the keyexpected
- the expected value, which may be null
value
- the updated value, which may be null
ttl
- the TTL, in seconds (0 for infinite)true
if the value was updated, or false
if not (the expected value was not found)protected boolean compareAndSet(String key, Object expected, Object value, long ttl)
public long addAndGet(String key, long delta) throws NumberFormatException
KeyValueStore
If the value does not exist (if KeyValueStore.get(java.lang.String)
would return null
), it is interpreted as 0
.
addAndGet
in interface KeyValueStore
addAndGet
in class AbstractKeyValueStoreProvider
key
- the keydelta
- the delta to addNumberFormatException
- if the existing value cannot be interpreted as a long
protected void sleepBeforeRetry()
protected long addAndGetGeneric(String key, long delta) throws NumberFormatException
NumberFormatException
public String toString()
toString
in class AbstractKeyValueStoreProvider
Copyright © 2019 Nuxeo. All rights reserved.