Search in sources :

Example 1 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatchedEntities.

@Test
public void queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatchedEntities() throws Exception {
    Entity a = new Entity("Widget", "a");
    a.setProperty("x", ImmutableList.<Long>of(1L, 2L));
    Entity b = new Entity("Widget", "b");
    b.setProperty("x", ImmutableList.<Long>of(1L, 2L, 3L));
    datastore.put(ImmutableList.<Entity>of(a, b));
    // [START surprising_behavior_example_4]
    Query q = new Query("Widget").setFilter(CompositeFilterOperator.and(new FilterPredicate("x", FilterOperator.NOT_EQUAL, 1), new FilterPredicate("x", FilterOperator.NOT_EQUAL, 2)));
    // [END surprising_behavior_example_4]
    // The two NOT_EQUAL filters in the query become like the combination of queries:
    // x < 1 OR (x > 1 AND x < 2) OR x > 2
    // 
    // Only "b" has some value which matches the "x > 2" portion of this query.
    // 
    // See the documentation for more details:
    // https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(b);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Example 2 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities.

@Test
public void ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities() throws Exception {
    // [START kindless_ancestor_query_example]
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity tom = new Entity("Person", "Tom");
    Key tomKey = tom.getKey();
    datastore.put(tom);
    Entity weddingPhoto = new Entity("Photo", tomKey);
    weddingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/wedding_photo.jpg");
    Entity weddingVideo = new Entity("Video", tomKey);
    weddingVideo.setProperty("videoURL", "http://domain.com/some/path/to/wedding_video.avi");
    List<Entity> mediaList = Arrays.asList(weddingPhoto, weddingVideo);
    datastore.put(mediaList);
    // By default, ancestor queries include the specified ancestor itself.
    // The following filter excludes the ancestor from the query results.
    Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, tomKey);
    Query mediaQuery = new Query().setAncestor(tomKey).setFilter(keyFilter);
    // Returns both weddingPhoto and weddingVideo,
    // even though they are of different entity kinds
    List<Entity> results = datastore.prepare(mediaQuery).asList(FetchOptions.Builder.withDefaults());
    // [END kindless_ancestor_query_example]
    assertThat(results).named("query result keys").containsExactly(weddingPhoto, weddingVideo);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) DatastoreService(com.google.appengine.api.datastore.DatastoreService) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 3 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryRestrictions_surprisingMultipleValuesEquals_returnsMatchedEntities.

@Test
public void queryRestrictions_surprisingMultipleValuesEquals_returnsMatchedEntities() throws Exception {
    Entity a = new Entity("Widget", "a");
    a.setProperty("x", ImmutableList.<Long>of(1L, 2L));
    Entity b = new Entity("Widget", "b");
    b.setProperty("x", ImmutableList.<Long>of(1L, 3L));
    Entity c = new Entity("Widget", "c");
    c.setProperty("x", ImmutableList.<Long>of(-6L, 2L));
    Entity d = new Entity("Widget", "d");
    d.setProperty("x", ImmutableList.<Long>of(-6L, 4L));
    Entity e = new Entity("Widget", "e");
    e.setProperty("x", ImmutableList.<Long>of(1L, 2L, 3L));
    datastore.put(ImmutableList.<Entity>of(a, b, c, d, e));
    // [START surprising_behavior_example_2]
    Query q = new Query("Widget").setFilter(CompositeFilterOperator.and(new FilterPredicate("x", FilterOperator.EQUAL, 1), new FilterPredicate("x", FilterOperator.EQUAL, 2)));
    // [END surprising_behavior_example_2]
    // Only "a" and "e" have both 1 and 2 in the "x" array-valued property.
    // See the documentation for more details:
    // https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(a, e);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Example 4 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryRestrictions_surprisingMultipleValuesAllMustMatch_returnsNoEntities.

@Test
public void queryRestrictions_surprisingMultipleValuesAllMustMatch_returnsNoEntities() throws Exception {
    Entity a = new Entity("Widget", "a");
    List<Long> xs = Arrays.asList(1L, 2L);
    a.setProperty("x", xs);
    datastore.put(a);
    // [START surprising_behavior_example_1]
    Query q = new Query("Widget").setFilter(CompositeFilterOperator.and(new FilterPredicate("x", FilterOperator.GREATER_THAN, 1), new FilterPredicate("x", FilterOperator.LESS_THAN, 2)));
    // [END surprising_behavior_example_1]
    // Entity "a" will not match because no individual value matches all filters.
    // See the documentation for more details:
    // https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").isEmpty();
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Example 5 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryInterface_singleFilter_returnsMatchedEntities.

@Test
public void queryInterface_singleFilter_returnsMatchedEntities() 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", 300);
    datastore.put(ImmutableList.<Entity>of(a, b, c));
    // Act
    long minHeight = 150;
    // [START interface_2]
    Filter heightMinFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);
    Query q = new Query("Person").setFilter(heightMinFilter);
    // [END interface_2]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(b, c);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Aggregations

FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)38 Query (com.google.appengine.api.datastore.Query)31 Entity (com.google.appengine.api.datastore.Entity)26 Filter (com.google.appengine.api.datastore.Query.Filter)24 Test (org.junit.Test)21 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)20 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)20 Key (com.google.appengine.api.datastore.Key)6 DatastoreService (com.google.appengine.api.datastore.DatastoreService)3 PrintWriter (java.io.PrintWriter)3 ArrayList (java.util.ArrayList)3 StringWriter (java.io.StringWriter)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)1 Transaction (com.google.appengine.api.datastore.Transaction)1 Collection (java.util.Collection)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1