com.google.appengine.api.datastore.FetchOptions Java Examples

The following examples show how to use com.google.appengine.api.datastore.FetchOptions. 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: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void keysOnlyExample_returnsMatchingEntities() throws Exception {
  // Arrange
  Entity a = new Entity("Person", "a");
  Entity b = new Entity("Building", "b");
  Entity c = new Entity("Person", "c");
  datastore.put(ImmutableList.<Entity>of(a, b, c));

  // [START gae_java8_datastore_keys_only]
  Query q = new Query("Person").setKeysOnly();
  // [END gae_java8_datastore_keys_only]

  // Assert
  List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(a, c);
}
 
Example #4
Source File: ProjectionTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void projectionQuery_grouping_filtersDuplicates() {
  putTestData("some duplicate", 0L);
  putTestData("some duplicate", 0L);
  putTestData("too big", 1L);

  // [START grouping]
  Query q = new Query("TestKind");
  q.addProjection(new PropertyProjection("A", String.class));
  q.addProjection(new PropertyProjection("B", Long.class));
  q.setDistinct(true);
  q.setFilter(Query.FilterOperator.LESS_THAN.of("B", 1L));
  q.addSort("B", Query.SortDirection.DESCENDING);
  q.addSort("A");
  // [END grouping]

  List<Entity> entities = datastore.prepare(q).asList(FetchOptions.Builder.withLimit(5));
  assertThat(entities).hasSize(1);
  Entity entity = entities.get(0);
  assertWithMessage("entity.A")
      .that((String) entity.getProperty("A"))
      .isEqualTo("some duplicate");
  assertWithMessage("entity.B").that((long) entity.getProperty("B")).isEqualTo(0L);
}
 
Example #5
Source File: MyEndpoint.java    From endpoints-codelab-android with GNU General Public License v3.0 6 votes vote down vote up
@ApiMethod(name = "clearTasks")
public void clearTasks() {
    DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
    Transaction txn = datastoreService.beginTransaction();
    try {
        Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt");
        Query query = new Query(taskBeanParentKey);
        List<Entity> results = datastoreService.prepare(query).asList(FetchOptions.Builder.withDefaults());
        for (Entity result : results) {
            datastoreService.delete(result.getKey());
        }
        txn.commit();
    } finally {
        if (txn.isActive()) {
            txn.rollback();
        }
    }
}
 
Example #6
Source File: NamespaceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery() {
    NamespaceManager.set("");
    Query query = new Query("__namespace__");
    int nsCount = service.prepare(query)
        .countEntities(FetchOptions.Builder.withDefaults());
    assertTrue(nsCount > 0);
    String ns = "";
    for (Entity readRec : service.prepare(query).asIterable()) {
        ns = readRec.getKey().getName() + "," + ns;
    }
    for (int i = 0; i < namespaceDat.length; i++) {
        if (!namespaceDat[i].equals("")) {
            assertTrue(ns.indexOf(namespaceDat[i]) >= 0);
        } else {
            assertTrue(ns.indexOf("null") >= 0);
        }
    }
}
 
