Search in sources :

Example 11 with Cursor

use of com.google.cloud.datastore.Cursor in project spring-cloud-gcp by spring-cloud.

the class SimpleDatastoreRepositoryTests method findAllPageableCursor.

@Test
public void findAllPageableCursor() {
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    Pageable pageable = DatastorePageable.from(PageRequest.of(1, 5, Sort.Direction.DESC, "property1", "property2"), cursor, 10L);
    this.simpleDatastoreRepository.findAll(pageable);
    verify(this.datastoreTemplate, times(1)).findAll(eq(Object.class), eq(new DatastoreQueryOptions.Builder().setLimit(5).setOffset(5).setSort(Sort.by(new Sort.Order(Sort.Direction.DESC, "property1"), new Sort.Order(Sort.Direction.DESC, "property2"))).setCursor(cursor).build()));
    verify(this.datastoreTemplate, times(0)).count(any());
}
Also used : Pageable(org.springframework.data.domain.Pageable) DatastorePageable(org.springframework.cloud.gcp.data.datastore.repository.query.DatastorePageable) DatastoreQueryOptions(org.springframework.cloud.gcp.data.datastore.core.DatastoreQueryOptions) Cursor(com.google.cloud.datastore.Cursor) Test(org.junit.Test)

Example 12 with Cursor

use of com.google.cloud.datastore.Cursor in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQueryTests method prepareDeleteResults.

private void prepareDeleteResults(boolean isCollection) {
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    List<Integer> datastoreMatchingRecords = Arrays.asList(3, 4, 5);
    when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
        StructuredQuery<?> statement = invocation.getArgument(0);
        StructuredQuery.Builder builder = isCollection ? StructuredQuery.newEntityQueryBuilder() : StructuredQuery.newKeyQueryBuilder();
        StructuredQuery<?> expected = builder.setFilter(PropertyFilter.eq("action", "BUY")).setKind("trades").build();
        assertThat(statement).isEqualTo(expected);
        return new DatastoreResultsIterable(datastoreMatchingRecords.iterator(), cursor);
    });
}
Also used : StructuredQuery(com.google.cloud.datastore.StructuredQuery) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) Cursor(com.google.cloud.datastore.Cursor)

Example 13 with Cursor

use of com.google.cloud.datastore.Cursor in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQuery method applyQueryBody.

private StructuredQuery applyQueryBody(Object[] parameters, Builder builder, boolean total, boolean singularResult, Cursor cursor) {
    ParameterAccessor paramAccessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
    if (this.tree.hasPredicate()) {
        applySelectWithFilter(parameters, builder);
    }
    Pageable pageable = paramAccessor.getPageable();
    Integer limit = null;
    Integer offset = null;
    if (singularResult || this.tree.isExistsProjection()) {
        limit = 1;
    } else if (this.tree.isLimiting()) {
        limit = this.tree.getMaxResults();
    }
    if (!singularResult && !total && pageable.isPaged()) {
        limit = pageable.getPageSize();
    }
    Sort sort = this.tree.getSort();
    if (getQueryMethod().getParameters().hasPageableParameter()) {
        sort = sort.and(pageable.getSort());
    }
    if (getQueryMethod().getParameters().hasSortParameter()) {
        sort = sort.and(paramAccessor.getSort());
    }
    if (pageable.isPaged() && !total) {
        offset = (int) pageable.getOffset();
    }
    Cursor cursorToApply = null;
    if (cursor != null) {
        cursorToApply = cursor;
    } else if (pageable instanceof DatastorePageable) {
        cursorToApply = ((DatastorePageable) pageable).toCursor();
    }
    DatastoreTemplate.applyQueryOptions(builder, new DatastoreQueryOptions.Builder().setLimit(limit).setOffset(offset).setSort(sort).setCursor(cursorToApply).build(), this.datastorePersistentEntity);
    return builder.build();
}
Also used : Pageable(org.springframework.data.domain.Pageable) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) Builder(com.google.cloud.datastore.StructuredQuery.Builder) Sort(org.springframework.data.domain.Sort) Cursor(com.google.cloud.datastore.Cursor)

Example 14 with Cursor

use of com.google.cloud.datastore.Cursor in project spring-cloud-gcp by spring-cloud.

the class GqlDatastoreQuery method buildPageOrSlice.

private Object buildPageOrSlice(Object[] parameters, ParsedQueryWithTagsAndValues parsedQueryWithTagsAndValues, DatastoreResultsIterable found) {
    Pageable pageableParam = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters).getPageable();
    List resultsList = found == null ? Collections.emptyList() : (List) StreamSupport.stream(found.spliterator(), false).collect(Collectors.toList());
    Cursor cursor = found != null ? found.getCursor() : null;
    Slice result = isPageQuery() ? buildPage(pageableParam, parsedQueryWithTagsAndValues, cursor, resultsList) : buildSlice(pageableParam, parsedQueryWithTagsAndValues, cursor, resultsList);
    return processRawObjectForProjection(result);
}
Also used : Pageable(org.springframework.data.domain.Pageable) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) Slice(org.springframework.data.domain.Slice) ArrayList(java.util.ArrayList) List(java.util.List) Cursor(com.google.cloud.datastore.Cursor)

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