Search in sources :

Example 1 with Filter

use of com.google.cloud.datastore.StructuredQuery.Filter in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplate method applyQueryOptions.

public static void applyQueryOptions(StructuredQuery.Builder builder, DatastoreQueryOptions queryOptions, DatastorePersistentEntity<?> persistentEntity) {
    if (persistentEntity.getDiscriminationFieldName() != null && persistentEntity.getDiscriminatorValue() != null) {
        StructuredQuery.Filter discriminationFilter = PropertyFilter.eq(persistentEntity.getDiscriminationFieldName(), persistentEntity.getDiscriminatorValue());
        StructuredQuery.Filter filter = builder.build().getFilter();
        if (filter != null) {
            discriminationFilter = StructuredQuery.CompositeFilter.and(filter, discriminationFilter);
        }
        builder.setFilter(discriminationFilter);
    }
    if (queryOptions == null) {
        return;
    }
    if (queryOptions.getLimit() != null) {
        builder.setLimit(queryOptions.getLimit());
    }
    if (queryOptions.getCursor() == null && queryOptions.getOffset() != null) {
        builder.setOffset(queryOptions.getOffset());
    }
    if (queryOptions.getCursor() != null) {
        builder.setStartCursor(queryOptions.getCursor());
    }
    if (queryOptions.getSort() != null && persistentEntity != null) {
        queryOptions.getSort().stream().map((order) -> createOrderBy(persistentEntity, order)).forEachOrdered((orderBy) -> builder.addOrderBy(orderBy));
    }
}
Also used : StructuredQuery(com.google.cloud.datastore.StructuredQuery) FullEntity(com.google.cloud.datastore.FullEntity) Query(com.google.cloud.datastore.Query) Builder(com.google.cloud.datastore.Entity.Builder) PathElement(com.google.cloud.datastore.PathElement) SliceUtil(org.springframework.cloud.gcp.data.datastore.core.util.SliceUtil) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) Filter(com.google.cloud.datastore.StructuredQuery.Filter) AfterQueryEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent) KeyQuery(com.google.cloud.datastore.KeyQuery) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) QueryResults(com.google.cloud.datastore.QueryResults) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) PersistentProperty(org.springframework.data.mapping.PersistentProperty) AfterFindByKeyEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) Collection(java.util.Collection) KeyValue(com.google.cloud.datastore.KeyValue) AfterSaveEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterSaveEvent) Set(java.util.Set) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) Example(org.springframework.data.domain.Example) Collectors(java.util.stream.Collectors) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Slice(org.springframework.data.domain.Slice) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) KeyUtil(org.springframework.cloud.gcp.data.datastore.core.util.KeyUtil) ListValue(com.google.cloud.datastore.ListValue) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) BeforeDeleteEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.BeforeDeleteEvent) Key(com.google.cloud.datastore.Key) BeforeSaveEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.BeforeSaveEvent) DatastoreEntityConverter(org.springframework.cloud.gcp.data.datastore.core.convert.DatastoreEntityConverter) SliceImpl(org.springframework.data.domain.SliceImpl) HashMap(java.util.HashMap) Datastore(com.google.cloud.datastore.Datastore) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Cursor(com.google.cloud.datastore.Cursor) HashSet(java.util.HashSet) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) ValueUtil(org.springframework.cloud.gcp.data.datastore.core.util.ValueUtil) Entity(com.google.cloud.datastore.Entity) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) NullValue(com.google.cloud.datastore.NullValue) StreamSupport(java.util.stream.StreamSupport) Nullable(org.springframework.lang.Nullable) LinkedList(java.util.LinkedList) StructuredQuery(com.google.cloud.datastore.StructuredQuery) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Iterator(java.util.Iterator) TypeUtils(org.springframework.util.TypeUtils) ObjectToKeyFactory(org.springframework.cloud.gcp.data.datastore.core.convert.ObjectToKeyFactory) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) DatastoreReaderWriter(com.google.cloud.datastore.DatastoreReaderWriter) EntityQuery(com.google.cloud.datastore.EntityQuery) AfterDeleteEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterDeleteEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) AssociationHandler(org.springframework.data.mapping.AssociationHandler) BaseKey(com.google.cloud.datastore.BaseKey) Value(com.google.cloud.datastore.Value) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty) Collections(java.util.Collections) DatastorePageable(org.springframework.cloud.gcp.data.datastore.repository.query.DatastorePageable) Assert(org.springframework.util.Assert) Filter(com.google.cloud.datastore.StructuredQuery.Filter)