Example #7
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 #8
Source File: TestReport.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the {@code TestReport} with the given build type id ordered by build id in the descendant order.
 *
 * @param buildTypeId      the build type id.
 * @param limit            the optional fetch limit, by default {@link com.google.appengine.tck.site.endpoints.TestReport#DEFAULT_FETCH_LIMIT}.
 * @param reports          the reports entry point
 * @return the matching test reports list or an empty one if none.
 */
@SuppressWarnings("unchecked")
public static List<TestReport> findByBuildTypeIdOrderByBuildIdDesc(String buildTypeId, Optional<Integer> limit, Reports reports) {
    final MemcacheService memcacheService = reports.getMemcacheService();
    List<TestReport> results = (List<TestReport>) memcacheService.get(buildTypeId);
    if (results == null) {
        final Filter buildTypeFilter = new Query.FilterPredicate("buildTypeId", FilterOperator.EQUAL, buildTypeId);
        final Query query = new Query(TEST_REPORT).setFilter(buildTypeFilter).addSort("buildId", DESCENDING);

        final DatastoreService datastoreService = reports.getDatastoreService();
        final PreparedQuery preparedQuery = datastoreService.prepare(query);
        final List<Entity> entities = preparedQuery.asList(FetchOptions.Builder.withLimit(limit.or(DEFAULT_FETCH_LIMIT)));

        results = new ArrayList<>();
        for (Entity oneEntity : entities) {
            final TestReport report = from(oneEntity);
            results.add(report);
        }

        memcacheService.put(buildTypeId, results);
    }
    return results;
}
 
Example #9
Source File: QueryResultTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testCursor() throws Exception {
    Entity parent = createTestEntityWithUniqueMethodNameKey("Person", "testKeysOnly");
    Key key = parent.getKey();

    Entity john = createEntity("Person", key)
        .withProperty("name", "John")
        .withProperty("surname", "Doe")
        .store();

    Query query = new Query("Person")
        .setAncestor(key)
        .setKeysOnly();

    PreparedQuery preparedQuery = service.prepare(query);
    QueryResultIterator<Entity> iter = preparedQuery.asQueryResultIterator();
    Assert.assertNotNull(iter.next());
    Cursor cursor = iter.getCursor();

    iter = service.prepare(query).asQueryResultIterator(FetchOptions.Builder.withStartCursor(cursor));
    Assert.assertFalse(iter.hasNext());
}
 
Example #10
Source File: QueryResultTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndexListFromList() throws Exception {
    Entity parent = createTestEntityWithUniqueMethodNameKey("Person", "testKeysOnly");
    Key key = parent.getKey();

    Entity joe = createEntity("Person", key)
        .withProperty("name", "Joe")
        .withProperty("surname", "Moe")
        .store();

    Query query = new Query("Person")
        .setAncestor(key)
        .setKeysOnly();

    PreparedQuery preparedQuery = service.prepare(query);
    QueryResultList<Entity> list = preparedQuery.asQueryResultList(FetchOptions.Builder.withDefaults());
    List<Index> indexes = list.getIndexList();
    if (indexes != null) {
        // TODO -- something useful
        System.out.println("indexes = " + indexes);
    }
}
 
Example #11
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 #12
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 #13
Source File: TransactionsTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueriesWithDifferentAncestorsInsideSameTransactionNoUsage() {
    Transaction tx = service.beginTransaction();
    try {
        Key a1 = KeyFactory.createKey("ancestor", "1");
        prepareQueryWithAncestor(tx, a1).asIterator();

        Key a2 = KeyFactory.createKey("ancestor", "2");
        prepareQueryWithAncestor(tx, a2).asList(FetchOptions.Builder.withDefaults());

        Key a3 = KeyFactory.createKey("ancestor", "3");
        prepareQueryWithAncestor(tx, a3).asIterable();

        Key a4 = KeyFactory.createKey("ancestor", "4");
        prepareQueryWithAncestor(tx, a4).asQueryResultIterable();

        Key a5 = KeyFactory.createKey("ancestor", "5");
        prepareQueryWithAncestor(tx, a5).asQueryResultIterator();

        Key a6 = KeyFactory.createKey("ancestor", "6");
        prepareQueryWithAncestor(tx, a6).asQueryResultList(FetchOptions.Builder.withDefaults());
    } finally {
        tx.rollback();
    }
}
 
Example #14
Source File: SessionCleanupServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
private void clearAll(HttpServletResponse response) {
  Query query = new Query(SESSION_ENTITY_TYPE);
  query.setKeysOnly();
  query.addFilter(EXPIRES_PROP, Query.FilterOperator.LESS_THAN,
      System.currentTimeMillis());
  ArrayList<Key> killList = new ArrayList<Key>();
  Iterable<Entity> entities = datastore.prepare(query).asIterable(
      FetchOptions.Builder.withLimit(MAX_SESSION_COUNT));
  for (Entity expiredSession : entities) {
    Key key = expiredSession.getKey();
    killList.add(key);
  }
  datastore.delete(killList);
  response.setStatus(HttpServletResponse.SC_OK);
  try {
    response.getWriter().println("Cleared " + killList.size() + " expired sessions.");
  } catch (IOException ex) {
    // We still did the work, and successfully... just send an empty body.
  }
}
 
Example #15
Source File: AppEngineBackEnd.java    From appengine-pipelines with Apache License 2.0 6 votes vote down vote up
private void deleteAll(final String kind, final Key rootJobKey) {
  logger.info("Deleting all " + kind + " with rootJobKey=" + rootJobKey);
  final int chunkSize = 100;
  final FetchOptions fetchOptions = FetchOptions.Builder.withChunkSize(chunkSize);
  final PreparedQuery preparedQuery = dataStore.prepare(new Query(kind).setKeysOnly().setFilter(
      new FilterPredicate(ROOT_JOB_KEY_PROPERTY, EQUAL, rootJobKey)));
  tryFiveTimes(new Operation<Void>("delete") {
    @Override
    public Void call() {
      Iterator<Entity> iter = preparedQuery.asIterator(fetchOptions);
      while (iter.hasNext()) {
        ArrayList<Key> keys = new ArrayList<>(chunkSize);
        for (int i = 0; i < chunkSize && iter.hasNext(); i++) {
          keys.add(iter.next().getKey());
        }
        logger.info("Deleting  " + keys.size() + " " + kind + "s with rootJobKey=" + rootJobKey);
        dataStore.delete(null, keys);
      }
      return null;
    }
  });
}
 
Example #16
Source File: ServerUtils.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * Counts the entities returned by the specified query.
 * @param ds    reference to the datastore service
 * @param query query whose results to count
 * @return the number of entities returned by the query
 */
public static int countEntities( final DatastoreService ds, final com.google.appengine.api.datastore.Query q ) {
	q.setKeysOnly();
	
	final int          batchSize    = 1000;
	final FetchOptions fetchOptions = FetchOptions.Builder.withLimit( batchSize );
	
	Cursor cursor = null;
	int    count  = 0;
	while ( true ) {
		if ( cursor != null )
			fetchOptions.startCursor( cursor );
		
		final QueryResultList< Entity > resultList = ds.prepare( q ).asQueryResultList( fetchOptions );
		
		count += resultList.size();
		
		if ( resultList.size() < batchSize )
			return count;
		
		cursor = resultList.getCursor();
	}
}
 
Example #17
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 #18
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 #19
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void ancestorFilterExample_returnsMatchingEntities() throws Exception {
  Entity a = new Entity("Person", "a");
  Entity b = new Entity("Person", "b");
  Entity aa = new Entity("Person", "aa", a.getKey());
  Entity ab = new Entity("Person", "ab", a.getKey());
  Entity bb = new Entity("Person", "bb", b.getKey());
  datastore.put(ImmutableList.<Entity>of(a, b, aa, ab, bb));

  Key ancestorKey = a.getKey();
  // [START gae_java8_datastore_ancestor_filter]
  Query q = new Query("Person").setAncestor(ancestorKey);
  // [END gae_java8_datastore_ancestor_filter]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(a, aa, ab);
}
 
Example #20
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooksByUser(String userId, String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      // Only for this user
      .setFilter(new Query.FilterPredicate(
          Book.CREATED_BY_ID, Query.FilterOperator.EQUAL, userId))
      // a custom datastore index is required since you are filtering by one property
      // but ordering by another
      .addSort(Book.TITLE, SortDirection.ASCENDING);
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example #21
Source File: QueryFilteringByGAEPropertyTypesTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilterByEntityKey() {
    Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, "testFilterByEntityKey");
    Key parentKey = parentEntity.getKey();

    Key fooKey = KeyFactory.createKey(parentKey, "foo", 1);
    Entity fooEntity = new Entity(fooKey);
    service.put(fooEntity);

    Query query = new Query("foo")
        .setAncestor(parentKey)
        .setFilter(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, EQUAL, fooKey));

    PreparedQuery preparedQuery = service.prepare(query);
    List<Entity> results = preparedQuery.asList(FetchOptions.Builder.withDefaults());

    assertEquals(1, results.size());
    assertEquals(fooEntity, results.get(0));
}
 
