Skip to content

Commit

Permalink
more cleanup;
Browse files Browse the repository at this point in the history
  • Loading branch information
dconeybe committed Jan 7, 2025
1 parent d21fb17 commit 2ca4d15
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Firestore/core/src/core/composite_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ const FieldFilter* CompositeFilter::Rep::FindFirstMatchingFilter(
return nullptr;
}

std::shared_ptr<std::vector<FieldFilter>> CompositeFilter::Rep::CalculateFlattenedFilters() const {
std::shared_ptr<std::vector<FieldFilter>>
CompositeFilter::Rep::CalculateFlattenedFilters() const {
auto flattened_filters = std::make_shared<std::vector<FieldFilter>>();
for (const auto& filter : filters())
std::copy(filter.GetFlattenedFilters().begin(),
Expand Down
3 changes: 2 additions & 1 deletion Firestore/core/src/core/composite_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ class CompositeFilter : public Filter {
return filters_.empty();
}

std::shared_ptr<std::vector<FieldFilter>> CalculateFlattenedFilters() const override;
std::shared_ptr<std::vector<FieldFilter>> CalculateFlattenedFilters()
const override;

std::vector<Filter> GetFilters() const override {
return filters();
Expand Down
3 changes: 2 additions & 1 deletion Firestore/core/src/core/field_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ FieldFilter::FieldFilter(std::shared_ptr<const Filter::Rep> rep)
: Filter(std::move(rep)) {
}

std::shared_ptr<std::vector<FieldFilter>> FieldFilter::Rep::CalculateFlattenedFilters() const {
std::shared_ptr<std::vector<FieldFilter>>
FieldFilter::Rep::CalculateFlattenedFilters() const {
// This is already a field filter, so we return a vector of size one.
auto filters = std::make_shared<std::vector<FieldFilter>>();
filters->push_back(FieldFilter(std::make_shared<const Rep>(*this)));
Expand Down
3 changes: 2 additions & 1 deletion Firestore/core/src/core/field_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ class FieldFilter : public Filter {

bool MatchesComparison(util::ComparisonResult comparison) const;

std::shared_ptr<std::vector<FieldFilter>> CalculateFlattenedFilters() const override;
std::shared_ptr<std::vector<FieldFilter>> CalculateFlattenedFilters()
const override;

private:
friend class FieldFilter;
Expand Down
6 changes: 4 additions & 2 deletions Firestore/core/src/core/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,16 @@ class Filter {
virtual std::vector<Filter> GetFilters() const = 0;

protected:
virtual std::shared_ptr<std::vector<FieldFilter>> CalculateFlattenedFilters() const = 0;
virtual std::shared_ptr<std::vector<FieldFilter>>
CalculateFlattenedFilters() const = 0;

private:
/**
* Memoized list of all field filters that can be found by
* traversing the tree of filters contained in this composite filter.
*/
mutable util::ThreadSafeMemoizer<std::vector<FieldFilter>> memoized_flattened_filters_;
mutable util::ThreadSafeMemoizer<std::vector<FieldFilter>>
memoized_flattened_filters_;
};

explicit Filter(std::shared_ptr<const Rep>&& rep) : rep_(rep) {
Expand Down
12 changes: 6 additions & 6 deletions Firestore/core/src/core/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ absl::optional<Operator> Query::FindOpInsideFilters(
return absl::nullopt;
}

std::shared_ptr<std::vector<OrderBy>> Query::CalculateNormalizedOrderBys() const {
std::shared_ptr<std::vector<OrderBy>> Query::CalculateNormalizedOrderBys()
const {
// Any explicit order by fields should be added as is.
auto result = std::make_shared<std::vector<OrderBy>>(explicit_order_bys_);

std::set<FieldPath> fieldsNormalized;
for (const OrderBy& order_by : explicit_order_bys_) {
fieldsNormalized.insert(order_by.field());
Expand All @@ -111,21 +112,20 @@ std::shared_ptr<std::vector<OrderBy>> Query::CalculateNormalizedOrderBys() const
// the same field, the field should be added only once. Note:
// `std::set<model::FieldPath>` sorts the key field before other fields.
// However, we want the key field to be sorted last.
const std::set<model::FieldPath> inequality_fields =
InequalityFilterFields();
const std::set<model::FieldPath> inequality_fields = InequalityFilterFields();

for (const model::FieldPath& field : inequality_fields) {
if (fieldsNormalized.find(field) == fieldsNormalized.end() &&
!field.IsKeyFieldPath()) {
result->push_back(OrderBy(field, last_direction));
}
}
}

// Add the document key field to the last if it is not explicitly ordered.
if (fieldsNormalized.find(FieldPath::KeyFieldPath()) ==
fieldsNormalized.end()) {
result->push_back(OrderBy(FieldPath::KeyFieldPath(), last_direction));
}
}

return result;
}
Expand Down
3 changes: 2 additions & 1 deletion Firestore/core/src/core/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ class Query {

// The memoized list of sort orders.
std::shared_ptr<std::vector<OrderBy>> CalculateNormalizedOrderBys() const;
mutable util::ThreadSafeMemoizer<std::vector<OrderBy>> memoized_normalized_order_bys_;
mutable util::ThreadSafeMemoizer<std::vector<OrderBy>>
memoized_normalized_order_bys_;

// The corresponding Target of this Query instance.
std::shared_ptr<Target> CalculateTarget() const;
Expand Down
10 changes: 7 additions & 3 deletions Firestore/core/src/util/thread_safe_memoizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ class ThreadSafeMemoizer {
public:
ThreadSafeMemoizer() = default;

ThreadSafeMemoizer(const ThreadSafeMemoizer& other) : memoized_(std::atomic_load(&other.memoized_)) {}
ThreadSafeMemoizer(const ThreadSafeMemoizer& other)
: memoized_(std::atomic_load(&other.memoized_)) {
}

ThreadSafeMemoizer& operator=(const ThreadSafeMemoizer& other) {
std::atomic_store(&memoized_, std::atomic_load(&other.memoized_));
return *this;
}

ThreadSafeMemoizer(ThreadSafeMemoizer&& other) noexcept : memoized_(std::atomic_load(&other.memoized_)) {
ThreadSafeMemoizer(ThreadSafeMemoizer&& other) noexcept
: memoized_(std::atomic_load(&other.memoized_)) {
}

ThreadSafeMemoizer& operator=(ThreadSafeMemoizer&& other) noexcept {
Expand Down Expand Up @@ -75,7 +78,8 @@ class ThreadSafeMemoizer {

std::shared_ptr<T> new_memoized = func();

if (std::atomic_compare_exchange_weak(&memoized_, &old_memoized, new_memoized)) {
if (std::atomic_compare_exchange_weak(&memoized_, &old_memoized,
new_memoized)) {
return *new_memoized;
}
}
Expand Down

0 comments on commit 2ca4d15

Please sign in to comment.