Search in sources :

Example 1 with ReadOption

use of com.google.cloud.spanner.Options.ReadOption in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method findMultipleKeysTest.

@Test
public void findMultipleKeysTest() {
    ResultSet results = mock(ResultSet.class);
    ReadOption readOption = mock(ReadOption.class);
    SpannerReadOptions options = new SpannerReadOptions().addReadOption(readOption).setTimestampBound(TimestampBound.ofMinReadTimestamp(Timestamp.ofTimeMicroseconds(333L)));
    KeySet keySet = KeySet.singleKey(Key.of("key"));
    when(this.readContext.read(any(), any(), any(), any())).thenReturn(results);
    when(this.databaseClient.singleUse(eq(TimestampBound.ofMinReadTimestamp(Timestamp.ofTimeMicroseconds(333L))))).thenReturn(this.readContext);
    verifyAfterEvents(new AfterReadEvent(Collections.emptyList(), keySet, options), () -> this.spannerTemplate.read(TestEntity.class, keySet, options), x -> {
        verify(this.objectMapper, times(1)).mapToList(same(results), eq(TestEntity.class), isNull(), eq(false));
        verify(this.readContext, times(1)).read(eq("custom_test_table"), same(keySet), any(), same(readOption));
    });
    verify(this.databaseClient, times(1)).singleUse(TimestampBound.ofMinReadTimestamp(Timestamp.ofTimeMicroseconds(333L)));
}
Also used : KeySet(com.google.cloud.spanner.KeySet) AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent) ResultSet(com.google.cloud.spanner.ResultSet) ReadOption(com.google.cloud.spanner.Options.ReadOption) Test(org.junit.Test)

Example 2 with ReadOption

use of com.google.cloud.spanner.Options.ReadOption in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method findMultipleKeysWithIndexTest.

@Test
public void findMultipleKeysWithIndexTest() {
    ResultSet results = mock(ResultSet.class);
    ReadOption readOption = mock(ReadOption.class);
    SpannerReadOptions options = new SpannerReadOptions().addReadOption(readOption).setIndex("index");
    KeySet keySet = KeySet.singleKey(Key.of("key"));
    when(this.readContext.readUsingIndex(any(), any(), any(), any(), any())).thenReturn(results);
    this.spannerTemplate.read(TestEntity.class, keySet, options);
    verify(this.objectMapper, times(1)).mapToList(same(results), eq(TestEntity.class), isNull(), eq(false));
    verify(this.readContext, times(1)).readUsingIndex(eq("custom_test_table"), eq("index"), same(keySet), any(), same(readOption));
    verify(this.databaseClient, times(1)).singleUse();
}
Also used : KeySet(com.google.cloud.spanner.KeySet) ResultSet(com.google.cloud.spanner.ResultSet) ReadOption(com.google.cloud.spanner.Options.ReadOption) Test(org.junit.Test)

Example 3 with ReadOption

use of com.google.cloud.spanner.Options.ReadOption in project spring-cloud-gcp by spring-cloud.

the class SpannerReadOptionsTests method addReadOptionTest.

@Test
public void addReadOptionTest() {
    SpannerReadOptions spannerReadOptions = new SpannerReadOptions();
    ReadOption r1 = mock(ReadOption.class);
    ReadOption r2 = mock(ReadOption.class);
    spannerReadOptions.addReadOption(r1).addReadOption(r2);
    assertThat(Arrays.asList(spannerReadOptions.getOptions())).containsExactlyInAnyOrder(r1, r2);
}
Also used : ReadOption(com.google.cloud.spanner.Options.ReadOption) Test(org.junit.Test)

Example 4 with ReadOption

use of com.google.cloud.spanner.Options.ReadOption in project spring-cloud-gcp by spring-cloud.

the class SpannerReadOptions method toQueryOptions.

/**
 * In many cases a {@link SpannerReadOptions} class instance could be compatible with {@link SpannerQueryOptions}.
 * The method executes such conversion or throws an exception if it's impossible.
 * @return query-parameters
 * @throws IllegalArgumentException when {@link SpannerQueryOptions} can't be converted to {@link SpannerQueryOptions}.
 */
public SpannerQueryOptions toQueryOptions() {
    SpannerQueryOptions query = new SpannerQueryOptions();
    query.setAllowPartialRead(this.isAllowPartialRead());
    query.setIncludeProperties(this.getIncludeProperties());
    query.setTimestampBound(this.getTimestampBound());
    for (ReadOption ro : this.getOptions()) {
        if (ro instanceof Options.ReadAndQueryOption) {
            query.addQueryOption((Options.ReadAndQueryOption) ro);
        } else {
            throw new IllegalArgumentException(String.format("Can't convert %s to SpannerQueryOptions ", this));
        }
    }
    return query;
}
Also used : Options(com.google.cloud.spanner.Options) ReadOption(com.google.cloud.spanner.Options.ReadOption)

Example 5 with ReadOption

use of com.google.cloud.spanner.Options.ReadOption in project spring-cloud-gcp by spring-cloud.

the class SpannerTransactionManager method doBegin.

