com.google.appengine.api.datastore.Query.Filter Java Examples

The following examples show how to use com.google.appengine.api.datastore.Query.Filter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 9 votes vote down vote up
@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 gae_java8_datastore_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 gae_java8_datastore_interface_3]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(a, c);
}
 
Example #2
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void queryRestrictions_sortWrongOrderOnInequality_isInvalid() throws Exception {
  long minBirthYear = 1940;
  // [START gae_java8_datastore_inequality_filters_sort_orders_invalid_2]
  Filter birthYearMinFilter =
      new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);

  // Not valid. Sort on birthYear needs to be first.
  Query q =
      new Query("Person")
          .setFilter(birthYearMinFilter)
          .addSort("lastName", SortDirection.ASCENDING)
          .addSort("birthYear", SortDirection.ASCENDING);
  // [END gae_java8_datastore_inequality_filters_sort_orders_invalid_2]

  // 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.
}
 
Example #3
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void queryRestrictions_missingSortOnInequality_isInvalid() throws Exception {
  long minBirthYear = 1940;
  // [START gae_java8_datastore_inequality_filters_sort_orders_invalid_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 gae_java8_datastore_inequality_filters_sort_orders_invalid_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.
}
 
Example #4
Source File: QueryTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompositeFilter() {
    Query query = new Query(kindName, rootKey);
    Filter filter = Query.CompositeFilterOperator.and(
        Query.FilterOperator.LESS_THAN_OR_EQUAL.of("intData", 40),
        Query.FilterOperator.GREATER_THAN.of("intData", 0));
    query.setFilter(filter);
    query.addSort("intData", Query.SortDirection.DESCENDING);
    List<Entity> es = service.prepare(query).asList(fo);
    assertEquals("check return count", 2, es.size());
    assertEquals("check query filter", filter, query.getFilter());
    assertEquals("check query key only", false, query.isKeysOnly());

    Query.CompositeFilter cf = (Query.CompositeFilter) query.getFilter();
    assertEquals(2, cf.getSubFilters().size());
    assertEquals(Query.CompositeFilterOperator.AND, cf.getOperator());
}
 
Example #5
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@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 gae_java8_datastore_property_filter]
  Filter propertyFilter =
      new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);
  Query q = new Query("Person").setFilter(propertyFilter);
  // [END gae_java8_datastore_property_filter]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(p2, p3);
}
 
Example #6
Source File: QueryTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithPropertyProjection() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("geoptData", GeoPt.class));
    Filter filter1 = Query.CompositeFilterOperator.or(
        Query.FilterOperator.LESS_THAN.of("intList", 5),
        Query.FilterOperator.GREATER_THAN.of("intList", 90));
    Filter filter2 = Query.FilterOperator.EQUAL.of("intList", 52);
    query.setFilter(Query.CompositeFilterOperator.and(filter1, filter2));
    // sql statement
    String sql = "SELECT geoptData FROM " + kindName;
    sql += " WHERE ((intList < 5 or intList > 90) AND intList = 52)";
    sql += " AND __ancestor__ is " + rootKey;
    assertEquals(sql.toLowerCase(), query.toString().toLowerCase());
    // check query result
    List<Entity> results = service.prepare(query).asList(fo);
    Assert.assertTrue(results.size() > 0);
    assertEquals(new GeoPt((float) (2.12), (float) (2.98)), results.get(0).getProperty("geoptData"));
    for (Entity e : results) {
        assertEquals(1, e.getProperties().size());
        assertTrue(e.getProperties().containsKey("geoptData"));
    }
}
 
Example #7
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void queryRestrictions_compositeFilter_isInvalid() throws Exception {
  long minBirthYear = 1940;
  long maxHeight = 200;
  // [START gae_java8_datastore_inequality_filters_one_property_invalid]
  Filter birthYearMinFilter =
      new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);

  Filter heightMaxFilter =
      new FilterPredicate("height", FilterOperator.LESS_THAN_OR_EQUAL, maxHeight);

  Filter invalidFilter = CompositeFilterOperator.and(birthYearMinFilter, heightMaxFilter);

  Query q = new Query("Person").setFilter(invalidFilter);
  // [END gae_java8_datastore_inequality_filters_one_property_invalid]

  // 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.
}
 
Example #8
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@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 gae_java8_datastore_interface_2]
  Filter heightMinFilter =
      new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);

  Query q = new Query("Person").setFilter(heightMinFilter);
  // [END gae_java8_datastore_interface_2]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(b, c);
}
 
