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