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);
});
}
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));
}
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));
}
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;
}
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);
}
Aggregations