Class SQLKVDatabase
- All Implemented Interfaces:
KVDatabase
- Direct Known Subclasses:
CockroachKVDatabase,MSSQLKVDatabase,MySQLKVDatabase,SQLiteKVDatabase
KVDatabase implementations.
Key watches are not supported.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DataSourcestatic final StringDefault key column name ("kv_key").static final StringDefault table name ("KV").static final StringDefault value column name ("kv_value").protected IsolationLevelThe default transaction isolation level.protected StringThe name of the key column.protected final Loggerstatic final StringOption key forcreateTransaction(Map).protected StringThe name of the key/value table.protected StringThe name of the value column. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbeginTransaction(Connection connection) Open a new SQL transaction on the givenConnection.protected voidconfigureConnection(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?1and value?2A row with key?1may already exist; if so, the value should be updated to?2(if syntax requires it, the value may be updated to?3instead;?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 SQLKVTransactioncreateSQLKVTransaction(Connection connection) Create a newSQLKVTransactionfor a new transaction given the specifiedConnection.Create a new transaction.createTransaction(Map<String, ?> options) Create a new transaction.protected ConnectionCreate aConnectionfor a new transaction.Get theDataSourceused with this instance.Get the default transaction isolation level.protected IsolationLevelgetIsolationLevel(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 voidinitializeDatabaseIfNecessary(Connection connection) Perform any required database initialization.booleanGet whether to rollback read-only transactions.limitSingleRow(String sql) Modify the given SQL statement so that only one row is returned.protected voidpostBeginTransaction(Connection connection) Subclass hook invoked just after opening a new SQL transaction.protected voidpreBeginTransaction(Connection connection) Subclass hook invoked just before opening a new SQL transaction.Enquote a table or column name as necessary.voidsetDataSource(DataSource dataSource) Configure theDataSourceused with this instance.voidsetIsolationLevel(IsolationLevel isolationLevel) Configure the default transaction isolation level.voidsetKeyColumnName(String keyColumnName) Configure the name of the column containing keys.voidsetRollbackForReadOnly(boolean rollbackForReadOnly) Configure whether to rollback read-only transactions.voidsetTableName(String tableName) Set the name of the key/value table.voidsetValueColumnName(String valueColumnName) Configure the name of the column containing values.voidstart()Start this instance.voidstop()Stop this instance.wrapException(SQLKVTransaction transaction, SQLException e) Wrap the givenSQLExceptionin 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 anIsolationLevelinstance, 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 theDataSourceused with this instance.- Returns:
- the associated
DataSource
-
setDataSource
Configure theDataSourceused 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- iftableis 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- ifkeyColumnNameis 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- ifvalueColumnNameis 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- ifisolationLevelis 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:KVDatabaseStart 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:
startin interfaceKVDatabase
-
stop
@PreDestroy public void stop()Description copied from interface:KVDatabaseStop this instance.This method is idempotent: if this instance has not been started, or is already stopped, nothing happens.
- Specified by:
stopin interfaceKVDatabase
-
initializeDatabaseIfNecessary
Perform any required database initialization.The implementation in
SQLKVDatabasedoes nothing. Subclasses will typically invokeCREATE TABLE ... IF NOT EXISTShere, 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.SQLKVDatabaseinvokescreateTransactionConnection()to get aConnectionfor the new transaction, then invokes these methods in order:- Specified by:
createTransactionin interfaceKVDatabase- Parameters:
options- optional transaction options; may be null- Returns:
- newly created transaction
- Throws:
KVDatabaseException- if an unexpected error occursIllegalStateException- if noDataSourceis configured
-
createTransaction
Create a new transaction.The implementation in
SQLKVDatabaseinvokescreateTransaction(null)(i.e., with no options) and returns the result.- Specified by:
createTransactionin interfaceKVDatabase- Returns:
- newly created transaction
- Throws:
KVDatabaseException- if an unexpected error occursIllegalStateException- if noDataSourceis configured
-
getIsolationLevel
Extract theIsolationLevel, if any, from the transaction options.The implementation in
SQLKVDatabasesupports anIsolationLevelunder the keyOPTION_ISOLATION; also, theIsolationLevelmay be configured by the SpringPermazenTransactionManager, for example, using the@Transactionalannotation.- Parameters:
options- transaction options- Returns:
- transaction isolation
-
createTransactionConnection
Create aConnectionfor a new transaction.The implementation in
SQLKVDatabaseinvokesDataSource.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
SQLKVDatabasedoes 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
SQLKVDatabasedoes nothing. Note: subclasses must ensure the transaction is configured for theIsolationLevelconfigured on this instance.- Parameters:
connection- theConnectionfor a new transaction- Throws:
SQLException- if an error occurs- See Also:
-
beginTransaction
Open a new SQL transaction on the givenConnection.The implementation in
SQLKVDatabaseinvokesConnection.setAutoCommit(false).- Parameters:
connection- theConnectionfor 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
SQLKVDatabasedoes nothing. Note: subclasses must ensure the transaction is configured for theIsolationLevelconfigured on this instance.- Parameters:
connection- theConnectionfor a new transaction- Throws:
SQLException- if an error occurs- See Also:
-
createSQLKVTransaction
Create a newSQLKVTransactionfor a new transaction given the specifiedConnection. There will already be an SQL transaction open onconnection.The implementation in
SQLKVDatabasejust invokesnew SQLKVTransaction(this, connection).- Parameters:
connection- theConnectionfor 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?1and value?2A row with key?1may already exist; if so, the value should be updated to?2(if syntax requires it, the value may be updated to?3instead;?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
statementunmodified 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
SQLKVDatabasereturns its parameter unchanged.- Parameters:
sql- SQL statement- Returns:
- SQL statement
-
quote
Enquote a table or column name as necessary.The implementation in
SQLKVDatabasereturns its parameter unchanged.- Parameters:
name- table or column name- Returns:
nameenquoted as necessary for this database type- Throws:
IllegalArgumentException- ifnameis null
-
wrapException
Wrap the givenSQLExceptionin the appropriateKVTransactionException.- Parameters:
transaction- theSQLKVTransactionin which the exception occurede- SQL exception- Returns:
- appropriate
KVTransactionExceptionwith chained exceptione - Throws:
NullPointerException- ifeis null
-