Package com.google.cloud.spanner.watcher
Class SpannerTableChangeSetPoller
- java.lang.Object
-
- com.google.api.core.AbstractApiService
-
- com.google.cloud.spanner.watcher.SpannerTableChangeSetPoller
-
- All Implemented Interfaces:
com.google.api.core.ApiService,SpannerTableChangeWatcher
public class SpannerTableChangeSetPoller extends com.google.api.core.AbstractApiService implements SpannerTableChangeWatcher
Implementation of theSpannerTableChangeWatcherinterface that continuously polls a change set table for new rows, and then polls an actual data table for any changes that was part of the last seen change set. This requires all client applications to insert a new row to a CHANGE_SETS table for each read/write transaction that is executed. While this does add an extra write operation to each read/write transaction, it has two advantages:- It does not require each data table to have a column that stores the last commit timestamp. Commit timestamp columns that are filled using DML will prevent the same transaction from reading anything from the same table for the remainder of the transaction. See https://cloud.google.com/spanner/docs/commit-timestamp#dml.
- Instead of having a commit timestamp column in each data table, the data tables contain a CHANGE_SET_ID column. This column can be of any type and can contain a random value. This makes the column easier to index than a commit timestamp column, as an index on a truly random id will not suffer from hotspots (https://cloud.google.com/spanner/docs/schema-design#primary-key-prevent-hotspots).
Usage:
{@code String instance = "my-instance"; String database = "my-database"; String table = "MY_TABLE"; Spanner spanner = SpannerOptions.getDefaultInstance().getService(); TableId tableId = TableId.of(DatabaseId.of(SpannerOptions.getDefaultProjectId(), instance, database), table); SpannerTableChangeSetPoller watcher = SpannerTableChangeSetPoller.newBuilder(spanner, tableId).build(); watcher.addCallback( new RowChangeCallback() {
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSpannerTableChangeSetPoller.BuilderBuilder for aSpannerTableChangeSetPoller.-
Nested classes/interfaces inherited from interface com.google.api.core.ApiService
com.google.api.core.ApiService.Listener, com.google.api.core.ApiService.State
-
Nested classes/interfaces inherited from interface com.google.cloud.spanner.watcher.SpannerTableChangeWatcher
SpannerTableChangeWatcher.Row, SpannerTableChangeWatcher.RowChangeCallback
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCallback(SpannerTableChangeWatcher.RowChangeCallback callback)Adds aSpannerTableChangeWatcher.RowChangeCallbackfor thisSpannerTableChangeWatcher.protected voiddoStart()protected voiddoStop()TableIdgetTable()Returns the id of the table that is monitored by this watcher.static SpannerTableChangeSetPoller.BuildernewBuilder(com.google.cloud.spanner.Spanner spanner, TableId table)Creates a newSpannerTableChangeSetPoller.Builderfor the given table.static SpannerTableChangeSetPoller.BuildernewBuilder(com.google.cloud.spanner.Spanner spanner, TableId changeSetTable, TableId table)Creates a newSpannerTableChangeSetPoller.Builderfor the given table and using the specified change set table.-
Methods inherited from class com.google.api.core.AbstractApiService
addListener, awaitRunning, awaitRunning, awaitTerminated, awaitTerminated, failureCause, isRunning, notifyFailed, notifyStarted, notifyStopped, startAsync, state, stopAsync
-
-
-
-
Method Detail
-
newBuilder
public static SpannerTableChangeSetPoller.Builder newBuilder(com.google.cloud.spanner.Spanner spanner, TableId table)
Creates a newSpannerTableChangeSetPoller.Builderfor the given table. The poller will assume that all change sets are stored in a table called CHANGE_SETS.
-
newBuilder
public static SpannerTableChangeSetPoller.Builder newBuilder(com.google.cloud.spanner.Spanner spanner, TableId changeSetTable, TableId table)
Creates a newSpannerTableChangeSetPoller.Builderfor the given table and using the specified change set table.
-
getTable
public TableId getTable()
Description copied from interface:SpannerTableChangeWatcherReturns the id of the table that is monitored by this watcher.- Specified by:
getTablein interfaceSpannerTableChangeWatcher
-
addCallback
public void addCallback(SpannerTableChangeWatcher.RowChangeCallback callback)
Description copied from interface:SpannerTableChangeWatcherAdds aSpannerTableChangeWatcher.RowChangeCallbackfor thisSpannerTableChangeWatcher. Callbacks may only be added when theApiService.state()of thisSpannerTableChangeWatcherisApiService.State.NEW. Callbacks should be lightweight and non-blocking. The callback should hand off any heavy computations or blocking operations to a non-blocking executor or buffer.The
SpannerTableChangeWatcherguarantees that at most oneSpannerTableChangeWatcher.RowChangeCallbackwill be active at any given time, and all callbacks will receive all changes in order of commit timestamp. There is no guarantee as to the order of which callback is called first if aSpannerTableChangeWatcherhas registered multiple callbacks.- Specified by:
addCallbackin interfaceSpannerTableChangeWatcher
-
doStart
protected void doStart()
- Specified by:
doStartin classcom.google.api.core.AbstractApiService
-
doStop
protected void doStop()
- Specified by:
doStopin classcom.google.api.core.AbstractApiService
-
-