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

The following examples show how to use com.google.appengine.api.datastore.Query.FilterOperator. 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_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 gae_java8_datastore_surprising_behavior_1]
  Query q =
      new Query("Widget")
          .setFilter(
              CompositeFilterOperator.and(
                  new FilterPredicate("x", FilterOperator.GREATER_THAN, 1),
                  new FilterPredicate("x", FilterOperator.LESS_THAN, 2)));
  // [END gae_java8_datastore_surprising_behavior_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());
  assertWithMessage("query results").that(results).isEmpty();
}
 
Example #3
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.beginTransaction();
  try {
    Query query = new Query(SESSION_KIND)
        .setFilter(new FilterPredicate(varName, FilterOperator.EQUAL, varValue));
    Iterator<Entity> results = datastore.prepare(transaction, query).asIterator();
    while (results.hasNext()) {
      Entity stateEntity = results.next();
      datastore.delete(transaction, stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example #4
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.beginTransaction();
  try {
    Query query = new Query(SESSION_KIND)
        .setFilter(new FilterPredicate(varName, FilterOperator.EQUAL, varValue));
    Iterator<Entity> results = datastore.prepare(transaction, query).asIterator();
    while (results.hasNext()) {
      Entity stateEntity = results.next();
      datastore.delete(transaction, stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example #5
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.beginTransaction();
  try {
    Query query = new Query(SESSION_KIND)
        .setFilter(new FilterPredicate(varName, FilterOperator.EQUAL, varValue));
    Iterator<Entity> results = datastore.prepare(transaction, query).asIterator();
    while (results.hasNext()) {
      Entity stateEntity = results.next();
      datastore.delete(transaction, stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example #6
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.beginTransaction();
  try {
    Query query = new Query(SESSION_KIND)
        .setFilter(new FilterPredicate(varName, FilterOperator.EQUAL, varValue));
    Iterator<Entity> results = datastore.prepare(transaction, query).asIterator();
    while (results.hasNext()) {
      Entity stateEntity = results.next();
      datastore.delete(transaction, stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example #7
Source File: CachingService.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the Account key associated with the specified user.
 * @param pm   reference to the persistence manager
 * @param user user to return the account key for
 * @return the Account key associated with the specified user; or <code>null</code> if no account is associated with the specified user
 */
public static Key getAccountKeyByUser( final PersistenceManager pm, final User user ) {
	final String memcacheKey = CACHE_KEY_USER_ACCOUNT_KEY_PREFIX + user.getEmail();
	final String accountKeyString = (String) memcacheService.get( memcacheKey );
	if ( accountKeyString != null )
		return KeyFactory.stringToKey( accountKeyString );
	
	final Query q = new Query( Account.class.getSimpleName() );
	q.setFilter( new FilterPredicate( "user", FilterOperator.EQUAL, user ) );
	q.setKeysOnly();
	final List< Entity > entityList = DatastoreServiceFactory.getDatastoreService().prepare( q ).asList( FetchOptions.Builder.withDefaults() );
	if ( entityList.isEmpty() )
		return null;
	
	final Key accountKey = entityList.get( 0 ).getKey();
	try {
		memcacheService.put( memcacheKey, KeyFactory.keyToString( accountKey ) );
	}
	catch ( final MemcacheServiceException mse ) {
		LOGGER.log( Level.WARNING, "Failed to put key to memcache: " + memcacheKey, mse );
		// Ignore memcache errors, do not prevent serving user request
	}
	
	return accountKey;
}
 
Example #8
Source File: IndexesServlet.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
  // [START exploding_index_example_1]
  Query q =
      new Query("Widget")
          .setFilter(
              CompositeFilterOperator.and(
                  new FilterPredicate("x", FilterOperator.EQUAL, 1),
                  new FilterPredicate("y", FilterOperator.EQUAL, 2)))
          .addSort("date", Query.SortDirection.ASCENDING);
  // [END exploding_index_example_1]
  List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());

  PrintWriter out = resp.getWriter();
  out.printf("Got %d widgets.\n", results.size());
}
 
Example #9
Source File: CachingService.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the Account key associated with the specified authorization key.
 * @param pm               reference to the persistence manager
 * @param authorizationKey authorization key to return the account key for
 * @return the Account key associated with the specified authorization key; or <code>null</code> if the authorization key is invalid
 */
public static Key getAccountKeyByAuthKey( final PersistenceManager pm, final String authorizationKey ) {
	final String memcacheKey = CACHE_KEY_AUTH_KEY_ACCOUNT_KEY_PREFIX + authorizationKey;
	final String accountKeyString = (String) memcacheService.get( memcacheKey );
	if ( accountKeyString != null )
		return KeyFactory.stringToKey( accountKeyString );
	
	final Query q = new Query( Account.class.getSimpleName() );
	q.setFilter( new FilterPredicate( "authorizationKey", FilterOperator.EQUAL, authorizationKey ) );
	q.setKeysOnly();
	final List< Entity > entityList = DatastoreServiceFactory.getDatastoreService().prepare( q ).asList( FetchOptions.Builder.withDefaults() );
	if ( entityList.isEmpty() )
		return null;
	
	final Key accountKey = entityList.get( 0 ).getKey();
	try {
		memcacheService.put( memcacheKey, KeyFactory.keyToString( accountKey ) );
	}
	catch ( final MemcacheServiceException mse ) {
		LOGGER.log( Level.WARNING, "Failed to put key to memcache: " + memcacheKey, mse );
		// Ignore memcache errors, do not prevent serving user request
	}
	
	return accountKey;
}
 
Example #10
Source File: SchemaTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testPropertyMetadata() {
    NamespaceManager.set(namespaceDat[2]);
    // sort by kind/property, kindDat[1] < kindDat[0] < kindDat[2]
    Query q = new Query("__property__").addSort(Entity.KEY_RESERVED_PROPERTY).setKeysOnly();
    // filter out properties for kind "testing"
    Key key1 = Entities.createPropertyKey(kindDat[0], "urlData");
    Key key2 = Entities.createPropertyKey(kindDat[2], "urlData");
    q.setFilter(CompositeFilterOperator.and(
        new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, key1),
        new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN_OR_EQUAL, key2)));
    List<Entity> el = service.prepare(q).asList(fo);
    // un-indexed property, textData, will not be returned in __property__ queries.
    assertEquals(13, el.size());
    for (int i = 0; i < el.size(); i++) {
        assertEquals(namespaceDat[2], el.get(i).getKey().getNamespace());
        assertEquals(kindDat[2], el.get(i).getKey().getParent().getName());
        if (i == 0) {
            assertEquals("adressData", el.get(0).getKey().getName());
        } else if (i == el.size() - 1) {
            assertEquals("urlData", el.get(el.size() - 1).getKey().getName());
        }
    }
}
 
Example #11
Source File: SchemaTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testKindMetadata() {
    // check non empty namespace only
    for (int i = 1; i < namespaceDat.length; i++) {
        NamespaceManager.set(namespaceDat[i]);
        Query q = new Query("__kind__").addSort(Entity.KEY_RESERVED_PROPERTY);
        int count = 0;
        for (Entity e : service.prepare(q).asIterable()) {
            // do not count those stats entities for namespace.
            if (!e.getKey().getName().startsWith("__Stat_Ns_")) {
                count++;
            }
        }
        // For each namespace, only 3 user defined kinds.
        assertEquals(3, count);
        // check a specified namespace
        Key key1 = Entities.createKindKey("testing");
        q.setFilter(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.EQUAL, key1));
        assertEquals(1, service.prepare(q).countEntities(fo));
        Entity ke = service.prepare(q).asSingleEntity();
        assertEquals("testing", ke.getKey().getName());
        assertEquals(namespaceDat[i], ke.getKey().getNamespace());
        assertEquals(namespaceDat[i], ke.getNamespace());
    }
}
 
Example #12
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 #13
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 #14
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 #15
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 #16
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 #17
Source File: BatchTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
@InSequence(4)
public void testStep4BigAsIterator() {
    Query q = new Query(kindName, rootKey);
    q.setFilter(new FilterPredicate("count", FilterOperator.LESS_THAN, bigCount));
    Iterator<Entity> eData = service.prepare(q).asIterator(fo);
    assertEquals(bigCount, getSize(eData));
}
 
Example #18
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 #19
Source File: BatchTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
@InSequence(9)
public void testStep9BigFilterIn() {
    int filterNum = 500;
    Query q = new Query(kindName, rootKey);
    q.setFilter(new FilterPredicate("count", FilterOperator.IN, getFilterIn(filterNum)));
    FetchOptions fo = FetchOptions.Builder.withDefaults();
    int ttl = service.prepare(q).countEntities(fo);
    assertEquals(filterNum, ttl);
}
 
Example #20
Source File: NotificationCleanupServlet.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
private void doCleanup() {
  log.log(Level.INFO, "Starting a job to clean up processed notification records");

  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  Calendar cutoffTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  cutoffTime.add(Calendar.HOUR, -HOURS_TO_KEEP_RECORDS_OF_PROCESSED_NOTIFICATIONS);

  Query query = new Query(Worker.PROCESSED_NOTIFICATION_TASKS_ENTITY_KIND)
    .setFilter(new FilterPredicate("processedAt", FilterOperator.LESS_THAN, cutoffTime.getTime()))
    .setKeysOnly();

  PreparedQuery preparedQuery = datastore.prepare(query);

  // Delete in batches
  List<Entity> entitiesToBeDeleted = null;
  do {
    entitiesToBeDeleted = preparedQuery.asList(FetchOptions.Builder.withLimit(5));

    List<Key> keys = new ArrayList<Key>();

    for (Entity entity : entitiesToBeDeleted) {
      keys.add(entity.getKey());
    }

    datastore.delete(keys);
  } while (entitiesToBeDeleted.size() > 0);

  log.log(Level.INFO, "Finished a job to clean up processed notification records");
}
 
Example #21
Source File: SchemaTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testNamespaceMetadata() {
    NamespaceManager.set(""); // TODO -- shouldn't Entities.createNamespaceKey already do this by default?
    Query q = new Query("__namespace__").addSort(Entity.KEY_RESERVED_PROPERTY);
    Key key1 = Entities.createNamespaceKey(namespaceDat[1]);
    Key key2 = Entities.createNamespaceKey(namespaceDat[2]);
    q.setFilter(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.IN, Arrays.asList(key1, key2)));
    List<Entity> ns = service.prepare(q).asList(fo);
    assertEquals(2, ns.size());

    for (int i = 0; i < ns.size(); i++) {
        assertEquals(namespaceDat[i + 1], ns.get(i).getKey().getName());
    }
}
 
Example #22
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 #23
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private Entity retrievePersonWithLastName(String targetLastName) {
  // [START gae_java8_datastore_single_retrieval]
  Query q =
      new Query("Person")
          .setFilter(new FilterPredicate("lastName", FilterOperator.EQUAL, targetLastName));

  PreparedQuery pq = datastore.prepare(q);
  Entity result = pq.asSingleEntity();
  // [END gae_java8_datastore_single_retrieval]
  return result;
}
 
Example #24
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 #25
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@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 gae_java8_datastore_surprising_behavior_4]
  Query q =
      new Query("Widget")
          .setFilter(
              CompositeFilterOperator.and(
                  new FilterPredicate("x", FilterOperator.NOT_EQUAL, 1),
                  new FilterPredicate("x", FilterOperator.NOT_EQUAL, 2)));
  // [END gae_java8_datastore_surprising_behavior_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());
  assertWithMessage("query results").that(results).containsExactly(b);
}
 
Example #26
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 #27
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void queryRestrictions_surprisingMultipleValuesNotEquals_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));
  datastore.put(ImmutableList.<Entity>of(a, b, c, d, e));

  // [START gae_java8_datastore_surprising_behavior_3]
  Query q = new Query("Widget").setFilter(new FilterPredicate("x", FilterOperator.NOT_EQUAL, 1));
  // [END gae_java8_datastore_surprising_behavior_3]

  // The query matches any entity that has a some value other than 1. Only
  // entity "e" is not matched.  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());
  assertWithMessage("query results").that(results).containsExactly(a, b, c, d);
}
 
Example #28
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@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 gae_java8_datastore_surprising_behavior_2]
  Query q =
      new Query("Widget")
          .setFilter(
              CompositeFilterOperator.and(
                  new FilterPredicate("x", FilterOperator.EQUAL, 1),
                  new FilterPredicate("x", FilterOperator.EQUAL, 2)));
  // [END gae_java8_datastore_surprising_behavior_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());
  assertWithMessage("query results").that(results).containsExactly(a, e);
}
 
