|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "database/src/desktop/persistence/prune_forest.h" |
| 16 | + |
| 17 | +#include <set> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "app/src/assert.h" |
| 21 | +#include "app/src/path.h" |
| 22 | +#include "database/src/desktop/core/tree.h" |
| 23 | + |
| 24 | +namespace firebase { |
| 25 | +namespace database { |
| 26 | +namespace internal { |
| 27 | + |
| 28 | +static const Tree<bool> kPruneTree(true); // NOLINT |
| 29 | +static const Tree<bool> kKeepTree(false); // NOLINT |
| 30 | + |
| 31 | +static bool KeepPredicate(bool prune) { return !prune; } |
| 32 | +static bool PrunePredicate(bool prune) { return prune; } |
| 33 | + |
| 34 | +PruneForestRef::PruneForestRef(PruneForest* prune_forest) |
| 35 | + : prune_forest_(prune_forest) {} |
| 36 | + |
| 37 | +bool PruneForestRef::operator==(const PruneForestRef& other) const { |
| 38 | + return (prune_forest_ == other.prune_forest_) || |
| 39 | + (prune_forest_ != nullptr && other.prune_forest_ != nullptr && |
| 40 | + *prune_forest_ == *other.prune_forest_); |
| 41 | +} |
| 42 | + |
| 43 | +bool PruneForestRef::PrunesAnything() const { |
| 44 | + return prune_forest_->ContainsMatchingValue(PrunePredicate); |
| 45 | +} |
| 46 | + |
| 47 | +bool PruneForestRef::ShouldPruneUnkeptDescendants(const Path& path) const { |
| 48 | + const bool* should_prune = prune_forest_->LeafMostValue(path); |
| 49 | + return should_prune != nullptr && *should_prune; |
| 50 | +} |
| 51 | + |
| 52 | +bool PruneForestRef::ShouldKeep(const Path& path) const { |
| 53 | + const bool* should_prune = prune_forest_->LeafMostValue(path); |
| 54 | + return should_prune != nullptr && !*should_prune; |
| 55 | +} |
| 56 | + |
| 57 | +bool PruneForestRef::AffectsPath(const Path& path) const { |
| 58 | + return prune_forest_->RootMostValue(path) != nullptr || |
| 59 | + (prune_forest_->GetChild(path) && |
| 60 | + !prune_forest_->GetChild(path)->IsEmpty()); |
| 61 | +} |
| 62 | + |
| 63 | +PruneForestRef PruneForestRef::GetChild(const std::string& key) { |
| 64 | + return PruneForestRef(prune_forest_->GetOrMakeSubtree(Path(key))); |
| 65 | +} |
| 66 | + |
| 67 | +const PruneForestRef PruneForestRef::GetChild(const std::string& key) const { |
| 68 | + return PruneForestRef(prune_forest_->GetOrMakeSubtree(Path(key))); |
| 69 | +} |
| 70 | + |
| 71 | +PruneForestRef PruneForestRef::GetChild(const Path& path) { |
| 72 | + return PruneForestRef(prune_forest_->GetOrMakeSubtree(path)); |
| 73 | +} |
| 74 | + |
| 75 | +const PruneForestRef PruneForestRef::GetChild(const Path& path) const { |
| 76 | + return PruneForestRef(prune_forest_->GetOrMakeSubtree(path)); |
| 77 | +} |
| 78 | + |
| 79 | +void PruneForestRef::Prune(const Path& path) { |
| 80 | + FIREBASE_DEV_ASSERT_MESSAGE( |
| 81 | + prune_forest_->RootMostValueMatching(path, KeepPredicate) == nullptr, |
| 82 | + "Can't prune path that was kept previously!") |
| 83 | + if (prune_forest_->RootMostValueMatching(path, PrunePredicate) != nullptr) { |
| 84 | + // This path will already be pruned |
| 85 | + } else { |
| 86 | + *prune_forest_->GetOrMakeSubtree(path) = kPruneTree; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +void PruneForestRef::Keep(const Path& path) { |
| 91 | + if (prune_forest_->RootMostValueMatching(path, KeepPredicate) != nullptr) { |
| 92 | + // This path will already be kept |
| 93 | + } else { |
| 94 | + *prune_forest_->GetOrMakeSubtree(path) = kKeepTree; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void PruneForestRef::KeepAll(const Path& path, |
| 99 | + const std::set<std::string>& children) { |
| 100 | + if (prune_forest_->RootMostValueMatching(path, KeepPredicate) != nullptr) { |
| 101 | + // This path will already be kept. |
| 102 | + } else { |
| 103 | + DoAll(path, children, kKeepTree); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +void PruneForestRef::PruneAll(const Path& path, |
| 108 | + const std::set<std::string>& children) { |
| 109 | + FIREBASE_DEV_ASSERT_MESSAGE( |
| 110 | + prune_forest_->RootMostValueMatching(path, KeepPredicate) == nullptr, |
| 111 | + "Can't prune path that was kept previously!"); |
| 112 | + |
| 113 | + if (prune_forest_->RootMostValueMatching(path, PrunePredicate) != nullptr) { |
| 114 | + // This path will already be pruned. |
| 115 | + } else { |
| 116 | + DoAll(path, children, kPruneTree); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +void PruneForestRef::DoAll(const Path& path, |
| 121 | + const std::set<std::string>& children, |
| 122 | + const Tree<bool>& keep_or_prune_tree) { |
| 123 | + Tree<bool>* subtree = prune_forest_->GetChild(path); |
| 124 | + for (const std::string& directory : children) { |
| 125 | + *subtree->GetOrMakeSubtree(Path(directory)) = keep_or_prune_tree; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace internal |
| 130 | +} // namespace database |
| 131 | +} // namespace firebase |
0 commit comments