Example #9
Source File: QueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetFilterInt() {
    Query query = new Query(kindName, rootKey);
    List<Filter> filterList = new ArrayList<>();
    filterList.add(Query.FilterOperator.EQUAL.of("intData", 20));
    filterList.add(Query.FilterOperator.GREATER_THAN.of("intData", 0));
    Filter filter = Query.CompositeFilterOperator.and(filterList);
    query.setFilter(filter);
    assertEquals(1, service.prepare(query).countEntities(fo));
}
 
Example #10
Source File: FilterDto.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
private List<Filter> getSubfilters(List<FilterDto> values) {
  List<Filter> subfilters = new LinkedList<Filter>();
  for (FilterDto cb : values) {
    subfilters.add(cb.getDatastoreFilter());
  }
  return subfilters;
}
 
Example #11
Source File: FilterDto.java    From solutions-mobile-backend-starter-java with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the tree of {@link FilterDto}s to a tree of {@link FilterDto}s.
 */
public Filter getDatastoreFilter() {

  switch (this.operator) {
  case EQ:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.EQUAL, getOperand());
  case LT:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.LESS_THAN, getOperand());
  case LE:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.LESS_THAN_OR_EQUAL,
        getOperand());
  case GT:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.GREATER_THAN,
        getOperand());
  case GE:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.GREATER_THAN_OR_EQUAL,
        getOperand());
  case NE:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.NOT_EQUAL, getOperand());
  case IN:
    LinkedList<Object> l = new LinkedList<Object>(values);
    l.removeFirst();
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.IN, l);
  case AND:
    return new Query.CompositeFilter(CompositeFilterOperator.AND, getSubfilters(subfilters));
  case OR:
    return new Query.CompositeFilter(CompositeFilterOperator.OR, getSubfilters(subfilters));
  }
  return null;
}
 
Example #12
Source File: FilterDto.java    From solutions-mobile-backend-starter-java with Apache License 2.0 5 votes vote down vote up
private List<Filter> getSubfilters(List<FilterDto> values) {
  List<Filter> subfilters = new LinkedList<Query.Filter>();
  for (FilterDto cb : values) {
    subfilters.add(cb.getDatastoreFilter());
  }
  return subfilters;
}
 
Example #13
Source File: QueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetFilterString() {
    Query query = new Query(kindName, rootKey);
    Filter filter = Query.CompositeFilterOperator.or(
        Query.FilterOperator.GREATER_THAN.of("stringData", "string data2"),
        Query.FilterOperator.LESS_THAN.of("stringData", "string data1"));
    query.setFilter(filter);
    assertEquals(1, service.prepare(query).countEntities(fo));
}
 
Example #14
Source File: QueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetFilterShortBlob() {
    Query query = new Query(kindName, rootKey);
    Filter filter1 = Query.FilterOperator.EQUAL.of("shortBlobData", new ShortBlob("shortBlobData0".getBytes()));
    Filter filter2 = Query.FilterOperator.LESS_THAN_OR_EQUAL.of("shortBlobData", new ShortBlob("shortBlobData1".getBytes()));
    query.setFilter(Query.CompositeFilterOperator.or(filter1, filter2));
    assertEquals(2, service.prepare(query).countEntities(fo));
}
 
Example #15
Source File: PhotoManagerNoSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Photo> getDeactivedPhotos() {
  Query query = new Query(getKind());
  Query.Filter filter = new Query.FilterPredicate(PhotoNoSql.FIELD_NAME_ACTIVE,
      FilterOperator.EQUAL, false);
  query.setFilter(filter);
  FetchOptions options = FetchOptions.Builder.withDefaults();
  return queryEntities(query, options);
}
 
Example #16
Source File: QueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetFilterRating() {
    Query query = new Query(kindName, rootKey);
    List<Filter> filterList = new ArrayList<>();
    filterList.add(Query.FilterOperator.LESS_THAN.of("ratingData", new Rating(30)));
    filterList.add(Query.FilterOperator.GREATER_THAN.of("ratingData", new Rating(0)));
    Filter filter1 = Query.CompositeFilterOperator.or(filterList);
    Filter filter2 = Query.FilterOperator.EQUAL.of("ratingData", new Rating(20));
    query.setFilter(Query.CompositeFilterOperator.and(filter1, filter2));
    assertEquals(1, service.prepare(query).countEntities(fo));
}
 
Example #17
Source File: QueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetFilterList() {
    // [0,50,90], [1,51,91], [2,52,92]
    Query query = new Query(kindName, rootKey);
    List<Filter> filterList = new ArrayList<>();
    filterList.add(Query.FilterOperator.LESS_THAN.of("intList", 5));
    filterList.add(Query.FilterOperator.GREATER_THAN.of("intList", 90));
    Filter filter1 = Query.CompositeFilterOperator.OR.of(filterList);
    Filter filter2 = Query.FilterOperator.EQUAL.of("intList", 52);
    query.setFilter(Query.CompositeFilterOperator.AND.of(filter1, filter2));
    assertEquals(1, service.prepare(query).countEntities(fo));
}
 
