-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add allMutationsAffectingDocumentKeys to FSTMutationQueue #1479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,6 @@ | |
|
||
#include "Firestore/core/src/firebase/firestore/auth/user.h" | ||
#include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h" | ||
#include "Firestore/core/src/firebase/firestore/model/document_key.h" | ||
#include "Firestore/core/src/firebase/firestore/model/resource_path.h" | ||
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" | ||
#include "Firestore/core/src/firebase/firestore/util/string_apple.h" | ||
|
@@ -46,6 +45,7 @@ | |
using Firestore::StringView; | ||
using firebase::firestore::auth::User; | ||
using firebase::firestore::model::DocumentKey; | ||
using firebase::firestore::model::DocumentKeySet; | ||
using firebase::firestore::model::ResourcePath; | ||
using leveldb::DB; | ||
using leveldb::Iterator; | ||
|
@@ -396,6 +396,39 @@ - (nullable FSTMutationBatch *)nextMutationBatchAfterBatchID:(FSTBatchID)batchID | |
return result; | ||
} | ||
|
||
- (NSArray<FSTMutationBatch *> *)allMutationBatchesAffectingDocumentKeys: | ||
(const DocumentKeySet &)documentKeys { | ||
NSString *userID = self.userID; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: is storing this value in a variable intended as an optimization or readability improvement? In the latter case, I think moving it inline would be slightly better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a minor optimization. This is done fairly consistently throughout so I prefer to leave it as is. As we rewrite to C++ this will become an instance variable reference. |
||
|
||
// Take a pass through the document keys and collect the set of unique mutation batchIDs that | ||
// affect them all. Some batches can affect more than one key. | ||
std::set<FSTBatchID> batchIDs; | ||
|
||
auto indexIterator = _db.currentTransaction->NewIterator(); | ||
FSTLevelDBDocumentMutationKey *rowKey = [[FSTLevelDBDocumentMutationKey alloc] init]; | ||
for (const DocumentKey &documentKey : documentKeys) { | ||
std::string indexPrefix = | ||
[FSTLevelDBDocumentMutationKey keyPrefixWithUserID:userID resourcePath:documentKey.path()]; | ||
indexIterator->Seek(indexPrefix); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very optional nit: I think you can move this expression to the initialization of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
for (; indexIterator->Valid(); indexIterator->Next()) { | ||
// Only consider rows matching exactly the specific key of interest. Note that because we | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: it's probably too much trouble, but I think a hypothetical example of how rows are laid out would make this comment easier to grasp and perhaps allow making it shorter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. This helped tighten the text too. |
||
// order by path first, and we order terminators before path separators, we'll encounter all | ||
// the index rows for documentKey contiguously. In particular, all the rows for documentKey | ||
// will occur before any rows for documents nested in a subcollection beneath documentKey so | ||
// we can stop as soon as we hit any such row. | ||
if (!absl::StartsWith(indexIterator->key(), indexPrefix) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There seems to be four variations of this check throughout the file. One could go away if the single-key version of this function were to be removed or rewritten to delegate to this function. Nevertheless, perhaps this check deserves its own named function? I can do this in a subsequent PR if you think it's worth it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The trouble is that these aren't all the same. In three out of four cases we examine other parts of the row key after decoding it. In one case we consult the size of the key rather than an exact match. I'm also not keen on hiding the decoding the key too much. The best way to solve this would probably be to add some mechanism for constructing the prefix including the marker that would start the next segment. That way we could do something like: if (![rowKey decodeKey:indexIterator->key() withExactPrefix:suffixedIndexPrefix]) {
break;
} This would effectively combine the cheap indexPrefix check we have now with the exact DocumentKey check. However, I'm struggling with what to call that thing, since we need both kinds: we need exact matches as we do in this case, but also any path matching the prefix as we need for Given everything we have to do I'm inclined to just leave it as is. I'd rather not obsess about this too much--we need to make progress on the larger goals we have and this, while verbose, avoids being magical. |
||
![rowKey decodeKey:indexIterator->key()] || | ||
DocumentKey{rowKey.documentKey} != documentKey) { | ||
break; | ||
} | ||
|
||
batchIDs.insert(rowKey.batchID); | ||
} | ||
} | ||
|
||
return [self allMutationBatchesWithBatchIDs:batchIDs]; | ||
} | ||
|
||
- (NSArray<FSTMutationBatch *> *)allMutationBatchesAffectingQuery:(FSTQuery *)query { | ||
HARD_ASSERT(![query isDocumentQuery], "Document queries shouldn't go down this path"); | ||
NSString *userID = self.userID; | ||
|
@@ -417,11 +450,10 @@ - (nullable FSTMutationBatch *)nextMutationBatchAfterBatchID:(FSTBatchID)batchID | |
// index for more than a single document so the associated batchIDs will be neither necessarily | ||
// unique nor in order. This means an efficient simultaneous scan isn't possible. | ||
std::string indexPrefix = | ||
[FSTLevelDBDocumentMutationKey keyPrefixWithUserID:self.userID resourcePath:queryPath]; | ||
[FSTLevelDBDocumentMutationKey keyPrefixWithUserID:userID resourcePath:queryPath]; | ||
auto indexIterator = _db.currentTransaction->NewIterator(); | ||
indexIterator->Seek(indexPrefix); | ||
|
||
NSMutableArray *result = [NSMutableArray array]; | ||
FSTLevelDBDocumentMutationKey *rowKey = [[FSTLevelDBDocumentMutationKey alloc] init]; | ||
|
||
// Collect up unique batchIDs encountered during a scan of the index. Use a set<FSTBatchID> to | ||
|
@@ -430,7 +462,7 @@ - (nullable FSTMutationBatch *)nextMutationBatchAfterBatchID:(FSTBatchID)batchID | |
// This method is faster than performing lookups of the keys with _db->Get and keeping a hash of | ||
// batchIDs that have already been looked up. The performance difference is minor for small | ||
// numbers of keys but > 30% faster for larger numbers of keys. | ||
std::set<FSTBatchID> uniqueBatchIds; | ||
std::set<FSTBatchID> uniqueBatchIDs; | ||
for (; indexIterator->Valid(); indexIterator->Next()) { | ||
if (!absl::StartsWith(indexIterator->key(), indexPrefix) || | ||
![rowKey decodeKey:indexIterator->key()]) { | ||
|
@@ -444,14 +476,25 @@ - (nullable FSTMutationBatch *)nextMutationBatchAfterBatchID:(FSTBatchID)batchID | |
continue; | ||
} | ||
|
||
uniqueBatchIds.insert(rowKey.batchID); | ||
uniqueBatchIDs.insert(rowKey.batchID); | ||
} | ||
|
||
return [self allMutationBatchesWithBatchIDs:uniqueBatchIDs]; | ||
} | ||
|
||
/** | ||
* Constructs an array of matching batches, sorted by batchID to ensure that multiple mutations | ||
* affecting the same document key are applied in order. | ||
*/ | ||
- (NSArray<FSTMutationBatch *> *)allMutationBatchesWithBatchIDs: | ||
(const std::set<FSTBatchID> &)batchIDs { | ||
NSMutableArray *result = [NSMutableArray array]; | ||
NSString *userID = self.userID; | ||
|
||
// Given an ordered set of unique batchIDs perform a skipping scan over the main table to find | ||
// the mutation batches. | ||
auto mutationIterator = _db.currentTransaction->NewIterator(); | ||
|
||
for (FSTBatchID batchID : uniqueBatchIds) { | ||
for (FSTBatchID batchID : batchIDs) { | ||
std::string mutationKey = [FSTLevelDBMutationKey keyWithUserID:userID batchID:batchID]; | ||
mutationIterator->Seek(mutationKey); | ||
if (!mutationIterator->Valid() || mutationIterator->key() != mutationKey) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ultranit: could you replace the
b
infob
with a letter that has less resemblance to ano
, say,x
ork
ory
? It took me three passes to finally spot the difference between this element and the next one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that. The idea is that this key precedes the others so I've chosen
i
.