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());
}
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());
}
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;
}
}
Aggregations