Search in sources :

Example 1 with QueryResults

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

the class DatastoreTemplateTests method setup.

@Before
public void setup() {
    this.datastoreTemplate = new DatastoreTemplate(() -> this.datastore, this.datastoreEntityConverter, new DatastoreMappingContext(), this.objectToKeyFactory);
    when(this.datastoreEntityConverter.getConversions()).thenReturn(this.readWriteConversions);
    // The readWriteConversions are only mocked for purposes of collection-conversion
    // for
    // descendants. no other conversions take place in the template.
    doAnswer((invocation) -> new LinkedList<>(invocation.getArgument(0))).when(this.readWriteConversions).convertOnRead(any(), any(), (Class) any());
    this.ob1 = new TestEntity();
    this.ob2 = new TestEntity();
    this.ob1.id = "value1";
    this.ob2.id = "value2";
    Entity ce1 = Entity.newBuilder(this.keyChild1).build();
    Query childTestEntityQuery = Query.newEntityQueryBuilder().setKind("child_entity").setFilter(PropertyFilter.hasAncestor(this.key1)).build();
    this.childEntity1 = createChildEntity();
    this.ob1.childEntities = new LinkedList<>();
    this.childEntity2 = new ChildEntity();
    this.ob1.childEntities.add(this.childEntity2);
    this.childEntity3 = new ChildEntity();
    this.ob1.childEntities.add(this.childEntity3);
    this.childEntity4 = new ChildEntity();
    this.ob1.singularReference = this.childEntity4;
    this.ob1.multipleReference = new LinkedList<>();
    this.childEntity5 = new ChildEntity();
    this.ob1.multipleReference.add(this.childEntity5);
    this.childEntity6 = new ChildEntity();
    this.ob1.multipleReference.add(this.childEntity6);
    this.ob1.lazyMultipleReference = new LinkedList<>();
    this.childEntity7 = new ChildEntity();
    this.ob1.lazyMultipleReference.add(this.childEntity7);
    // mocked query results for entities and child entities.
    QueryResults childTestEntityQueryResults = mock(QueryResults.class);
    doAnswer((invocation) -> {
        Arrays.asList(ce1).iterator().forEachRemaining(invocation.getArgument(0));
        return null;
    }).when(childTestEntityQueryResults).forEachRemaining(any());
    QueryResults testEntityQueryResults = mock(QueryResults.class);
    doAnswer((invocation) -> {
        Arrays.asList(this.e1, this.e2).iterator().forEachRemaining(invocation.getArgument(0));
        return null;
    }).when(testEntityQueryResults).forEachRemaining(any());
    setUpConverters(ce1, childTestEntityQuery, childTestEntityQueryResults, testEntityQueryResults);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) Query(com.google.cloud.datastore.Query) KeyQuery(com.google.cloud.datastore.KeyQuery) GqlQuery(com.google.cloud.datastore.GqlQuery) StructuredQuery(com.google.cloud.datastore.StructuredQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) QueryResults(com.google.cloud.datastore.QueryResults) Before(org.junit.Before)

Example 2 with QueryResults

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

the class DatastoreTemplate method findAll.

@Override
public <T> DatastoreResultsCollection<T> findAll(Class<T> entityClass, DatastoreQueryOptions queryOptions) {
    DatastorePersistentEntity<?> persistentEntity = this.datastoreMappingContext.getPersistentEntity(entityClass);
    EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind(persistentEntity.kindName());
    applyQueryOptions(builder, queryOptions, persistentEntity);
    Query query = builder.build();
    QueryResults queryResults = getDatastoreReadWriter().run(query);
    Collection<T> convertedResults = convertEntitiesForRead(queryResults, entityClass);
    maybeEmitEvent(new AfterQueryEvent(convertedResults, query));
    return new DatastoreResultsCollection<>(convertedResults, queryResults != null ? queryResults.getCursorAfter() : null);
}
Also used : AfterQueryEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent) Query(com.google.cloud.datastore.Query) KeyQuery(com.google.cloud.datastore.KeyQuery) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) StructuredQuery(com.google.cloud.datastore.StructuredQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) QueryResults(com.google.cloud.datastore.QueryResults)

Example 3 with QueryResults

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

the class DatastoreTemplate method queryKeysOrEntities.

@Override
public <T> DatastoreResultsIterable<?> queryKeysOrEntities(Query query, Class<T> entityClass) {
    QueryResults results = getDatastoreReadWriter().run(query);
    DatastoreResultsIterable resultsIterable;
    if (results.getResultClass() == Key.class) {
        resultsIterable = new DatastoreResultsIterable(results, results.getCursorAfter());
    } else {
        resultsIterable = new DatastoreResultsIterable<>(convertEntitiesForRead(results, entityClass), results.getCursorAfter());
    }
    maybeEmitEvent(new AfterQueryEvent(resultsIterable, query));
    return resultsIterable;
}
Also used : AfterQueryEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent) QueryResults(com.google.cloud.datastore.QueryResults)

Aggregations

QueryResults (com.google.cloud.datastore.QueryResults)3 EntityQuery (com.google.cloud.datastore.EntityQuery)2 KeyQuery (com.google.cloud.datastore.KeyQuery)2 Query (com.google.cloud.datastore.Query)2 StructuredQuery (com.google.cloud.datastore.StructuredQuery)2 AfterQueryEvent (org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent)2 Entity (com.google.cloud.datastore.Entity)1 FullEntity (com.google.cloud.datastore.FullEntity)1 GqlQuery (com.google.cloud.datastore.GqlQuery)1 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)1 Before (org.junit.Before)1 DatastoreMappingContext (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext)1