Skip to content

fix latency-compensated query #3363

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

Merged
6 commits merged into from
Jul 16, 2019
Merged

fix latency-compensated query #3363

6 commits merged into from
Jul 16, 2019

Conversation

ghost
Copy link

@ghost ghost commented Jul 16, 2019

No description provided.

@ghost ghost added the api: firestore label Jul 16, 2019
@ghost ghost force-pushed the wuandy/FixLatencyCompPatchQuery branch from 649c8a0 to 47190a2 Compare July 16, 2019 14:21
@ghost ghost requested a review from var-const July 16, 2019 14:25
@ghost ghost assigned var-const Jul 16, 2019
@ghost ghost force-pushed the wuandy/FixLatencyCompPatchQuery branch from cb9c572 to aa85e8d Compare July 16, 2019 15:02
* also has every document in `existingDocs`.
*/
model::DocumentMap AddMissingBaseDocument(
const std::vector<FSTMutationBatch*>& matchingBatches,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: C++ uses snake_case for variable names (this can be a little confusing because Objective-C uses camelCase). We consider files under core/src to be C++ and files under Source to be Objective-C, so this file should follow C++ style rules. Some of the surrounding code gets this wrong (no need to fix anything you didn't add in this PR).

Applies throughout.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

* `existingDocs` add them to the returning `DocumentMap`. The returned map
* also has every document in `existingDocs`.
*/
model::DocumentMap AddMissingBaseDocument(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: pluralize the name (AddMissingBaseDocuments).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -215,6 +217,38 @@
return results;
}

// It is possible that a `PatchMutation` can make a document match a query, even
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: I'm not against adding a new comment for this function in the header, but you could simply move this comment to the header (replacing the current one) as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

MaybeDocumentMap missingDocs = remote_document_cache_->GetAll(missingDocKeys);
for (const auto& kv : missingDocs) {
if (kv.second != nil && [kv.second isKindOfClass:[FSTDocument class]]) {
mergedDocs = mergedDocs.insert(kv.first, (FSTDocument*)kv.second);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: please use static_cast to make casting more visible:

static_cast<FSTDocument*>(kv.second)

Note that C++ casts work fine on Objective-C classes (in Objective-C++ mode).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// if the version in the `RemoteDocumentCache` is not a match yet (waiting for
// server to ack). To handle this, we find all document keys affected by the
// `PatchMutation`s that are not in `existingDocs` yet, and back fill them via
// `remoteDocumentCache.getAll`, otherwise those `PatchMutation`s will be
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remote_document_cache.GetAll().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// for the query.
DocumentMap LocalDocumentsView::AddMissingBaseDocument(
const std::vector<FSTMutationBatch*>& matchingBatches,
const DocumentMap& existingDocs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function's signature mandates an unnecessary copy. Even though DocumentMap is relatively cheap to copy, it strikes me as an avoidable inefficiency. I'd suggest moving the map instead. Make the function take existing_docs by value:

DocumentMap existing_docs) {

and then move results into the function parameter at the call point:

results = AddMissingBaseDocuments(matching_batches, std::move(results));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
}

DocumentMap mergedDocs = existingDocs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In C++, this copy isn't entirely free. If you follow the suggestion above to make existing_docs a value, you will no longer need this copy, and I'd suggest you omit it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

DocumentMap mergedDocs = existingDocs;
MaybeDocumentMap missingDocs = remote_document_cache_->GetAll(missingDocKeys);
for (const auto& kv : missingDocs) {
if (kv.second != nil && [kv.second isKindOfClass:[FSTDocument class]]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: kv.second is repeated three times in this loop, consider adding a variable:

FSTMaybeDocument* maybe_doc = kv.second;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@var-const var-const assigned ghost and unassigned var-const Jul 16, 2019
@@ -1,6 +1,8 @@
# Unreleased

# 1.4.2
- [fixed] Fixed an issue where query results were temporarily missing documents
that previously had not matched but had been updated to now match the query.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's OK to refer to issues in other repos, like we've done below. In this case it would be https://github.com/firebase/firebase-android-sdk/issues/155.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@ghost ghost assigned var-const and unassigned ghost Jul 16, 2019
@@ -103,6 +103,18 @@ class LocalDocumentsView {
/** Queries the remote documents and overlays mutations. */
model::DocumentMap GetDocumentsMatchingCollectionQuery(FSTQuery* query);

/** It is possible that a `PatchMutation` can make a document match a query,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: in a multiline comment, /** should be on its own line:

/**
 * It is possible that a `PatchMutation` can make a document match a query,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@var-const var-const assigned ghost and unassigned var-const Jul 16, 2019
@ghost ghost merged commit f3396f5 into master Jul 16, 2019
@firebase firebase locked and limited conversation to collaborators Oct 11, 2019
@paulb777 paulb777 deleted the wuandy/FixLatencyCompPatchQuery branch July 8, 2020 23:58
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants