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();
}
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 -> {
});
}
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();
}
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;
}
}
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;
}
}
Aggregations