Example #29
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 #30
Source File: NotificationCleanupServlet.java    From solutions-ios-push-notification-sample-backend-java with Apache License 2.0 5 votes vote down vote up
private void doCleanup() {
  log.log(Level.INFO, "Starting a job to clean up processed notification records");
  
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  Calendar cutoffTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  cutoffTime.add(Calendar.HOUR, -HOURS_TO_KEEP_RECORDS_OF_PROCESSED_NOTIFICATIONS);
  
  Query query = new Query(PushNotificationWorker.PROCESSED_NOTIFICATION_TASKS_ENTITY_KIND)
    .setFilter(new FilterPredicate("processedAt", FilterOperator.LESS_THAN, cutoffTime.getTime()))
    .setKeysOnly();
  
  PreparedQuery preparedQuery = datastore.prepare(query);

  // Delete in batches
  List<Entity> entitiesToBeDeleted = null;
  do {
    entitiesToBeDeleted = preparedQuery.asList(FetchOptions.Builder.withLimit(5));
    
    List<Key> keys = new ArrayList<Key>();
    
    for (Entity entity : entitiesToBeDeleted) {
      keys.add(entity.getKey());
    }
  
    datastore.delete(keys);
  } while (entitiesToBeDeleted.size() > 0);

  log.log(Level.INFO, "Finished a job to clean up processed notification records");
}