@@ -40,9 +40,11 @@ public SQLiteDocumentOverlayCache(SQLitePersistence db, LocalSerializer serializ
40
40
@ Nullable
41
41
@ Override
42
42
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 )
46
48
.firstValue (
47
49
row -> {
48
50
if (row == null ) return null ;
@@ -57,11 +59,14 @@ public Mutation getOverlay(DocumentKey key) {
57
59
}
58
60
59
61
private void saveOverlay (int largestBatchId , DocumentKey key , @ Nullable Mutation mutation ) {
62
+ String collectionPath = EncodedPath .encode (key .getPath ().popLast ());
63
+ String documentId = key .getPath ().getLastSegment ();
60
64
db .execute (
61
65
"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 (?, ?, ?, ?, ?)" ,
63
67
uid ,
64
- EncodedPath .encode (key .getPath ()),
68
+ collectionPath ,
69
+ documentId ,
65
70
largestBatchId ,
66
71
serializer .encodeMutation (mutation ).toByteArray ());
67
72
}
@@ -83,36 +88,22 @@ public void removeOverlaysForBatchId(int batchId) {
83
88
84
89
@ Override
85
90
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 );
90
92
91
93
Map <DocumentKey , Mutation > result = new HashMap <>();
92
94
93
95
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 )
97
99
.forEach (
98
100
row -> {
99
101
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 );
112
103
Write write = Write .parseFrom (row .getBlob (1 ));
113
104
Mutation mutation = serializer .decodeMutation (write );
114
105
115
- result .put (DocumentKey .fromPath (path ), mutation );
106
+ result .put (DocumentKey .fromPath (collection . append ( documentId ) ), mutation );
116
107
} catch (InvalidProtocolBufferException e ) {
117
108
throw fail ("Overlay failed to parse: %s" , e );
118
109
}
0 commit comments