Example 2 with Filter

use of com.google.cloud.datastore.StructuredQuery.Filter in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplate method validateExample.

private <T> void validateExample(Example<T> example) {
    Assert.notNull(example, "A non-null example is expected");
    ExampleMatcher matcher = example.getMatcher();
    if (!matcher.isAllMatching()) {
        throw new DatastoreDataException("Unsupported MatchMode. Only MatchMode.ALL is supported");
    }
    if (matcher.isIgnoreCaseEnabled()) {
        throw new DatastoreDataException("Ignore case matching is not supported");
    }
    if (!(matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.EXACT || matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.DEFAULT)) {
        throw new DatastoreDataException("Unsupported StringMatcher. Only EXACT and DEFAULT are supported");
    }
    Optional<String> path = example.getMatcher().getIgnoredPaths().stream().filter((s) -> s.contains(".")).findFirst();
    if (path.isPresent()) {
        throw new DatastoreDataException("Ignored paths deeper than 1 are not supported");
    }
    if (matcher.getPropertySpecifiers().hasValues()) {
        throw new DatastoreDataException("Property matchers are not supported");
    }
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Query(com.google.cloud.datastore.Query) Builder(com.google.cloud.datastore.Entity.Builder) PathElement(com.google.cloud.datastore.PathElement) SliceUtil(org.springframework.cloud.gcp.data.datastore.core.util.SliceUtil) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) Filter(com.google.cloud.datastore.StructuredQuery.Filter) AfterQueryEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent) KeyQuery(com.google.cloud.datastore.KeyQuery) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) QueryResults(com.google.cloud.datastore.QueryResults) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) PersistentProperty(org.springframework.data.mapping.PersistentProperty) AfterFindByKeyEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) Collection(java.util.Collection) KeyValue(com.google.cloud.datastore.KeyValue) AfterSaveEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterSaveEvent) Set(java.util.Set) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) Example(org.springframework.data.domain.Example) Collectors(java.util.stream.Collectors) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Slice(org.springframework.data.domain.Slice) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) KeyUtil(org.springframework.cloud.gcp.data.datastore.core.util.KeyUtil) ListValue(com.google.cloud.datastore.ListValue) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) BeforeDeleteEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.BeforeDeleteEvent) Key(com.google.cloud.datastore.Key) BeforeSaveEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.BeforeSaveEvent) DatastoreEntityConverter(org.springframework.cloud.gcp.data.datastore.core.convert.DatastoreEntityConverter) SliceImpl(org.springframework.data.domain.SliceImpl) HashMap(java.util.HashMap) Datastore(com.google.cloud.datastore.Datastore) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Cursor(com.google.cloud.datastore.Cursor) HashSet(java.util.HashSet) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) ValueUtil(org.springframework.cloud.gcp.data.datastore.core.util.ValueUtil) Entity(com.google.cloud.datastore.Entity) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) NullValue(com.google.cloud.datastore.NullValue) StreamSupport(java.util.stream.StreamSupport) Nullable(org.springframework.lang.Nullable) LinkedList(java.util.LinkedList) StructuredQuery(com.google.cloud.datastore.StructuredQuery) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Iterator(java.util.Iterator) TypeUtils(org.springframework.util.TypeUtils) ObjectToKeyFactory(org.springframework.cloud.gcp.data.datastore.core.convert.ObjectToKeyFactory) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) DatastoreReaderWriter(com.google.cloud.datastore.DatastoreReaderWriter) EntityQuery(com.google.cloud.datastore.EntityQuery) AfterDeleteEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterDeleteEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) AssociationHandler(org.springframework.data.mapping.AssociationHandler) BaseKey(com.google.cloud.datastore.BaseKey) Value(com.google.cloud.datastore.Value) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty) Collections(java.util.Collections) DatastorePageable(org.springframework.cloud.gcp.data.datastore.repository.query.DatastorePageable) Assert(org.springframework.util.Assert) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) ExampleMatcher(org.springframework.data.domain.ExampleMatcher)

Example 3 with Filter

use of com.google.cloud.datastore.StructuredQuery.Filter in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQuery method applySelectWithFilter.

