document_set.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "Firestore/core/src/model/document_set.h"
  17. #include <ostream>
  18. #include <utility>
  19. #include "Firestore/core/src/immutable/sorted_set.h"
  20. #include "Firestore/core/src/model/document_key.h"
  21. #include "Firestore/core/src/util/hashing.h"
  22. #include "Firestore/core/src/util/to_string.h"
  23. #include "absl/algorithm/container.h"
  24. namespace firebase {
  25. namespace firestore {
  26. namespace model {
  27. namespace {
  28. inline absl::optional<Document> none() {
  29. return absl::optional<Document>{};
  30. }
  31. } // namespace
  32. DocumentComparator DocumentComparator::ByKey() {
  33. return DocumentComparator([](const Document& lhs, const Document& rhs) {
  34. return util::Compare(lhs->key(), rhs->key());
  35. });
  36. }
  37. DocumentSet::DocumentSet(DocumentComparator&& comparator)
  38. : index_{}, sorted_set_{std::move(comparator)} {
  39. }
  40. bool operator==(const DocumentSet& lhs, const DocumentSet& rhs) {
  41. return absl::c_equal(lhs.sorted_set_, rhs.sorted_set_);
  42. }
  43. std::string DocumentSet::ToString() const {
  44. return util::ToString(sorted_set_);
  45. }
  46. std::ostream& operator<<(std::ostream& os, const DocumentSet& set) {
  47. return os << set.ToString();
  48. }
  49. size_t DocumentSet::Hash() const {
  50. return util::Hash(sorted_set_);
  51. }
  52. bool DocumentSet::ContainsKey(const DocumentKey& key) const {
  53. return index_.find(key) != index_.end();
  54. }
  55. absl::optional<Document> DocumentSet::GetDocument(
  56. const DocumentKey& key) const {
  57. auto found = index_.find(key);
  58. return found != index_.end() ? Document(found->second) : none();
  59. }
  60. absl::optional<Document> DocumentSet::GetFirstDocument() const {
  61. auto result = sorted_set_.min();
  62. return result != sorted_set_.end() ? *result : none();
  63. }
  64. absl::optional<Document> DocumentSet::GetLastDocument() const {
  65. auto result = sorted_set_.max();
  66. return result != sorted_set_.end() ? *result : none();
  67. }
  68. size_t DocumentSet::IndexOf(const DocumentKey& key) const {
  69. absl::optional<Document> doc = GetDocument(key);
  70. return doc ? sorted_set_.find_index(*doc) : npos;
  71. }
  72. DocumentSet DocumentSet::insert(
  73. const absl::optional<Document>& document) const {
  74. // TODO(mcg): look into making document non-optional.
  75. if (!document) {
  76. return *this;
  77. }
  78. // Remove any prior mapping of the document's key before adding, preventing
  79. // the sorted_set_ from accumulating values that aren't in the index.
  80. const DocumentKey& key = (*document)->key();
  81. DocumentSet removed = erase(key);
  82. DocumentMap index = removed.index_.insert(key, *document);
  83. SetType set = removed.sorted_set_.insert(*document);
  84. return {std::move(index), std::move(set)};
  85. }
  86. DocumentSet DocumentSet::erase(const DocumentKey& key) const {
  87. absl::optional<Document> doc = GetDocument(key);
  88. if (!doc) {
  89. return *this;
  90. }
  91. DocumentMap index = index_.erase(key);
  92. SetType set = sorted_set_.erase(*doc);
  93. return {std::move(index), std::move(set)};
  94. }
  95. } // namespace model
  96. } // namespace firestore
  97. } // namespace firebase