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

The following examples show how to use com.google.appengine.api.datastore.Query.FilterOperator#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: StartupServletTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@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();
  assertWithMessage("name").that(result.getProperty("name")).isEqualTo("George Washington");
}
 
Example 2
Source File: DatastoreUtil.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
public Entity getMarker(String marker) {
    DatastoreService service = DatastoreServiceFactory.getDatastoreService();
    FilterPredicate testRunFilter = new FilterPredicate(TEST_RUN_ID, FilterOperator.EQUAL, testRunId);
    FilterPredicate markerFilter = new FilterPredicate(MARKER, FilterOperator.EQUAL, marker);
    CompositeFilter filter = CompositeFilterOperator.and(testRunFilter, markerFilter);
    Query query = new Query(entityName).setFilter(filter);
    return service.prepare(query).asSingleEntity();
}
 
Example 3
Source File: DatastoreUtil.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
public void purgeTestRunRecords() {
    DatastoreService datastoreService = DatastoreServiceFactory. getDatastoreService();
    FilterPredicate testRunFilter = new FilterPredicate(TEST_RUN_ID, FilterOperator.EQUAL, testRunId);
    Query query = new Query(entityName).setFilter(testRunFilter).setKeysOnly();
    for (Entity readRec : datastoreService.prepare(query).asIterable()) {
        datastoreService.delete(readRec.getKey());
    }
}
 
Example 4
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 5
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 6
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 7
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 8
Source File: TaskServlet.java    From sc2gears with Apache License 2.0 4 votes vote down vote up
private void recalcFileStats( final PersistenceManager pm, final Key accountKey ) {
	LOGGER.fine( "Account key: " + accountKey );
	
	final Key fsKey = ServerUtils.getSingleKeyQueryResult( FileStat.class, "ownerKey", accountKey );
	if ( fsKey == null )
		return;
	
	final FileStat fileStat = pm.getObjectById( FileStat.class, fsKey );
	
	// First perform a fast check: count the different file types (keys only). If this differs, then perform a recalculation.
	final DatastoreService ds     = DatastoreServiceFactory.getDatastoreService();
	final Filter           filter = new FilterPredicate( "ownerk", FilterOperator.EQUAL, accountKey );
	
	for ( final FileType fileType : new FileType[] { FileType.SC2REPLAY, FileType.MOUSE_PRINT, FileType.OTHER } ) {
		final Query q = new Query( FILE_TYPE_CLASS_MAP.get( fileType ).getSimpleName() ).setFilter( filter );
		int count = ServerUtils.countEntities( ds, q );
		if ( fileStat.getCount( fileType ) != count ) {
			count = 0;
			int storage = 0;
			final JQBuilder< ? extends FileMetaData > qb = new JQBuilder<>( pm, FILE_TYPE_CLASS_MAP.get( fileType ) ).filter( "ownerk==p1", "KEY p1" ).range( 0, 1000 );
			while ( true ) {
				List< ? extends FileMetaData > fileMetaDataList = qb.get( accountKey );
				for ( final FileMetaData fileMetaData : fileMetaDataList ) {
					count   ++;
					storage += fileMetaData.getSize();
				}
				
				if ( fileMetaDataList.size() < 1000 )
					break;
				
				qb.cursor( fileMetaDataList );
			}
			switch ( fileType ) {
			case SC2REPLAY :
				fileStat.setRepCount    ( count   );
				fileStat.setRepStorage  ( storage );
				break;
			case MOUSE_PRINT :
				fileStat.setSmpdCount   ( count   );
				fileStat.setSmpdStorage ( storage );
				break;
			case OTHER :
				fileStat.setOtherCount  ( count   );
				fileStat.setOtherStorage( storage );
				break;
			}
		}
	}
	
	// Totals
	fileStat.setCount  ( fileStat.getRepCount  () + fileStat.getSmpdCount  () + fileStat.getOtherCount  () );
	fileStat.setStorage( fileStat.getRepStorage() + fileStat.getSmpdStorage() + fileStat.getOtherStorage() );
	
	fileStat.setRecalced( new Date() );
}
 
Example 9
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);
}
 
Example 10
Source File: DatastoreUtil.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
private CompositeFilter getTestMethodFilter(String testMethodTag) {
    FilterPredicate testRunFilter = new FilterPredicate(TEST_RUN_ID, FilterOperator.EQUAL, testRunId);
    FilterPredicate method = new FilterPredicate(TEST_METHOD_TAG, FilterOperator.EQUAL, testMethodTag);
    return CompositeFilterOperator.and(testRunFilter, method);
}