Java Code Examples for com.google.cloud.spanner.KeySet#singleKey()

The following examples show how to use com.google.cloud.spanner.KeySet#singleKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpannerTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void existsByIdTest() {
	ResultSet results = mock(ResultSet.class);
	when(results.next()).thenReturn(true);

	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(TestEntity.class, key)).isTrue();

	verify(this.databaseClient, times(1)).singleUse();
	verify(this.readContext, times(1))
			.read(eq("custom_test_table"), eq(keySet), eq(Collections.singleton("id")));
}
 
Example 2
Source File: SpannerTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@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")));
}
 
Example 3
Source File: SpannerTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@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)));
}
 
Example 4
Source File: SpannerTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 5
Source File: SpannerTemplate.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@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();
	}
}