use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent 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 org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplate method read.
@Override
public <T> List<T> read(Class<T> entityClass, KeySet keys, SpannerReadOptions options) {
SpannerPersistentEntity<T> persistentEntity = (SpannerPersistentEntity<T>) this.mappingContext.getPersistentEntity(entityClass);
List<T> entities;
if (persistentEntity.hasEagerlyLoadedProperties() || persistentEntity.hasWhere()) {
entities = executeReadQueryAndResolveChildren(keys, persistentEntity, toQueryOption(keys, options), options != null ? options.getIndex() : null);
} else {
entities = mapToListAndResolveChildren(executeRead(persistentEntity.tableName(), keys, persistentEntity.columns(), options), entityClass, (options != null) ? options.getIncludeProperties() : null, options != null && options.isAllowPartialRead());
}
maybeEmitEvent(new AfterReadEvent(entities, keys, options));
return entities;
}
use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplateTests method resolveChildEntityTest.
@Test
public void resolveChildEntityTest() {
ParentEntity p = new ParentEntity();
p.id = "key";
p.id2 = "key2";
ChildEntity c = new ChildEntity();
c.id = "key";
c.id_2 = "key2";
c.id3 = "key3";
GrandChildEntity gc = new GrandChildEntity();
gc.id = "key";
gc.id_2 = "key2";
gc.id3 = "key3";
gc.id4 = "key4";
when(this.objectMapper.mapToList(any(), eq(ParentEntity.class))).thenReturn(Arrays.asList(p));
when(this.objectMapper.mapToList(any(), eq(ParentEntity.class), any(), eq(false))).thenReturn(Arrays.asList(p));
when(this.objectMapper.mapToList(any(), eq(ChildEntity.class), any(), eq(false))).thenReturn(Arrays.asList(c));
when(this.objectMapper.mapToList(any(), eq(GrandChildEntity.class), any(), eq(false))).thenReturn(Arrays.asList(gc));
// Verify that only a single event containing only the parent p is published.
// the recursive resolution of children should NOT give events since there is only one
// user-call.
verifyAfterEvents(new AfterReadEvent(Collections.singletonList(p), KeySet.all(), null), () -> {
ParentEntity resultWithoutChildren = this.spannerTemplate.readAll(ParentEntity.class, new SpannerReadOptions().setIncludeProperties(Collections.singleton("id"))).get(0);
assertThat(resultWithoutChildren.childEntities).isNull();
ParentEntity result = this.spannerTemplate.readAll(ParentEntity.class).get(0);
assertThat(result.childEntities).hasSize(1);
assertThat(result.childEntities.get(0)).isSameAs(c);
assertThat(result.childEntities.get(0).childEntities).hasSize(1);
assertThat(result.childEntities.get(0).childEntities.get(0)).isSameAs(gc);
}, x -> {
});
}
use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent 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)));
}
use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplateTests method findSingleKeyNullTest.
@Test
public void findSingleKeyNullTest() {
when(this.readContext.read(any(), any(), any())).thenReturn(null);
Key key = Key.of("key");
KeySet keys = KeySet.newBuilder().addKey(key).build();
verifyAfterEvents(new AfterReadEvent(Collections.emptyList(), keys, null), () -> assertThat(this.spannerTemplate.read(TestEntity.class, key)).isNull(), x -> {
});
verify(this.databaseClient, times(1)).singleUse();
}
Aggregations