Java Code Examples for com.google.appengine.api.datastore.QueryResultIterator#getCursor()
The following examples show how to use
com.google.appengine.api.datastore.QueryResultIterator#getCursor() .
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: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@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 2
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@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 3
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@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 4
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@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 5
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@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 6
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@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 7
Source File: CursorIterator.java From teammates with GNU General Public License v2.0 | 6 votes |
/** * Fetches entities in batches and puts them into the buffer. */ private void batchFetching() { Query<T> newQuery = this.query.limit(BUFFER_SIZE); if (this.cursor != null) { newQuery = newQuery.startAt(this.cursor); } QueryResultIterator<T> iterator = newQuery.iterator(); boolean shouldContinue = false; while (iterator.hasNext()) { shouldContinue = true; this.buffer.offer(iterator.next()); } if (shouldContinue) { this.cursor = iterator.getCursor(); } }
Example 8
Source File: QueryResultTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@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 9
Source File: CursorTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testReverse() { Query query = new Query(kindName, rootKey); query.addSort(Entity.KEY_RESERVED_PROPERTY); QueryResultIterator<Entity> iter = service.prepare(query).asQueryResultIterator(); Entity e1 = iter.next(); Entity e2 = iter.next(); Cursor cursor = iter.getCursor(); //reverse query = query.reverse(); cursor = cursor.reverse(); iter = service.prepare(query).asQueryResultIterator(FetchOptions.Builder.withStartCursor(cursor)); Assert.assertEquals(e2, iter.next()); Assert.assertEquals(e1, iter.next()); }
Example 10
Source File: DataMigrationEntitiesBaseScript.java From teammates with GNU General Public License v2.0 | 4 votes |
@Override @SuppressWarnings("unchecked") protected void doOperation() { log("Running " + getClass().getSimpleName() + "..."); log("Preview: " + isPreview()); Cursor cursor = readPositionOfCursorFromFile().orElse(null); if (cursor == null) { log("Start from the beginning"); } else { log("Start from cursor position: " + cursor.toWebSafeString()); } boolean shouldContinue = true; while (shouldContinue) { shouldContinue = false; Query<T> filterQueryKeys = getFilterQuery().limit(BATCH_SIZE); if (cursor != null) { filterQueryKeys = filterQueryKeys.startAt(cursor); } QueryResultIterator<?> iterator; if (shouldUseTransaction()) { iterator = filterQueryKeys.keys().iterator(); } else { iterator = filterQueryKeys.iterator(); } while (iterator.hasNext()) { shouldContinue = true; // migrate if (shouldUseTransaction()) { migrateWithTrx((Key<T>) iterator.next()); } else { migrateWithoutTrx((T) iterator.next()); } numberOfScannedKey.incrementAndGet(); } if (shouldContinue) { cursor = iterator.getCursor(); flushEntitiesSavingBuffer(); savePositionOfCursorToFile(cursor); log(String.format("Cursor Position: %s", cursor.toWebSafeString())); log(String.format("Number Of Entity Key Scanned: %d", numberOfScannedKey.get())); log(String.format("Number Of Entity affected: %d", numberOfAffectedEntities.get())); log(String.format("Number Of Entity updated: %d", numberOfUpdatedEntities.get())); } } deleteCursorPositionFile(); log(isPreview() ? "Preview Completed!" : "Migration Completed!"); log("Total number of entities: " + numberOfScannedKey.get()); log("Number of affected entities: " + numberOfAffectedEntities.get()); log("Number of updated entities: " + numberOfUpdatedEntities.get()); }
Example 11
Source File: LocalRawGcsService.java From appengine-gcs-client with Apache License 2.0 | 4 votes |
@Override public ListItemBatch list(String bucket, String prefix, String delimiter, String marker, int maxResults, long timeoutMillis) throws IOException { ensureInitialized(); Query query = makeQuery(bucket); int prefixLength; if (!Strings.isNullOrEmpty(prefix)) { Key keyPrefix = makeKey(bucket, prefix); query.setFilter(new FilterPredicate(KEY_RESERVED_PROPERTY, GREATER_THAN_OR_EQUAL, keyPrefix)); prefixLength = prefix.length(); } else { prefixLength = 0; } FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); if (marker != null) { fetchOptions.startCursor(Cursor.fromWebSafeString(marker)); } List<ListItem> items = new ArrayList<>(maxResults); Set<String> prefixes = new HashSet<>(); QueryResultIterator<Entity> dsResults = datastore.prepare(query).asQueryResultIterator(fetchOptions); while (items.size() < maxResults && dsResults.hasNext()) { Entity entity = dsResults.next(); String name = entity.getKey().getName(); if (prefixLength > 0 && !name.startsWith(prefix)) { break; } if (!Strings.isNullOrEmpty(delimiter)) { int delimiterIdx = name.indexOf(delimiter, prefixLength); if (delimiterIdx > 0) { name = name.substring(0, delimiterIdx + 1); if (prefixes.add(name)) { items.add(new ListItem.Builder().setName(name).setDirectory(true).build()); } continue; } } GcsFilename filename = new GcsFilename(bucket, name); GcsFileMetadata metadata = createGcsFileMetadata(entity, filename); ListItem listItem = new ListItem.Builder() .setName(name) .setLength(metadata.getLength()) .setLastModified(metadata.getLastModified()) .build(); items.add(listItem); } Cursor cursor = dsResults.getCursor(); String nextMarker = null; if (items.size() == maxResults && cursor != null) { nextMarker = cursor.toWebSafeString(); } return new ListItemBatch(items, nextMarker); }