Search in sources :

Example 1 with Filter

use of com.google.firestore.v1.StructuredQuery.Filter in project beam by apache.

the class FirestoreTestingHelper method listDocumentIds.

Stream<String> listDocumentIds(String collectionPath) {
    int index = collectionPath.lastIndexOf('/');
    String parent = collectionPath.substring(0, index);
    String collectionId = collectionPath.substring(index + 1);
    ListDocumentsRequest ldr = ListDocumentsRequest.newBuilder().setParent(parent).setCollectionId(collectionId).setShowMissing(true).build();
    // LOGGER.debug("ldr = {}", ldr);
    ListDocumentsPagedResponse response = rpc.listDocumentsPagedCallable().call(ldr);
    return StreamSupport.stream(response.iteratePages().spliterator(), false).flatMap(page -> page.getResponse().getDocumentsList().stream()).map(Document::getName).filter(s -> !s.isEmpty());
}
Also used : ListDocumentsRequest(com.google.firestore.v1.ListDocumentsRequest) ListDocumentsPagedResponse(com.google.cloud.firestore.v1.FirestoreClient.ListDocumentsPagedResponse)

Example 2 with Filter

use of com.google.firestore.v1.StructuredQuery.Filter in project beam by apache.

the class FirestoreTestingHelper method listDocumentsViaQuery.

Stream<String> listDocumentsViaQuery(String collectionPath) {
    int index = collectionPath.lastIndexOf('/');
    String parent = collectionPath.substring(0, index);
    String collectionId = collectionPath.substring(index + 1);
    FieldReference nameField = FieldReference.newBuilder().setFieldPath("__name__").build();
    RunQueryRequest rqr = RunQueryRequest.newBuilder().setParent(parent).setStructuredQuery(StructuredQuery.newBuilder().addFrom(CollectionSelector.newBuilder().setCollectionId(collectionId)).addOrderBy(Order.newBuilder().setField(nameField).setDirection(Direction.ASCENDING).build()).setSelect(Projection.newBuilder().addFields(nameField).build())).build();
    return StreamSupport.stream(rpc.runQueryCallable().call(rqr).spliterator(), false).filter(RunQueryResponse::hasDocument).map(RunQueryResponse::getDocument).map(Document::getName);
}
Also used : FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference) RunQueryResponse(com.google.firestore.v1.RunQueryResponse) RunQueryRequest(com.google.firestore.v1.RunQueryRequest) Document(com.google.firestore.v1.Document)

Example 3 with Filter

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

the class PartTreeFirestoreQuery method createBuilderWithFilter.

private StructuredQuery.Builder createBuilderWithFilter(Object[] parameters) {
    StructuredQuery.Builder builder = StructuredQuery.newBuilder();
    Iterator it = Arrays.asList(parameters).iterator();
    StructuredQuery.CompositeFilter.Builder compositeFilter = StructuredQuery.CompositeFilter.newBuilder();
    compositeFilter.setOp(StructuredQuery.CompositeFilter.Operator.AND);
    this.tree.getParts().forEach(part -> {
        StructuredQuery.FieldReference fieldReference = StructuredQuery.FieldReference.newBuilder().setFieldPath(buildName(part)).build();
        StructuredQuery.Filter.Builder filter = StructuredQuery.Filter.newBuilder();
        if (part.getType() == Part.Type.IS_NULL) {
            filter.getUnaryFilterBuilder().setField(fieldReference).setOp(StructuredQuery.UnaryFilter.Operator.IS_NULL);
        } else {
            if (!it.hasNext()) {
                throw new FirestoreDataException("Too few parameters are provided for query method: " + getQueryMethod().getName());
            }
            Object value = it.next();
            filter.getFieldFilterBuilder().setField(fieldReference).setOp(getOperator(part, value)).setValue(this.classMapper.toFirestoreValue(value));
        }
        compositeFilter.addFilters(filter.build());
    });
    builder.setWhere(StructuredQuery.Filter.newBuilder().setCompositeFilter(compositeFilter.build()));
    return builder;
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) Iterator(java.util.Iterator) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference)

