Search in sources :

Example 1 with KeyQuery

use of com.google.cloud.datastore.KeyQuery in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplateTests method queryKeysTest.

@Test
public void queryKeysTest() {
    KeyQuery keyQuery = GqlQuery.newKeyQueryBuilder().build();
    this.datastoreTemplate.queryKeys(keyQuery).iterator();
    verify(this.datastore, times(1)).run(keyQuery);
}
Also used : KeyQuery(com.google.cloud.datastore.KeyQuery) Test(org.junit.Test)

Example 2 with KeyQuery

use of com.google.cloud.datastore.KeyQuery in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQuery method executeSliceQuery.

private Slice executeSliceQuery(Object[] parameters) {
    StructuredQuery structuredQuery = buildSliceQuey(parameters);
    Slice<?> results = structuredQuery instanceof KeyQuery ? this.datastoreOperations.queryKeysSlice((KeyQuery) structuredQuery, this.entityType, getPageable(parameters)) : this.datastoreOperations.queryEntitiesSlice(structuredQuery, this.entityType, getPageable(parameters));
    return (Slice) this.processRawObjectForProjection(results);
}
Also used : StructuredQuery(com.google.cloud.datastore.StructuredQuery) Slice(org.springframework.data.domain.Slice) KeyQuery(com.google.cloud.datastore.KeyQuery)

Example 3 with KeyQuery

use of com.google.cloud.datastore.KeyQuery in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQueryTests method preparePageResults.

private void preparePageResults(int offset, Integer limit, Cursor cursor, List<Integer> pageResults, List<Integer> fullResults) {
    when(this.datastoreTemplate.queryKeysOrEntities(isA(EntityQuery.class), any())).thenAnswer((invocation) -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setStartCursor(cursor).setOffset(cursor != null ? 0 : offset).setOrderBy(OrderBy.desc("__key__")).setLimit(limit).build();
        assertThat(statement).isEqualTo(expected);
        return new DatastoreResultsIterable(pageResults.iterator(), Cursor.copyFrom("abc".getBytes()));
    });
    when(this.datastoreTemplate.queryKeysOrEntities(isA(KeyQuery.class), any())).thenAnswer((invocation) -> {
        KeyQuery statement = invocation.getArgument(0);
        KeyQuery expected = StructuredQuery.newKeyQueryBuilder().setFilter(FILTER).setKind("trades").setOrderBy(OrderBy.desc("__key__")).build();
        assertThat(statement).isEqualTo(expected);
        return new DatastoreResultsIterable(fullResults.iterator(), Cursor.copyFrom("def".getBytes()));
    });
}
Also used : DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) KeyQuery(com.google.cloud.datastore.KeyQuery)

Example 4 with KeyQuery

use of com.google.cloud.datastore.KeyQuery in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplateTests method nextPageTest.

private boolean nextPageTest(boolean hasNextPage) {
    QueryResults<Key> queryResults = mock(QueryResults.class);
    when(queryResults.getResultClass()).thenReturn((Class) Key.class);
    doAnswer((invocation) -> {
        Arrays.asList(this.key1, this.key2).iterator().forEachRemaining(invocation.getArgument(0));
        return null;
    }).when(queryResults).forEachRemaining(any());
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    when(queryResults.getCursorAfter()).thenReturn(cursor);
    KeyQuery query = Query.newKeyQueryBuilder().setKind("custom_test_kind").setLimit(1).build();
    when(this.datastore.run(eq(query))).thenReturn(queryResults);
    QueryResults<Key> nextPageQueryResults = mock(QueryResults.class);
    when(nextPageQueryResults.hasNext()).thenReturn(hasNextPage);
    KeyQuery nextPageQuery = query.toBuilder().setStartCursor(cursor).setOffset(0).setLimit(1).build();
    when(this.datastore.run(eq(nextPageQuery))).thenReturn(nextPageQueryResults);
    Slice<Key> resultsSlice = this.datastoreTemplate.queryKeysSlice(query, TestEntity.class, PageRequest.of(0, 1));
    return resultsSlice.hasNext();
}
Also used : Cursor(com.google.cloud.datastore.Cursor) KeyQuery(com.google.cloud.datastore.KeyQuery) Key(com.google.cloud.datastore.Key)

Example 5 with KeyQuery

use of com.google.cloud.datastore.KeyQuery in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplate method exampleToQuery.

