Search in sources :

Example 21 with Target

use of com.google.firestore.v1.Target in project firebase-android-sdk by firebase.

the class RemoteSerializerTest method testEncodesMultipleFiltersOnDeeperCollections.

@Test
public void testEncodesMultipleFiltersOnDeeperCollections() {
    Query q = Query.atPath(ResourcePath.fromString("rooms/1/messages/10/attachments")).filter(filter("prop", "<", 42)).filter(filter("author", "==", "dimond")).filter(filter("tags", "array-contains", "pending"));
    Target actual = serializer.encodeTarget(wrapTargetData(q));
    StructuredQuery.Builder structuredQueryBuilder = StructuredQuery.newBuilder().addFrom(CollectionSelector.newBuilder().setCollectionId("attachments")).setWhere(Filter.newBuilder().setCompositeFilter(StructuredQuery.CompositeFilter.newBuilder().setOp(CompositeFilter.Operator.AND).addFilters(Filter.newBuilder().setFieldFilter(StructuredQuery.FieldFilter.newBuilder().setField(FieldReference.newBuilder().setFieldPath("prop")).setOp(Operator.LESS_THAN).setValue(Value.newBuilder().setIntegerValue(42)))).addFilters(Filter.newBuilder().setFieldFilter(StructuredQuery.FieldFilter.newBuilder().setField(FieldReference.newBuilder().setFieldPath("author")).setOp(Operator.EQUAL).setValue(Value.newBuilder().setStringValue("dimond")))).addFilters(Filter.newBuilder().setFieldFilter(StructuredQuery.FieldFilter.newBuilder().setField(FieldReference.newBuilder().setFieldPath("tags")).setOp(Operator.ARRAY_CONTAINS).setValue(Value.newBuilder().setStringValue("pending")))))).addOrderBy(Order.newBuilder().setField(FieldReference.newBuilder().setFieldPath("prop")).setDirection(Direction.ASCENDING)).addOrderBy(defaultKeyOrder());
    QueryTarget.Builder queryBuilder = QueryTarget.newBuilder().setParent("projects/p/databases/d/documents/rooms/1/messages/10").setStructuredQuery(structuredQueryBuilder);
    Target expected = Target.newBuilder().setQuery(queryBuilder).setTargetId(1).setResumeToken(ByteString.EMPTY).build();
    assertEquals(expected, actual);
    assertEquals(serializer.decodeQueryTarget(serializer.encodeQueryTarget(q.toTarget())), q.toTarget());
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) QueryTarget(com.google.firestore.v1.Target.QueryTarget) DocumentsTarget(com.google.firestore.v1.Target.DocumentsTarget) Target(com.google.firestore.v1.Target) QueryTarget(com.google.firestore.v1.Target.QueryTarget) StructuredQuery(com.google.firestore.v1.StructuredQuery) Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 22 with Target

use of com.google.firestore.v1.Target in project firebase-android-sdk by firebase.

the class RemoteSerializerTest method testEncodesCompositeFiltersOnDeeperCollections.

@Test
public void testEncodesCompositeFiltersOnDeeperCollections() {
    // (prop < 42) || (author == "ehsann" && tags array-contains "pending")
    Query q = Query.atPath(ResourcePath.fromString("rooms/1/messages/10/attachments")).filter(orFilters(filter("prop", "<", 42), andFilters(filter("author", "==", "ehsann"), filter("tags", "array-contains", "pending"))));
    Target actual = serializer.encodeTarget(wrapTargetData(q));
    StructuredQuery.Builder structuredQueryBuilder = StructuredQuery.newBuilder().addFrom(CollectionSelector.newBuilder().setCollectionId("attachments")).setWhere(Filter.newBuilder().setCompositeFilter(StructuredQuery.CompositeFilter.newBuilder().setOp(CompositeFilter.Operator.OPERATOR_UNSPECIFIED).addFilters(Filter.newBuilder().setFieldFilter(StructuredQuery.FieldFilter.newBuilder().setField(FieldReference.newBuilder().setFieldPath("prop")).setOp(Operator.LESS_THAN).setValue(Value.newBuilder().setIntegerValue(42)))).addFilters(Filter.newBuilder().setCompositeFilter(StructuredQuery.CompositeFilter.newBuilder().setOp(CompositeFilter.Operator.AND).addFilters(Filter.newBuilder().setFieldFilter(StructuredQuery.FieldFilter.newBuilder().setField(FieldReference.newBuilder().setFieldPath("author")).setOp(Operator.EQUAL).setValue(Value.newBuilder().setStringValue("ehsann")))).addFilters(Filter.newBuilder().setFieldFilter(StructuredQuery.FieldFilter.newBuilder().setField(FieldReference.newBuilder().setFieldPath("tags")).setOp(Operator.ARRAY_CONTAINS).setValue(Value.newBuilder().setStringValue("pending")))))))).addOrderBy(Order.newBuilder().setField(FieldReference.newBuilder().setFieldPath("prop")).setDirection(Direction.ASCENDING)).addOrderBy(defaultKeyOrder());
    QueryTarget.Builder queryBuilder = QueryTarget.newBuilder().setParent("projects/p/databases/d/documents/rooms/1/messages/10").setStructuredQuery(structuredQueryBuilder);
    Target expected = Target.newBuilder().setQuery(queryBuilder).setTargetId(1).setResumeToken(ByteString.EMPTY).build();
    assertEquals(expected, actual);
    com.google.firebase.firestore.core.Target roundTripped = serializer.decodeQueryTarget(serializer.encodeQueryTarget(q.toTarget()));
    assertEquals(roundTripped, q.toTarget());
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) QueryTarget(com.google.firestore.v1.Target.QueryTarget) DocumentsTarget(com.google.firestore.v1.Target.DocumentsTarget) Target(com.google.firestore.v1.Target) QueryTarget(com.google.firestore.v1.Target.QueryTarget) StructuredQuery(com.google.firestore.v1.StructuredQuery) Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 23 with Target

