Search in sources :

Example 11 with Target

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

the class BundleSerializer method decodeBundledQuery.

private BundledQuery decodeBundledQuery(JSONObject bundledQuery) throws JSONException {
    JSONObject structuredQuery = bundledQuery.getJSONObject("structuredQuery");
    verifyNoSelect(structuredQuery);
    ResourcePath parent = decodeName(bundledQuery.getString("parent"));
    JSONArray from = structuredQuery.getJSONArray("from");
    verifyCollectionSelector(from);
    JSONObject collectionSelector = from.getJSONObject(0);
    boolean allDescendants = collectionSelector.optBoolean("allDescendants", false);
    @Nullable String collectionGroup = null;
    if (allDescendants) {
        collectionGroup = collectionSelector.getString("collectionId");
    } else {
        parent = parent.append(collectionSelector.getString("collectionId"));
    }
    List<Filter> filters = decodeWhere(structuredQuery.optJSONObject("where"));
    List<OrderBy> orderBys = decodeOrderBy(structuredQuery.optJSONArray("orderBy"));
    @Nullable Bound startAt = decodeStartAtBound(structuredQuery.optJSONObject("startAt"));
    @Nullable Bound endAt = decodeEndAtBound(structuredQuery.optJSONObject("endAt"));
    verifyNoOffset(structuredQuery);
    int limit = decodeLimit(structuredQuery);
    Query.LimitType limitType = decodeLimitType(bundledQuery);
    return new BundledQuery(new Target(parent, collectionGroup, filters, orderBys, limit, startAt, endAt), limitType);
}
Also used : OrderBy(com.google.firebase.firestore.core.OrderBy) Query(com.google.firebase.firestore.core.Query) JSONArray(org.json.JSONArray) Bound(com.google.firebase.firestore.core.Bound) ByteString(com.google.protobuf.ByteString) Target(com.google.firebase.firestore.core.Target) ResourcePath(com.google.firebase.firestore.model.ResourcePath) JSONObject(org.json.JSONObject) FieldFilter(com.google.firebase.firestore.core.FieldFilter) Filter(com.google.firebase.firestore.core.Filter) Nullable(androidx.annotation.Nullable)

Example 12 with Target

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

the class LocalSerializer method decodeBundledQuery.

public BundledQuery decodeBundledQuery(com.google.firestore.bundle.BundledQuery bundledQuery) {
    LimitType limitType = bundledQuery.getLimitType().equals(com.google.firestore.bundle.BundledQuery.LimitType.FIRST) ? LimitType.LIMIT_TO_FIRST : LimitType.LIMIT_TO_LAST;
    Target target = rpcSerializer.decodeQueryTarget(bundledQuery.getParent(), bundledQuery.getStructuredQuery());
    return new BundledQuery(target, limitType);
}
Also used : Target(com.google.firebase.firestore.core.Target) BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) LimitType(com.google.firebase.firestore.core.Query.LimitType)

Example 13 with Target

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

the class LocalStoreTestCase method testHandlesSavingAndLoadingNamedQueries.

@Test
public void testHandlesSavingAndLoadingNamedQueries() {
    Target target = query("foo").toTarget();
    NamedQuery namedQuery = new NamedQuery("testQuery", new BundledQuery(target, Query.LimitType.LIMIT_TO_FIRST), SnapshotVersion.NONE);
    saveNamedQuery(namedQuery);
    assertHasNamedQuery(namedQuery);
}
Also used : Target(com.google.firebase.firestore.core.Target) BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) NamedQuery(com.google.firebase.firestore.bundle.NamedQuery) Test(org.junit.Test)

Example 14 with Target

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

the class LocalStoreTestCase method testLoadingNamedQueriesAllocatesTargetsAndUpdatesTargetDocumentMapping.

@Test
public void testLoadingNamedQueriesAllocatesTargetsAndUpdatesTargetDocumentMapping() {
    bundleDocuments(doc("foo1/bar", 1, map("sum", 1337)), doc("foo2/bar", 1, map("sum", 42)));
    assertChanged(doc("foo1/bar", 1, map("sum", 1337)), doc("foo2/bar", 1, map("sum", 42)));
    assertContains(doc("foo1/bar", 1, map("sum", 1337)));
    assertContains(doc("foo2/bar", 1, map("sum", 42)));
    Target target1 = Query.atPath(ResourcePath.fromString("foo1")).toTarget();
    NamedQuery namedQuery1 = new NamedQuery("query-1", new BundledQuery(target1, Query.LimitType.LIMIT_TO_FIRST), new SnapshotVersion(Timestamp.now()));
    saveNamedQuery(namedQuery1, key("foo1/bar"));
    assertHasNamedQuery(namedQuery1);
    assertQueryDocumentMapping(/* targetId= */
    4, key("foo1/bar"));
    Target target2 = Query.atPath(ResourcePath.fromString("foo2")).toTarget();
    NamedQuery namedQuery2 = new NamedQuery("query-2", new BundledQuery(target2, Query.LimitType.LIMIT_TO_FIRST), new SnapshotVersion(Timestamp.now()));
    saveNamedQuery(namedQuery2, key("foo2/bar"));
    assertHasNamedQuery(namedQuery2);
    assertQueryDocumentMapping(/* targetId= */
    6, key("foo2/bar"));
}
Also used : Target(com.google.firebase.firestore.core.Target) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) NamedQuery(com.google.firebase.firestore.bundle.NamedQuery) Test(org.junit.Test)

