|
17 | 17 | #include "Firestore/core/src/core/query.h" |
18 | 18 |
|
19 | 19 | #include <algorithm> |
| 20 | +#include <memory> |
20 | 21 | #include <ostream> |
21 | 22 |
|
22 | 23 | #include "Firestore/core/src/core/bound.h" |
@@ -91,44 +92,42 @@ absl::optional<Operator> Query::FindOpInsideFilters( |
91 | 92 | return absl::nullopt; |
92 | 93 | } |
93 | 94 |
|
94 | | -const std::vector<OrderBy>& Query::normalized_order_bys() const { |
95 | | - return memoized_normalized_order_bys_->memoize([&]() { |
96 | | - // Any explicit order by fields should be added as is. |
97 | | - std::vector<OrderBy> result = explicit_order_bys_; |
98 | | - std::set<FieldPath> fieldsNormalized; |
99 | | - for (const OrderBy& order_by : explicit_order_bys_) { |
100 | | - fieldsNormalized.insert(order_by.field()); |
101 | | - } |
| 95 | +std::shared_ptr<std::vector<OrderBy>> Query::CalculateNormalizedOrderBys() |
| 96 | + const { |
| 97 | + // Any explicit order by fields should be added as is. |
| 98 | + auto result = std::make_shared<std::vector<OrderBy>>(explicit_order_bys_); |
| 99 | + std::set<FieldPath> fieldsNormalized; |
| 100 | + for (const OrderBy& order_by : explicit_order_bys_) { |
| 101 | + fieldsNormalized.insert(order_by.field()); |
| 102 | + } |
102 | 103 |
|
103 | | - // The order of the implicit ordering always matches the last explicit order |
104 | | - // by. |
105 | | - Direction last_direction = explicit_order_bys_.empty() |
106 | | - ? Direction::Ascending |
107 | | - : explicit_order_bys_.back().direction(); |
108 | | - |
109 | | - // Any inequality fields not explicitly ordered should be implicitly ordered |
110 | | - // in a lexicographical order. When there are multiple inequality filters on |
111 | | - // the same field, the field should be added only once. Note: |
112 | | - // `std::set<model::FieldPath>` sorts the key field before other fields. |
113 | | - // However, we want the key field to be sorted last. |
114 | | - const std::set<model::FieldPath> inequality_fields = |
115 | | - InequalityFilterFields(); |
116 | | - |
117 | | - for (const model::FieldPath& field : inequality_fields) { |
118 | | - if (fieldsNormalized.find(field) == fieldsNormalized.end() && |
119 | | - !field.IsKeyFieldPath()) { |
120 | | - result.push_back(OrderBy(field, last_direction)); |
121 | | - } |
| 104 | + // The order of the implicit ordering always matches the last explicit order |
| 105 | + // by. |
| 106 | + Direction last_direction = explicit_order_bys_.empty() |
| 107 | + ? Direction::Ascending |
| 108 | + : explicit_order_bys_.back().direction(); |
| 109 | + |
| 110 | + // Any inequality fields not explicitly ordered should be implicitly ordered |
| 111 | + // in a lexicographical order. When there are multiple inequality filters on |
| 112 | + // the same field, the field should be added only once. Note: |
| 113 | + // `std::set<model::FieldPath>` sorts the key field before other fields. |
| 114 | + // However, we want the key field to be sorted last. |
| 115 | + const std::set<model::FieldPath> inequality_fields = InequalityFilterFields(); |
| 116 | + |
| 117 | + for (const model::FieldPath& field : inequality_fields) { |
| 118 | + if (fieldsNormalized.find(field) == fieldsNormalized.end() && |
| 119 | + !field.IsKeyFieldPath()) { |
| 120 | + result->push_back(OrderBy(field, last_direction)); |
122 | 121 | } |
| 122 | + } |
123 | 123 |
|
124 | | - // Add the document key field to the last if it is not explicitly ordered. |
125 | | - if (fieldsNormalized.find(FieldPath::KeyFieldPath()) == |
126 | | - fieldsNormalized.end()) { |
127 | | - result.push_back(OrderBy(FieldPath::KeyFieldPath(), last_direction)); |
128 | | - } |
| 124 | + // Add the document key field to the last if it is not explicitly ordered. |
| 125 | + if (fieldsNormalized.find(FieldPath::KeyFieldPath()) == |
| 126 | + fieldsNormalized.end()) { |
| 127 | + result->push_back(OrderBy(FieldPath::KeyFieldPath(), last_direction)); |
| 128 | + } |
129 | 129 |
|
130 | | - return result; |
131 | | - }); |
| 130 | + return result; |
132 | 131 | } |
133 | 132 |
|
134 | 133 | LimitType Query::limit_type() const { |
@@ -296,14 +295,12 @@ std::string Query::ToString() const { |
296 | 295 | return absl::StrCat("Query(canonical_id=", CanonicalId(), ")"); |
297 | 296 | } |
298 | 297 |
|
299 | | -const Target& Query::ToTarget() const& { |
300 | | - return memoized_target_->memoize( |
301 | | - [&]() { return ToTarget(normalized_order_bys()); }); |
| 298 | +std::shared_ptr<Target> Query::CalculateTarget() const { |
| 299 | + return std::make_shared<Target>(ToTarget(normalized_order_bys())); |
302 | 300 | } |
303 | 301 |
|
304 | | -const Target& Query::ToAggregateTarget() const& { |
305 | | - return memoized_aggregate_target_->memoize( |
306 | | - [&]() { return ToTarget(explicit_order_bys_); }); |
| 302 | +std::shared_ptr<Target> Query::CalculateAggregateTarget() const { |
| 303 | + return std::make_shared<Target>(ToTarget(explicit_order_bys_)); |
307 | 304 | } |
308 | 305 |
|
309 | 306 | Target Query::ToTarget(const std::vector<OrderBy>& order_bys) const { |
|
0 commit comments