@@ -65,6 +65,32 @@ public void setIndexManager(IndexManager indexManager) {
65
65
this .indexManager = indexManager ;
66
66
}
67
67
68
+ private enum DocumentType {
69
+ NO_DOCUMENT (1 ),
70
+ FOUND_DOCUMENT (2 ),
71
+ UNKNOWN_DOCUMENT (3 ),
72
+ INVALID_DOCUMENT (4 );
73
+
74
+ final int dbValue ;
75
+
76
+ DocumentType (int dbValue ) {
77
+ this .dbValue = dbValue ;
78
+ }
79
+
80
+ static DocumentType forMutableDocument (MutableDocument document ) {
81
+ if (document .isNoDocument ()) {
82
+ return NO_DOCUMENT ;
83
+ } else if (document .isFoundDocument ()) {
84
+ return FOUND_DOCUMENT ;
85
+ } else if (document .isUnknownDocument ()) {
86
+ return UNKNOWN_DOCUMENT ;
87
+ } else {
88
+ hardAssert (!document .isValidDocument (), "MutableDocument has an unknown type" );
89
+ return INVALID_DOCUMENT ;
90
+ }
91
+ }
92
+ }
93
+
68
94
@ Override
69
95
public void add (MutableDocument document , SnapshotVersion readTime ) {
70
96
hardAssert (
@@ -77,12 +103,13 @@ public void add(MutableDocument document, SnapshotVersion readTime) {
77
103
78
104
db .execute (
79
105
"INSERT OR REPLACE INTO remote_documents "
80
- + "(path, path_length, read_time_seconds, read_time_nanos, contents) "
81
- + "VALUES (?, ?, ?, ?, ?)" ,
106
+ + "(path, path_length, read_time_seconds, read_time_nanos, document_type, contents) "
107
+ + "VALUES (?, ?, ?, ?, ?, ? )" ,
82
108
EncodedPath .encode (documentKey .getPath ()),
83
109
documentKey .getPath ().length (),
84
110
timestamp .getSeconds (),
85
111
timestamp .getNanoseconds (),
112
+ DocumentType .forMutableDocument (document ).dbValue ,
86
113
message .toByteArray ());
87
114
88
115
indexManager .addToCollectionParentIndex (document .getKey ().getCollectionPath ());
@@ -182,6 +209,7 @@ private Map<DocumentKey, MutableDocument> getAll(
182
209
List <ResourcePath > collections ,
183
210
IndexOffset offset ,
184
211
int count ,
212
+ @ Nullable DocumentType filterDocumentType ,
185
213
@ Nullable Function <MutableDocument , Boolean > filter ,
186
214
@ Nullable QueryContext context ) {
187
215
Timestamp readTime = offset .getReadTime ().getTimestamp ();
@@ -192,20 +220,24 @@ private Map<DocumentKey, MutableDocument> getAll(
192
220
"SELECT contents, read_time_seconds, read_time_nanos, path "
193
221
+ "FROM remote_documents "
194
222
+ "WHERE path >= ? AND path < ? AND path_length = ? "
223
+ + (filterDocumentType == null ? "" : " AND document_type = ? " )
195
224
+ "AND (read_time_seconds > ? OR ( "
196
225
+ "read_time_seconds = ? AND read_time_nanos > ?) OR ( "
197
226
+ "read_time_seconds = ? AND read_time_nanos = ? and path > ?)) " ,
198
227
collections .size (),
199
228
" UNION " );
200
229
sql .append ("ORDER BY read_time_seconds, read_time_nanos, path LIMIT ?" );
201
230
202
- Object [] bindVars = new Object [BINDS_PER_STATEMENT * collections .size () + 1 ];
231
+ Object [] bindVars = new Object [BINDS_PER_STATEMENT * collections .size () + 1 + ( filterDocumentType != null ? 1 : 0 ) ];
203
232
int i = 0 ;
204
233
for (ResourcePath collection : collections ) {
205
234
String prefixPath = EncodedPath .encode (collection );
206
235
bindVars [i ++] = prefixPath ;
207
236
bindVars [i ++] = EncodedPath .prefixSuccessor (prefixPath );
208
237
bindVars [i ++] = collection .length () + 1 ;
238
+ if (filterDocumentType != null ) {
239
+ bindVars [i ++] = filterDocumentType .dbValue ;
240
+ }
209
241
bindVars [i ++] = readTime .getSeconds ();
210
242
bindVars [i ++] = readTime .getSeconds ();
211
243
bindVars [i ++] = readTime .getNanoseconds ();
@@ -235,7 +267,7 @@ private Map<DocumentKey, MutableDocument> getAll(
235
267
IndexOffset offset ,
236
268
int count ,
237
269
@ Nullable Function <MutableDocument , Boolean > filter ) {
238
- return getAll (collections , offset , count , filter , /*context*/ null );
270
+ return getAll (collections , offset , count , /*filterDocumentType*/ null , filter , /*context*/ null );
239
271
}
240
272
241
273
private void processRowInBackground (
@@ -278,6 +310,7 @@ public Map<DocumentKey, MutableDocument> getDocumentsMatchingQuery(
278
310
Collections .singletonList (query .getPath ()),
279
311
offset ,
280
312
Integer .MAX_VALUE ,
313
+ DocumentType .FOUND_DOCUMENT ,
281
314
(MutableDocument doc ) -> query .matches (doc ) || mutatedKeys .contains (doc .getKey ()),
282
315
context );
283
316
}
0 commit comments