use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method queryRestrictions_compositeFilter_returnsMatchedEntities.
@Test
public void queryRestrictions_compositeFilter_returnsMatchedEntities() throws Exception {
// Arrange
Entity a = new Entity("Person", "a");
a.setProperty("birthYear", 1930);
Entity b = new Entity("Person", "b");
b.setProperty("birthYear", 1960);
Entity c = new Entity("Person", "c");
c.setProperty("birthYear", 1990);
datastore.put(ImmutableList.<Entity>of(a, b, c));
// Act
long minBirthYear = 1940;
long maxBirthYear = 1980;
// [START inequality_filters_one_property_valid_example_1]
Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);
Filter birthYearMaxFilter = new FilterPredicate("birthYear", FilterOperator.LESS_THAN_OR_EQUAL, maxBirthYear);
Filter birthYearRangeFilter = CompositeFilterOperator.and(birthYearMinFilter, birthYearMaxFilter);
Query q = new Query("Person").setFilter(birthYearRangeFilter);
// [END inequality_filters_one_property_valid_example_1]
// Assert
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(b);
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method queryRestrictions_inequalitySortedFirst_returnsMatchedEntities.
@Test
public void queryRestrictions_inequalitySortedFirst_returnsMatchedEntities() throws Exception {
// Arrange
Entity a = new Entity("Person", "a");
a.setProperty("birthYear", 1930);
a.setProperty("lastName", "Someone");
Entity b = new Entity("Person", "b");
b.setProperty("birthYear", 1990);
b.setProperty("lastName", "Bravo");
Entity c = new Entity("Person", "c");
c.setProperty("birthYear", 1960);
c.setProperty("lastName", "Charlie");
Entity d = new Entity("Person", "d");
d.setProperty("birthYear", 1960);
d.setProperty("lastName", "Delta");
datastore.put(ImmutableList.<Entity>of(a, b, c, d));
long minBirthYear = 1940;
// [START inequality_filters_sort_orders_valid_example]
Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);
Query q = new Query("Person").setFilter(birthYearMinFilter).addSort("birthYear", SortDirection.ASCENDING).addSort("lastName", SortDirection.ASCENDING);
// [END inequality_filters_sort_orders_valid_example]
// Assert
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(c, d, b).inOrder();
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class StartupServletTest method doGet_emptyDatastore_writesPresidents.
@Test
public void doGet_emptyDatastore_writesPresidents() throws Exception {
servletUnderTest.doGet(mockRequest, mockResponse);
Filter nameFilter = new FilterPredicate("name", FilterOperator.EQUAL, "George Washington");
Query q = new Query("Person").setFilter(nameFilter);
Entity result = datastore.prepare(q).asSingleEntity();
assertThat(result.getProperty("name")).named("name").isEqualTo("George Washington");
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class IndexesTest method propertyFilterExample_returnsMatchingEntities.
@Test
public void propertyFilterExample_returnsMatchingEntities() throws Exception {
// [START unindexed_properties_1]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key acmeKey = KeyFactory.createKey("Company", "Acme");
Entity tom = new Entity("Person", "Tom", acmeKey);
tom.setProperty("name", "Tom");
tom.setProperty("age", 32);
datastore.put(tom);
Entity lucy = new Entity("Person", "Lucy", acmeKey);
lucy.setProperty("name", "Lucy");
lucy.setUnindexedProperty("age", 29);
datastore.put(lucy);
Filter ageFilter = new FilterPredicate("age", FilterOperator.GREATER_THAN, 25);
Query q = new Query("Person").setAncestor(acmeKey).setFilter(ageFilter);
// Returns tom but not lucy, because her age is unindexed
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
// [END unindexed_properties_1]
assertThat(results).named("query results").containsExactly(tom);
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class MetadataNamespacesTest method getNamespaces.
// [START namespace_query_example]
List<String> getNamespaces(DatastoreService ds, String start, String end) {
// Start with unrestricted namespace query
Query q = new Query(Entities.NAMESPACE_METADATA_KIND);
List<Filter> subFilters = new ArrayList();
// Limit to specified range, if any
if (start != null) {
subFilters.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN_OR_EQUAL, Entities.createNamespaceKey(start)));
}
if (end != null) {
subFilters.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN_OR_EQUAL, Entities.createNamespaceKey(end)));
}
q.setFilter(CompositeFilterOperator.and(subFilters));
// Initialize result list
List<String> results = new ArrayList<String>();
// Build list of query results
for (Entity e : ds.prepare(q).asIterable()) {
results.add(Entities.getNamespaceFromNamespaceKey(e.getKey()));
}
// Return result list
return results;
}
Aggregations