Search in sources :

Example 6 with Target

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

the class IndexBackfillerTest method testBackfillAppliesDeleteToRemoteDoc.

@Test
public void testBackfillAppliesDeleteToRemoteDoc() {
    addFieldIndex("coll", "foo");
    addDoc("coll/doc", version(5), "foo", 1);
    int documentsProcessed = backfiller.backfill();
    assertEquals(1, documentsProcessed);
    Mutation delete = deleteMutation("coll/doc");
    addMutationToOverlay("coll/doc", delete);
    documentsProcessed = backfiller.backfill();
    assertEquals(1, documentsProcessed);
    Target target = query("coll").filter(filter("foo", "==", 2)).toTarget();
    Set<DocumentKey> matching = indexManager.getDocumentsMatchingTarget(target);
    assertTrue(matching.isEmpty());
}
Also used : Target(com.google.firebase.firestore.core.Target) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Mutation(com.google.firebase.firestore.model.mutation.Mutation) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) Test(org.junit.Test)

Example 7 with Target

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

the class BundleSerializerTest method assertDecodesNamedQuery.

private void assertDecodesNamedQuery(String json, Query query) throws JSONException {
    String queryJson = "{\n" + "  name: 'query-1',\n" + "  bundledQuery: {\n" + "    parent: '" + TEST_PROJECT + "',\n" + "    structuredQuery:\n" + json + ",\n" + "    limitType: '" + (query.hasLimitToLast() ? "LAST" : "FIRST") + "'\n" + "   },\n" + " readTime: '2020-01-01T00:00:01.000000001Z'\n" + "}";
    NamedQuery actualNamedQuery = serializer.decodeNamedQuery(new JSONObject(queryJson));
    long limit = query.hasLimitToFirst() ? query.getLimitToFirst() : (query.hasLimitToLast() ? query.getLimitToLast() : Target.NO_LIMIT);
    Target target = new Target(query.getPath(), query.getCollectionGroup(), query.getFilters(), query.getExplicitOrderBy(), limit, query.getStartAt(), query.getEndAt());
    BundledQuery bundledQuery = new BundledQuery(target, query.hasLimitToLast() ? Query.LimitType.LIMIT_TO_LAST : Query.LimitType.LIMIT_TO_FIRST);
    NamedQuery expectedNamedQuery = new NamedQuery("query-1", bundledQuery, new SnapshotVersion(new com.google.firebase.Timestamp(1577836801, 1)));
    assertEquals(expectedNamedQuery, actualNamedQuery);
}
Also used : Target(com.google.firebase.firestore.core.Target) JSONObject(org.json.JSONObject) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) Timestamp(com.google.protobuf.Timestamp)

Example 8 with Target

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

the class WatchChangeAggregator method handleExistenceFilter.

/**
 * Handles existence filters and synthesizes deletes for filter mismatches. Targets that are
 * invalidated by filter mismatches are added to `pendingTargetResets`.
 */
public void handleExistenceFilter(ExistenceFilterWatchChange watchChange) {
    int targetId = watchChange.getTargetId();
    int expectedCount = watchChange.getExistenceFilter().getCount();
    TargetData targetData = queryDataForActiveTarget(targetId);
    if (targetData != null) {
        Target target = targetData.getTarget();
        if (target.isDocumentQuery()) {
            if (expectedCount == 0) {
                // The existence filter told us the document does not exist. We deduce that this document
                // does not exist and apply a deleted document to our updates. Without applying this
                // deleted document there might be another query that will raise this document as part of
                // a snapshot  until it is resolved, essentially exposing inconsistency between queries.
                DocumentKey key = DocumentKey.fromPath(target.getPath());
                MutableDocument result = MutableDocument.newNoDocument(key, SnapshotVersion.NONE);
                removeDocumentFromTarget(targetId, key, result);
            } else {
                hardAssert(expectedCount == 1, "Single document existence filter with count: %d", expectedCount);
            }
        } else {
            long currentSize = getCurrentDocumentCountForTarget(targetId);
            if (currentSize != expectedCount) {
                // Existence filter mismatch: We reset the mapping and raise a new snapshot with
                // `isFromCache:true`.
                resetTarget(targetId);
                pendingTargetResets.add(targetId);
            }
        }
    }
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) Target(com.google.firebase.firestore.core.Target) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 9 with Target

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

the class LocalSerializerTest method testEncodesLimitToLastQuery.

@Test
public void testEncodesLimitToLastQuery() {
    Target target = new Target(path("room"), /* collectionGroup= */
    null, Collections.emptyList(), Collections.emptyList(), /* limit=*/
    42, /* startAt= */
    null, /* endAt= */
    null);
    BundledQuery bundledQuery = new BundledQuery(target, Query.LimitType.LIMIT_TO_LAST);
    com.google.firestore.bundle.BundledQuery encodedBundledQuery = serializer.encodeBundledQuery(bundledQuery);
    BundledQuery decodedBundledQuery = serializer.decodeBundledQuery(encodedBundledQuery);
    assertEquals(bundledQuery, decodedBundledQuery);
}
Also used : Target(com.google.firebase.firestore.core.Target) BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) Test(org.junit.Test)

Example 10 with Target

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

the class LocalStoreTestCase method testLastLimboFreeSnapshotIsAdvancedDuringViewProcessing.

@Test
public void testLastLimboFreeSnapshotIsAdvancedDuringViewProcessing() {
    // This test verifies that the `lastLimboFreeSnapshot` version for TargetData is advanced when
    // we compute a limbo-free free view and that the mapping is persisted when we release a target.
    Query query = query("foo");
    Target target = query.toTarget();
    int targetId = allocateQuery(query);
    // Advance the target snapshot.
    applyRemoteEvent(noChangeEvent(targetId, 10));
    // At this point, we have not yet confirmed that the target is limbo free.
    TargetData cachedTargetData = localStore.getTargetData(target);
    Assert.assertEquals(SnapshotVersion.NONE, cachedTargetData.getLastLimboFreeSnapshotVersion());
    // Mark the view synced, which updates the last limbo free snapshot version.
    updateViews(targetId, /* fromCache=*/
    false);
    cachedTargetData = localStore.getTargetData(target);
    Assert.assertEquals(version(10), cachedTargetData.getLastLimboFreeSnapshotVersion());
    // The last limbo free snapshot version is persisted even if we release the target.
    releaseTarget(targetId);
    if (!garbageCollectorIsEager()) {
        cachedTargetData = localStore.getTargetData(target);
        Assert.assertEquals(version(10), cachedTargetData.getLastLimboFreeSnapshotVersion());
    }
}
Also used : Target(com.google.firebase.firestore.core.Target) BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) NamedQuery(com.google.firebase.firestore.bundle.NamedQuery) Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

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