Example 4 with Filter

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

the class PartTreeFirestoreQuery method validate.

private void validate() {
    List parts = this.tree.get().collect(Collectors.toList());
    if (parts.size() > 1 && parts.get(0) instanceof PartTree.OrPart) {
        throw new FirestoreDataException("Cloud Firestore doesn't support 'OR' (method name: " + this.getQueryMethod().getName() + ")");
    }
    List<String> unsupportedParts = this.tree.getParts().stream().filter(part -> !isSupportedPart(part.getType())).map(part -> part.getType().toString()).collect(Collectors.toList());
    if (!unsupportedParts.isEmpty()) {
        throw new FirestoreDataException("Unsupported predicate keywords: " + unsupportedParts + " in " + this.getQueryMethod().getName());
    }
}
Also used : GREATER_THAN(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN) Arrays(java.util.Arrays) Order(org.springframework.data.domain.Sort.Order) FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference) QueryMethod(org.springframework.data.repository.query.QueryMethod) LESS_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN_EQUAL) Part(org.springframework.data.repository.query.parser.Part) ArrayList(java.util.ArrayList) NOT_IN(org.springframework.data.repository.query.parser.Part.Type.NOT_IN) NEGATING_SIMPLE_PROPERTY(org.springframework.data.repository.query.parser.Part.Type.NEGATING_SIMPLE_PROPERTY) Map(java.util.Map) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) StreamSupport(java.util.stream.StreamSupport) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Direction(org.springframework.data.domain.Sort.Direction) Int32Value(com.google.protobuf.Int32Value) LESS_THAN(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) Iterator(java.util.Iterator) StructuredQuery(com.google.firestore.v1.StructuredQuery) FirestorePersistentEntity(org.springframework.cloud.gcp.data.firestore.mapping.FirestorePersistentEntity) PartTree(org.springframework.data.repository.query.parser.PartTree) FirestoreReactiveOperations(org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) SIMPLE_PROPERTY(org.springframework.data.repository.query.parser.Part.Type.SIMPLE_PROPERTY) Collectors(java.util.stream.Collectors) CONTAINING(org.springframework.data.repository.query.parser.Part.Type.CONTAINING) List(java.util.List) IN(org.springframework.data.repository.query.parser.Part.Type.IN) ReturnedType(org.springframework.data.repository.query.ReturnedType) GREATER_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN_EQUAL) FirestoreClassMapper(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreClassMapper) PropertyPath(org.springframework.data.mapping.PropertyPath) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) FirestoreMappingContext(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreMappingContext) RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) ArrayList(java.util.ArrayList) List(java.util.List) PartTree(org.springframework.data.repository.query.parser.PartTree)

Aggregations

FieldReference (com.google.firestore.v1.StructuredQuery.FieldReference)3 StructuredQuery (com.google.firestore.v1.StructuredQuery)2 Iterator (java.util.Iterator)2 FirestoreDataException (org.springframework.cloud.gcp.data.firestore.FirestoreDataException)2 ListDocumentsPagedResponse (com.google.cloud.firestore.v1.FirestoreClient.ListDocumentsPagedResponse)1 Document (com.google.firestore.v1.Document)1 ListDocumentsRequest (com.google.firestore.v1.ListDocumentsRequest)1 RunQueryRequest (com.google.firestore.v1.RunQueryRequest)1 RunQueryResponse (com.google.firestore.v1.RunQueryResponse)1 Int32Value (com.google.protobuf.Int32Value)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 MapBuilder (org.springframework.cloud.gcp.core.util.MapBuilder)1 FirestoreReactiveOperations (org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations)1 FirestoreClassMapper (org.springframework.cloud.gcp.data.firestore.mapping.FirestoreClassMapper)1 FirestoreMappingContext (org.springframework.cloud.gcp.data.firestore.mapping.FirestoreMappingContext)1