@ThreadSafe public class SpannerKVDatabase extends Object implements KVDatabase
KVDatabase
implementation based on
Google Cloud Spanner.
Configuration
A SpannerKVDatabase
must be configured with a Spanner instance ID. You can create a Spanner instance
using the Google Cloud Console or the gcloud
command line utility.
Configure a SpannerOptions
to override the default project ID associated
with your environment. The default database ID ("permazen") and table name ("KV")
may also be overridden.
Caching
Because Spanner has relatively high latency vs. throughput, instances utilize a CachingKVStore
for caching and batch loading read-ahead.
Consistency Levels
Transactions may either have strong consistency (the default), or have some amount of staleness. Transactions that are not strongly consistent must be read-only.
A TimestampBound
may be passed as an option to createTransaction()
under the "TimestampBound" key.
Transactions that are not strongly consistent must be read-only.
In Spring applications, the transaction consistency level may be configured through the Spring
PermazenTransactionManager
by (ab)using the transaction isolation level setting,
for example, via the @Transactional
annotation's
isolation()
property:
Spring isolation level | SpannerKVDatabase consistency level |
---|---|
DEFAULT |
Strong consistency |
SERIALIZABLE |
Strong consistency |
READ_COMMITTED |
Exact staleness of ten seconds |
REPEATABLE_READ |
Exact staleness of three seconds |
READ_UNCOMMITTED |
N/A |
Key Watches
Key watches are not supported.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_DATABASE_ID
Default database ID: "permazen".
|
static String |
DEFAULT_TABLE_NAME
Default table name: "KV".
|
static int |
DEFAULT_THREAD_POOL_SIZE
Default background task thread pool size.
|
protected Logger |
log |
static String |
OPTION_TIMESTAMP_BOUND
Option key for
createTransaction(Map) . |
Constructor and Description |
---|
SpannerKVDatabase() |
Modifier and Type | Method and Description |
---|---|
SpannerKVTransaction |
createTransaction()
Create a new transaction.
|
SpannerKVTransaction |
createTransaction(Map<String,?> options)
Create a new transaction with the specified options.
|
protected SpannerKVTransaction |
createTransaction(TimestampBound consistency) |
protected ExecutorService |
getExecutorService() |
double |
getRttEstimate()
Get the current round trip time estimate.
|
int |
getThreadPoolSize()
Get the number of threads in the background task thread pool.
|
void |
setDatabaseId(String databaseId)
Set Spanner database ID.
|
void |
setInstanceId(String instanceId)
Set Spanner instance ID.
|
void |
setSpannerOptions(SpannerOptions spannerOptions)
Configure
SpannerOptions . |
void |
setTableName(String tableName)
Set Spanner table name.
|
void |
setThreadPoolSize(int threadPoolSize)
Set the number of threads in the background task thread pool.
|
ReadOnlySpannerView |
snapshot(TimestampBound consistency)
Create a read-only snapshot of the database with the given timestamp bound.
|
void |
start()
Start this instance.
|
void |
stop()
Stop this instance.
|
public static final String OPTION_TIMESTAMP_BOUND
createTransaction(Map)
. Value should be a TimestampBound
instance.public static final String DEFAULT_DATABASE_ID
public static final String DEFAULT_TABLE_NAME
public static final int DEFAULT_THREAD_POOL_SIZE
protected final Logger log
public void setSpannerOptions(SpannerOptions spannerOptions)
SpannerOptions
.spannerOptions
- Google spanner configuration optionsIllegalStateException
- if this instance is already startedIllegalArgumentException
- if spannerOptions
is nullpublic void setInstanceId(String instanceId)
Required property.
instanceId
- Spanner instance IDIllegalStateException
- if this instance is already startedIllegalArgumentException
- if instanceId
is nullpublic void setDatabaseId(String databaseId)
Default is "permazen".
databaseId
- Spanner instance IDIllegalStateException
- if this instance is already startedIllegalArgumentException
- if databaseId
is nullpublic void setTableName(String tableName)
Default is "KV".
tableName
- Spanner database table nameIllegalStateException
- if this instance is already startedIllegalArgumentException
- if tableName
is invalidIllegalArgumentException
- if tableName
is null@PostConstruct public void start()
The configured Spanner instance, database, and table will be created automatically as needed.
start
in interface KVDatabase
@PreDestroy public void stop()
KVDatabase
This method is idempotent: if this instance has not been started, or is already stopped, nothing happens.
stop
in interface KVDatabase
public int getThreadPoolSize()
Default value is 10.
public void setThreadPoolSize(int threadPoolSize)
Default value is 10.
threadPoolSize
- number of threads in thread poolIllegalStateException
- if this instance is already startedIllegalArgumentException
- if threadPoolSize <= 0
public double getRttEstimate()
IllegalStateException
- if this instance has never start()
edpublic SpannerKVTransaction createTransaction()
KVDatabase
createTransaction
in interface KVDatabase
public SpannerKVTransaction createTransaction(Map<String,?> options)
KVDatabase
createTransaction
in interface KVDatabase
options
- optional transaction options; may be nullprotected SpannerKVTransaction createTransaction(TimestampBound consistency)
protected ExecutorService getExecutorService()
public ReadOnlySpannerView snapshot(TimestampBound consistency)
consistency
- consistency for the snapshotIllegalArgumentException
- if consistency
is nullIllegalStateException
- if this instance is not start()
edCopyright © 2022. All rights reserved.