Search in sources :

Example 31 with ResultSet

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

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

the class SpannerTemplateTests method queryFuncTest.

@Test
public void queryFuncTest() {
    ResultSet resultSet = mock(ResultSet.class);
    when(resultSet.next()).thenReturn(false);
    Statement query = Statement.of("test");
    when(this.readContext.executeQuery(eq(query))).thenReturn(resultSet);
    verifyAfterEvents(new AfterQueryEvent(Collections.emptyList(), query, null), () -> assertThat(this.spannerTemplate.query(x -> null, query, null)).isEmpty(), x -> {
    });
}
Also used : TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) Arrays(java.util.Arrays) BeforeSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeSaveEvent) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BeforeDeleteEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeDeleteEvent) Timestamp(com.google.cloud.Timestamp) DatabaseClient(com.google.cloud.spanner.DatabaseClient) CachingComposingSupplier(org.springframework.cloud.gcp.data.spanner.core.admin.CachingComposingSupplier) ResultSet(com.google.cloud.spanner.ResultSet) Column(org.springframework.cloud.gcp.data.spanner.core.mapping.Column) SpannerWriteConverter(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerWriteConverter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Key(com.google.cloud.spanner.Key) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) AfterExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterExecuteDmlEvent) Mockito.doReturn(org.mockito.Mockito.doReturn) TransactionContext(com.google.cloud.spanner.TransactionContext) Set(java.util.Set) Mutation(com.google.cloud.spanner.Mutation) Embedded(org.springframework.cloud.gcp.data.spanner.core.mapping.Embedded) List(java.util.List) AfterDeleteEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterDeleteEvent) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) AfterSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterSaveEvent) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ReadContext(com.google.cloud.spanner.ReadContext) Interleaved(org.springframework.cloud.gcp.data.spanner.core.mapping.Interleaved) SpannerEntityProcessor(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerEntityProcessor) Table(org.springframework.cloud.gcp.data.spanner.core.mapping.Table) TransactionRunner(com.google.cloud.spanner.TransactionRunner) BeforeExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent) Mockito.spy(org.mockito.Mockito.spy) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ByteArray(com.google.cloud.ByteArray) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) SpannerSchemaUtils(org.springframework.cloud.gcp.data.spanner.core.admin.SpannerSchemaUtils) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) InOrder(org.mockito.InOrder) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) TimestampBound(com.google.cloud.spanner.TimestampBound) KeySet(com.google.cloud.spanner.KeySet) AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Statement(com.google.cloud.spanner.Statement) ApplicationEvent(org.springframework.context.ApplicationEvent) ReadOption(com.google.cloud.spanner.Options.ReadOption) Mockito(org.mockito.Mockito) Where(org.springframework.cloud.gcp.data.spanner.core.mapping.Where) Rule(org.junit.Rule) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) AfterQueryEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent) Collections(java.util.Collections) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) AfterQueryEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) Test(org.junit.Test)

Example 33 with ResultSet

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

the class SpannerDatabaseAdminTemplateTests method getTableRelationshipsTest.