Example #18
Source File: CommentManagerNoSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Comment> getComments(Photo photo) {
  Query query = new Query(getKind());
  Query.Filter photoIdFilter =
      new Query.FilterPredicate(CommentNoSql.FIELD_NAME_PHOTO_ID,
          FilterOperator.EQUAL, photo.getId());
  List<Filter> filters = Arrays.asList(photoIdFilter, new Query.FilterPredicate(
      CommentNoSql.FIELD_NAME_PHOTO_OWNER_ID, FilterOperator.EQUAL, photo.getOwnerId()));
  Filter filter = new Query.CompositeFilter(CompositeFilterOperator.AND, filters);
  query.setFilter(filter);
  query.addSort(CommentNoSql.FIELD_NAME_TIMESTAMP, SortDirection.DESCENDING);
  FetchOptions options = FetchOptions.Builder.withDefaults();
  return queryEntities(query, options);
}
 
Example #19
Source File: PhotoManagerNoSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Photo> getActivePhotos() {
  Query query = new Query(getKind());
  Query.Filter filter = new Query.FilterPredicate(PhotoNoSql.FIELD_NAME_ACTIVE,
      FilterOperator.EQUAL, true);
  query.addSort(PhotoNoSql.FIELD_NAME_UPLOAD_TIME, SortDirection.DESCENDING);
  query.setFilter(filter);
  FetchOptions options = FetchOptions.Builder.withDefaults();
  return queryEntities(query, options);
}
 
Example #20
Source File: PhotoManagerNoSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Photo> getOwnedPhotos(String userId) {
  Query query = new Query(getKind());
  query.setAncestor(userManager.createDemoUserKey(userId));
  Query.Filter filter = new Query.FilterPredicate(PhotoNoSql.FIELD_NAME_ACTIVE,
      FilterOperator.EQUAL, true);
  query.setFilter(filter);
  FetchOptions options = FetchOptions.Builder.withDefaults();
  return queryEntities(query, options);
}
 
Example #21
Source File: PhotoManagerNoSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Photo> getSharedPhotos(String userId) {
  Query query = new Query(getKind());
  Query.Filter ownerFilter =
      new Query.FilterPredicate(PhotoNoSql.FIELD_NAME_OWNER_ID, FilterOperator.NOT_EQUAL, userId);
  List<Query.Filter> filterList =
      Arrays.asList(ownerFilter,
          new Query.FilterPredicate(PhotoNoSql.FIELD_NAME_SHARED, FilterOperator.EQUAL, true),
          new Query.FilterPredicate(PhotoNoSql.FIELD_NAME_ACTIVE, FilterOperator.EQUAL, true)
          );
  Filter filter = new Query.CompositeFilter(CompositeFilterOperator.AND, filterList);
  query.setFilter(filter);
  FetchOptions options = FetchOptions.Builder.withDefaults();
  return queryEntities(query, options);
}
 
Example #22
Source File: SessionData.java    From webauthndemo with Apache License 2.0 5 votes vote down vote up
/**
 * Remove all stale sessions older than 1 hour.
 */
public static void removeAllOldSessions() {
  Filter filter = new FilterPredicate(TIMESTAMP_PROPERTY, FilterOperator.LESS_THAN_OR_EQUAL,
      new Date(System.currentTimeMillis() - HOUR_IN_MILLIS));
  Query query = new Query(KIND).setFilter(filter);

  List<Entity> results =
      Datastore.getDatastore().prepare(query).asList(FetchOptions.Builder.withDefaults());

  List<Key> keys = results.stream().map(entity -> entity.getKey()).collect(Collectors.toList());
  Datastore.getDatastore().delete(keys);
}
 
Example #23
Source File: FilterDto.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the tree of {@link FilterDto}s to a tree of {@link FilterDto}s.
 */
public Filter getDatastoreFilter() {

  switch (this.operator) {
  case EQ:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.EQUAL, getOperand());
  case LT:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.LESS_THAN, getOperand());
  case LE:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.LESS_THAN_OR_EQUAL,
        getOperand());
  case GT:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.GREATER_THAN,
        getOperand());
  case GE:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.GREATER_THAN_OR_EQUAL,
        getOperand());
  case NE:
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.NOT_EQUAL, getOperand());
  case IN:
    LinkedList<Object> l = new LinkedList<Object>(values);
    l.removeFirst();
    return new Query.FilterPredicate(getPropName(), Query.FilterOperator.IN, l);
  case AND:
    return new Query.CompositeFilter(CompositeFilterOperator.AND, getSubfilters(subfilters));
  case OR:
    return new Query.CompositeFilter(CompositeFilterOperator.OR, getSubfilters(subfilters));
  }
  return null;
}
 
