document_set.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef FIRESTORE_CORE_SRC_MODEL_DOCUMENT_SET_H_
  17. #define FIRESTORE_CORE_SRC_MODEL_DOCUMENT_SET_H_
  18. #include <iosfwd>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include "Firestore/core/src/immutable/sorted_container.h"
  24. #include "Firestore/core/src/immutable/sorted_set.h"
  25. #include "Firestore/core/src/model/document.h"
  26. #include "Firestore/core/src/model/model_fwd.h"
  27. #include "Firestore/core/src/util/comparison.h"
  28. namespace firebase {
  29. namespace firestore {
  30. namespace model {
  31. class DocumentComparator : public util::FunctionComparator<Document> {
  32. public:
  33. using FunctionComparator<Document>::FunctionComparator;
  34. static DocumentComparator ByKey();
  35. // TODO(wilhuff): Remove this using statement
  36. // This exists to put these two overloads on equal footing. Once the overload
  37. // below is gone, this using statement can be removed as well.
  38. using FunctionComparator<Document>::Compare;
  39. };
  40. /**
  41. * DocumentSet is an immutable (copy-on-write) collection that holds documents
  42. * in order specified by the provided comparator. We always add a document key
  43. * comparator on top of what is provided to guarantee document equality based on
  44. * the key.
  45. */
  46. class DocumentSet : public immutable::SortedContainer {
  47. public:
  48. /**
  49. * The type of the main collection of documents in an DocumentSet.
  50. * @see sorted_set_.
  51. */
  52. using SetType = immutable::SortedSet<Document, DocumentComparator>;
  53. // STL container types
  54. using value_type = Document;
  55. using const_iterator = SetType::const_iterator;
  56. /**
  57. * Creates a new, empty DocumentSet sorted by the given comparator, then by
  58. * keys.
  59. */
  60. explicit DocumentSet(DocumentComparator&& comparator);
  61. size_t size() const {
  62. return index_.size();
  63. }
  64. /** Returns true if the dictionary contains no elements. */
  65. bool empty() const {
  66. return index_.empty();
  67. }
  68. /** Returns true if this set contains a document with the given key. */
  69. bool ContainsKey(const DocumentKey& key) const;
  70. const DocumentComparator& comparator() const {
  71. return sorted_set_.comparator();
  72. }
  73. SetType::const_iterator begin() const {
  74. return sorted_set_.begin();
  75. }
  76. SetType::const_iterator end() const {
  77. return sorted_set_.end();
  78. }
  79. /**
  80. * Returns the document from this set with the given key if it exists or nil
  81. * if it doesn't.
  82. */
  83. absl::optional<Document> GetDocument(const DocumentKey& key) const;
  84. /**
  85. * Returns the first document in the set according to its built in ordering,
  86. * or nil if the set is empty.
  87. */
  88. absl::optional<Document> GetFirstDocument() const;
  89. /**
  90. * Returns the last document in the set according to its built in ordering, or
  91. * nil if the set is empty.
  92. */
  93. absl::optional<Document> GetLastDocument() const;
  94. /**
  95. * Returns the index of the document with the provided key in the document
  96. * set. Returns `npos` if the key is not present.
  97. */
  98. size_t IndexOf(const DocumentKey& key) const;
  99. /** Returns a new DocumentSet that contains the given document. */
  100. DocumentSet insert(const absl::optional<Document>& document) const;
  101. /**
  102. * Returns a new DocumentSet that excludes any document associated with
  103. * the given key.
  104. */
  105. DocumentSet erase(const DocumentKey& key) const;
  106. friend bool operator==(const DocumentSet& lhs, const DocumentSet& rhs);
  107. std::string ToString() const;
  108. friend std::ostream& operator<<(std::ostream& os, const DocumentSet& set);
  109. size_t Hash() const;
  110. private:
  111. DocumentSet(DocumentMap&& index, SetType&& sorted_set)
  112. : index_(std::move(index)), sorted_set_(std::move(sorted_set)) {
  113. }
  114. /**
  115. * An index of the documents in the DocumentSet, indexed by document key.
  116. * The index exists to guarantee the uniqueness of document keys in the set
  117. * and to allow lookup and removal of documents by key.
  118. */
  119. DocumentMap index_;
  120. /**
  121. * The main collection of documents in the DocumentSet. The documents are
  122. * ordered by a comparator supplied from a query. The SetType collection
  123. * exists in addition to the index to allow ordered traversal of the
  124. * DocumentSet.
  125. */
  126. SetType sorted_set_;
  127. };
  128. inline bool operator!=(const DocumentSet& lhs, const DocumentSet& rhs) {
  129. return !(lhs == rhs);
  130. }
  131. } // namespace model
  132. } // namespace firestore
  133. } // namespace firebase
  134. #endif // FIRESTORE_CORE_SRC_MODEL_DOCUMENT_SET_H_