@Override
protected void doBegin(Object transactionObject, TransactionDefinition transactionDefinition) throws TransactionException {
    if (transactionDefinition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        throw new IllegalStateException("SpannerTransactionManager supports only isolation level TransactionDefinition.ISOLATION_DEFAULT");
    }
    if (transactionDefinition.getPropagationBehavior() != TransactionDefinition.PROPAGATION_REQUIRED) {
        throw new IllegalStateException("SpannerTransactionManager supports only propagation behavior " + "TransactionDefinition.PROPAGATION_REQUIRED");
    }
    Tx tx = (Tx) transactionObject;
    if (transactionDefinition.isReadOnly()) {
        final ReadContext targetTransactionContext = this.databaseClientProvider.get().readOnlyTransaction();
        tx.isReadOnly = true;
        tx.transactionManager = null;
        tx.transactionContext = new TransactionContext() {

            @Override
            public void buffer(Mutation mutation) {
                throw new IllegalStateException("Spanner transaction cannot apply" + " mutation because it is in readonly mode");
            }

            @Override
            public void buffer(Iterable<Mutation> iterable) {
                throw new IllegalStateException("Spanner transaction cannot apply" + " mutations because it is in readonly mode");
            }

            @Override
            public long executeUpdate(Statement statement) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public ApiFuture<Long> executeUpdateAsync(Statement statement) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public long[] batchUpdate(Iterable<Statement> iterable) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public ApiFuture<long[]> batchUpdateAsync(Iterable<Statement> iterable) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public ResultSet read(String s, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
                return targetTransactionContext.read(s, keySet, iterable, readOptions);
            }

            @Override
            public AsyncResultSet readAsync(String s, KeySet keySet, Iterable<String> iterable, ReadOption... readOptions) {
                return targetTransactionContext.readAsync(s, keySet, iterable, readOptions);
            }

            @Override
            public ResultSet readUsingIndex(String s, String s1, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
                return targetTransactionContext.readUsingIndex(s, s1, keySet, iterable, readOptions);
            }

            @Override
            public AsyncResultSet readUsingIndexAsync(String s, String s1, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
                return targetTransactionContext.readUsingIndexAsync(s, s1, keySet, iterable, readOptions);
            }

            @Nullable
            @Override
            public Struct readRow(String s, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRow(s, key, iterable);
            }

            @Override
            public ApiFuture<Struct> readRowAsync(String s, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRowAsync(s, key, iterable);
            }

            @Nullable
            @Override
            public Struct readRowUsingIndex(String s, String s1, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRowUsingIndex(s, s1, key, iterable);
            }

            @Override
            public ApiFuture<Struct> readRowUsingIndexAsync(String s, String s1, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRowUsingIndexAsync(s, s1, key, iterable);
            }

            @Override
            public ResultSet executeQuery(Statement statement, Options.QueryOption... queryOptions) {
                return targetTransactionContext.executeQuery(statement, queryOptions);
            }

            @Override
            public AsyncResultSet executeQueryAsync(Statement statement, QueryOption... queryOptions) {
                return targetTransactionContext.executeQueryAsync(statement, queryOptions);
            }

            @Override
            public ResultSet analyzeQuery(Statement statement, QueryAnalyzeMode queryAnalyzeMode) {
                return targetTransactionContext.analyzeQuery(statement, queryAnalyzeMode);
            }

            @Override
            public void close() {
                targetTransactionContext.close();
            }
        };
    } else {
        tx.transactionManager = tx.databaseClient.transactionManager();
        tx.transactionContext = tx.getTransactionManager().begin();
        tx.isReadOnly = false;
    }
    TransactionSynchronizationManager.bindResource(tx.getDatabaseClient(), tx);
}
Also used : KeySet(com.google.cloud.spanner.KeySet) Options(com.google.cloud.spanner.Options) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) Statement(com.google.cloud.spanner.Statement) QueryOption(com.google.cloud.spanner.Options.QueryOption) Struct(com.google.cloud.spanner.Struct) ApiFuture(com.google.api.core.ApiFuture) QueryOption(com.google.cloud.spanner.Options.QueryOption) TransactionContext(com.google.cloud.spanner.TransactionContext) ReadContext(com.google.cloud.spanner.ReadContext) ResultSet(com.google.cloud.spanner.ResultSet) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) Mutation(com.google.cloud.spanner.Mutation) ReadOption(com.google.cloud.spanner.Options.ReadOption) ReadOption(com.google.cloud.spanner.Options.ReadOption) Nullable(javax.annotation.Nullable) Key(com.google.cloud.spanner.Key)

Aggregations

ReadOption (com.google.cloud.spanner.Options.ReadOption)5 KeySet (com.google.cloud.spanner.KeySet)3 ResultSet (com.google.cloud.spanner.ResultSet)3 Test (org.junit.Test)3 Options (com.google.cloud.spanner.Options)2 ApiFuture (com.google.api.core.ApiFuture)1 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)1 Key (com.google.cloud.spanner.Key)1 Mutation (com.google.cloud.spanner.Mutation)1 QueryOption (com.google.cloud.spanner.Options.QueryOption)1 ReadContext (com.google.cloud.spanner.ReadContext)1 Statement (com.google.cloud.spanner.Statement)1 Struct (com.google.cloud.spanner.Struct)1 TransactionContext (com.google.cloud.spanner.TransactionContext)1 Nullable (javax.annotation.Nullable)1 AfterReadEvent (org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent)1