use of com.google.firestore.v1.Target in project java-firestore by googleapis.

the class Watch method onResponse.

@Override
public synchronized void onResponse(ListenResponse listenResponse) {
    switch(listenResponse.getResponseTypeCase()) {
        case TARGET_CHANGE:
            TargetChange change = listenResponse.getTargetChange();
            boolean noTargetIds = change.getTargetIdsCount() == 0;
            switch(change.getTargetChangeType()) {
                case NO_CHANGE:
                    if (noTargetIds && change.hasReadTime() && current) {
                        // This means everything is up-to-date, so emit the current set of docs as a snapshot,
                        // if there were changes.
                        pushSnapshot(Timestamp.fromProto(change.getReadTime()), change.getResumeToken());
                    }
                    break;
                case ADD:
                    if (WATCH_TARGET_ID != change.getTargetIds(0)) {
                        closeStream(FirestoreException.forInvalidArgument("Target ID must be 0x01"));
                    }
                    break;
                case REMOVE:
                    Status status = change.hasCause() ? Status.fromCodeValue(change.getCause().getCode()) : Status.CANCELLED;
                    closeStream(FirestoreException.forServerRejection(status, "Backend ended Listen stream: " + change.getCause().getMessage()));
                    break;
                case CURRENT:
                    current = true;
                    break;
                case RESET:
                    resetDocs();
                    break;
                default:
                    closeStream(FirestoreException.forInvalidArgument("Encountered invalid target change type: " + change.getTargetChangeType()));
            }
            if (change.getResumeToken() != null && affectsTarget(change.getTargetIdsList(), WATCH_TARGET_ID)) {
                nextAttempt = backoff.createFirstAttempt();
            }
            break;
        case DOCUMENT_CHANGE:
            // No other targetIds can show up here, but we still need to see if the targetId was in the
            // added list or removed list.
            List<Integer> targetIds = listenResponse.getDocumentChange().getTargetIdsList();
            List<Integer> removedTargetIds = listenResponse.getDocumentChange().getRemovedTargetIdsList();
            boolean changed = targetIds.contains(WATCH_TARGET_ID);
            boolean removed = removedTargetIds.contains(WATCH_TARGET_ID);
            Document document = listenResponse.getDocumentChange().getDocument();
            ResourcePath name = ResourcePath.create(document.getName());
            if (changed) {
                changeMap.put(name, document);
            } else if (removed) {
                changeMap.put(name, null);
            }
            break;
        case DOCUMENT_DELETE:
            changeMap.put(ResourcePath.create(listenResponse.getDocumentDelete().getDocument()), null);
            break;
        case DOCUMENT_REMOVE:
            changeMap.put(ResourcePath.create(listenResponse.getDocumentRemove().getDocument()), null);
            break;
        case FILTER:
            if (listenResponse.getFilter().getCount() != currentSize()) {
                // We need to remove all the current results.
                resetDocs();
                // The filter didn't match, so re-issue the query.
                resetStream();
            }
            break;
        default:
            closeStream(FirestoreException.forInvalidArgument("Encountered invalid listen response type"));
            break;
    }
}
Also used : Status(io.grpc.Status) TargetChange(com.google.firestore.v1.TargetChange) Document(com.google.firestore.v1.Document)

Aggregations

StructuredQuery (com.google.firestore.v1.StructuredQuery)14 QueryTarget (com.google.firestore.v1.Target.QueryTarget)14 Test (org.junit.Test)14 Query (com.google.firebase.firestore.core.Query)13 Target (com.google.firestore.v1.Target)13 DocumentsTarget (com.google.firestore.v1.Target.DocumentsTarget)13 FieldIndex (com.google.firebase.firestore.model.FieldIndex)4 Value (com.google.firestore.v1.Value)4 TargetData (com.google.firebase.firestore.local.TargetData)3 Nullable (androidx.annotation.Nullable)2 Target (com.github.zhenwei.core.asn1.x509.Target)2 TargetInformation (com.github.zhenwei.core.asn1.x509.TargetInformation)2 Targets (com.github.zhenwei.core.asn1.x509.Targets)2 DocumentKey (com.google.firebase.firestore.model.DocumentKey)2 Status (io.grpc.Status)2 ArrayList (java.util.ArrayList)2 ASN1InputStream (com.github.zhenwei.core.asn1.ASN1InputStream)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 Extension (com.github.zhenwei.core.asn1.x509.Extension)1 X509AttributeCertificateHolder (com.github.zhenwei.pkix.cert.X509AttributeCertificateHolder)1