Example #22
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooks(String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title"
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example #23
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooks(String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title"
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example #24
Source File: CursorTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
private Cursor checkPage(Query query, Cursor stCursor, Cursor endCursor, int limit, int exptRet,
                         String chkSt, String chkEnd) {
    FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit);
    if (stCursor != null) {
        fetchOption = fetchOption.startCursor(stCursor);
    }
    if (endCursor != null) {
        fetchOption = fetchOption.endCursor(endCursor);
    }
    QueryResultList<Entity> nextBatch = service.prepare(query)
        .asQueryResultList(fetchOption);
    assertEquals(exptRet, nextBatch.size());
    if (chkSt != null) {
        assertEquals(chkSt, nextBatch.get(0).getProperty("name"));
    }
    if (chkEnd != null) {
        assertEquals(chkEnd, nextBatch.get(nextBatch.size() - 1).getProperty("name"));
    }
    return nextBatch.getCursor();
}
 
Example #25
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooks(String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title"
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example #26
Source File: QueryFilteringByGAEPropertyTypesTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testEntityKeyInequalityFilter() {
    Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, "testFilterByInequalityFilter");
    Key parentKey = parentEntity.getKey();

    Entity entity1 = new Entity("foo", parentKey);
    service.put(entity1);

    Entity entity2 = new Entity("foo", parentKey);
    service.put(entity2);

    Query query = new Query("foo")
        .setAncestor(parentKey)
        .setFilter(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, GREATER_THAN, entity1.getKey()));
    List<Entity> list = service.prepare(query).asList(FetchOptions.Builder.withDefaults());
    assertEquals(1, list.size());
    assertEquals(entity2.getKey(), list.get(0).getKey());
}
 
