Class SpannerKVDatabase
- All Implemented Interfaces:
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.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Default database ID: "permazen".static final String
Default table name: "KV".static final int
Default background task thread pool size.protected final Logger
static final String
Option key forcreateTransaction(Map)
. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionCreate a new transaction.protected SpannerKVTransaction
createTransaction
(com.google.cloud.spanner.TimestampBound consistency) createTransaction
(Map<String, ?> options) Create a new transaction with the specified options.protected ExecutorService
double
Get the current round trip time estimate.int
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
(com.google.cloud.spanner.SpannerOptions spannerOptions) ConfigureSpannerOptions
.void
setTableName
(String tableName) Set Spanner table name.void
setThreadPoolSize
(int threadPoolSize) Set the number of threads in the background task thread pool.snapshot
(com.google.cloud.spanner.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.
-
Field Details
-
OPTION_TIMESTAMP_BOUND
Option key forcreateTransaction(Map)
. Value should be aTimestampBound
instance.- See Also:
-
DEFAULT_DATABASE_ID
Default database ID: "permazen".- See Also:
-
DEFAULT_TABLE_NAME
Default table name: "KV".- See Also:
-
DEFAULT_THREAD_POOL_SIZE
public static final int DEFAULT_THREAD_POOL_SIZEDefault background task thread pool size.- See Also:
-
log
-
-
Constructor Details
-
SpannerKVDatabase
public SpannerKVDatabase()
-
-
Method Details
-
setSpannerOptions
public void setSpannerOptions(com.google.cloud.spanner.SpannerOptions spannerOptions) ConfigureSpannerOptions
.- Parameters:
spannerOptions
- Google spanner configuration options- Throws:
IllegalStateException
- if this instance is already startedIllegalArgumentException
- ifspannerOptions
is null
-
setInstanceId
Set Spanner instance ID.Required property.
- Parameters:
instanceId
- Spanner instance ID- Throws:
IllegalStateException
- if this instance is already startedIllegalArgumentException
- ifinstanceId
is null
-
setDatabaseId
Set Spanner database ID.Default is "permazen".
- Parameters:
databaseId
- Spanner instance ID- Throws:
IllegalStateException
- if this instance is already startedIllegalArgumentException
- ifdatabaseId
is null
-
setTableName
Set Spanner table name.Default is "KV".
- Parameters:
tableName
- Spanner database table name- Throws:
IllegalStateException
- if this instance is already startedIllegalArgumentException
- iftableName
is invalidIllegalArgumentException
- iftableName
is null
-
start
@PostConstruct public void start()Start this instance.The configured Spanner instance, database, and table will be created automatically as needed.
- 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
-
getThreadPoolSize
public int getThreadPoolSize()Get the number of threads in the background task thread pool.Default value is 10.
- Returns:
- number of threads in thread pool
-
setThreadPoolSize
public void setThreadPoolSize(int threadPoolSize) Set the number of threads in the background task thread pool.Default value is 10.
- Parameters:
threadPoolSize
- number of threads in thread pool- Throws:
IllegalStateException
- if this instance is already startedIllegalArgumentException
- ifthreadPoolSize <= 0
-
getRttEstimate
public double getRttEstimate()Get the current round trip time estimate.- Returns:
- current RTT estimate in nanoseconds
- Throws:
IllegalStateException
- if this instance has neverstart()
ed
-
createTransaction
Description copied from interface:KVDatabase
Create a new transaction.- Specified by:
createTransaction
in interfaceKVDatabase
- Returns:
- newly created transaction
-
createTransaction
Description copied from interface:KVDatabase
Create a new transaction with the specified options.- Specified by:
createTransaction
in interfaceKVDatabase
- Parameters:
options
- optional transaction options; may be null- Returns:
- newly created transaction
-
createTransaction
protected SpannerKVTransaction createTransaction(com.google.cloud.spanner.TimestampBound consistency) -
getExecutorService
-
snapshot
Create a read-only snapshot of the database with the given timestamp bound.- Parameters:
consistency
- consistency for the snapshot- Returns:
- read-only view of database
- Throws:
IllegalArgumentException
- ifconsistency
is nullIllegalStateException
- if this instance is notstart()
ed
-