Skip to content

Commit eec5f31

Browse files
authored
Use collection_path + document_id in overlay table (#3178)
1 parent 3b118bf commit eec5f31

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

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

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ public SQLiteDocumentOverlayCache(SQLitePersistence db, LocalSerializer serializ
4040
@Nullable
4141
@Override
4242
public Mutation getOverlay(DocumentKey key) {
43-
String path = EncodedPath.encode(key.getPath());
44-
return db.query("SELECT overlay_mutation FROM document_overlays WHERE uid = ? AND path = ?")
45-
.binding(uid, path)
43+
String collectionPath = EncodedPath.encode(key.getPath().popLast());
44+
String documentId = key.getPath().getLastSegment();
45+
return db.query(
46+
"SELECT overlay_mutation FROM document_overlays WHERE uid = ? AND collection_path = ? AND document_id = ?")
47+
.binding(uid, collectionPath, documentId)
4648
.firstValue(
4749
row -> {
4850
if (row == null) return null;
@@ -57,11 +59,14 @@ public Mutation getOverlay(DocumentKey key) {
5759
}
5860

5961
private void saveOverlay(int largestBatchId, DocumentKey key, @Nullable Mutation mutation) {
62+
String collectionPath = EncodedPath.encode(key.getPath().popLast());
63+
String documentId = key.getPath().getLastSegment();
6064
db.execute(
6165
"INSERT OR REPLACE INTO document_overlays "
62-
+ "(uid, path, largest_batch_id, overlay_mutation) VALUES (?, ?, ?, ?)",
66+
+ "(uid, collection_path, document_id, largest_batch_id, overlay_mutation) VALUES (?, ?, ?, ?, ?)",
6367
uid,
64-
EncodedPath.encode(key.getPath()),
68+
collectionPath,
69+
documentId,
6570
largestBatchId,
6671
serializer.encodeMutation(mutation).toByteArray());
6772
}
@@ -83,36 +88,22 @@ public void removeOverlaysForBatchId(int batchId) {
8388

8489
@Override
8590
public Map<DocumentKey, Mutation> getOverlays(ResourcePath collection, int sinceBatchId) {
86-
int immediateChildrenPathLength = collection.length() + 1;
87-
88-
String prefixPath = EncodedPath.encode(collection);
89-
String prefixSuccessorPath = EncodedPath.prefixSuccessor(prefixPath);
91+
String collectionPath = EncodedPath.encode(collection);
9092

9193
Map<DocumentKey, Mutation> result = new HashMap<>();
9294

9395
db.query(
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)
96+
"SELECT document_id, overlay_mutation FROM document_overlays "
97+
+ "WHERE uid = ? AND collection_path = ? AND largest_batch_id > ?")
98+
.binding(uid, collectionPath, sinceBatchId)
9799
.forEach(
98100
row -> {
99101
try {
100-
ResourcePath path = EncodedPath.decodeResourcePath(row.getString(0));
101-
// The query is actually returning any path that starts with the query path prefix
102-
// which may include documents in subcollections. For example, a query on 'rooms'
103-
// will return rooms/abc/messages/xyx but we shouldn't match it. Fix this by
104-
// discarding rows with document keys more than one segment longer than the query
105-
// path.
106-
// TODO(Overlay): Introduce a segment count of the path or a terminator to avoid
107-
// over selecting.
108-
if (path.length() != immediateChildrenPathLength) {
109-
return;
110-
}
111-
102+
String documentId = row.getString(0);
112103
Write write = Write.parseFrom(row.getBlob(1));
113104
Mutation mutation = serializer.decodeMutation(write);
114105

115-
result.put(DocumentKey.fromPath(path), mutation);
106+
result.put(DocumentKey.fromPath(collection.append(documentId)), mutation);
116107
} catch (InvalidProtocolBufferException e) {
117108
throw fail("Overlay failed to parse: %s", e);
118109
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,11 @@ private void createOverlays() {
635635
db.execSQL(
636636
"CREATE TABLE document_overlays ("
637637
+ "uid TEXT, "
638-
+ "path TEXT, "
638+
+ "collection_path TEXT, "
639+
+ "document_id TEXT, "
639640
+ "largest_batch_id INTEGER, "
640641
+ "overlay_mutation BLOB, "
641-
+ "PRIMARY KEY (uid, path))");
642+
+ "PRIMARY KEY (uid, collection_path, document_id))");
642643
db.execSQL("CREATE INDEX batch_id_overlay ON document_overlays (uid, largest_batch_id)");
643644
});
644645
}

0 commit comments

Comments
 (0)