use of com.google.firestore.v1.Target.QueryTarget in project firebase-android-sdk by firebase.
the class RemoteSerializer method encodeQueryTarget.
public QueryTarget encodeQueryTarget(com.google.firebase.firestore.core.Target target) {
// Dissect the path into parent, collectionId, and optional key filter.
QueryTarget.Builder builder = QueryTarget.newBuilder();
StructuredQuery.Builder structuredQueryBuilder = StructuredQuery.newBuilder();
ResourcePath path = target.getPath();
if (target.getCollectionGroup() != null) {
hardAssert(path.length() % 2 == 0, "Collection Group queries should be within a document path or root.");
builder.setParent(encodeQueryPath(path));
CollectionSelector.Builder from = CollectionSelector.newBuilder();
from.setCollectionId(target.getCollectionGroup());
from.setAllDescendants(true);
structuredQueryBuilder.addFrom(from);
} else {
hardAssert(path.length() % 2 != 0, "Document queries with filters are not supported.");
builder.setParent(encodeQueryPath(path.popLast()));
CollectionSelector.Builder from = CollectionSelector.newBuilder();
from.setCollectionId(path.getLastSegment());
structuredQueryBuilder.addFrom(from);
}
// Encode the filters.
if (target.getFilters().size() > 0) {
structuredQueryBuilder.setWhere(encodeFilters(target.getFilters()));
}
// Encode the orders.
for (OrderBy orderBy : target.getOrderBy()) {
structuredQueryBuilder.addOrderBy(encodeOrderBy(orderBy));
}
// Encode the limit.
if (target.hasLimit()) {
structuredQueryBuilder.setLimit(Int32Value.newBuilder().setValue((int) target.getLimit()));
}
if (target.getStartAt() != null) {
Cursor.Builder cursor = Cursor.newBuilder();
cursor.addAllValues(target.getStartAt().getPosition());
cursor.setBefore(target.getStartAt().isInclusive());
structuredQueryBuilder.setStartAt(cursor);
}
if (target.getEndAt() != null) {
Cursor.Builder cursor = Cursor.newBuilder();
cursor.addAllValues(target.getEndAt().getPosition());
cursor.setBefore(!target.getEndAt().isInclusive());
structuredQueryBuilder.setEndAt(cursor);
}
builder.setStructuredQuery(structuredQueryBuilder);
return builder.build();
}
use of com.google.firestore.v1.Target.QueryTarget in project firebase-android-sdk by firebase.
the class RemoteSerializerTest method testEncodesNestedAncestorQueries.
@Test
public void testEncodesNestedAncestorQueries() {
Query q = Query.atPath(ResourcePath.fromString("rooms/1/messages/10/attachments"));
Target actual = serializer.encodeTarget(wrapTargetData(q));
StructuredQuery.Builder structuredQueryBuilder = StructuredQuery.newBuilder().addFrom(CollectionSelector.newBuilder().setCollectionId("attachments")).addOrderBy(defaultKeyOrder());
QueryTarget queryBuilder = QueryTarget.newBuilder().setParent("projects/p/databases/d/documents/rooms/1/messages/10").setStructuredQuery(structuredQueryBuilder).build();
Target expected = Target.newBuilder().setQuery(queryBuilder).setTargetId(1).setResumeToken(ByteString.EMPTY).build();
assertEquals(expected, actual);
assertEquals(serializer.decodeQueryTarget(serializer.encodeQueryTarget(q.toTarget())), q.toTarget());
}
Aggregations