Example #27
Source File: CableKeyPair.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
public static KeyPair get(Long sessionId) throws IOException {
  Key sessionKey = KeyFactory.createKey(SessionData.KIND, sessionId);

  Query query = new Query(KIND).setAncestor(sessionKey);

  List<Entity> results =
      Datastore.getDatastore().prepare(query).asList(FetchOptions.Builder.withDefaults());
  
  for (Entity result : results) {
    if (result.getParent().getId() == sessionId.longValue()) {
      byte[] serializedKeyPair =
          BaseEncoding.base64().decode((String) result.getProperty(KEY_PAIR_PROPERTY));
      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedKeyPair));
      try {
        return (KeyPair) in.readObject();
      } catch (ClassNotFoundException e1) {
        throw new IOException(e1);
      }
    }
  }
  throw new IOException("KeyPair " + String.valueOf(sessionId) + "not found");
}
 
Example #28
Source File: ListPeopleServletTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private String getFirstCursor() {
  Query q = new Query("Person").addSort("name", SortDirection.ASCENDING);
  PreparedQuery pq = datastore.prepare(q);
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(ListPeopleServlet.PAGE_SIZE);
  QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);
  return results.getCursor().toWebSafeString();
}
 
Example #29
Source File: QueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testReverse() throws Exception {
    final Query query = new Query(kindName, rootKey);
    query.addSort(Entity.KEY_RESERVED_PROPERTY);
    query.addSort("intData");

    List<Entity> list1 = service.prepare(query).asList(FetchOptions.Builder.withDefaults());
    List<Entity> list2 = service.prepare(query.reverse()).asList(FetchOptions.Builder.withDefaults());

    int size = list1.size();
    Assert.assertEquals(size, list2.size());
    for (int i = 0; i < size; i++) {
        Assert.assertEquals(list1.get(i), list2.get(size - i - 1));
    }
}
 
Example #30
Source File: NotificationCleanupServlet.java    From solutions-mobile-backend-starter-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(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");
}