Skip to content

Update LocalStoreTestCase to validate overlays #3332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ DocumentOverlayCache getDocumentOverlayCache() {
return documentOverlayCache;
}

@VisibleForTesting
IndexManager getIndexManager() {
return indexManager;
}

/**
* Returns the the local view of the document identified by {@code key}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.google.firebase.firestore.local;

import androidx.annotation.Nullable;
import com.google.firebase.Timestamp;
import com.google.firebase.database.collection.ImmutableSortedMap;
import com.google.firebase.database.collection.ImmutableSortedSet;
import com.google.firebase.firestore.core.Query;
Expand All @@ -26,10 +25,8 @@
import com.google.firebase.firestore.model.ResourcePath;
import com.google.firebase.firestore.model.SnapshotVersion;
import com.google.firebase.firestore.model.mutation.Mutation;
import com.google.firebase.firestore.model.mutation.MutationBatch;
import com.google.protobuf.ByteString;
import com.google.firebase.firestore.model.mutation.Overlay;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -39,8 +36,8 @@
class CountingQueryEngine extends QueryEngine {
private final QueryEngine queryEngine;

private final int[] mutationsReadByCollection = new int[] {0};
private final int[] mutationsReadByKey = new int[] {0};
private final int[] overlaysReadByCollection = new int[] {0};
private final int[] overlaysReadByKey = new int[] {0};
private final int[] documentsReadByCollection = new int[] {0};
private final int[] documentsReadByKey = new int[] {0};

Expand All @@ -49,8 +46,8 @@ class CountingQueryEngine extends QueryEngine {
}

void resetCounts() {
mutationsReadByCollection[0] = 0;
mutationsReadByKey[0] = 0;
overlaysReadByCollection[0] = 0;
overlaysReadByKey[0] = 0;
documentsReadByCollection[0] = 0;
documentsReadByKey[0] = 0;
}
Expand All @@ -60,9 +57,9 @@ public void initialize(LocalDocumentsView localDocuments, IndexManager indexMana
LocalDocumentsView wrappedView =
new LocalDocumentsView(
wrapRemoteDocumentCache(localDocuments.getRemoteDocumentCache()),
wrapMutationQueue(localDocuments.getMutationQueue()),
localDocuments.getDocumentOverlayCache(),
localDocuments.getIndexManager());
localDocuments.getMutationQueue(),
wrapOverlayCache(localDocuments.getDocumentOverlayCache()),
indexManager);
queryEngine.initialize(wrappedView, indexManager);
}

Expand Down Expand Up @@ -96,20 +93,19 @@ int getDocumentsReadByKey() {
}

/**
* Returns the number of mutations returned by the MutationQueue's
* `getAllMutationBatchesAffectingQuery()` API (since the last call to `resetCounts()`)
* Returns the number of mutations returned by the OverlayCache's `getOverlays()` API (since the
* last call to `resetCounts()`)
*/
int getMutationsReadByCollection() {
return mutationsReadByCollection[0];
int getOverlaysReadByCollection() {
return overlaysReadByCollection[0];
}

/**
* Returns the number of mutations returned by the MutationQueue's
* `getAllMutationBatchesAffectingDocumentKey()` and
* `getAllMutationBatchesAffectingDocumentKeys()` APIs (since the last call to `resetCounts()`)
* Returns the number of mutations returned by the OverlayCache's `getOverlay()` API (since the
* last call to `resetCounts()`)
*/
int getMutationsReadByKey() {
return mutationsReadByKey[0];
int getOverlaysReadByKey() {
return overlaysReadByKey[0];
}

private RemoteDocumentCache wrapRemoteDocumentCache(RemoteDocumentCache subject) {
Expand Down Expand Up @@ -168,96 +164,40 @@ public SnapshotVersion getLatestReadTime() {
};
}

private MutationQueue wrapMutationQueue(MutationQueue subject) {
return new MutationQueue() {
@Override
public void start() {
subject.start();
}

@Override
public boolean isEmpty() {
return subject.isEmpty();
}

@Override
public void acknowledgeBatch(MutationBatch batch, ByteString streamToken) {
subject.acknowledgeBatch(batch, streamToken);
}

@Override
public ByteString getLastStreamToken() {
return subject.getLastStreamToken();
}

@Override
public void setLastStreamToken(ByteString streamToken) {
subject.setLastStreamToken(streamToken);
}

@Override
public MutationBatch addMutationBatch(
Timestamp localWriteTime, List<Mutation> baseMutations, List<Mutation> mutations) {
return subject.addMutationBatch(localWriteTime, baseMutations, mutations);
}

private DocumentOverlayCache wrapOverlayCache(DocumentOverlayCache subject) {
return new DocumentOverlayCache() {
@Nullable
@Override
public MutationBatch lookupMutationBatch(int batchId) {
return subject.lookupMutationBatch(batchId);
public Overlay getOverlay(DocumentKey key) {
++overlaysReadByKey[0];
return subject.getOverlay(key);
}

@Nullable
@Override
public MutationBatch getNextMutationBatchAfterBatchId(int batchId) {
return subject.getNextMutationBatchAfterBatchId(batchId);
public void saveOverlays(int largestBatchId, Map<DocumentKey, Mutation> overlays) {
subject.saveOverlays(largestBatchId, overlays);
}

@Override
public int getHighestUnacknowledgedBatchId() {
return subject.getHighestUnacknowledgedBatchId();
public void removeOverlaysForBatchId(int batchId) {
subject.removeOverlaysForBatchId(batchId);
}

@Override
public List<MutationBatch> getAllMutationBatches() {
List<MutationBatch> result = subject.getAllMutationBatches();
mutationsReadByKey[0] += result.size();
public Map<DocumentKey, Overlay> getOverlays(ResourcePath collection, int sinceBatchId) {
Map<DocumentKey, Overlay> result = subject.getOverlays(collection, sinceBatchId);
overlaysReadByCollection[0] += result.size();
return result;
}

@Override
public List<MutationBatch> getAllMutationBatchesAffectingDocumentKey(
DocumentKey documentKey) {
List<MutationBatch> result = subject.getAllMutationBatchesAffectingDocumentKey(documentKey);
mutationsReadByKey[0] += result.size();
public Map<DocumentKey, Overlay> getOverlays(
String collectionGroup, int sinceBatchId, int count) {
Map<DocumentKey, Overlay> result =
subject.getOverlays(collectionGroup, sinceBatchId, count);
overlaysReadByCollection[0] += result.size();
return result;
}

@Override
public List<MutationBatch> getAllMutationBatchesAffectingDocumentKeys(
Iterable<DocumentKey> documentKeys) {
List<MutationBatch> result =
subject.getAllMutationBatchesAffectingDocumentKeys(documentKeys);
mutationsReadByKey[0] += result.size();
return result;
}

@Override
public List<MutationBatch> getAllMutationBatchesAffectingQuery(Query query) {
List<MutationBatch> result = subject.getAllMutationBatchesAffectingQuery(query);
mutationsReadByCollection[0] += result.size();
return result;
}

@Override
public void removeMutationBatch(MutationBatch batch) {
subject.removeMutationBatch(batch);
}

@Override
public void performConsistencyCheck() {
subject.performConsistencyCheck();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ private void assertHasNamedQuery(NamedQuery expectedNamedQuery) {
}

/**
* Asserts the expected numbers of mutations read by the MutationQueue since the last call to
* Asserts the expected numbers of mutations read by the OverlayQueue since the last call to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OverlayCache?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* `resetPersistenceStats()`.
*/
private void assertMutationsRead(int byKey, int byCollection) {
private void assertOverlaysRead(int byKey, int byCollection) {
assertEquals("Overlays read (by key)", byKey, queryEngine.getOverlaysReadByKey());
assertEquals(
"Mutations read (by collection)", byCollection, queryEngine.getMutationsReadByCollection());
assertEquals("Mutations read (by key)", byKey, queryEngine.getMutationsReadByKey());
"Overlays read (by collection)", byCollection, queryEngine.getOverlaysReadByCollection());
}

/**
Expand Down Expand Up @@ -975,8 +975,7 @@ public void testReadsAllDocumentsForInitialCollectionQueries() {

localStore.executeQuery(query, /* usePreviousResults= */ true);
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 2);
// No mutations are read because only overlay is needed.
assertMutationsRead(/* byKey= */ 0, /* byCollection= */ 0);
assertOverlaysRead(/* byKey= */ 0, /* byCollection= */ 1);
}

@Test
Expand Down