Search in sources :

Example 26 with ResultSet

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

the class SpannerTemplate method existsById.

@Override
public <T> boolean existsById(Class<T> entityClass, Key key) {
    Assert.notNull(key, "A non-null key is required.");
    SpannerPersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(entityClass);
    KeySet keys = KeySet.singleKey(key);
    try (ResultSet resultSet = executeRead(persistentEntity.tableName(), keys, Collections.singleton(persistentEntity.getPrimaryKeyColumnName()), null)) {
        maybeEmitEvent(new AfterReadEvent(Collections.emptyList(), keys, null));
        return resultSet.next();
    }
}
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)

Example 27 with ResultSet

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

the class SpannerTemplate method executeRead.

private ResultSet executeRead(String tableName, KeySet keys, Iterable<String> columns, SpannerReadOptions options) {
    long startTime = LOGGER.isDebugEnabled() ? System.currentTimeMillis() : 0;
    ReadContext readContext = (options != null && options.getTimestampBound() != null) ? getReadContext(options.getTimestampBound()) : getReadContext();
    final ResultSet resultSet = options != null && options.getIndex() != null ? readContext.readUsingIndex(tableName, options.getIndex(), keys, columns, options.getOptions()) : readContext.read(tableName, keys, columns, options == null ? new ReadOption[0] : options.getOptions());
    if (LOGGER.isDebugEnabled()) {
        StringBuilder logs = logColumns(tableName, keys, columns);
        logReadOptions(options, logs);
        LOGGER.debug(logs.toString());
        LOGGER.debug("Read elapsed milliseconds: " + (System.currentTimeMillis() - startTime));
    }
    return resultSet;
}
Also used : ReadContext(com.google.cloud.spanner.ReadContext) ResultSet(com.google.cloud.spanner.ResultSet)

Example 28 with ResultSet

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

the class SpannerTemplate method executeQuery.

public ResultSet executeQuery(Statement statement, SpannerQueryOptions options) {
    long startTime = LOGGER.isDebugEnabled() ? System.currentTimeMillis() : 0;
    ResultSet resultSet = performQuery(statement, options);
    if (LOGGER.isDebugEnabled()) {
        String message;
        if (options == null) {
            message = "Executing query without additional options: " + statement;
        } else {
            message = getQueryLogMessageWithOptions(statement, options);
        }
        LOGGER.debug(message);
        LOGGER.debug("Query elapsed milliseconds: " + (System.currentTimeMillis() - startTime));
    }
    return resultSet;
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet)

Example 29 with ResultSet

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

the class SpannerTemplateTests method existsByIdEmbeddedKeyTest.

@Test
public void existsByIdEmbeddedKeyTest() {
    ResultSet results = mock(ResultSet.class);
    when(results.next()).thenReturn(false);
    when(this.readContext.read(any(), any(), any(), any())).thenReturn(results);
    when(this.databaseClient.singleUse(any())).thenReturn(this.readContext);
    Key key = Key.of("key");
    KeySet keySet = KeySet.singleKey(key);
    assertThat(this.spannerTemplate.existsById(TestEntityEmbeddedPK.class, key)).isFalse();
    verify(this.databaseClient, times(1)).singleUse();
    verify(this.readContext, times(1)).read(eq("test_table_embedded_pk"), eq(keySet), eq(Collections.singleton("stringId")));
}
Also used : KeySet(com.google.cloud.spanner.KeySet) ResultSet(com.google.cloud.spanner.ResultSet) Key(com.google.cloud.spanner.Key) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) Test(org.junit.Test)

Example 30 with ResultSet

use of com.google.cloud.spanner.ResultSet 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)

Aggregations

ResultSet (com.google.cloud.spanner.ResultSet)71 Test (org.junit.Test)25 Statement (com.google.cloud.spanner.Statement)15 Struct (com.google.cloud.spanner.Struct)12 ArrayList (java.util.ArrayList)12 KeySet (com.google.cloud.spanner.KeySet)11 IntegrationTest (com.google.cloud.spanner.IntegrationTest)10 ReadContext (com.google.cloud.spanner.ReadContext)10 DatabaseClient (com.google.cloud.spanner.DatabaseClient)8 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)5 Mutation (com.google.cloud.spanner.Mutation)5 Partition (com.google.cloud.spanner.Partition)5 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)5 HashMap (java.util.HashMap)5 Key (com.google.cloud.spanner.Key)4 ReadOption (com.google.cloud.spanner.Options.ReadOption)4 Spanner (com.google.cloud.spanner.Spanner)4 SpannerOptions (com.google.cloud.spanner.SpannerOptions)4 TransactionContext (com.google.cloud.spanner.TransactionContext)4 Joiner (com.google.common.base.Joiner)4