Search in sources :

Example 1 with AfterReadEvent

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();
    }
}
Also used : KeySet(com.google.cloud.spanner.KeySet) AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent) ResultSet(com.google.cloud.spanner.ResultSet)

Example 2 with AfterReadEvent

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;
}
Also used : SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent)

Example 3 with AfterReadEvent

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 -> {
    });
}
Also used : AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent) Test(org.junit.Test)

Example 4 with AfterReadEvent

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)));
}
Also used : KeySet(com.google.cloud.spanner.KeySet) AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent) ResultSet(com.google.cloud.spanner.ResultSet) ReadOption(com.google.cloud.spanner.Options.ReadOption) Test(org.junit.Test)

Example 5 with AfterReadEvent

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();
}
Also used : KeySet(com.google.cloud.spanner.KeySet) AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent) Key(com.google.cloud.spanner.Key) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) Test(org.junit.Test)

Aggregations

AfterReadEvent (org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent)5 KeySet (com.google.cloud.spanner.KeySet)3 Test (org.junit.Test)3 ResultSet (com.google.cloud.spanner.ResultSet)2 Key (com.google.cloud.spanner.Key)1 ReadOption (com.google.cloud.spanner.Options.ReadOption)1 PrimaryKey (org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey)1 SpannerPersistentEntity (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity)1