@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 ("jsimpledb") and table name ("KV")
may also be overridden.
Batch Loading
Because Spanner has relatively high latency vs. throughput, instances utilize a BatchingKVStore
for batch loading and 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
org.jsimpledb.spring.JSimpleDBTransactionManager 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: "jsimpledb".
|
static String |
DEFAULT_TABLE_NAME
Default table name: "KV".
|
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() |
SpannerKVTransaction |
createTransaction(Map<String,?> options) |
protected SpannerKVTransaction |
createTransaction(TimestampBound consistency) |
protected ExecutorService |
getExecutorService() |
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.
|
ReadOnlySpannerView |
snapshot(TimestampBound consistency)
Create a read-only snapshot of the database with the given timestamp bound.
|
void |
start()
Start this instance.
|
void |
stop() |
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
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 "jsimpledb".
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()
stop in interface KVDatabasepublic SpannerKVTransaction createTransaction()
createTransaction in interface KVDatabasepublic SpannerKVTransaction createTransaction(Map<String,?> options)
createTransaction in interface KVDatabaseprotected 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 © 2017. All rights reserved.