use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.
the class SimpleDatastoreRepositoryTests method findAllByExamplePageCursor.
@Test
public void findAllByExamplePageCursor() {
Example<Object> example = Example.of(new Object());
Sort sort = Sort.by("id");
Cursor cursor = Cursor.copyFrom("abc".getBytes());
doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1, 2), cursor)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(0).setSort(sort).build()));
doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(3, 4), null)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).setCursor(cursor).build()));
doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1, 2, 3, 4, 5), null)).when(this.datastoreTemplate).keyQueryByExample(same(example), isNull());
Page<Object> result = this.simpleDatastoreRepository.findAll(example, PageRequest.of(0, 2, sort));
assertThat(result).containsExactly(1, 2);
assertThat(result.getTotalElements()).isEqualTo(5);
Page<Object> resultNext = this.simpleDatastoreRepository.findAll(example, result.getPageable().next());
assertThat(resultNext).containsExactly(3, 4);
assertThat(resultNext.getTotalElements()).isEqualTo(5);
verify(this.datastoreTemplate, times(1)).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(0).setSort(sort).build()));
verify(this.datastoreTemplate, times(1)).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).setCursor(cursor).build()));
verify(this.datastoreTemplate, times(1)).keyQueryByExample(same(example), isNull());
}
use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.
the class SimpleDatastoreRepositoryTests method findAllByExamplePage.
@Test
public void findAllByExamplePage() {
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());
Page<Object> result = this.simpleDatastoreRepository.findAll(example, PageRequest.of(1, 2, sort));
assertThat(result).containsExactly(1, 2);
assertThat(result.getTotalElements()).isEqualTo(5);
verify(this.datastoreTemplate, times(1)).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).build()));
verify(this.datastoreTemplate, times(1)).keyQueryByExample(same(example), isNull());
}
use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable 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);
});
}
use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQuery method runQuery.
private Object runQuery(Object[] parameters, Class returnedElementType, Class<?> collectionType, boolean requiresCount) {
ExecutionOptions options = new ExecutionOptions(returnedElementType, collectionType, requiresCount);
DatastoreResultsIterable rawResults = getDatastoreOperations().queryKeysOrEntities(applyQueryBody(parameters, options.getQueryBuilder(), requiresCount, options.isSingularResult(), null), this.entityType);
Object result = StreamSupport.stream(rawResults.spliterator(), false).map(options.isReturnedTypeIsNumber() ? Function.identity() : this::processRawObjectForProjection).collect(options.getResultsCollector());
if (this.tree.isDelete()) {
deleteFoundEntities(options.isReturnedTypeIsNumber(), (Iterable) result);
}
if (!this.tree.isExistsProjection() && !options.isCountingQuery()) {
return new ExecutionResult(convertResultCollection(result, collectionType), rawResults.getCursor());
} else if (options.isCountingQuery() && this.tree.isDelete()) {
return ((List) result).size();
} else {
return result;
}
}
Aggregations