Java Code Examples for com.google.appengine.api.datastore.Query.FilterOperator#LESS_THAN_OR_EQUAL

The following examples show how to use com.google.appengine.api.datastore.Query.FilterOperator#LESS_THAN_OR_EQUAL . 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 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 2
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 3
Source File: CableKeyPair.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 removeAllOldKeyPairs() {
  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 4
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 5
Source File: DeviceSubscription.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes all device subscription entities continuously using task push queue.
 *
 * @param time Threshold time before which entities created will be deleted. If time is null,
 *             current time is used and set as Threshold time.
 * @param cursor Query cursor indicates last query result set position
 */
protected void deleteAllContinuously(Date time, String cursor) {
  if (time == null) {
    time = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
  }

  Query.FilterPredicate timeFilter = new Query.FilterPredicate(PROPERTY_TIMESTAMP,
      FilterOperator.LESS_THAN_OR_EQUAL, time);
  QueryResultIterable<Entity> entities;
  List<Key> keys = new ArrayList<Key> ();
  List<String> subIds = new ArrayList<String> ();
  Query queryAll;

  queryAll = new Query(DeviceSubscription.SUBSCRIPTION_KIND).setFilter(timeFilter);
  FetchOptions options = FetchOptions.Builder.withLimit(BATCH_DELETE_SIZE);
  if (!StringUtility.isNullOrEmpty(cursor)) {
    options.startCursor(Cursor.fromWebSafeString(cursor));
  }

  entities = this.datastoreService.prepare(queryAll).asQueryResultIterable(options);
  if (entities != null && entities.iterator() != null) {
    for (Entity entity : entities) {
      keys.add(entity.getKey());
      String[] ids = new Gson().fromJson((String) entity.getProperty(PROPERTY_SUBSCRIPTION_IDS),
          String[].class);
      subIds.addAll(Arrays.asList(ids));
    }
  }

  if (keys.size() > 0) {
    deleteInBatch(keys);
    enqueueDeleteDeviceSubscription(time, entities.iterator().getCursor().toWebSafeString());
  }
  if (subIds.size() > 0) {
    deletePsiSubscriptions(subIds);
  }
}
 
Example 6
Source File: DeviceSubscription.java    From solutions-mobile-backend-starter-java with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes all device subscription entities continuously using task push queue.
 *
 * @param time Threshold time before which entities created will be deleted. If time is null,
 *             current time is used and set as Threshold time.
 * @param cursor Query cursor indicates last query result set position
 */
protected void deleteAllContinuously(Date time, String cursor) {
  if (time == null) {
    time = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
  }

  Query.FilterPredicate timeFilter = new Query.FilterPredicate(PROPERTY_TIMESTAMP,
      FilterOperator.LESS_THAN_OR_EQUAL, time);
  QueryResultIterable<Entity> entities;
  List<Key> keys = new ArrayList<Key> ();
  List<String> subIds = new ArrayList<String> ();
  Query queryAll;

  queryAll = new Query(DeviceSubscription.SUBSCRIPTION_KIND).setFilter(timeFilter);
  FetchOptions options = FetchOptions.Builder.withLimit(BATCH_DELETE_SIZE);
  if (!StringUtility.isNullOrEmpty(cursor)) {
    options.startCursor(Cursor.fromWebSafeString(cursor));
  }

  entities = this.datastoreService.prepare(queryAll).asQueryResultIterable(options);
  if (entities != null && entities.iterator() != null) {
    for (Entity entity : entities) {
      keys.add(entity.getKey());
      String[] ids = new Gson().fromJson((String) entity.getProperty(PROPERTY_SUBSCRIPTION_IDS),
          String[].class);
      subIds.addAll(Arrays.asList(ids));
    }
  }

  if (keys.size() > 0) {
    deleteInBatch(keys);
    enqueueDeleteDeviceSubscription(time, entities.iterator().getCursor().toWebSafeString());
  }
  if (subIds.size() > 0) {
    deletePsiSubscriptions(subIds);
  }
}
 
Example 7
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Test
public void queryRestrictions_compositeEqualFilter_returnsMatchedEntities() throws Exception {
  // Arrange
  Entity a = new Entity("Person", "a");
  a.setProperty("birthYear", 1930);
  a.setProperty("city", "Somewhere");
  a.setProperty("lastName", "Someone");
  Entity b = new Entity("Person", "b");
  b.setProperty("birthYear", 1960);
  b.setProperty("city", "Somewhere");
  b.setProperty("lastName", "Someone");
  Entity c = new Entity("Person", "c");
  c.setProperty("birthYear", 1990);
  c.setProperty("city", "Somewhere");
  c.setProperty("lastName", "Someone");
  Entity d = new Entity("Person", "d");
  d.setProperty("birthYear", 1960);
  d.setProperty("city", "Nowhere");
  d.setProperty("lastName", "Someone");
  Entity e = new Entity("Person", "e");
  e.setProperty("birthYear", 1960);
  e.setProperty("city", "Somewhere");
  e.setProperty("lastName", "Noone");
  datastore.put(ImmutableList.<Entity>of(a, b, c, d, e));
  long minBirthYear = 1940;
  long maxBirthYear = 1980;
  String targetCity = "Somewhere";
  String targetLastName = "Someone";

  // [START gae_java8_datastore_inequality_filters_one_property_valid_2]
  Filter lastNameFilter = new FilterPredicate("lastName", FilterOperator.EQUAL, targetLastName);

  Filter cityFilter = new FilterPredicate("city", FilterOperator.EQUAL, targetCity);

  Filter birthYearMinFilter =
      new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);

  Filter birthYearMaxFilter =
      new FilterPredicate("birthYear", FilterOperator.LESS_THAN_OR_EQUAL, maxBirthYear);

  Filter validFilter =
      CompositeFilterOperator.and(
          lastNameFilter, cityFilter, birthYearMinFilter, birthYearMaxFilter);

  Query q = new Query("Person").setFilter(validFilter);
  // [END gae_java8_datastore_inequality_filters_one_property_valid_2]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(b);
}