//Find the next cursor Cursor cursor = iterator.getCursor(); if (cursor != null) { cursorString = cursor.toWebSafeString(); }
String cursorString = cursor.toWebSafeString(); // Send cursor around as string // Query q = the same query that produced the cursor; Cursor cursor = Cursor.fromWebSafeString(cursorString); Map<String, Object> extensionMap = new HashMap<String, Object>(); extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor); q.setExtensions(extensionMap); q.setRange(0, 20); //note, the same range; //the query should cover everything you expect to load. q.execute();
println(String.format("Cursor Position: %s", cursor.toWebSafeString())); println(String.format("Number Of Entity Key Scanned: %d", numberOfScannedKey.get())); println(String.format("Number Of Entity affected: %d", numberOfAffectedEntities.get()));
public static void serialize(SerializationStreamWriter streamWriter, Cursor instance) throws SerializationException { streamWriter.writeString(instance.toWebSafeString()); } }
cursor = JPACursorHelper.getCursor(execute); if (cursor != null) cursorString = cursor.toWebSafeString();
cursor = JPACursorHelper.getCursor(execute); if (cursor != null) cursorString = cursor.toWebSafeString();
cursor = JPACursorHelper.getCursor(execute); if (cursor != null) cursorString = cursor.toWebSafeString();
private void setCursor(QueryBuilder<?> builder, QueryResultList<Entity> queryResult) { if (queryResult.getCursor() != null) { builder.setCursor(queryResult.getCursor().toWebSafeString()); } }
@POST @Path("/") public JsonNode query(String queryJson) throws LeanException { QueryResult result = DatastoreUtils.queryEntityPrivate(LeanQuery.fromJson(queryJson)); ObjectNode jsonResult = JsonUtils.entityListToJson(result.getResult()); if (result.getCursor() != null) { jsonResult.put("cursor", result.getCursor().toWebSafeString()); } return jsonResult; }
@Override public Pair<? extends Iterable<JobRecord>, String> call() { QueryResultIterator<Entity> entities = preparedQuery.asQueryResultIterable(fetchOptions).iterator(); Cursor dsCursor = null; List<JobRecord> roots = new LinkedList<>(); while (entities.hasNext()) { if (limit > 0 && roots.size() >= limit) { dsCursor = entities.getCursor(); break; } JobRecord jobRecord = new JobRecord(entities.next()); roots.add(jobRecord); } return Pair.of(roots, dsCursor == null ? null : dsCursor.toWebSafeString()); } });
private void fanout() { QueryResultList<Entity> entities = getNamespaces(); if (entities.size() >= BATCH_SIZE) { enqueueNextBatch(entities.getCursor().toWebSafeString()); } for (Entity entity : entities) { enqueuePipeDropsTask(entity); } }
public void query(long toVersion, Optional<String> startAt) { LOGGER.info("Starting initial sync query..."); Stopwatch stopwatch = Stopwatch.createStarted(); Query<FormRecordEntity> query = ofy().load().type(FormRecordEntity.class) .ancestor(FormEntity.key(formClass)) .chunk(500); if(startAt.isPresent()) { query = query.startAt(Cursor.fromWebSafeString(startAt.get())); } QueryResultIterator<FormRecordEntity> it = query.iterator(); while(it.hasNext()) { FormRecordEntity record = it.next(); if(visibilityPredicate.test(record.getRecordId())) { add(record); if(!sizeEstimator.timeAndSpaceRemaining()) { stop(it.getCursor().toWebSafeString()); break; } } } LOGGER.info("Initial sync query complete in " + stopwatch.elapsed(TimeUnit.SECONDS) + " with estimate size: " + sizeEstimator.getEstimatedSizeInBytes() + " bytes"); }
cursor = JDOCursorHelper.getCursor(execute); if (cursor != null) cursorString = cursor.toWebSafeString();
public void query(long localVersion, long toVersion, Optional<String> startAt) { LOGGER.info("Starting VersionRange query..."); Stopwatch stopwatch = Stopwatch.createStarted(); Query<FormRecordSnapshotEntity> query = ofy().load().type(FormRecordSnapshotEntity.class) .ancestor(FormEntity.key(formClass)) .filter("version >", localVersion) .chunk(500); if(startAt.isPresent()) { query = query.startAt(Cursor.fromWebSafeString(startAt.get())); } QueryResultIterator<FormRecordSnapshotEntity> it = query.iterator(); while(it.hasNext()) { FormRecordSnapshotEntity snapshot = it.next(); if(snapshot.getVersion() <= toVersion) { add(snapshot); if(!sizeEstimator.timeAndSpaceRemaining()) { stop(it.getCursor().toWebSafeString()); break; } } } LOGGER.info("VersionRange query complete in " + stopwatch.elapsed(TimeUnit.SECONDS) + " with estimate size: " + sizeEstimator.getEstimatedSizeInBytes() + " bytes"); }
@Test public void testEndFetch() { int onePage = total - 30; Query query = new Query(kindName, rootKey); // fetch first page Cursor cursor = checkPage(query, null, null, onePage, onePage, null, null); Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch next page, get remaining after 1st page. checkPage(query, decodedCursor, null, onePage, total - onePage, null, null); }
@Test public void testEndCursor() { int limit = total / testDat.length; Query query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.ASCENDING); // fetch 1st page Cursor cursor = checkPage(query, null, null, limit, limit, testDat[0], testDat[0]); Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch 1st page again since using decodedCursor as end cursor checkPage(query, null, decodedCursor, limit, limit, testDat[0], testDat[0]); }
@Test public void testFilter() { int onePage = 5; String filterData = "ff"; Query query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("name", Query.FilterOperator.EQUAL, filterData)); // fetch first page Cursor cursor = checkPage(query, null, null, onePage, onePage, filterData, filterData); Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch next page checkPage(query, decodedCursor, null, onePage, onePage, filterData, filterData); }
@Test public void testSort() { int onePage = 6; Query query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.ASCENDING); // fetch first page aa,aa,aa,aa,aa,aa Cursor cursor = checkPage(query, null, null, onePage, onePage, testDat[0], testDat[0]); Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch next page aa,aa,aa,aa,bb,bb checkPage(query, decodedCursor, null, onePage, onePage, testDat[0], testDat[1]); // desc onePage = total / testDat.length; query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.DESCENDING); // fetch first page jj,jj,........,jj,jj String chkChar = testDat[testDat.length - 1]; cursor = checkPage(query, null, null, onePage, onePage, chkChar, chkChar); decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch next page ii,ii,........,ii,ii chkChar = testDat[testDat.length - 2]; checkPage(query, decodedCursor, null, onePage, onePage, chkChar, chkChar); }
public JsonNode toJson() throws LeanException { ObjectNode json = JsonUtils.getObjectMapper().createObjectNode(); json.put("kind", kind); if (!filters.isEmpty()) { ObjectNode jsonFilters = JsonUtils.getObjectMapper().createObjectNode(); for (QueryFilter filter : filters) { ObjectNode jsonFilter; if (jsonFilters.has(filter.getProperty())) { jsonFilter = (ObjectNode) jsonFilters.get(filter.getProperty()); } else { jsonFilter = JsonUtils.getObjectMapper().createObjectNode(); } JsonUtils.addTypedNode(jsonFilter, filter.getOperator().toJSON(), filter.getValue()); jsonFilters.put(filter.getProperty(), jsonFilter); } json.put("filter", jsonFilters); } if (!sorts.isEmpty()) { ObjectNode jsonSorts = JsonUtils.getObjectMapper().createObjectNode(); for (QuerySort sort : sorts) { jsonSorts.put(sort.getProperty(), sort.getDirection().toJSON()); } json.put("sort", jsonSorts); } if (this.cursor != null) { json.put("cursor", cursor.toWebSafeString()); } return json; }
@Test public void testStartEndCursor() { int limit = total / testDat.length; Query query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.ASCENDING); FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit); // fetch 1st page and get cursor1 QueryResultList<Entity> nextBatch = service.prepare(query) .asQueryResultList(fetchOption); Cursor cursor1 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString()); // fetch 2nd page and get cursor2 nextBatch = service.prepare(query).asQueryResultList(fetchOption.startCursor(cursor1)); Cursor cursor2 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString()); // cursor1 as start and cursor2 as end and 15 in limit -- -- should return 2nd page. checkPage(query, cursor1, cursor2, limit, limit, testDat[1], testDat[1]); // cursor1 as start and cursor2 as end and 30 in limit -- should return 2nd page. checkPage(query, cursor1, cursor2, 2 * limit, limit, testDat[1], testDat[1]); // cursor2 as start and cursor1 as end and 15 in limit -- should not return any. checkPage(query, cursor2, cursor1, limit, 0, null, null); }