Class SQLKVDatabase
- All Implemented Interfaces:
KVDatabase
- Direct Known Subclasses:
CockroachKVDatabase
,MSSQLKVDatabase
,MySQLKVDatabase
,SQLiteKVDatabase
KVDatabase
implementations.
Key watches are not supported.
-
Field Summary
Modifier and TypeFieldDescriptionprotected DataSource
static final String
Default key column name ("kv_key").static final String
Default table name ("KV").static final String
Default value column name ("kv_value").protected IsolationLevel
The default transaction isolation level.protected String
The name of the key column.protected final Logger
static final String
Option key forcreateTransaction(Map)
.protected String
The name of the key/value table.protected String
The name of the value column. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
beginTransaction
(Connection connection) Open a new SQL transaction on the givenConnection
.protected void
configureConnection
(Connection connection) Configure a newly createdConnection
.createGetAllStatement
(boolean reverse) Create an SQL statement that reads all of the key and value columns (in that order), possibly reversed.createGetAtLeastStatement
(boolean reverse) Create an SQL statement that reads the key and value columns (in that order) associated with the smallest key greater than or equal to?1
, if any.createGetAtMostStatement
(boolean reverse) Create an SQL statement that reads the key and value columns (in that order) associated with the greatest key strictly less than?1
, if any.createGetRangeStatement
(boolean reverse) Create an SQL statement that reads the key and value columns (in that order) associated with all keys in the range?1
(inclusive) to?2
(exclusive), possibly reversed.Create an SQL statement that reads the value column associated with key?1
.Create an SQL statement that inserts the key/value pair with key?1
and value?2
A row with key?1
may already exist; if so, the value should be updated to?2
(if syntax requires it, the value may be updated to?3
instead;?3
, if it exists, will be set to the same value as?2
).Create an SQL statement that deletes all rows.Create an SQL statement that deletes all rows with keys greater than or equal to?1
.Create an SQL statement that deletes all rows with keys strictly less than?1
.Create an SQL statement that deletes all rows with keys in the range?1
(inclusive} to?2
(exclusive).Create an SQL statement that deletes the row associated with key?1
, if any.protected SQLKVTransaction
createSQLKVTransaction
(Connection connection) Create a newSQLKVTransaction
for a new transaction given the specifiedConnection
.Create a new transaction.createTransaction
(Map<String, ?> options) Create a new transaction.protected Connection
Create aConnection
for a new transaction.Get theDataSource
used with this instance.Get the default transaction isolation level.protected IsolationLevel
getIsolationLevel
(Map<String, ?> options) Extract theIsolationLevel
, if any, from the transaction options.Get the name of the column containing keys.Get the name of the table containing keys and values.Get the name of the column containing values.protected void
initializeDatabaseIfNecessary
(Connection connection) Perform any required database initialization.boolean
Get whether to rollback read-only transactions.limitSingleRow
(String sql) Modify the given SQL statement so that only one row is returned.protected void
postBeginTransaction
(Connection connection) Subclass hook invoked just after opening a new SQL transaction.protected void
preBeginTransaction
(Connection connection) Subclass hook invoked just before opening a new SQL transaction.Enquote a table or column name as necessary.void
setDataSource
(DataSource dataSource) Configure theDataSource
used with this instance.void
setIsolationLevel
(IsolationLevel isolationLevel) Configure the default transaction isolation level.void
setKeyColumnName
(String keyColumnName) Configure the name of the column containing keys.void
setRollbackForReadOnly
(boolean rollbackForReadOnly) Configure whether to rollback read-only transactions.void
setTableName
(String tableName) Set the name of the key/value table.void
setValueColumnName
(String valueColumnName) Configure the name of the column containing values.void
start()
Start this instance.void
stop()
Stop this instance.wrapException
(SQLKVTransaction transaction, SQLException e) Wrap the givenSQLException
in the appropriateKVTransactionException
.
-
Field Details
-
DEFAULT_TABLE_NAME
Default table name ("KV").- See Also:
-
DEFAULT_KEY_COLUMN_NAME
Default key column name ("kv_key").- See Also:
-
DEFAULT_VALUE_COLUMN_NAME
Default value column name ("kv_value").- See Also:
-
OPTION_ISOLATION
Option key forcreateTransaction(Map)
. Value should be anIsolationLevel
instance, or thename()
thereof.- See Also:
-
dataSource
-
tableName
The name of the key/value table. Default value is "KV". -
keyColumnName
The name of the key column. Default value is "kv_key". -
valueColumnName
The name of the value column. Default value is "kv_value". -
isolationLevel
The default transaction isolation level. Default isIsolationLevel.SERIALIZABLE
. -
log
-
-
Constructor Details
-
SQLKVDatabase
public SQLKVDatabase()
-
-
Method Details
-
getDataSource
Get theDataSource
used with this instance.- Returns:
- the associated
DataSource
-
setDataSource
Configure theDataSource
used with this instance.Required property.
- Parameters:
dataSource
- access to the underlying database
-
getTableName
Get the name of the table containing keys and values.Default value is "KV".
- Returns:
- key/value table name
-
setTableName
Set the name of the key/value table.- Parameters:
tableName
- the name of the key/value table- Throws:
IllegalArgumentException
- iftable
is null
-
getKeyColumnName
Get the name of the column containing keys.Default value is "kv_key".
- Returns:
- the name of the key column
-
setKeyColumnName
Configure the name of the column containing keys.- Parameters:
keyColumnName
- the name of the key column- Throws:
IllegalArgumentException
- ifkeyColumnName
is null
-
getValueColumnName
Get the name of the column containing values.Default value is "kv_value".
- Returns:
- the name of the value column
-
setValueColumnName
Configure the name of the column containing values.- Parameters:
valueColumnName
- the name of the value column- Throws:
IllegalArgumentException
- ifvalueColumnName
is null
-
getIsolationLevel
Get the default transaction isolation level.This may be overridden on a per-transaction basis; see
getIsolationLevel(Map)
.Default value is
IsolationLevel.SERIALIZABLE
.- Returns:
- isolation level
-
setIsolationLevel
Configure the default transaction isolation level.- Parameters:
isolationLevel
- isolation level- Throws:
IllegalArgumentException
- ifisolationLevel
is null
-
isRollbackForReadOnly
public boolean isRollbackForReadOnly()Get whether to rollback read-only transactions.If true, read-only transactions will be rolled back on commit. If false, mutations during read-only transactions will be captured in memory and not written to the database, and the underlying SQL transaction committed on commit. Some implementations require the latter in order to guarantee up-to-date reads within the transaction.
Default value is true.
- Returns:
- whether to use rollback when committing a read-only transaction
-
setRollbackForReadOnly
public void setRollbackForReadOnly(boolean rollbackForReadOnly) Configure whether to rollback read-only transactions.- Parameters:
rollbackForReadOnly
- true to use rollback when committing a read-only transaction.- See Also:
-
start
@PostConstruct public void start()Description copied from interface:KVDatabase
Start this instance. This method must be called prior to creating any transactions.This method is idempotent: if this instance is already started, nothing happens.
Whether an instance that has been started and stopped can be restarted is implementation-dependent.
- Specified by:
start
in interfaceKVDatabase
-
stop
@PreDestroy public void stop()Description copied from interface:KVDatabase
Stop this instance.This method is idempotent: if this instance has not been started, or is already stopped, nothing happens.
- Specified by:
stop
in interfaceKVDatabase
-
initializeDatabaseIfNecessary
Perform any required database initialization.The implementation in
SQLKVDatabase
does nothing. Subclasses will typically invokeCREATE TABLE ... IF NOT EXISTS
here, etc.- Parameters:
connection
- open database connection (will be closed by the caller of this method)- Throws:
SQLException
- if an error occurs
-
createTransaction
Create a new transaction.The implementation in
and returns the result.SQLKVDatabase
invokescreateTransactionConnection()
to get aConnection
for the new transaction, then invokes these methods in order:- Specified by:
createTransaction
in interfaceKVDatabase
- Parameters:
options
- optional transaction options; may be null- Returns:
- newly created transaction
- Throws:
KVDatabaseException
- if an unexpected error occursIllegalStateException
- if noDataSource
is configured
-
createTransaction
Create a new transaction.The implementation in
SQLKVDatabase
invokescreateTransaction(null)
(i.e., with no options) and returns the result.- Specified by:
createTransaction
in interfaceKVDatabase
- Returns:
- newly created transaction
- Throws:
KVDatabaseException
- if an unexpected error occursIllegalStateException
- if noDataSource
is configured
-
getIsolationLevel
Extract theIsolationLevel
, if any, from the transaction options.The implementation in
SQLKVDatabase
supports anIsolationLevel
under the keyOPTION_ISOLATION
; also, theIsolationLevel
may be configured by the SpringPermazenTransactionManager
, for example, using the@Transactional
annotation.- Parameters:
options
- transaction options- Returns:
- transaction isolation
-
createTransactionConnection
Create aConnection
for a new transaction.The implementation in
SQLKVDatabase
invokesDataSource.getConnection()
on the configuredDataSource
, and then deletates toconfigureConnection()
to perform any required configuration of the newConnection
.- Returns:
- new transaction
Connection
- Throws:
SQLException
- if an error occurs
-
configureConnection
Configure a newly createdConnection
.The implementation in
SQLKVDatabase
does nothing.- Parameters:
connection
- newly createdConnection
- Throws:
SQLException
- if an error occurs
-
preBeginTransaction
Subclass hook invoked just before opening a new SQL transaction.The implementation in
SQLKVDatabase
does nothing. Note: subclasses must ensure the transaction is configured for theIsolationLevel
configured on this instance.- Parameters:
connection
- theConnection
for a new transaction- Throws:
SQLException
- if an error occurs- See Also:
-
beginTransaction
Open a new SQL transaction on the givenConnection
.The implementation in
SQLKVDatabase
invokesConnection.setAutoCommit(false)
.- Parameters:
connection
- theConnection
for a new transaction- Throws:
SQLException
- if an error occurs- See Also:
-
postBeginTransaction
Subclass hook invoked just after opening a new SQL transaction.The implementation in
SQLKVDatabase
does nothing. Note: subclasses must ensure the transaction is configured for theIsolationLevel
configured on this instance.- Parameters:
connection
- theConnection
for a new transaction- Throws:
SQLException
- if an error occurs- See Also:
-
createSQLKVTransaction
Create a newSQLKVTransaction
for a new transaction given the specifiedConnection
. There will already be an SQL transaction open onconnection
.The implementation in
SQLKVDatabase
just invokesnew SQLKVTransaction(this, connection)
.- Parameters:
connection
- theConnection
for a new transaction- Returns:
- newly created transaction
- Throws:
SQLException
- if an error occurs
-
createGetStatement
Create an SQL statement that reads the value column associated with key?1
.- Returns:
- SQL query statement
-
createGetAtLeastStatement
Create an SQL statement that reads the key and value columns (in that order) associated with the smallest key greater than or equal to?1
, if any.- Parameters:
reverse
- true to return rows in descending key order, false to return rows in ascending key order- Returns:
- SQL query statement
-
createGetAtMostStatement
Create an SQL statement that reads the key and value columns (in that order) associated with the greatest key strictly less than?1
, if any.- Parameters:
reverse
- true to return rows in descending key order, false to return rows in ascending key order- Returns:
- SQL query statement
-
createGetRangeStatement
Create an SQL statement that reads the key and value columns (in that order) associated with all keys in the range?1
(inclusive) to?2
(exclusive), possibly reversed.- Parameters:
reverse
- true to return rows in descending key order, false to return rows in ascending key order- Returns:
- SQL query statement
-
createGetAllStatement
Create an SQL statement that reads all of the key and value columns (in that order), possibly reversed.- Parameters:
reverse
- true to return rows in descending key order, false to return rows in ascending key order- Returns:
- SQL query statement
-
createPutStatement
Create an SQL statement that inserts the key/value pair with key?1
and value?2
A row with key?1
may already exist; if so, the value should be updated to?2
(if syntax requires it, the value may be updated to?3
instead;?3
, if it exists, will be set to the same value as?2
).- Returns:
- SQL insertion statement
-
createRemoveStatement
Create an SQL statement that deletes the row associated with key?1
, if any. Note that the key may or may not exist prior to this method being invoked.- Returns:
- SQL delete statement
-
createRemoveRangeStatement
Create an SQL statement that deletes all rows with keys in the range?1
(inclusive} to?2
(exclusive).- Returns:
- SQL delete statement
-
createRemoveAtLeastStatement
Create an SQL statement that deletes all rows with keys greater than or equal to?1
.- Returns:
- SQL delete statement
-
createRemoveAtMostStatement
Create an SQL statement that deletes all rows with keys strictly less than?1
.- Returns:
- SQL delete statement
-
createRemoveAllStatement
Create an SQL statement that deletes all rows.- Returns:
- SQL delete statement
-
limitSingleRow
Modify the given SQL statement so that only one row is returned.This is an optional method; returning
statement
unmodified is acceptable, but subclasses may be able to improve efficiency by modifying the SQL statement in a vendor-specific manner to only return one row.The implementation in
SQLKVDatabase
returns its parameter unchanged.- Parameters:
sql
- SQL statement- Returns:
- SQL statement
-
quote
Enquote a table or column name as necessary.The implementation in
SQLKVDatabase
returns its parameter unchanged.- Parameters:
name
- table or column name- Returns:
name
enquoted as necessary for this database type- Throws:
IllegalArgumentException
- ifname
is null
-
wrapException
Wrap the givenSQLException
in the appropriateKVTransactionException
.- Parameters:
transaction
- theSQLKVTransaction
in which the exception occurede
- SQL exception- Returns:
- appropriate
KVTransactionException
with chained exceptione
- Throws:
NullPointerException
- ife
is null
-