Class SpannerTableChangeSetPoller

  • All Implemented Interfaces:
    com.google.api.core.ApiService, SpannerTableChangeWatcher

    public class SpannerTableChangeSetPoller
    extends com.google.api.core.AbstractApiService
    implements SpannerTableChangeWatcher
    Implementation of the SpannerTableChangeWatcher interface 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:
    1. 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.
    2. 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() {