use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.
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()) {
result = convertCollectionResult(returnedItemType, found);
} else {
result = convertSingularResult(returnedItemType, isNonEntityReturnType, found);
}
return result;
}
use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.
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);
}
use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.
the class SimpleDatastoreRepositoryTests method findOneByExample.
@Test
public void findOneByExample() {
Example<Object> example = Example.of(new Object());
doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1), null)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(1).build()));
assertThat(this.simpleDatastoreRepository.findOne(example).get()).isEqualTo(1);
verify(this.datastoreTemplate, times(1)).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(1).build()));
}
use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method usingIdField.
@Test
public void usingIdField() throws NoSuchMethodException {
Trade trade = new Trade();
queryWithMockResult("findByActionAndId", null, getClass().getMethod("findByActionAndId", String.class, String.class), true, null);
Object[] params = new Object[] { "BUY", "id1" };
when(this.datastoreTemplate.createKey(eq("trades"), eq("id1"))).thenAnswer((invocation) -> Key.newBuilder("project", invocation.getArgument(0), invocation.getArgument(1)).build());
when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
EntityQuery statement = invocation.getArgument(0);
EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(CompositeFilter.and(PropertyFilter.eq("action", "BUY"), PropertyFilter.eq("__key__", KeyValue.of(Key.newBuilder("project", "trades", "id1").build())))).setKind("trades").setLimit(1).build();
assertThat(statement).isEqualTo(expected);
List<Trade> results = Collections.singletonList(trade);
return new DatastoreResultsIterable(results.iterator(), null);
});
assertThat(this.partTreeDatastoreQuery.execute(params)).isEqualTo(trade);
}
use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method preparePageResults.
private void preparePageResults(int offset, Integer limit, Cursor cursor, List<Integer> pageResults, List<Integer> fullResults) {
when(this.datastoreTemplate.queryKeysOrEntities(isA(EntityQuery.class), any())).thenAnswer((invocation) -> {
EntityQuery statement = invocation.getArgument(0);
EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setStartCursor(cursor).setOffset(cursor != null ? 0 : offset).setOrderBy(OrderBy.desc("__key__")).setLimit(limit).build();
assertThat(statement).isEqualTo(expected);
return new DatastoreResultsIterable(pageResults.iterator(), Cursor.copyFrom("abc".getBytes()));
});
when(this.datastoreTemplate.queryKeysOrEntities(isA(KeyQuery.class), any())).thenAnswer((invocation) -> {
KeyQuery statement = invocation.getArgument(0);
KeyQuery expected = StructuredQuery.newKeyQueryBuilder().setFilter(FILTER).setKind("trades").setOrderBy(OrderBy.desc("__key__")).build();
assertThat(statement).isEqualTo(expected);
return new DatastoreResultsIterable(fullResults.iterator(), Cursor.copyFrom("def".getBytes()));
});
}
Aggregations