Example #24
Source File: AppEngineBackEnd.java    From appengine-pipelines with Apache License 2.0 5 votes vote down vote up
@Override
public Pair<? extends Iterable<JobRecord>, String> queryRootPipelines(String classFilter,
    String cursor, final int limit) {
  Query query = new Query(JobRecord.DATA_STORE_KIND);
  Filter filter = classFilter == null || classFilter.isEmpty() ? new FilterPredicate(
      ROOT_JOB_DISPLAY_NAME, GREATER_THAN, null)
      : new FilterPredicate(ROOT_JOB_DISPLAY_NAME, EQUAL, classFilter);
  query.setFilter(filter);
  final PreparedQuery preparedQuery = dataStore.prepare(query);
  final FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();
  if (limit > 0) {
    fetchOptions.limit(limit + 1);
  }
  if (cursor != null) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(cursor));
  }
  return tryFiveTimes(
      new Operation<Pair<? extends Iterable<JobRecord>, String>>("queryRootPipelines") {
        @Override
        public Pair<? extends Iterable<JobRecord>, String> call() {
          QueryResultIterator<Entity> entities =
              preparedQuery.asQueryResultIterable(fetchOptions).iterator();
          Cursor dsCursor = null;
          List<JobRecord> roots = new LinkedList<>();
          while (entities.hasNext()) {
            if (limit > 0 && roots.size() >= limit) {
              dsCursor = entities.getCursor();
              break;
            }
            JobRecord jobRecord = new JobRecord(entities.next());
            roots.add(jobRecord);
          }
          return Pair.of(roots, dsCursor == null ? null : dsCursor.toWebSafeString());
        }
      });
}
 
Example #25
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@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 gae_java8_datastore_inequality_filters_sort_orders_valid]
  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 gae_java8_datastore_inequality_filters_sort_orders_valid]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(c, d, b).inOrder();
}
 
Example #26
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@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 gae_java8_datastore_inequality_filters_one_property_valid_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 gae_java8_datastore_inequality_filters_one_property_valid_1]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(b);
}
 
Example #27
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities()
    throws Exception {
  // [START gae_java8_datastore_kindless_ancestor_query]
  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 gae_java8_datastore_kindless_ancestor_query]

  assertWithMessage("query result keys")
      .that(results)
      .containsExactly(weddingPhoto, weddingVideo);
}
 
Example #28
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@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 gae_java8_datastore_kindless_ancestor_key_query]
  Filter keyFilter =
      new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
  Query q = new Query().setAncestor(ancestorKey).setFilter(keyFilter);
  // [END gae_java8_datastore_kindless_ancestor_key_query]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(bc, bbb);
}
 
Example #29
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void keyFilterExample_kindless_returnsMatchingEntities() throws Exception {
  // Arrange
  Entity a = new Entity("Child", "a");
  Entity b = new Entity("Child", "b");
  Entity c = new Entity("Child", "c");
  Entity aa = new Entity("Child", "aa", b.getKey());
  Entity bb = new Entity("Child", "bb", b.getKey());
  Entity aaa = new Entity("Child", "aaa", bb.getKey());
  Entity bbb = new Entity("Child", "bbb", bb.getKey());
  Entity adult = new Entity("Adult", "a");
  Entity zooAnimal = new Entity("ZooAnimal", "a");
  datastore.put(ImmutableList.<Entity>of(a, b, c, aa, bb, aaa, bbb, adult, zooAnimal));

  // Act
  Key lastSeenKey = bb.getKey();
  // [START gae_java8_datastore_kindless_query]
  Filter keyFilter =
      new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
  Query q = new Query().setFilter(keyFilter);
  // [END gae_java8_datastore_kindless_query]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results")
      .that(results)
      .containsExactly(
          aaa, // Ancestor path "b/bb/aaa" is greater than "b/bb".
          bbb, // Ancestor path "b/bb/bbb" is greater than "b/bb".
          zooAnimal, // Kind "ZooAnimal" is greater than "Child"
          c); // Key name identifier "c" is greater than b.
}
 
Example #30
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@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 gae_java8_datastore_key_filter]
  Filter keyFilter =
      new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
  Query q = new Query("Person").setFilter(keyFilter);
  // [END gae_java8_datastore_key_filter]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results")
      .that(results)
      .containsExactly(
          aaa, // Ancestor path "b/bb/aaa" is greater than "b/bb".
          bbb, // Ancestor path "b/bb/bbb" is greater than "b/bb".
          c); // Key name identifier "c" is greater than b.
}