@Test
public void getTableRelationshipsTest() {
    ReadContext readContext = mock(ReadContext.class);
    Struct s1 = Struct.newBuilder().set("table_name").to(Value.string("grandpa")).set("parent_table_name").to(Value.string(null)).build();
    Struct s2 = Struct.newBuilder().set("table_name").to(Value.string("parent_a")).set("parent_table_name").to(Value.string("grandpa")).build();
    Struct s3 = Struct.newBuilder().set("table_name").to(Value.string("parent_b")).set("parent_table_name").to(Value.string("grandpa")).build();
    Struct s4 = Struct.newBuilder().set("table_name").to(Value.string("child")).set("parent_table_name").to(Value.string("parent_a")).build();
    MockResults mockResults = new MockResults();
    mockResults.structs = Arrays.asList(s1, s2, s3, s4);
    ResultSet results = mock(ResultSet.class);
    when(results.next()).thenAnswer((invocation) -> mockResults.next());
    when(results.getCurrentRowAsStruct()).thenAnswer((invocation) -> mockResults.getCurrent());
    when(this.databaseClient.singleUse()).thenReturn(readContext);
    when(readContext.executeQuery(any())).thenReturn(results);
    Map<String, Set<String>> relationships = this.spannerDatabaseAdminTemplate.getParentChildTablesMap();
    assertThat(relationships).hasSize(2);
    assertThat(relationships.get("grandpa")).containsExactlyInAnyOrder("parent_a", "parent_b");
    assertThat(relationships.get("parent_a")).containsExactlyInAnyOrder("child");
    assertThat(this.spannerDatabaseAdminTemplate.isInterleaved("grandpa", "child")).as("verify grand-child relationship").isTrue();
    assertThat(this.spannerDatabaseAdminTemplate.isInterleaved("grandpa", "parent_a")).as("verify parent-child relationship").isTrue();
    assertThat(this.spannerDatabaseAdminTemplate.isInterleaved("parent_a", "child")).as("verify parent-child relationship").isTrue();
    assertThat(this.spannerDatabaseAdminTemplate.isInterleaved("grandpa", "parent_b")).as("verify parent-child relationship").isTrue();
    assertThat(this.spannerDatabaseAdminTemplate.isInterleaved("parent_a", "parent_b")).as("verify not parent-child relationship").isFalse();
    assertThat(this.spannerDatabaseAdminTemplate.isInterleaved("parent_b", "child")).as("verify not parent-child relationship").isFalse();
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) Set(java.util.Set) ReadContext(com.google.cloud.spanner.ReadContext) ResultSet(com.google.cloud.spanner.ResultSet) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 34 with ResultSet

use of com.google.cloud.spanner.ResultSet in project YCSB by brianfrankcooper.

the class CloudSpannerClient method readUsingQuery.

private Status readUsingQuery(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    Statement query;
    Iterable<String> columns = fields == null ? STANDARD_FIELDS : fields;
    if (fields == null || fields.size() == fieldCount) {
        query = Statement.newBuilder(standardQuery).bind("key").to(key).build();
    } else {
        Joiner joiner = Joiner.on(',');
        query = Statement.newBuilder("SELECT ").append(joiner.join(fields)).append(" FROM ").append(table).append(" WHERE id=@key").bind("key").to(key).build();
    }
    try (ResultSet resultSet = dbClient.singleUse(timestampBound).executeQuery(query)) {
        resultSet.next();
        decodeStruct(columns, resultSet, result);
        if (resultSet.next()) {
            throw new Exception("Expected exactly one row for each read.");
        }
        return Status.OK;
    } catch (Exception e) {
        LOGGER.log(Level.INFO, "readUsingQuery()", e);
        return Status.ERROR;
    }
}
Also used : Joiner(com.google.common.base.Joiner) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) DBException(com.yahoo.ycsb.DBException)

Example 35 with ResultSet

use of com.google.cloud.spanner.ResultSet in project YCSB by brianfrankcooper.

the class CloudSpannerClient method scanUsingQuery.

private Status scanUsingQuery(String table, String startKey, int recordCount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    Iterable<String> columns = fields == null ? STANDARD_FIELDS : fields;
    Statement query;
    if (fields == null || fields.size() == fieldCount) {
        query = Statement.newBuilder(standardScan).bind("startKey").to(startKey).bind("count").to(recordCount).build();
    } else {
        Joiner joiner = Joiner.on(',');
        query = Statement.newBuilder("SELECT ").append(joiner.join(fields)).append(" FROM ").append(table).append(" WHERE id>=@startKey LIMIT @count").bind("startKey").to(startKey).bind("count").to(recordCount).build();
    }
    try (ResultSet resultSet = dbClient.singleUse(timestampBound).executeQuery(query)) {
        while (resultSet.next()) {
            HashMap<String, ByteIterator> row = new HashMap<>();
            decodeStruct(columns, resultSet, row);
            result.add(row);
        }
        return Status.OK;
    } catch (Exception e) {
        LOGGER.log(Level.INFO, "scanUsingQuery()", e);
        return Status.ERROR;
    }
}
Also used : Joiner(com.google.common.base.Joiner) StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) HashMap(java.util.HashMap) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) DBException(com.yahoo.ycsb.DBException)

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