use of com.google.cloud.datastore.StructuredQuery in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method applyPageable.
private StructuredQuery applyPageable(StructuredQuery query, Pageable pageable) {
if (pageable == Pageable.unpaged()) {
return query;
}
Cursor cursor = null;
if (pageable instanceof DatastorePageable) {
cursor = ((DatastorePageable) pageable).toCursor();
}
StructuredQuery.Builder builder = query.toBuilder();
if (cursor != null) {
builder.setStartCursor(cursor).setOffset(0);
} else {
builder.setOffset(Math.toIntExact(pageable.getOffset()));
}
return builder.setLimit(pageable.getPageSize()).build();
}
use of com.google.cloud.datastore.StructuredQuery in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQuery method executeSliceQuery.
private Slice executeSliceQuery(Object[] parameters) {
StructuredQuery structuredQuery = buildSliceQuey(parameters);
Slice<?> results = structuredQuery instanceof KeyQuery ? this.datastoreOperations.queryKeysSlice((KeyQuery) structuredQuery, this.entityType, getPageable(parameters)) : this.datastoreOperations.queryEntitiesSlice(structuredQuery, this.entityType, getPageable(parameters));
return (Slice) this.processRawObjectForProjection(results);
}
use of com.google.cloud.datastore.StructuredQuery in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method compoundNameConventionProjectionTest.
@Test
public void compoundNameConventionProjectionTest() throws NoSuchMethodException, IntrospectionException {
ProjectionInformation projectionInformation = mock(ProjectionInformation.class);
doReturn(TradeProjection.class).when(projectionInformation).getType();
doReturn(true).when(projectionInformation).isClosed();
doReturn(Arrays.asList(new PropertyDescriptor("id", null, null), new PropertyDescriptor("symbol", null, null))).when(projectionInformation).getInputProperties();
queryWithMockResult("findTop333ByActionAndSymbolAndPriceLessThan" + "AndPriceGreaterThanEqual" + "AndEmbeddedEntityStringFieldEquals" + "AndIdIsNullOrderByIdDesc", null, getClass().getMethod("tradeMethodProjection", String.class, String.class, double.class, double.class, String.class), projectionInformation);
Object[] params = new Object[] { "BUY", "abcd", // this int param requires custom conversion
8, 3.33, "abc" };
when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
StructuredQuery statement = invocation.getArgument(0);
StructuredQuery expected = StructuredQuery.newProjectionEntityQueryBuilder().addProjection("__key__", "ticker").setFilter(CompositeFilter.and(PropertyFilter.eq("action", "BUY"), PropertyFilter.eq("ticker", "abcd"), PropertyFilter.lt("price", 8L), PropertyFilter.ge("price", 3.33), PropertyFilter.eq("embeddedEntity.stringField", "abc"), PropertyFilter.isNull("__key__"))).setKind("trades").setOrderBy(OrderBy.desc("__key__")).setLimit(333).build();
assertThat(statement).isEqualTo(expected);
return EMPTY_RESPONSE;
});
when(this.queryMethod.getCollectionReturnType()).thenReturn(List.class);
this.partTreeDatastoreQuery.execute(params);
verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), any());
}
use of com.google.cloud.datastore.StructuredQuery in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method exampleToQuery.
private <T> StructuredQuery exampleToQuery(Example<T> example, DatastoreQueryOptions queryOptions, boolean keyQuery) {
validateExample(example);
T probe = example.getProbe();
FullEntity.Builder<IncompleteKey> probeEntityBuilder = Entity.newBuilder();
this.datastoreEntityConverter.write(probe, probeEntityBuilder);
FullEntity<IncompleteKey> probeEntity = probeEntityBuilder.build();
DatastorePersistentEntity<?> persistentEntity = this.datastoreMappingContext.getPersistentEntity(example.getProbeType());
LinkedList<StructuredQuery.Filter> filters = new LinkedList<>();
NullHandler nullHandler = example.getMatcher().getNullHandler();
persistentEntity.doWithColumnBackedProperties((persistentProperty) -> {
if (!ignoredProperty(example, persistentProperty)) {
Value<?> value = getValue(example, probeEntity, persistentEntity, persistentProperty);
addFilter(nullHandler, filters, persistentProperty.getFieldName(), value);
}
});
persistentEntity.doWithAssociations((AssociationHandler<DatastorePersistentProperty>) association -> {
PersistentPropertyAccessor<?> accessor = persistentEntity.getPropertyAccessor(example.getProbe());
DatastorePersistentProperty property = association.getInverse();
Object value = accessor.getProperty(property);
Value<?> key = value == null ? NullValue.of() : KeyValue.of(objectToKeyFactory.getKeyFromObject(value, this.datastoreMappingContext.getPersistentEntity(value.getClass())));
addFilter(nullHandler, filters, property.getFieldName(), key);
});
StructuredQuery.Builder<?> builder = keyQuery ? Query.newKeyQueryBuilder() : Query.newEntityQueryBuilder();
builder.setKind(persistentEntity.kindName());
if (!filters.isEmpty()) {
builder.setFilter(StructuredQuery.CompositeFilter.and(filters.pop(), filters.toArray(new StructuredQuery.Filter[0])));
}
applyQueryOptions(builder, queryOptions, persistentEntity);
return builder.build();
}
use of com.google.cloud.datastore.StructuredQuery 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);
});
}
Aggregations