private <T> StructuredQuery exampleToQuery(Example<T> example, DatastoreQueryOptions queryOptions, boolean keyQuery) {
    validateExample(example);
    T probe = example.getProbe();
    FullEntity.Builder<IncompleteKey> probeEntityBuilder = Entity.newBuilder();
    this.datastoreEntityConverter.write(probe, probeEntityBuilder);
    FullEntity<IncompleteKey> probeEntity = probeEntityBuilder.build();
    DatastorePersistentEntity<?> persistentEntity = this.datastoreMappingContext.getPersistentEntity(example.getProbeType());
    LinkedList<StructuredQuery.Filter> filters = new LinkedList<>();
    NullHandler nullHandler = example.getMatcher().getNullHandler();
    persistentEntity.doWithColumnBackedProperties((persistentProperty) -> {
        if (!ignoredProperty(example, persistentProperty)) {
            Value<?> value = getValue(example, probeEntity, persistentEntity, persistentProperty);
            addFilter(nullHandler, filters, persistentProperty.getFieldName(), value);
        }
    });
    persistentEntity.doWithAssociations((AssociationHandler<DatastorePersistentProperty>) association -> {
        PersistentPropertyAccessor<?> accessor = persistentEntity.getPropertyAccessor(example.getProbe());
        DatastorePersistentProperty property = association.getInverse();
        Object value = accessor.getProperty(property);
        Value<?> key = value == null ? NullValue.of() : KeyValue.of(objectToKeyFactory.getKeyFromObject(value, this.datastoreMappingContext.getPersistentEntity(value.getClass())));
        addFilter(nullHandler, filters, property.getFieldName(), key);
    });
    StructuredQuery.Builder<?> builder = keyQuery ? Query.newKeyQueryBuilder() : Query.newEntityQueryBuilder();
    builder.setKind(persistentEntity.kindName());
    if (!filters.isEmpty()) {
        builder.setFilter(StructuredQuery.CompositeFilter.and(filters.pop(), filters.toArray(new StructuredQuery.Filter[0])));
    }
    applyQueryOptions(builder, queryOptions, persistentEntity);
    return builder.build();
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Query(com.google.cloud.datastore.Query) Builder(com.google.cloud.datastore.Entity.Builder) PathElement(com.google.cloud.datastore.PathElement) SliceUtil(org.springframework.cloud.gcp.data.datastore.core.util.SliceUtil) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) Filter(com.google.cloud.datastore.StructuredQuery.Filter) AfterQueryEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent) KeyQuery(com.google.cloud.datastore.KeyQuery) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) QueryResults(com.google.cloud.datastore.QueryResults) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) PersistentProperty(org.springframework.data.mapping.PersistentProperty) AfterFindByKeyEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) Collection(java.util.Collection) KeyValue(com.google.cloud.datastore.KeyValue) AfterSaveEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterSaveEvent) Set(java.util.Set) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) Example(org.springframework.data.domain.Example) Collectors(java.util.stream.Collectors) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Slice(org.springframework.data.domain.Slice) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) KeyUtil(org.springframework.cloud.gcp.data.datastore.core.util.KeyUtil) ListValue(com.google.cloud.datastore.ListValue) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) BeforeDeleteEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.BeforeDeleteEvent) Key(com.google.cloud.datastore.Key) BeforeSaveEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.BeforeSaveEvent) DatastoreEntityConverter(org.springframework.cloud.gcp.data.datastore.core.convert.DatastoreEntityConverter) SliceImpl(org.springframework.data.domain.SliceImpl) HashMap(java.util.HashMap) Datastore(com.google.cloud.datastore.Datastore) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Cursor(com.google.cloud.datastore.Cursor) HashSet(java.util.HashSet) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) ValueUtil(org.springframework.cloud.gcp.data.datastore.core.util.ValueUtil) Entity(com.google.cloud.datastore.Entity) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) NullValue(com.google.cloud.datastore.NullValue) StreamSupport(java.util.stream.StreamSupport) Nullable(org.springframework.lang.Nullable) LinkedList(java.util.LinkedList) StructuredQuery(com.google.cloud.datastore.StructuredQuery) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Iterator(java.util.Iterator) TypeUtils(org.springframework.util.TypeUtils) ObjectToKeyFactory(org.springframework.cloud.gcp.data.datastore.core.convert.ObjectToKeyFactory) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) DatastoreReaderWriter(com.google.cloud.datastore.DatastoreReaderWriter) EntityQuery(com.google.cloud.datastore.EntityQuery) AfterDeleteEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterDeleteEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) AssociationHandler(org.springframework.data.mapping.AssociationHandler) BaseKey(com.google.cloud.datastore.BaseKey) Value(com.google.cloud.datastore.Value) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty) Collections(java.util.Collections) DatastorePageable(org.springframework.cloud.gcp.data.datastore.repository.query.DatastorePageable) Assert(org.springframework.util.Assert) StructuredQuery(com.google.cloud.datastore.StructuredQuery) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) FullEntity(com.google.cloud.datastore.FullEntity) LinkedList(java.util.LinkedList) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Filter(com.google.cloud.datastore.StructuredQuery.Filter) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) KeyValue(com.google.cloud.datastore.KeyValue) ListValue(com.google.cloud.datastore.ListValue) NullValue(com.google.cloud.datastore.NullValue) Value(com.google.cloud.datastore.Value) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Aggregations

KeyQuery (com.google.cloud.datastore.KeyQuery)5 Cursor (com.google.cloud.datastore.Cursor)2 EntityQuery (com.google.cloud.datastore.EntityQuery)2 Key (com.google.cloud.datastore.Key)2 StructuredQuery (com.google.cloud.datastore.StructuredQuery)2 BaseEntity (com.google.cloud.datastore.BaseEntity)1 BaseKey (com.google.cloud.datastore.BaseKey)1 Datastore (com.google.cloud.datastore.Datastore)1 DatastoreReaderWriter (com.google.cloud.datastore.DatastoreReaderWriter)1 Entity (com.google.cloud.datastore.Entity)1 Builder (com.google.cloud.datastore.Entity.Builder)1 FullEntity (com.google.cloud.datastore.FullEntity)1 IncompleteKey (com.google.cloud.datastore.IncompleteKey)1 KeyValue (com.google.cloud.datastore.KeyValue)1 ListValue (com.google.cloud.datastore.ListValue)1 NullValue (com.google.cloud.datastore.NullValue)1 PathElement (com.google.cloud.datastore.PathElement)1 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)1 Query (com.google.cloud.datastore.Query)1 QueryResults (com.google.cloud.datastore.QueryResults)1