use of com.google.cloud.datastore.Cursor in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method applyPageable.
private StructuredQuery applyPageable(StructuredQuery query, Pageable pageable) {
if (pageable == Pageable.unpaged()) {
return query;
}
Cursor cursor = null;
if (pageable instanceof DatastorePageable) {
cursor = ((DatastorePageable) pageable).toCursor();
}
StructuredQuery.Builder builder = query.toBuilder();
if (cursor != null) {
builder.setStartCursor(cursor).setOffset(0);
} else {
builder.setOffset(Math.toIntExact(pageable.getOffset()));
}
return builder.setLimit(pageable.getPageSize()).build();
}
use of com.google.cloud.datastore.Cursor in project java-docs-samples by GoogleCloudPlatform.
the class ConceptsTest method testCursorPaging.
@Test
public void testCursorPaging() {
setUpQueryTests();
datastore.put(testEntity);
Cursor nextPageCursor = cursorPaging(1, null);
assertNotNull(nextPageCursor);
nextPageCursor = cursorPaging(1, nextPageCursor);
assertNotNull(nextPageCursor);
}
use of com.google.cloud.datastore.Cursor in project java-docs-samples by GoogleCloudPlatform.
the class ConceptsTest method cursorPaging.
private Cursor cursorPaging(int pageSize, Cursor pageCursor) {
// [START cursor_paging]
EntityQuery.Builder queryBuilder = Query.newEntityQueryBuilder().setKind("Task").setLimit(pageSize);
if (pageCursor != null) {
queryBuilder.setStartCursor(pageCursor);
}
QueryResults<Entity> tasks = datastore.run(queryBuilder.build());
while (tasks.hasNext()) {
Entity task = tasks.next();
// do something with the task
}
Cursor nextPageCursor = tasks.getCursorAfter();
// [END cursor_paging]
return nextPageCursor;
}
use of com.google.cloud.datastore.Cursor in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method prepareSliceResults.
private void prepareSliceResults(int offset, Integer queryLimit, Boolean hasNext) {
Cursor cursor = Cursor.copyFrom("abc".getBytes());
List<Integer> datastoreMatchingRecords = Arrays.asList(3, 4, 5);
when(this.datastoreTemplate.queryEntitiesSlice(isA(EntityQuery.class), any(), any())).thenAnswer((invocation) -> {
EntityQuery statement = invocation.getArgument(0);
EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setOffset(offset).setOrderBy(OrderBy.desc("__key__")).setLimit(queryLimit).build();
assertThat(statement).isEqualTo(expected);
return new SliceImpl(new DatastoreResultsIterable(datastoreMatchingRecords.iterator(), cursor).toList(), Pageable.unpaged(), hasNext);
});
}
use of com.google.cloud.datastore.Cursor in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method pageableQueryNextPage.
@Test
public void pageableQueryNextPage() throws NoSuchMethodException {
queryWithMockResult("findByActionAndSymbolAndPriceLessThanAndPriceGreater" + "ThanEqualAndIdIsNull", null, getClass().getMethod("tradeMethod", String.class, String.class, double.class, double.class, Pageable.class));
this.partTreeDatastoreQuery = createQuery(true, false, null);
PageRequest pageRequest = PageRequest.of(1, 2, Sort.Direction.DESC, "id");
Cursor cursor = Cursor.copyFrom("abc".getBytes());
Object[] params = new Object[] { "BUY", "abcd", 8.88, 3.33, DatastorePageable.from(pageRequest, cursor, 99L) };
preparePageResults(2, 2, cursor, Arrays.asList(3, 4), Arrays.asList(1, 2, 3, 4));
when(this.queryMethod.getCollectionReturnType()).thenReturn(List.class);
Page result = (Page) this.partTreeDatastoreQuery.execute(params);
assertThat(result.getTotalElements()).isEqualTo(99L);
assertThat(result.getTotalPages()).isEqualTo(50);
assertThat(result.getNumberOfElements()).isEqualTo(2);
verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), any());
}
Aggregations