From 602a3365ec7627b520959e5b2be3ad470abf2c16 Mon Sep 17 00:00:00 2001 From: Marek Gilbert Date: Sun, 24 Mar 2019 00:30:38 -0700 Subject: [PATCH 1/2] Add nanopb::CheckedSize Fix implicit narrowing warnings and reduce pb_size_t copypasta. --- .../firestore/local/local_serializer.cc | 14 +++---- .../firestore/nanopb/nanopb_string.cc | 4 +- .../firebase/firestore/nanopb/nanopb_util.h | 42 +++++++++++++++++++ .../firebase/firestore/remote/serializer.cc | 31 +++++++------- .../firebase/firestore/remote/serializer.h | 2 +- 5 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h diff --git a/Firestore/core/src/firebase/firestore/local/local_serializer.cc b/Firestore/core/src/firebase/firestore/local/local_serializer.cc index 7f252969186..2b7944f09b3 100644 --- a/Firestore/core/src/firebase/firestore/local/local_serializer.cc +++ b/Firestore/core/src/firebase/firestore/local/local_serializer.cc @@ -29,6 +29,7 @@ #include "Firestore/core/src/firebase/firestore/model/no_document.h" #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h" #include "Firestore/core/src/firebase/firestore/model/unknown_document.h" +#include "Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h" #include "Firestore/core/src/firebase/firestore/util/hard_assert.h" #include "Firestore/core/src/firebase/firestore/util/string_format.h" @@ -44,6 +45,7 @@ using model::MutationBatch; using model::NoDocument; using model::SnapshotVersion; using model::UnknownDocument; +using nanopb::CheckedSize; using nanopb::Reader; using nanopb::Writer; using remote::MakeArray; @@ -123,10 +125,8 @@ google_firestore_v1_Document LocalSerializer::EncodeDocument( rpc_serializer_.EncodeString(rpc_serializer_.EncodeKey(doc.key())); // Encode Document.fields (unless it's empty) - size_t count = doc.data().GetInternalValue().size(); - HARD_ASSERT(count <= std::numeric_limits::max(), - "Unable to encode specified document. Too many fields."); - result.fields_count = static_cast(count); + pb_size_t count = CheckedSize(doc.data().GetInternalValue().size()); + result.fields_count = count; result.fields = MakeArray(count); int i = 0; for (const auto& kv : doc.data().GetInternalValue()) { @@ -257,10 +257,8 @@ firestore_client_WriteBatch LocalSerializer::EncodeMutationBatch( firestore_client_WriteBatch result{}; result.batch_id = mutation_batch.batch_id(); - size_t count = mutation_batch.mutations().size(); - HARD_ASSERT(count <= std::numeric_limits::max(), - "Unable to encode specified mutation batch. Too many mutations."); - result.writes_count = static_cast(count); + pb_size_t count = CheckedSize(mutation_batch.mutations().size()); + result.writes_count = count; result.writes = MakeArray(count); int i = 0; for (const std::unique_ptr& mutation : mutation_batch.mutations()) { diff --git a/Firestore/core/src/firebase/firestore/nanopb/nanopb_string.cc b/Firestore/core/src/firebase/firestore/nanopb/nanopb_string.cc index 98151c1561b..90850021e76 100644 --- a/Firestore/core/src/firebase/firestore/nanopb/nanopb_string.cc +++ b/Firestore/core/src/firebase/firestore/nanopb/nanopb_string.cc @@ -19,6 +19,8 @@ #include #include +#include "Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h" + namespace firebase { namespace firestore { namespace nanopb { @@ -28,7 +30,7 @@ String::~String() { } /* static */ pb_bytes_array_t* String::MakeBytesArray(absl::string_view value) { - auto size = static_cast(value.size()); + pb_size_t size = CheckedSize(value.size()); // Allocate one extra byte for the null terminator that's not necessarily // there in a string_view. As long as we're making a copy, might as well diff --git a/Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h b/Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h new file mode 100644 index 00000000000..667c28a6b6d --- /dev/null +++ b/Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h @@ -0,0 +1,42 @@ +/* + * Copyright 2019 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_NANOPB_NANOPB_UTIL_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_NANOPB_NANOPB_UTIL_H_ + +#include + +#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" + +namespace firebase { +namespace firestore { +namespace nanopb { + +/** + * Static casts the given size_t value down to a nanopb compatible size, after + * asserting that the value isn't out of range. + */ +inline pb_size_t CheckedSize(size_t size) { + HARD_ASSERT(size <= PB_SIZE_MAX, + "Size exceeds nanopb limits. Too many entries."); + return static_cast(size); +} + +} // namespace nanopb +} // namespace firestore +} // namespace firebase + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_NANOPB_NANOPB_UTIL_H_ diff --git a/Firestore/core/src/firebase/firestore/remote/serializer.cc b/Firestore/core/src/firebase/firestore/remote/serializer.cc index 9c0c008caea..cd0b9a15c4f 100644 --- a/Firestore/core/src/firebase/firestore/remote/serializer.cc +++ b/Firestore/core/src/firebase/firestore/remote/serializer.cc @@ -35,6 +35,7 @@ #include "Firestore/core/src/firebase/firestore/model/field_value.h" #include "Firestore/core/src/firebase/firestore/model/no_document.h" #include "Firestore/core/src/firebase/firestore/model/resource_path.h" +#include "Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h" #include "Firestore/core/src/firebase/firestore/nanopb/reader.h" #include "Firestore/core/src/firebase/firestore/nanopb/writer.h" #include "Firestore/core/src/firebase/firestore/timestamp_internal.h" @@ -67,13 +68,14 @@ using firebase::firestore::model::Precondition; using firebase::firestore::model::ResourcePath; using firebase::firestore::model::SetMutation; using firebase::firestore::model::SnapshotVersion; +using firebase::firestore::nanopb::CheckedSize; using firebase::firestore::nanopb::Reader; using firebase::firestore::nanopb::Writer; using firebase::firestore::util::Status; using firebase::firestore::util::StringFormat; pb_bytes_array_t* Serializer::EncodeString(const std::string& str) { - auto size = static_cast(str.size()); + pb_size_t size = CheckedSize(str.size()); auto result = static_cast(malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(size))); result->size = size; @@ -83,11 +85,12 @@ pb_bytes_array_t* Serializer::EncodeString(const std::string& str) { std::string Serializer::DecodeString(const pb_bytes_array_t* str) { if (str == nullptr) return ""; - return std::string{reinterpret_cast(str->bytes), str->size}; + size_t size = static_cast(str->size); + return std::string{reinterpret_cast(str->bytes), size}; } pb_bytes_array_t* Serializer::EncodeBytes(const std::vector& bytes) { - auto size = static_cast(bytes.size()); + pb_size_t size = CheckedSize(bytes.size()); auto result = static_cast(malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(size))); result->size = size; @@ -150,9 +153,9 @@ FieldValue::Map DecodeFields( google_firestore_v1_MapValue EncodeMapValue(const ObjectValue& object_value) { google_firestore_v1_MapValue result{}; - size_t count = object_value.GetInternalValue().size(); + pb_size_t count = CheckedSize(object_value.GetInternalValue().size()); - result.fields_count = static_cast(count); + result.fields_count = count; result.fields = MakeArray(count); int i = 0; @@ -450,8 +453,8 @@ google_firestore_v1_Document Serializer::EncodeDocument( result.name = EncodeString(EncodeKey(key)); // Encode Document.fields (unless it's empty) - size_t count = object_value.GetInternalValue().size(); - result.fields_count = static_cast(count); + pb_size_t count = CheckedSize(object_value.GetInternalValue().size()); + result.fields_count = count; result.fields = MakeArray(count); int i = 0; for (const auto& kv : object_value.GetInternalValue()) { @@ -701,10 +704,8 @@ google_firestore_v1_DocumentMask Serializer::EncodeDocumentMask( const FieldMask& mask) { google_firestore_v1_DocumentMask result{}; - size_t count = mask.size(); - HARD_ASSERT(count <= PB_SIZE_MAX, - "Unable to encode specified document mask. Too many fields."); - result.field_paths_count = static_cast(count); + pb_size_t count = CheckedSize(mask.size()); + result.field_paths_count = count; result.field_paths = MakeArray(count); int i = 0; @@ -748,8 +749,8 @@ google_firestore_v1_Target_QueryTarget Serializer::EncodeQueryTarget( google_firestore_v1_Target_QueryTarget_structured_query_tag; if (!collection_id.empty()) { - size_t count = 1; - result.structured_query.from_count = static_cast(count); + pb_size_t count = 1; + result.structured_query.from_count = count; result.structured_query.from = MakeArray( count); @@ -894,9 +895,7 @@ google_firestore_v1_ArrayValue Serializer::EncodeArray( const std::vector& array_value) { google_firestore_v1_ArrayValue result{}; - size_t count = array_value.size(); - HARD_ASSERT(count <= PB_SIZE_MAX, - "Unable to encode specified array. Too many entries."); + pb_size_t count = CheckedSize(array_value.size()); result.values_count = count; result.values = MakeArray(count); diff --git a/Firestore/core/src/firebase/firestore/remote/serializer.h b/Firestore/core/src/firebase/firestore/remote/serializer.h index df8c2a12ddc..ae011b3defa 100644 --- a/Firestore/core/src/firebase/firestore/remote/serializer.h +++ b/Firestore/core/src/firebase/firestore/remote/serializer.h @@ -53,7 +53,7 @@ class LocalSerializer; namespace remote { template -T* MakeArray(size_t count) { +T* MakeArray(pb_size_t count) { return reinterpret_cast(calloc(count, sizeof(T))); } From 519d51b03741d00550ac89f2529f0cbfe80d8378 Mon Sep 17 00:00:00 2001 From: Marek Gilbert Date: Sun, 24 Mar 2019 00:41:22 -0700 Subject: [PATCH 2/2] Rename local::DocumentReference to DocumentKeyReference This cleans up a build-time warning about a duplicate file in the Copy Headers phase and cleans up a massive duplicate id warning during pod install. --- .../firebase/firestore/local/CMakeLists.txt | 4 +-- ...reference.cc => document_key_reference.cc} | 20 +++++++------ ...t_reference.h => document_key_reference.h} | 28 +++++++++---------- .../firestore/local/memory_mutation_queue.h | 8 +++--- .../firestore/local/memory_mutation_queue.mm | 14 +++++----- .../firebase/firestore/local/reference_set.cc | 21 +++++++------- .../firebase/firestore/local/reference_set.h | 11 ++++---- 7 files changed, 55 insertions(+), 51 deletions(-) rename Firestore/core/src/firebase/firestore/local/{document_reference.cc => document_key_reference.cc} (74%) rename Firestore/core/src/firebase/firestore/local/{document_reference.h => document_key_reference.h} (74%) diff --git a/Firestore/core/src/firebase/firestore/local/CMakeLists.txt b/Firestore/core/src/firebase/firestore/local/CMakeLists.txt index 82c6e9af325..dbffa9e46c7 100644 --- a/Firestore/core/src/firebase/firestore/local/CMakeLists.txt +++ b/Firestore/core/src/firebase/firestore/local/CMakeLists.txt @@ -54,8 +54,8 @@ endif() cc_library( firebase_firestore_local SOURCES - document_reference.h - document_reference.cc + document_key_reference.h + document_key_reference.cc index_manager.h listen_sequence.h local_documents_view.h diff --git a/Firestore/core/src/firebase/firestore/local/document_reference.cc b/Firestore/core/src/firebase/firestore/local/document_key_reference.cc similarity index 74% rename from Firestore/core/src/firebase/firestore/local/document_reference.cc rename to Firestore/core/src/firebase/firestore/local/document_key_reference.cc index e3f1794ed18..61c1b718456 100644 --- a/Firestore/core/src/firebase/firestore/local/document_reference.cc +++ b/Firestore/core/src/firebase/firestore/local/document_key_reference.cc @@ -14,10 +14,11 @@ * limitations under the License. */ +#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h" + #include #include -#include "Firestore/core/src/firebase/firestore/local/document_reference.h" #include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/util/comparison.h" #include "Firestore/core/src/firebase/firestore/util/hashing.h" @@ -29,22 +30,23 @@ namespace local { using model::DocumentKey; using util::ComparisonResult; -bool operator==(const DocumentReference& lhs, const DocumentReference& rhs) { +bool operator==(const DocumentKeyReference& lhs, + const DocumentKeyReference& rhs) { return lhs.key_ == rhs.key_ && lhs.ref_id_ == rhs.ref_id_; } -size_t DocumentReference::Hash() const { +size_t DocumentKeyReference::Hash() const { return util::Hash(key_.ToString(), ref_id_); } -std::string DocumentReference::ToString() const { - return util::StringFormat("", +std::string DocumentKeyReference::ToString() const { + return util::StringFormat("", key_.ToString(), ref_id_); } /** Sorts document references by key then ID. */ -bool DocumentReference::ByKey::operator()(const DocumentReference& lhs, - const DocumentReference& rhs) const { +bool DocumentKeyReference::ByKey::operator()( + const DocumentKeyReference& lhs, const DocumentKeyReference& rhs) const { util::Comparator key_less; if (key_less(lhs.key_, rhs.key_)) return true; if (key_less(rhs.key_, lhs.key_)) return false; @@ -54,8 +56,8 @@ bool DocumentReference::ByKey::operator()(const DocumentReference& lhs, } /** Sorts document references by ID then key. */ -bool DocumentReference::ById::operator()(const DocumentReference& lhs, - const DocumentReference& rhs) const { +bool DocumentKeyReference::ById::operator()( + const DocumentKeyReference& lhs, const DocumentKeyReference& rhs) const { util::Comparator id_less; if (id_less(lhs.ref_id_, rhs.ref_id_)) return true; if (id_less(rhs.ref_id_, lhs.ref_id_)) return false; diff --git a/Firestore/core/src/firebase/firestore/local/document_reference.h b/Firestore/core/src/firebase/firestore/local/document_key_reference.h similarity index 74% rename from Firestore/core/src/firebase/firestore/local/document_reference.h rename to Firestore/core/src/firebase/firestore/local/document_key_reference.h index 3160ca42788..257971b5b2d 100644 --- a/Firestore/core/src/firebase/firestore/local/document_reference.h +++ b/Firestore/core/src/firebase/firestore/local/document_key_reference.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_REFERENCE_H_ -#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_REFERENCE_H_ +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_KEY_REFERENCE_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_KEY_REFERENCE_H_ #include #include @@ -39,13 +39,13 @@ namespace local { * * Not to be confused with FIRDocumentReference. */ -class DocumentReference { +class DocumentKeyReference { public: - DocumentReference() { + DocumentKeyReference() { } /** Initializes the document reference with the given key and Id. */ - DocumentReference(model::DocumentKey key, int32_t ref_id) + DocumentKeyReference(model::DocumentKey key, int32_t ref_id) : key_{std::move(key)}, ref_id_{ref_id} { } @@ -63,23 +63,23 @@ class DocumentReference { return ref_id_; } - friend bool operator==(const DocumentReference& lhs, - const DocumentReference& rhs); + friend bool operator==(const DocumentKeyReference& lhs, + const DocumentKeyReference& rhs); size_t Hash() const; std::string ToString() const; /** Sorts document references by key then Id. */ - struct ByKey : public util::Comparator { - bool operator()(const DocumentReference& lhs, - const DocumentReference& rhs) const; + struct ByKey : public util::Comparator { + bool operator()(const DocumentKeyReference& lhs, + const DocumentKeyReference& rhs) const; }; /** Sorts document references by Id then key. */ - struct ById : public util::Comparator { - bool operator()(const DocumentReference& lhs, - const DocumentReference& rhs) const; + struct ById : public util::Comparator { + bool operator()(const DocumentKeyReference& lhs, + const DocumentKeyReference& rhs) const; }; private: @@ -94,4 +94,4 @@ class DocumentReference { } // namespace firestore } // namespace firebase -#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_REFERENCE_H_ +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_KEY_REFERENCE_H_ diff --git a/Firestore/core/src/firebase/firestore/local/memory_mutation_queue.h b/Firestore/core/src/firebase/firestore/local/memory_mutation_queue.h index 463439e9331..242b92fe718 100644 --- a/Firestore/core/src/firebase/firestore/local/memory_mutation_queue.h +++ b/Firestore/core/src/firebase/firestore/local/memory_mutation_queue.h @@ -29,7 +29,7 @@ #import "Firestore/Source/Public/FIRTimestamp.h" #include "Firestore/core/src/firebase/firestore/immutable/sorted_set.h" -#include "Firestore/core/src/firebase/firestore/local/document_reference.h" +#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h" #include "Firestore/core/src/firebase/firestore/local/mutation_queue.h" #include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/document_key_set.h" @@ -94,8 +94,8 @@ class MemoryMutationQueue : public MutationQueue { void SetLastStreamToken(NSData* _Nullable token) override; private: - using DocumentReferenceSet = - immutable::SortedSet; + using DocumentKeyReferenceSet = + immutable::SortedSet; std::vector AllMutationBatchesWithIds( const std::set& batch_ids); @@ -146,7 +146,7 @@ class MemoryMutationQueue : public MutationQueue { NSData* _Nullable last_stream_token_; /** An ordered mapping between documents and the mutation batch IDs. */ - DocumentReferenceSet batches_by_document_key_; + DocumentKeyReferenceSet batches_by_document_key_; }; } // namespace local diff --git a/Firestore/core/src/firebase/firestore/local/memory_mutation_queue.mm b/Firestore/core/src/firebase/firestore/local/memory_mutation_queue.mm index 52ff632d808..aace5e5ce3c 100644 --- a/Firestore/core/src/firebase/firestore/local/memory_mutation_queue.mm +++ b/Firestore/core/src/firebase/firestore/local/memory_mutation_queue.mm @@ -25,7 +25,7 @@ #import "Firestore/Source/Model/FSTMutation.h" #import "Firestore/Source/Model/FSTMutationBatch.h" -#include "Firestore/core/src/firebase/firestore/local/document_reference.h" +#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h" #include "Firestore/core/src/firebase/firestore/model/resource_path.h" #include "Firestore/core/src/firebase/firestore/util/hard_assert.h" @@ -98,7 +98,7 @@ // Track references by document key and index collection parents. for (FSTMutation* mutation : [batch mutations]) { batches_by_document_key_ = batches_by_document_key_.insert( - DocumentReference{mutation.key, batch_id}); + DocumentKeyReference{mutation.key, batch_id}); persistence_.indexManager->AddToCollectionParentIndex( mutation.key.path().PopLast()); @@ -121,7 +121,7 @@ const DocumentKey& key = mutation.key; [persistence_.referenceDelegate removeMutationReference:key]; - DocumentReference reference{key, batch.batchID}; + DocumentKeyReference reference{key, batch.batchID}; batches_by_document_key_ = batches_by_document_key_.erase(reference); } } @@ -132,7 +132,7 @@ // First find the set of affected batch IDs. std::set batch_ids; for (const DocumentKey& key : document_keys) { - DocumentReference start{key, 0}; + DocumentKeyReference start{key, 0}; for (const auto& reference : batches_by_document_key_.values_from(start)) { if (key != reference.key()) break; @@ -149,7 +149,7 @@ const DocumentKey& key) { std::vector result; - DocumentReference start{key, 0}; + DocumentKeyReference start{key, 0}; for (const auto& reference : batches_by_document_key_.values_from(start)) { if (key != reference.key()) break; @@ -178,7 +178,7 @@ if (!DocumentKey::IsDocumentKey(start_path)) { start_path = start_path.Append(""); } - DocumentReference start{DocumentKey{start_path}, 0}; + DocumentKeyReference start{DocumentKey{start_path}, 0}; // Find unique batchIDs referenced by all documents potentially matching the // query. @@ -242,7 +242,7 @@ bool MemoryMutationQueue::ContainsKey(const model::DocumentKey& key) { // Create a reference with a zero ID as the start position to find any // document reference with this key. - DocumentReference reference{key, 0}; + DocumentKeyReference reference{key, 0}; auto range = batches_by_document_key_.values_from(reference); auto begin = range.begin(); return begin != range.end() && begin->key() == key; diff --git a/Firestore/core/src/firebase/firestore/local/reference_set.cc b/Firestore/core/src/firebase/firestore/local/reference_set.cc index 9529b1ffed7..57431d75c9f 100644 --- a/Firestore/core/src/firebase/firestore/local/reference_set.cc +++ b/Firestore/core/src/firebase/firestore/local/reference_set.cc @@ -15,8 +15,9 @@ */ #include "Firestore/core/src/firebase/firestore/local/reference_set.h" + #include "Firestore/core/src/firebase/firestore/immutable/sorted_set.h" -#include "Firestore/core/src/firebase/firestore/local/document_reference.h" +#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h" #include "Firestore/core/src/firebase/firestore/model/document_key.h" namespace firebase { @@ -27,7 +28,7 @@ using model::DocumentKey; using model::DocumentKeySet; void ReferenceSet::AddReference(const DocumentKey& key, int id) { - DocumentReference reference{key, id}; + DocumentKeyReference reference{key, id}; by_key_ = by_key_.insert(reference); by_id_ = by_id_.insert(reference); } @@ -39,7 +40,7 @@ void ReferenceSet::AddReferences(const DocumentKeySet& keys, int id) { } void ReferenceSet::RemoveReference(const DocumentKey& key, int id) { - RemoveReference(DocumentReference{key, id}); + RemoveReference(DocumentKeyReference{key, id}); } void ReferenceSet::RemoveReferences( @@ -50,8 +51,8 @@ void ReferenceSet::RemoveReferences( } DocumentKeySet ReferenceSet::RemoveReferences(int id) { - DocumentReference start{DocumentKey::Empty(), id}; - DocumentReference end{DocumentKey::Empty(), id + 1}; + DocumentKeyReference start{DocumentKey::Empty(), id}; + DocumentKeyReference end{DocumentKey::Empty(), id + 1}; DocumentKeySet removed{}; @@ -65,19 +66,19 @@ DocumentKeySet ReferenceSet::RemoveReferences(int id) { void ReferenceSet::RemoveAllReferences() { auto initial = by_key_; - for (const DocumentReference& reference : initial) { + for (const DocumentKeyReference& reference : initial) { RemoveReference(reference); } } -void ReferenceSet::RemoveReference(const DocumentReference& reference) { +void ReferenceSet::RemoveReference(const DocumentKeyReference& reference) { by_key_ = by_key_.erase(reference); by_id_ = by_id_.erase(reference); } DocumentKeySet ReferenceSet::ReferencedKeys(int id) { - DocumentReference start{DocumentKey::Empty(), id}; - DocumentReference end{DocumentKey::Empty(), id + 1}; + DocumentKeyReference start{DocumentKey::Empty(), id}; + DocumentKeyReference end{DocumentKey::Empty(), id + 1}; DocumentKeySet keys; for (const auto& reference : by_id_.values_in(start, end)) { @@ -89,7 +90,7 @@ DocumentKeySet ReferenceSet::ReferencedKeys(int id) { bool ReferenceSet::ContainsKey(const DocumentKey& key) { // Create a reference with a zero ID as the start position to find any // document reference with this key. - DocumentReference start{key, 0}; + DocumentKeyReference start{key, 0}; auto range = by_key_.values_from(start); auto begin = range.begin(); diff --git a/Firestore/core/src/firebase/firestore/local/reference_set.h b/Firestore/core/src/firebase/firestore/local/reference_set.h index 58677e200ff..0e14c998bbd 100644 --- a/Firestore/core/src/firebase/firestore/local/reference_set.h +++ b/Firestore/core/src/firebase/firestore/local/reference_set.h @@ -18,7 +18,7 @@ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_REFERENCE_SET_H_ #include "Firestore/core/src/firebase/firestore/immutable/sorted_set.h" -#include "Firestore/core/src/firebase/firestore/local/document_reference.h" +#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h" #include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/document_key_set.h" @@ -31,7 +31,7 @@ namespace local { * (either a TargetId or BatchId). As references are added to or removed from * the set corresponding events are emitted to a registered garbage collector. * - * Each reference is represented by a DocumentReference object. Each of them + * Each reference is represented by a DocumentKeyReference object. Each of them * contains enough information to uniquely identify the reference. They are all * stored primarily in a set sorted by key. A document is considered garbage if * there's no references in that set (this can be efficiently checked thanks to @@ -81,10 +81,11 @@ class ReferenceSet { bool ContainsKey(const model::DocumentKey& key); private: - void RemoveReference(const DocumentReference& reference); + void RemoveReference(const DocumentKeyReference& reference); - immutable::SortedSet by_key_; - immutable::SortedSet by_id_; + immutable::SortedSet + by_key_; + immutable::SortedSet by_id_; }; } // namespace local