aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm
diff options
context:
space:
mode:
Diffstat (limited to 'Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm')
-rw-r--r--Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm11
1 files changed, 8 insertions, 3 deletions
diff --git a/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm b/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm
index b2df67f..02f9f3e 100644
--- a/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm
+++ b/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm
@@ -21,6 +21,7 @@
#include <string>
#import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h"
+#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Local/FSTLevelDBKey.h"
#import "Firestore/Source/Local/FSTLocalSerializer.h"
#import "Firestore/Source/Local/FSTWriteGroup.h"
@@ -28,6 +29,7 @@
#import "Firestore/Source/Model/FSTDocumentDictionary.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Model/FSTDocumentSet.h"
+#import "Firestore/Source/Model/FSTPath.h"
#import "Firestore/Source/Util/FSTAssert.h"
#include "Firestore/Port/ordered_code.h"
@@ -102,10 +104,11 @@ static ReadOptions StandardReadOptions() {
}
- (FSTDocumentDictionary *)documentsMatchingQuery:(FSTQuery *)query {
- // TODO(mikelehen): PERF: At least filter to the documents that match the path of the query.
FSTDocumentDictionary *results = [FSTDocumentDictionary documentDictionary];
- std::string startKey = [FSTLevelDBRemoteDocumentKey keyPrefix];
+ // Documents are ordered by key, so we can use a prefix scan to narrow down
+ // the documents we need to match the query against.
+ std::string startKey = [FSTLevelDBRemoteDocumentKey keyPrefixWithResourcePath:query.path];
std::unique_ptr<Iterator> it(_db->NewIterator(StandardReadOptions()));
it->Seek(startKey);
@@ -113,7 +116,9 @@ static ReadOptions StandardReadOptions() {
for (; it->Valid() && [currentKey decodeKey:it->key()]; it->Next()) {
FSTMaybeDocument *maybeDoc =
[self decodedMaybeDocument:it->value() withKey:currentKey.documentKey];
- if ([maybeDoc isKindOfClass:[FSTDocument class]]) {
+ if (![query.path isPrefixOfPath:maybeDoc.key.path]) {
+ break;
+ } else if ([maybeDoc isKindOfClass:[FSTDocument class]]) {
results = [results dictionaryBySettingObject:(FSTDocument *)maybeDoc forKey:maybeDoc.key];
}
}