Skip to content

Commit 4e336c6

Browse files
Add API to filter overlays by batch ID (#3145)
1 parent 710046b commit 4e336c6

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/DocumentOverlayCache.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public interface DocumentOverlayCache {
4646
/** Removes the overlay whose largest-batch-id equals to the given Id. */
4747
void removeOverlaysForBatchId(int batchId);
4848

49-
/** Returns all saved overlay for the given collection. */
50-
Map<DocumentKey, Mutation> getOverlays(ResourcePath collection);
49+
/**
50+
* Returns all saved overlays for the given collection.
51+
*
52+
* @param collection The collection path to get the overlays for.
53+
* @param sinceBatchId The minimum batch ID to filter by (exclusive). Only overlays that contain a
54+
* change past `sinceBatchId` are returned.
55+
*/
56+
Map<DocumentKey, Mutation> getOverlays(ResourcePath collection, int sinceBatchId);
5157
}

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalDocumentsView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
323323
Query query, SnapshotVersion sinceReadTime) {
324324
ImmutableSortedMap<DocumentKey, MutableDocument> remoteDocuments =
325325
remoteDocumentCache.getAllDocumentsMatchingQuery(query, sinceReadTime);
326-
Map<DocumentKey, Mutation> overlays = documentOverlayCache.getOverlays(query.getPath());
326+
Map<DocumentKey, Mutation> overlays = documentOverlayCache.getOverlays(query.getPath(), -1);
327327

328328
// As documents might match the query because of their overlay we need to include all documents
329329
// in the result.

firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryDocumentOverlayCache.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
public class MemoryDocumentOverlayCache implements DocumentOverlayCache {
2929
// A map sorted by DocumentKey, whose value is a pair of the largest batch id for the overlay
3030
// and the overlay itself.
31-
private TreeMap<DocumentKey, Pair<Integer, Mutation>> overlays = new TreeMap<>();
32-
Map<Integer, Set<DocumentKey>> overlayByBatchId = new HashMap<>();
31+
private final TreeMap<DocumentKey, Pair<Integer, Mutation>> overlays = new TreeMap<>();
32+
private final Map<Integer, Set<DocumentKey>> overlayByBatchId = new HashMap<>();
3333

3434
@Nullable
3535
@Override
3636
public Mutation getOverlay(DocumentKey key) {
37-
if (overlays.get(key) != null) {
38-
return overlays.get(key).second;
37+
Pair<Integer, Mutation> overlay = overlays.get(key);
38+
if (overlay != null) {
39+
return overlay.second;
3940
}
40-
4141
return null;
4242
}
4343

@@ -80,7 +80,7 @@ public void removeOverlaysForBatchId(int batchId) {
8080
}
8181

8282
@Override
83-
public Map<DocumentKey, Mutation> getOverlays(ResourcePath collection) {
83+
public Map<DocumentKey, Mutation> getOverlays(ResourcePath collection, int sinceBatchId) {
8484
Map<DocumentKey, Mutation> result = new HashMap<>();
8585

8686
int immediateChildrenPathLength = collection.length() + 1;
@@ -96,7 +96,11 @@ public Map<DocumentKey, Mutation> getOverlays(ResourcePath collection) {
9696
if (key.getPath().length() != immediateChildrenPathLength) {
9797
continue;
9898
}
99-
result.put(entry.getKey(), entry.getValue().second);
99+
100+
Pair<Integer, Mutation> batchIdToOverlay = entry.getValue();
101+
if (batchIdToOverlay.first > sinceBatchId) {
102+
result.put(entry.getKey(), batchIdToOverlay.second);
103+
}
100104
}
101105

102106
return result;

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteDocumentOverlayCache.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void removeOverlaysForBatchId(int batchId) {
8282
}
8383

8484
@Override
85-
public Map<DocumentKey, Mutation> getOverlays(ResourcePath collection) {
85+
public Map<DocumentKey, Mutation> getOverlays(ResourcePath collection, int sinceBatchId) {
8686
int immediateChildrenPathLength = collection.length() + 1;
8787

8888
String prefixPath = EncodedPath.encode(collection);
@@ -91,8 +91,9 @@ public Map<DocumentKey, Mutation> getOverlays(ResourcePath collection) {
9191
Map<DocumentKey, Mutation> result = new HashMap<>();
9292

9393
db.query(
94-
"SELECT path, overlay_mutation FROM document_overlays WHERE uid = ? AND path >= ? AND path < ?")
95-
.binding(uid, prefixPath, prefixSuccessorPath)
94+
"SELECT path, overlay_mutation FROM document_overlays "
95+
+ "WHERE uid = ? AND path >= ? AND path < ? AND largest_batch_id > ?")
96+
.binding(uid, prefixPath, prefixSuccessorPath, sinceBatchId)
9697
.forEach(
9798
row -> {
9899
try {

firebase-firestore/src/test/java/com/google/firebase/firestore/local/DocumentOverlayCacheTestCase.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,31 @@ public void testGetAllOverlaysForCollection() {
152152

153153
m.remove(key("coll/doc1/sub/sub_doc"));
154154
m.remove(key("other/doc1"));
155-
assertEquals(m, overlays.getOverlays(path("coll")));
155+
assertEquals(m, overlays.getOverlays(path("coll"), -1));
156+
}
157+
158+
@Test
159+
public void testGetAllOverlaysSinceBatchId() {
160+
Mutation m1 = patchMutation("coll/doc1", map("foo", "bar"));
161+
Mutation m2 = setMutation("coll/doc2", map("foo", "bar"));
162+
Map<DocumentKey, Mutation> m = new HashMap<>();
163+
m.put(key("coll/doc1"), m1);
164+
m.put(key("coll/doc2"), m2);
165+
overlays.saveOverlays(2, m);
166+
167+
Mutation m3 = deleteMutation("coll/doc3");
168+
m = new HashMap<>();
169+
m.put(key("coll/doc3"), m3);
170+
overlays.saveOverlays(3, m);
171+
172+
Mutation m4 = deleteMutation("coll/doc4");
173+
m = new HashMap<>();
174+
m.put(key("coll/doc4"), m4);
175+
overlays.saveOverlays(4, m);
176+
177+
Map<DocumentKey, Mutation> expected = new HashMap<>();
178+
expected.put(key("coll/doc3"), m3);
179+
expected.put(key("coll/doc4"), m4);
180+
assertEquals(expected, overlays.getOverlays(path("coll"), 2));
156181
}
157182
}

0 commit comments

Comments
 (0)