Example 15 with Target

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

the class SQLiteIndexManager method getDocumentsMatchingTarget.

@Override
public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
    hardAssert(started, "IndexManager not started");
    List<String> subQueries = new ArrayList<>();
    List<Object> bindings = new ArrayList<>();
    for (Target subTarget : getSubTargets(target)) {
        FieldIndex fieldIndex = getFieldIndex(subTarget);
        if (fieldIndex == null) {
            return null;
        }
        @Nullable List<Value> arrayValues = subTarget.getArrayValues(fieldIndex);
        @Nullable Collection<Value> notInValues = subTarget.getNotInValues(fieldIndex);
        @Nullable Bound lowerBound = subTarget.getLowerBound(fieldIndex);
        @Nullable Bound upperBound = subTarget.getUpperBound(fieldIndex);
        if (Logger.isDebugEnabled()) {
            Logger.debug(TAG, "Using index '%s' to execute '%s' (Arrays: %s, Lower bound: %s, Upper bound: %s)", fieldIndex, subTarget, arrayValues, lowerBound, upperBound);
        }
        Object[] lowerBoundEncoded = encodeBound(fieldIndex, subTarget, lowerBound);
        String lowerBoundOp = lowerBound != null && lowerBound.isInclusive() ? ">=" : ">";
        Object[] upperBoundEncoded = encodeBound(fieldIndex, subTarget, upperBound);
        String upperBoundOp = upperBound != null && upperBound.isInclusive() ? "<=" : "<";
        Object[] notInEncoded = encodeValues(fieldIndex, subTarget, notInValues);
        Object[] subQueryAndBindings = generateQueryAndBindings(subTarget, fieldIndex.getIndexId(), arrayValues, lowerBoundEncoded, lowerBoundOp, upperBoundEncoded, upperBoundOp, notInEncoded);
        subQueries.add(String.valueOf(subQueryAndBindings[0]));
        bindings.addAll(Arrays.asList(subQueryAndBindings).subList(1, subQueryAndBindings.length));
    }
    String queryString;
    if (subQueries.size() == 1) {
        // If there's only one subQuery, just execute the one subQuery.
        queryString = subQueries.get(0);
    } else {
        // Construct "SELECT * FROM (subQuery1 UNION subQuery2 UNION ...) LIMIT N"
        queryString = "SELECT * FROM (" + TextUtils.join(" UNION ", subQueries) + ")";
        if (target.getLimit() != -1) {
            queryString = queryString + " LIMIT " + target.getLimit();
        }
    }
    hardAssert(bindings.size() < 1000, "Cannot perform query with more than 999 bind elements");
    SQLitePersistence.Query query = db.query(queryString).binding(bindings.toArray());
    Set<DocumentKey> result = new HashSet<>();
    query.forEach(row -> result.add(DocumentKey.fromPath(ResourcePath.fromString(row.getString(0)))));
    Logger.debug(TAG, "Index scan returned %s documents", result.size());
    return result;
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) ArrayList(java.util.ArrayList) Bound(com.google.firebase.firestore.core.Bound) Target(com.google.firebase.firestore.core.Target) Value(com.google.firestore.v1.Value) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Nullable(androidx.annotation.Nullable) HashSet(java.util.HashSet)

Aggregations

Target (com.google.firebase.firestore.core.Target)23 Test (org.junit.Test)14 BundledQuery (com.google.firebase.firestore.bundle.BundledQuery)11 NamedQuery (com.google.firebase.firestore.bundle.NamedQuery)8 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)7 DocumentKey (com.google.firebase.firestore.model.DocumentKey)5 Filter (com.google.firebase.firestore.core.Filter)3 Query (com.google.firebase.firestore.core.Query)3 ArrayList (java.util.ArrayList)3 Nullable (androidx.annotation.Nullable)2 Bound (com.google.firebase.firestore.core.Bound)2 FieldFilter (com.google.firebase.firestore.core.FieldFilter)2 FieldIndex (com.google.firebase.firestore.model.FieldIndex)2 ByteString (com.google.protobuf.ByteString)2 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)1 User (com.google.firebase.firestore.auth.User)1 CompositeFilter (com.google.firebase.firestore.core.CompositeFilter)1 OrderBy (com.google.firebase.firestore.core.OrderBy)1 LimitType (com.google.firebase.firestore.core.Query.LimitType)1 TargetData (com.google.firebase.firestore.local.TargetData)1