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();
}
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));
}
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);
}
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()));
});
}
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());
}
Aggregations