Search in sources :

Example 6 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

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(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Cursor(com.google.cloud.datastore.Cursor)

Example 7 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class SimpleDatastoreRepositoryTests method findByExampleFluentQueryPage.

@Test
void findByExampleFluentQueryPage() {
    Example<Object> example = Example.of(new Object());
    Sort sort = Sort.by("id");
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(Arrays.asList(1, 2), null)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).build()));
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(Arrays.asList(1, 2, 3, 4, 5), null)).when(this.datastoreTemplate).keyQueryByExample(same(example), isNull());
    PageRequest pageRequest = PageRequest.of(1, 2, sort);
    this.spyRepo.findBy(example, q -> q.page(pageRequest));
    verify(this.spyRepo).findAll(same(example), same(pageRequest));
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Sort(org.springframework.data.domain.Sort) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Test(org.junit.jupiter.api.Test)

Example 8 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class SimpleDatastoreRepositoryTests method findByExampleFluentQueryAll.

@Test
void findByExampleFluentQueryAll() {
    Example<Object> example = Example.of(new Object());
    Sort sort = Sort.by("id");
    Iterable entities = Arrays.asList();
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(entities, null)).when(this.datastoreTemplate).queryByExample(same(example), any());
    this.spyRepo.findBy(example, FetchableFluentQuery::all);
    verify(this.spyRepo).findAll(same(example), eq(Sort.unsorted()));
    this.spyRepo.findBy(example, query -> query.sortBy(sort).all());
    verify(this.spyRepo).findAll(same(example), eq(sort));
}
Also used : FetchableFluentQuery(org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Sort(org.springframework.data.domain.Sort) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Test(org.junit.jupiter.api.Test)

Example 9 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class GqlDatastoreQuery method execute.

@Override
public Object execute(Object[] parameters) {
    if (getAnnotation(this.entityType.getSuperclass(), DiscriminatorField.class) != null) {
        throw new DatastoreDataException("Can't append discrimination condition");
    }
    ParsedQueryWithTagsAndValues parsedQueryWithTagsAndValues = new ParsedQueryWithTagsAndValues(this.originalParamTags, parameters);
    GqlQuery query = parsedQueryWithTagsAndValues.bindArgsToGqlQuery();
    Class returnedItemType = this.queryMethod.getReturnedObjectType();
    boolean isNonEntityReturnType = isNonEntityReturnedType(returnedItemType);
    DatastoreResultsIterable found = isNonEntityReturnType ? this.datastoreOperations.queryIterable(query, GqlDatastoreQuery::getNonEntityObjectFromRow) : this.datastoreOperations.queryKeysOrEntities(query, this.entityType);
    Object result;
    if (isPageQuery() || isSliceQuery()) {
        result = buildPageOrSlice(parameters, parsedQueryWithTagsAndValues, found);
    } else if (this.queryMethod.isCollectionQuery() || this.queryMethod.isStreamQuery()) {
        result = convertCollectionResult(returnedItemType, found);
    } else {
        result = convertSingularResult(returnedItemType, isNonEntityReturnType, found);
    }
    return result;
}
Also used : DiscriminatorField(com.google.cloud.spring.data.datastore.core.mapping.DiscriminatorField) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) GqlQuery(com.google.cloud.datastore.GqlQuery)

Example 10 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class SimpleDatastoreRepository method findAll.

@Override
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
    Assert.notNull(pageable, "A non-null pageable is required.");
    Iterable<S> entities = this.datastoreTemplate.queryByExample(example, new DatastoreQueryOptions.Builder().setLimit(pageable.getPageSize()).setOffset((int) pageable.getOffset()).setSort(pageable.getSort()).setCursor(getCursor(pageable)).build());
    List<S> result = StreamSupport.stream(entities.spliterator(), false).collect(Collectors.toList());
    Long totalCount = getOrComputeTotalCount(pageable, () -> count(example));
    Pageable cursorPageable = DatastorePageable.from(pageable, entities instanceof DatastoreResultsIterable ? ((DatastoreResultsIterable) entities).getCursor() : null, totalCount);
    return new PageImpl<>(result, cursorPageable, totalCount);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Pageable(org.springframework.data.domain.Pageable) DatastorePageable(com.google.cloud.spring.data.datastore.repository.query.DatastorePageable) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable)

Aggregations

DatastoreResultsIterable (com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable)19 Test (org.junit.jupiter.api.Test)13 Cursor (com.google.cloud.datastore.Cursor)7 GqlQuery (com.google.cloud.datastore.GqlQuery)5 Sort (org.springframework.data.domain.Sort)5 EntityQuery (com.google.cloud.datastore.EntityQuery)4 Parameters (org.springframework.data.repository.query.Parameters)4 DoubleValue (com.google.cloud.datastore.DoubleValue)3 KeyValue (com.google.cloud.datastore.KeyValue)3 LongValue (com.google.cloud.datastore.LongValue)3 Value (com.google.cloud.datastore.Value)3 Slice (org.springframework.data.domain.Slice)3 DatastoreQueryOptions (com.google.cloud.spring.data.datastore.core.DatastoreQueryOptions)2 Page (org.springframework.data.domain.Page)2 FetchableFluentQuery (org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery)2 KeyQuery (com.google.cloud.datastore.KeyQuery)1 StructuredQuery (com.google.cloud.datastore.StructuredQuery)1 DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)1 DiscriminatorField (com.google.cloud.spring.data.datastore.core.mapping.DiscriminatorField)1 DatastorePageable (com.google.cloud.spring.data.datastore.repository.query.DatastorePageable)1