private void applySelectWithFilter(Object[] parameters, Builder builder) {
    Iterator it = Arrays.asList(parameters).iterator();
    Filter[] filters = this.filterParts.stream().map((part) -> {
        // build properties chain for nested properties
        // if the property is not nested, the list would contain only one property
        List<DatastorePersistentProperty> propertiesChain = getPropertiesChain(part);
        String fieldName = propertiesChain.stream().map(DatastorePersistentProperty::getFieldName).collect(Collectors.joining("."));
        if (part.getType() == Part.Type.IS_NULL) {
            return PropertyFilter.isNull(fieldName);
        }
        BiFunction<String, Value, PropertyFilter> filterFactory = FILTER_FACTORIES.get(part.getType());
        if (filterFactory == null) {
            throw new DatastoreDataException("Unsupported predicate keyword: " + part.getType());
        }
        if (!it.hasNext()) {
            throw new DatastoreDataException("Too few parameters are provided for query method: " + getQueryMethod().getName());
        }
        Value convertedValue = convertParam(propertiesChain.get(propertiesChain.size() - 1), it.next());
        return filterFactory.apply(fieldName, convertedValue);
    }).toArray(Filter[]::new);
    builder.setFilter((filters.length > 1) ? CompositeFilter.and(filters[0], Arrays.copyOfRange(filters, 1, filters.length)) : filters[0]);
}
Also used : Query(com.google.cloud.datastore.Query) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) LESS_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN_EQUAL) Filter(com.google.cloud.datastore.StructuredQuery.Filter) KeyQuery(com.google.cloud.datastore.KeyQuery) Map(java.util.Map) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Collector(java.util.stream.Collector) DatastoreTemplate(org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate) LESS_THAN(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) Builder(com.google.cloud.datastore.StructuredQuery.Builder) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) KeyValue(com.google.cloud.datastore.KeyValue) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) SIMPLE_PROPERTY(org.springframework.data.repository.query.parser.Part.Type.SIMPLE_PROPERTY) Collectors(java.util.stream.Collectors) Slice(org.springframework.data.domain.Slice) List(java.util.List) PropertyDescriptor(java.beans.PropertyDescriptor) OrPart(org.springframework.data.repository.query.parser.PartTree.OrPart) Optional(java.util.Optional) PropertyPath(org.springframework.data.mapping.PropertyPath) PageImpl(org.springframework.data.domain.PageImpl) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) GREATER_THAN(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN) ProjectionFactory(org.springframework.data.projection.ProjectionFactory) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) Function(java.util.function.Function) Part(org.springframework.data.repository.query.parser.Part) Cursor(com.google.cloud.datastore.Cursor) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) StreamSupport(java.util.stream.StreamSupport) StructuredQuery(com.google.cloud.datastore.StructuredQuery) Iterator(java.util.Iterator) PartTree(org.springframework.data.repository.query.parser.PartTree) CompositeFilter(com.google.cloud.datastore.StructuredQuery.CompositeFilter) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) DatastoreOperations(org.springframework.cloud.gcp.data.datastore.core.DatastoreOperations) Value(com.google.cloud.datastore.Value) DatastoreQueryOptions(org.springframework.cloud.gcp.data.datastore.core.DatastoreQueryOptions) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty) GREATER_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN_EQUAL) Collections(java.util.Collections) Assert(org.springframework.util.Assert) Filter(com.google.cloud.datastore.StructuredQuery.Filter) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) CompositeFilter(com.google.cloud.datastore.StructuredQuery.CompositeFilter) BiFunction(java.util.function.BiFunction) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) Iterator(java.util.Iterator) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) List(java.util.List) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Aggregations

Cursor (com.google.cloud.datastore.Cursor)3 EntityQuery (com.google.cloud.datastore.EntityQuery)3 KeyQuery (com.google.cloud.datastore.KeyQuery)3 KeyValue (com.google.cloud.datastore.KeyValue)3 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)3 Query (com.google.cloud.datastore.Query)3 StructuredQuery (com.google.cloud.datastore.StructuredQuery)3 Filter (com.google.cloud.datastore.StructuredQuery.Filter)3 PropertyFilter (com.google.cloud.datastore.StructuredQuery.PropertyFilter)3 Value (com.google.cloud.datastore.Value)3 Collections (java.util.Collections)3 Iterator (java.util.Iterator)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3 StreamSupport (java.util.stream.StreamSupport)3 BaseEntity (com.google.cloud.datastore.BaseEntity)2 BaseKey (com.google.cloud.datastore.BaseKey)2