use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method keyFilterExample_returnsMatchingEntities.
@Test
public void keyFilterExample_returnsMatchingEntities() throws Exception {
// Arrange
Entity a = new Entity("Person", "a");
Entity b = new Entity("Person", "b");
Entity c = new Entity("Person", "c");
Entity aa = new Entity("Person", "aa", b.getKey());
Entity bb = new Entity("Person", "bb", b.getKey());
Entity aaa = new Entity("Person", "aaa", bb.getKey());
Entity bbb = new Entity("Person", "bbb", bb.getKey());
datastore.put(ImmutableList.<Entity>of(a, b, c, aa, bb, aaa, bbb));
// Act
Key lastSeenKey = bb.getKey();
// [START key_filter_example]
Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
Query q = new Query("Person").setFilter(keyFilter);
// [END key_filter_example]
// Assert
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(// Ancestor path "b/bb/aaa" is greater than "b/bb".
aaa, // Ancestor path "b/bb/bbb" is greater than "b/bb".
bbb, // Key name identifier "c" is greater than b.
c);
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method queryInterface_orFilter_printsMatchedEntities.
@Test
public void queryInterface_orFilter_printsMatchedEntities() throws Exception {
// Arrange
Entity a = new Entity("Person", "a");
a.setProperty("height", 100);
Entity b = new Entity("Person", "b");
b.setProperty("height", 150);
Entity c = new Entity("Person", "c");
c.setProperty("height", 200);
datastore.put(ImmutableList.<Entity>of(a, b, c));
StringWriter buf = new StringWriter();
PrintWriter out = new PrintWriter(buf);
long minHeight = 125;
long maxHeight = 175;
// Act
// [START interface_3]
Filter tooShortFilter = new FilterPredicate("height", FilterOperator.LESS_THAN, minHeight);
Filter tooTallFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN, maxHeight);
Filter heightOutOfRangeFilter = CompositeFilterOperator.or(tooShortFilter, tooTallFilter);
Query q = new Query("Person").setFilter(heightOutOfRangeFilter);
// [END interface_3]
// Assert
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(a, c);
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method queryRestrictions_missingSortOnInequality_isInvalid.
@Test
public void queryRestrictions_missingSortOnInequality_isInvalid() throws Exception {
long minBirthYear = 1940;
// [START inequality_filters_sort_orders_invalid_example_1]
Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);
// Not valid. Missing sort on birthYear.
Query q = new Query("Person").setFilter(birthYearMinFilter).addSort("lastName", SortDirection.ASCENDING);
// [END inequality_filters_sort_orders_invalid_example_1]
// Note: The local devserver behavior is different than the production
// version of Cloud Datastore, so there aren't any assertions we can make
// in this test. The query appears to work with the local test runner,
// but will fail in production.
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method ancestorQueryExample_kindlessKeyFilter_returnsMatchingEntities.
@Test
public void ancestorQueryExample_kindlessKeyFilter_returnsMatchingEntities() throws Exception {
// Arrange
Entity a = new Entity("Grandparent", "a");
Entity b = new Entity("Grandparent", "b");
Entity c = new Entity("Grandparent", "c");
Entity aa = new Entity("Parent", "aa", a.getKey());
Entity ba = new Entity("Parent", "ba", b.getKey());
Entity bb = new Entity("Parent", "bb", b.getKey());
Entity bc = new Entity("Parent", "bc", b.getKey());
Entity cc = new Entity("Parent", "cc", c.getKey());
Entity aaa = new Entity("Child", "aaa", aa.getKey());
Entity bbb = new Entity("Child", "bbb", bb.getKey());
datastore.put(ImmutableList.<Entity>of(a, b, c, aa, ba, bb, bc, cc, aaa, bbb));
// Act
Key ancestorKey = b.getKey();
Key lastSeenKey = bb.getKey();
// [START kindless_ancestor_key_query_example]
Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
Query q = new Query().setAncestor(ancestorKey).setFilter(keyFilter);
// [END kindless_ancestor_key_query_example]
// Assert
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(bc, bbb);
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method propertyFilterExample_returnsMatchingEntities.
@Test
public void propertyFilterExample_returnsMatchingEntities() throws Exception {
// Arrange
Entity p1 = new Entity("Person");
p1.setProperty("height", 120);
Entity p2 = new Entity("Person");
p2.setProperty("height", 180);
Entity p3 = new Entity("Person");
p3.setProperty("height", 160);
datastore.put(ImmutableList.<Entity>of(p1, p2, p3));
// Act
long minHeight = 160;
// [START property_filter_example]
Filter propertyFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);
Query q = new Query("Person").setFilter(propertyFilter);
// [END property_filter_example]
// Assert
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(p2, p3);
}
Aggregations