Search in sources :

Example 1 with EntityQuery

use of com.google.cloud.datastore.EntityQuery in project java-docs-samples by GoogleCloudPlatform.

the class Guestbook method getGreetings.

public List<Greeting> getGreetings() {
    // This query requires the index defined in index.yaml to work because of the orderBy on date.
    EntityQuery query = Query.entityQueryBuilder().kind("Greeting").filter(hasAncestor(key)).orderBy(desc("date")).limit(5).build();
    QueryResults<Entity> results = getDatastore().run(query);
    Builder<Greeting> resultListBuilder = ImmutableList.builder();
    while (results.hasNext()) {
        resultListBuilder.add(new Greeting(results.next()));
    }
    return resultListBuilder.build();
}
Also used : Entity(com.google.cloud.datastore.Entity) EntityQuery(com.google.cloud.datastore.EntityQuery)

Example 2 with EntityQuery

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

the class DatastoreTemplateTests method queryByExampleSimpleEntityTest.

@Test
public void queryByExampleSimpleEntityTest() {
    EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("test_kind");
    StructuredQuery.CompositeFilter filter = StructuredQuery.CompositeFilter.and(PropertyFilter.eq("color", "simple_test_color"), PropertyFilter.eq("int_field", 1));
    EntityQuery query = builder.setFilter(filter).build();
    verifyBeforeAndAfterEvents(null, new AfterQueryEvent(Collections.emptyList(), query), () -> this.datastoreTemplate.queryByExample(Example.of(this.simpleTestEntity, ExampleMatcher.matching().withIgnorePaths("id")), null), x -> x.verify(this.datastore, times(1)).run(query));
}
Also used : StructuredQuery(com.google.cloud.datastore.StructuredQuery) AfterQueryEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Example 3 with EntityQuery

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

the class PartTreeDatastoreQueryTests method usingIdField.

@Test
public void usingIdField() throws NoSuchMethodException {
    Trade trade = new Trade();
    queryWithMockResult("findByActionAndId", null, getClass().getMethod("findByActionAndId", String.class, String.class), true, null);
    Object[] params = new Object[] { "BUY", "id1" };
    when(this.datastoreTemplate.createKey(eq("trades"), eq("id1"))).thenAnswer((invocation) -> Key.newBuilder("project", invocation.getArgument(0), invocation.getArgument(1)).build());
    when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(CompositeFilter.and(PropertyFilter.eq("action", "BUY"), PropertyFilter.eq("__key__", KeyValue.of(Key.newBuilder("project", "trades", "id1").build())))).setKind("trades").setLimit(1).build();
        assertThat(statement).isEqualTo(expected);
        List<Trade> results = Collections.singletonList(trade);
        return new DatastoreResultsIterable(results.iterator(), null);
    });
    assertThat(this.partTreeDatastoreQuery.execute(params)).isEqualTo(trade);
}
Also used : DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Example 4 with EntityQuery

use of com.google.cloud.datastore.EntityQuery 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 5 with EntityQuery

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

the class PartTreeDatastoreQueryTests method ambiguousSort.

@Test
public void ambiguousSort() throws NoSuchMethodException {
    queryWithMockResult("findByActionAndSymbolAndPriceLessThanAndPriceGreater" + "ThanEqualAndIdIsNullOrderByIdDesc", null, getClass().getMethod("tradeMethod", String.class, String.class, double.class, double.class, Sort.class));
    Object[] params = new Object[] { "BUY", "abcd", 8.88, 3.33, Sort.by(Sort.Direction.ASC, "price") };
    when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setOrderBy(OrderBy.desc("__key__"), OrderBy.asc("price")).build();
        assertThat(statement).isEqualTo(expected);
        return EMPTY_RESPONSE;
    });
    when(this.queryMethod.getCollectionReturnType()).thenReturn(List.class);
    this.partTreeDatastoreQuery.execute(params);
    verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), any());
}
Also used : Sort(org.springframework.data.domain.Sort) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Aggregations

EntityQuery (com.google.cloud.datastore.EntityQuery)15 Test (org.junit.Test)11 DatastoreResultsIterable (org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable)4 Pageable (org.springframework.data.domain.Pageable)3 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)2 StructuredQuery (com.google.cloud.datastore.StructuredQuery)2 ArrayList (java.util.ArrayList)2 Sort (org.springframework.data.domain.Sort)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 Credentials (com.google.auth.Credentials)1 BaseKey (com.google.cloud.datastore.BaseKey)1 Cursor (com.google.cloud.datastore.Cursor)1 Datastore (com.google.cloud.datastore.Datastore)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 Entity (com.google.cloud.datastore.Entity)1 IncompleteKey (com.google.cloud.datastore.IncompleteKey)1 Key (com.google.cloud.datastore.Key)1 KeyQuery (com.google.cloud.datastore.KeyQuery)1 Query (com.google.cloud.datastore.Query)1