diff options
Diffstat (limited to 'Firestore/Source/Local')
-rw-r--r-- | Firestore/Source/Local/FSTLevelDBKey.mm | 25 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTLevelDBMutationQueue.mm | 12 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTLevelDBQueryCache.mm | 4 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm | 2 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTLocalDocumentsView.mm | 5 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTMemoryMutationQueue.mm | 18 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTMemoryRemoteDocumentCache.mm | 5 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTReferenceSet.mm | 4 |
8 files changed, 44 insertions, 31 deletions
diff --git a/Firestore/Source/Local/FSTLevelDBKey.mm b/Firestore/Source/Local/FSTLevelDBKey.mm index 41aea39..bd7b44a 100644 --- a/Firestore/Source/Local/FSTLevelDBKey.mm +++ b/Firestore/Source/Local/FSTLevelDBKey.mm @@ -17,12 +17,18 @@ #import "Firestore/Source/Local/FSTLevelDBKey.h" #include <string> +#include <utility> +#include <vector> #import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTPath.h" +#include "Firestore/core/src/firebase/firestore/model/resource_path.h" #include "Firestore/core/src/firebase/firestore/util/ordered_code.h" +namespace util = firebase::firestore::util; +using firebase::firestore::model::ResourcePath; + NS_ASSUME_NONNULL_BEGIN using firebase::firestore::util::OrderedCode; @@ -271,7 +277,7 @@ BOOL ReadDocumentKey(Slice *contents, FSTDocumentKey *__strong *result) { Slice completeSegments = *contents; std::string segment; - NSMutableArray<NSString *> *pathSegments = [NSMutableArray array]; + std::vector<std::string> path_segments{}; for (;;) { // Advance a temporary slice to avoid advancing contents into the next key component which may // not be a path segment. @@ -283,15 +289,14 @@ BOOL ReadDocumentKey(Slice *contents, FSTDocumentKey *__strong *result) { return NO; } - NSString *pathSegment = [[NSString alloc] initWithUTF8String:segment.c_str()]; - [pathSegments addObject:pathSegment]; + path_segments.push_back(std::move(segment)); segment.clear(); completeSegments = leveldb::Slice(readPosition.data(), readPosition.size()); } - FSTResourcePath *path = [FSTResourcePath pathWithSegments:pathSegments]; - if (path.length > 0 && [FSTDocumentKey isDocumentKey:path]) { + ResourcePath path{std::move(path_segments)}; + if (path.size() > 0 && [FSTDocumentKey isDocumentKey:path]) { *contents = completeSegments; *result = [FSTDocumentKey keyWithPath:path]; return YES; @@ -391,7 +396,7 @@ NSString *InvalidKey(const Slice &key) { if (!ReadDocumentKey(&tmp, &documentKey)) { break; } - [description appendFormat:@" key=%@", [documentKey.path description]]; + [description appendFormat:@" key=%s", documentKey.path.CanonicalString().c_str()]; } else if (label == FSTComponentLabelTableName) { std::string table; @@ -531,7 +536,7 @@ NSString *InvalidKey(const Slice &key) { std::string result; WriteTableName(&result, kDocumentMutationsTable); WriteUserID(&result, userID); - WriteResourcePath(&result, documentKey.path); + WriteResourcePath(&result, [FSTResourcePath resourcePathWithCPPResourcePath:documentKey.path]); WriteBatchID(&result, batchID); WriteTerminator(&result); return result; @@ -685,7 +690,7 @@ NSString *InvalidKey(const Slice &key) { std::string result; WriteTableName(&result, kTargetDocumentsTable); WriteTargetID(&result, targetID); - WriteResourcePath(&result, documentKey.path); + WriteResourcePath(&result, [FSTResourcePath resourcePathWithCPPResourcePath:documentKey.path]); WriteTerminator(&result); return result; } @@ -719,7 +724,7 @@ NSString *InvalidKey(const Slice &key) { + (std::string)keyWithDocumentKey:(FSTDocumentKey *)documentKey targetID:(FSTTargetID)targetID { std::string result; WriteTableName(&result, kDocumentTargetsTable); - WriteResourcePath(&result, documentKey.path); + WriteResourcePath(&result, [FSTResourcePath resourcePathWithCPPResourcePath:documentKey.path]); WriteTargetID(&result, targetID); WriteTerminator(&result); return result; @@ -754,7 +759,7 @@ NSString *InvalidKey(const Slice &key) { + (std::string)keyWithDocumentKey:(FSTDocumentKey *)key { std::string result; WriteTableName(&result, kRemoteDocumentsTable); - WriteResourcePath(&result, key.path); + WriteResourcePath(&result, [FSTResourcePath resourcePathWithCPPResourcePath:key.path]); WriteTerminator(&result); return result; } diff --git a/Firestore/Source/Local/FSTLevelDBMutationQueue.mm b/Firestore/Source/Local/FSTLevelDBMutationQueue.mm index 9041ddc..1f6484d 100644 --- a/Firestore/Source/Local/FSTLevelDBMutationQueue.mm +++ b/Firestore/Source/Local/FSTLevelDBMutationQueue.mm @@ -375,8 +375,9 @@ static ReadOptions StandardReadOptions() { NSString *userID = self.userID; // Scan the document-mutation index starting with a prefix starting with the given documentKey. - std::string indexPrefix = - [FSTLevelDBDocumentMutationKey keyPrefixWithUserID:self.userID resourcePath:documentKey.path]; + std::string indexPrefix = [FSTLevelDBDocumentMutationKey + keyPrefixWithUserID:self.userID + resourcePath:[FSTResourcePath resourcePathWithCPPResourcePath:documentKey.path]]; std::unique_ptr<Iterator> indexIterator(_db->NewIterator(StandardReadOptions())); indexIterator->Seek(indexPrefix); @@ -467,7 +468,7 @@ static ReadOptions StandardReadOptions() { // Rows with document keys more than one segment longer than the query path can't be matches. // For example, a query on 'rooms' can't match the document /rooms/abc/messages/xyx. // TODO(mcg): we'll need a different scanner when we implement ancestor queries. - if (rowKey.documentKey.path.length != immediateChildrenPathLength) { + if (rowKey.documentKey.path.size() != immediateChildrenPathLength) { continue; } @@ -614,8 +615,9 @@ static ReadOptions StandardReadOptions() { #pragma mark - FSTGarbageSource implementation - (BOOL)containsKey:(FSTDocumentKey *)documentKey { - std::string indexPrefix = - [FSTLevelDBDocumentMutationKey keyPrefixWithUserID:self.userID resourcePath:documentKey.path]; + std::string indexPrefix = [FSTLevelDBDocumentMutationKey + keyPrefixWithUserID:self.userID + resourcePath:[FSTResourcePath resourcePathWithCPPResourcePath:documentKey.path]]; std::unique_ptr<Iterator> indexIterator(_db->NewIterator(StandardReadOptions())); indexIterator->Seek(indexPrefix); diff --git a/Firestore/Source/Local/FSTLevelDBQueryCache.mm b/Firestore/Source/Local/FSTLevelDBQueryCache.mm index fe1bf19..dcbcee1 100644 --- a/Firestore/Source/Local/FSTLevelDBQueryCache.mm +++ b/Firestore/Source/Local/FSTLevelDBQueryCache.mm @@ -27,6 +27,7 @@ #import "Firestore/Source/Local/FSTQueryData.h" #import "Firestore/Source/Local/FSTWriteGroup.h" #import "Firestore/Source/Model/FSTDocumentKey.h" +#import "Firestore/Source/Model/FSTPath.h" #import "Firestore/Source/Util/FSTAssert.h" NS_ASSUME_NONNULL_BEGIN @@ -339,7 +340,8 @@ using leveldb::WriteOptions; #pragma mark - FSTGarbageSource implementation - (BOOL)containsKey:(FSTDocumentKey *)key { - std::string indexPrefix = [FSTLevelDBDocumentTargetKey keyPrefixWithResourcePath:key.path]; + std::string indexPrefix = [FSTLevelDBDocumentTargetKey + keyPrefixWithResourcePath:[FSTResourcePath resourcePathWithCPPResourcePath:key.path]]; std::unique_ptr<Iterator> indexIterator(_db->NewIterator([FSTLevelDB standardReadOptions])); indexIterator->Seek(indexPrefix); diff --git a/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm b/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm index 17ecb53..423912f 100644 --- a/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm +++ b/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm @@ -113,7 +113,7 @@ static ReadOptions StandardReadOptions() { for (; it->Valid() && [currentKey decodeKey:it->key()]; it->Next()) { FSTMaybeDocument *maybeDoc = [self decodedMaybeDocument:it->value() withKey:currentKey.documentKey]; - if (!query.path.IsPrefixOf([maybeDoc.key.path toCPPResourcePath])) { + if (!query.path.IsPrefixOf(maybeDoc.key.path)) { break; } else if ([maybeDoc isKindOfClass:[FSTDocument class]]) { results = [results dictionaryBySettingObject:(FSTDocument *)maybeDoc forKey:maybeDoc.key]; diff --git a/Firestore/Source/Local/FSTLocalDocumentsView.mm b/Firestore/Source/Local/FSTLocalDocumentsView.mm index 4bcdf47..0059e28 100644 --- a/Firestore/Source/Local/FSTLocalDocumentsView.mm +++ b/Firestore/Source/Local/FSTLocalDocumentsView.mm @@ -75,7 +75,7 @@ NS_ASSUME_NONNULL_BEGIN } - (FSTDocumentDictionary *)documentsMatchingQuery:(FSTQuery *)query { - if ([FSTDocumentKey isDocumentKey:[FSTResourcePath resourcePathWithCPPResourcePath:query.path]]) { + if ([FSTDocumentKey isDocumentKey:query.path]) { return [self documentsMatchingDocumentQuery:[FSTResourcePath resourcePathWithCPPResourcePath:query.path]]; } else { @@ -86,7 +86,8 @@ NS_ASSUME_NONNULL_BEGIN - (FSTDocumentDictionary *)documentsMatchingDocumentQuery:(FSTResourcePath *)docPath { FSTDocumentDictionary *result = [FSTDocumentDictionary documentDictionary]; // Just do a simple document lookup. - FSTMaybeDocument *doc = [self documentForKey:[FSTDocumentKey keyWithPath:docPath]]; + FSTMaybeDocument *doc = + [self documentForKey:[FSTDocumentKey keyWithPath:[docPath toCPPResourcePath]]]; if ([doc isKindOfClass:[FSTDocument class]]) { result = [result dictionaryBySettingObject:(FSTDocument *)doc forKey:doc.key]; } diff --git a/Firestore/Source/Local/FSTMemoryMutationQueue.mm b/Firestore/Source/Local/FSTMemoryMutationQueue.mm index bf4f600..5b2fca6 100644 --- a/Firestore/Source/Local/FSTMemoryMutationQueue.mm +++ b/Firestore/Source/Local/FSTMemoryMutationQueue.mm @@ -24,6 +24,10 @@ #import "Firestore/Source/Model/FSTPath.h" #import "Firestore/Source/Util/FSTAssert.h" +#include "Firestore/core/src/firebase/firestore/model/resource_path.h" + +using firebase::firestore::model::ResourcePath; + NS_ASSUME_NONNULL_BEGIN static const NSComparator NumberComparator = ^NSComparisonResult(NSNumber *left, NSNumber *right) { @@ -248,15 +252,15 @@ static const NSComparator NumberComparator = ^NSComparisonResult(NSNumber *left, - (NSArray<FSTMutationBatch *> *)allMutationBatchesAffectingQuery:(FSTQuery *)query { // Use the query path as a prefix for testing if a document matches the query. - FSTResourcePath *prefix = [FSTResourcePath resourcePathWithCPPResourcePath:query.path]; - int immediateChildrenPathLength = prefix.length + 1; + const ResourcePath &prefix = query.path; + size_t immediateChildrenPathLength = prefix.size() + 1; // Construct a document reference for actually scanning the index. Unlike the prefix, the document // key in this reference must have an even number of segments. The empty segment can be used as // a suffix of the query path because it precedes all other segments in an ordered traversal. - FSTResourcePath *startPath = [FSTResourcePath resourcePathWithCPPResourcePath:query.path]; + ResourcePath startPath = query.path; if (![FSTDocumentKey isDocumentKey:startPath]) { - startPath = [startPath pathByAppendingSegment:@""]; + startPath = startPath.Append(""); } FSTDocumentReference *start = [[FSTDocumentReference alloc] initWithKey:[FSTDocumentKey keyWithPath:startPath] ID:0]; @@ -265,8 +269,8 @@ static const NSComparator NumberComparator = ^NSComparisonResult(NSNumber *left, __block FSTImmutableSortedSet<NSNumber *> *uniqueBatchIDs = [FSTImmutableSortedSet setWithComparator:NumberComparator]; FSTDocumentReferenceBlock block = ^(FSTDocumentReference *reference, BOOL *stop) { - FSTResourcePath *rowKeyPath = reference.key.path; - if (![prefix isPrefixOfPath:rowKeyPath]) { + const ResourcePath &rowKeyPath = reference.key.path; + if (!prefix.IsPrefixOf(rowKeyPath)) { *stop = YES; return; } @@ -274,7 +278,7 @@ static const NSComparator NumberComparator = ^NSComparisonResult(NSNumber *left, // Rows with document keys more than one segment longer than the query path can't be matches. // For example, a query on 'rooms' can't match the document /rooms/abc/messages/xyx. // TODO(mcg): we'll need a different scanner when we implement ancestor queries. - if (rowKeyPath.length != immediateChildrenPathLength) { + if (rowKeyPath.size() != immediateChildrenPathLength) { return; } diff --git a/Firestore/Source/Local/FSTMemoryRemoteDocumentCache.mm b/Firestore/Source/Local/FSTMemoryRemoteDocumentCache.mm index 7d5e1e2..b0d6807 100644 --- a/Firestore/Source/Local/FSTMemoryRemoteDocumentCache.mm +++ b/Firestore/Source/Local/FSTMemoryRemoteDocumentCache.mm @@ -60,11 +60,10 @@ NS_ASSUME_NONNULL_BEGIN // Documents are ordered by key, so we can use a prefix scan to narrow down the documents // we need to match the query against. - FSTDocumentKey *prefix = [FSTDocumentKey - keyWithPath:[FSTResourcePath resourcePathWithCPPResourcePath:query.path.Append("")]]; + FSTDocumentKey *prefix = [FSTDocumentKey keyWithPath:query.path.Append("")]; NSEnumerator<FSTDocumentKey *> *enumerator = [self.docs keyEnumeratorFrom:prefix]; for (FSTDocumentKey *key in enumerator) { - if (!query.path.IsPrefixOf([key.path toCPPResourcePath])) { + if (!query.path.IsPrefixOf(key.path)) { break; } FSTMaybeDocument *maybeDoc = self.docs[key]; diff --git a/Firestore/Source/Local/FSTReferenceSet.mm b/Firestore/Source/Local/FSTReferenceSet.mm index 2acd64b..15bb033 100644 --- a/Firestore/Source/Local/FSTReferenceSet.mm +++ b/Firestore/Source/Local/FSTReferenceSet.mm @@ -82,7 +82,7 @@ NS_ASSUME_NONNULL_BEGIN } - (void)removeReferencesForID:(int)ID { - FSTDocumentKey *emptyKey = [FSTDocumentKey keyWithSegments:@[]]; + FSTDocumentKey *emptyKey = [FSTDocumentKey keyWithSegments:{}]; FSTDocumentReference *start = [[FSTDocumentReference alloc] initWithKey:emptyKey ID:ID]; FSTDocumentReference *end = [[FSTDocumentReference alloc] initWithKey:emptyKey ID:(ID + 1)]; @@ -106,7 +106,7 @@ NS_ASSUME_NONNULL_BEGIN } - (FSTDocumentKeySet *)referencedKeysForID:(int)ID { - FSTDocumentKey *emptyKey = [FSTDocumentKey keyWithSegments:@[]]; + FSTDocumentKey *emptyKey = [FSTDocumentKey keyWithSegments:{}]; FSTDocumentReference *start = [[FSTDocumentReference alloc] initWithKey:emptyKey ID:ID]; FSTDocumentReference *end = [[FSTDocumentReference alloc] initWithKey:emptyKey ID:(ID + 1)]; |