Search in sources :

Example 1 with QueryTarget

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();
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) OrderBy(com.google.firebase.firestore.core.OrderBy) QueryTarget(com.google.firestore.v1.Target.QueryTarget) ResourcePath(com.google.firebase.firestore.model.ResourcePath) CollectionSelector(com.google.firestore.v1.StructuredQuery.CollectionSelector) Cursor(com.google.firestore.v1.Cursor)

Example 2 with QueryTarget

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

Aggregations

StructuredQuery (com.google.firestore.v1.StructuredQuery)2 QueryTarget (com.google.firestore.v1.Target.QueryTarget)2 OrderBy (com.google.firebase.firestore.core.OrderBy)1 Query (com.google.firebase.firestore.core.Query)1 ResourcePath (com.google.firebase.firestore.model.ResourcePath)1 Cursor (com.google.firestore.v1.Cursor)1 CollectionSelector (com.google.firestore.v1.StructuredQuery.CollectionSelector)1 Target (com.google.firestore.v1.Target)1 DocumentsTarget (com.google.firestore.v1.Target.DocumentsTarget)1 Test (org.junit.Test)1