Search in sources :

Example 1 with Cursor

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();
}
Also used : StructuredQuery(com.google.cloud.datastore.StructuredQuery) Cursor(com.google.cloud.datastore.Cursor) DatastorePageable(org.springframework.cloud.gcp.data.datastore.repository.query.DatastorePageable)

Example 2 with Cursor

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);
}
Also used : Cursor(com.google.cloud.datastore.Cursor) Test(org.junit.Test)

Example 3 with Cursor

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;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) EntityQuery(com.google.cloud.datastore.EntityQuery) Cursor(com.google.cloud.datastore.Cursor)

Example 4 with Cursor

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);
    });
}
Also used : DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) Cursor(com.google.cloud.datastore.Cursor) EntityQuery(com.google.cloud.datastore.EntityQuery) SliceImpl(org.springframework.data.domain.SliceImpl)

Example 5 with Cursor

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());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Page(org.springframework.data.domain.Page) Cursor(com.google.cloud.datastore.Cursor) Test(org.junit.Test)

Aggregations

Cursor (com.google.cloud.datastore.Cursor)14 Test (org.junit.Test)7 DatastoreResultsIterable (org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable)6 Pageable (org.springframework.data.domain.Pageable)4 Slice (org.springframework.data.domain.Slice)4 DoubleValue (com.google.cloud.datastore.DoubleValue)3 GqlQuery (com.google.cloud.datastore.GqlQuery)3 KeyValue (com.google.cloud.datastore.KeyValue)3 LongValue (com.google.cloud.datastore.LongValue)3 Value (com.google.cloud.datastore.Value)3 Page (org.springframework.data.domain.Page)3 Parameters (org.springframework.data.repository.query.Parameters)3 EntityQuery (com.google.cloud.datastore.EntityQuery)2 StructuredQuery (com.google.cloud.datastore.StructuredQuery)2 ArrayList (java.util.ArrayList)2 DatastoreQueryOptions (org.springframework.cloud.gcp.data.datastore.core.DatastoreQueryOptions)2 DatastorePageable (org.springframework.cloud.gcp.data.datastore.repository.query.DatastorePageable)2 Sort (org.springframework.data.domain.Sort)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 Entity (com.google.cloud.datastore.Entity)1