Skip to content

Commit

Permalink
Traversal functions use IteratorControl values rather than true/false…
Browse files Browse the repository at this point in the history
… which is more expressive
  • Loading branch information
ironage committed Sep 14, 2022
1 parent e7e2b8a commit c3379db
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 104 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
-----------

### Internals
* None.
* Traversal functions use a typed IteratorControl value rather than true/false. ([#5857](https://github.com/realm/realm-core/issues/5857))

----------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions src/realm/bplustree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ size_t BPlusTreeLeaf::bptree_erase(size_t ndx, EraseFunc func)

bool BPlusTreeLeaf::bptree_traverse(TraverseFunc func)
{
return func(this, 0);
return func(this, 0) == IteratorControl::Stop;
}

/****************************** BPlusTreeInner *******************************/
Expand Down Expand Up @@ -465,7 +465,7 @@ bool BPlusTreeInner::bptree_traverse(TraverseFunc func)
bool child_is_leaf = !Array::get_is_inner_bptree_node_from_header(child_header);
if (child_is_leaf) {
auto leaf = cache_leaf(mem, i, child_offset + m_my_offset);
done = func(leaf, child_offset + m_my_offset);
done = (func(leaf, child_offset + m_my_offset) == IteratorControl::Stop);
}
else {
BPlusTreeInner node(m_tree);
Expand Down
18 changes: 9 additions & 9 deletions src/realm/bplustree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class BPlusTreeNode {
// Erase element at erase_pos. May cause nodes to be merged
using EraseFunc = util::FunctionRef<size_t(BPlusTreeNode*, size_t erase_pos)>;
// Function to be called for all leaves in the tree until the function
// returns 'true'. 'offset' gives index of the first element in the leaf.
using TraverseFunc = util::FunctionRef<bool(BPlusTreeNode*, size_t offset)>;
// returns 'IteratorControl::Stop'. 'offset' gives index of the first element in the leaf.
using TraverseFunc = util::FunctionRef<IteratorControl(BPlusTreeNode*, size_t offset)>;

BPlusTreeNode(BPlusTreeBase* tree)
: m_tree(tree)
Expand Down Expand Up @@ -392,7 +392,7 @@ class BPlusTree : public BPlusTreeBase {
for (size_t i = 0; i < sz; i++) {
all_values.push_back(leaf->get(i));
}
return false;
return IteratorControl::AdvanceToNext;
};

m_root->bptree_traverse(func);
Expand Down Expand Up @@ -497,9 +497,9 @@ class BPlusTree : public BPlusTreeBase {
auto i = leaf->find_first(value, 0, sz);
if (i < sz) {
result = i + offset;
return true;
return IteratorControl::Stop;
}
return false;
return IteratorControl::AdvanceToNext;
};

m_root->bptree_traverse(func);
Expand All @@ -516,7 +516,7 @@ class BPlusTree : public BPlusTreeBase {
while ((i = leaf->find_first(value, i + 1, sz)) < sz) {
callback(i + offset);
}
return false;
return IteratorControl::AdvanceToNext;
};

m_root->bptree_traverse(func);
Expand All @@ -532,7 +532,7 @@ class BPlusTree : public BPlusTreeBase {
for (size_t i = 0; i < sz; i++) {
o << indent << leaf->get(i) << std::endl;
}
return false;
return IteratorControl::AdvanceToNext;
};

m_root->bptree_traverse(func);
Expand Down Expand Up @@ -593,7 +593,7 @@ typename SumAggType<T>::ResultType bptree_sum(const BPlusTree<T>& tree, size_t*
auto val = leaf->get(i);
agg.accumulate(val);
}
return false;
return IteratorControl::AdvanceToNext;
};

tree.traverse(func);
Expand Down Expand Up @@ -625,7 +625,7 @@ util::Optional<typename util::RemoveOptional<T>::type> bptree_min_max(const BPlu
*return_ndx = i + offset;
}
}
return false;
return IteratorControl::AdvanceToNext;
};

tree.traverse(func);
Expand Down
31 changes: 15 additions & 16 deletions src/realm/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void Cluster::create()
arr.create();
arr.set_parent(this, col_ndx.val + s_first_col_index);
arr.update_parent();
return false;
return IteratorControl::AdvanceToNext;
}
switch (type) {
case col_type_Int:
Expand Down Expand Up @@ -192,7 +192,7 @@ void Cluster::create()
default:
throw LogicError(LogicError::illegal_type);
}
return false;
return IteratorControl::AdvanceToNext;
};
m_tree_top.for_each_and_every_column(column_initialize);

Expand Down Expand Up @@ -373,7 +373,7 @@ void Cluster::insert_row(size_t ndx, ObjKey k, const FieldValues& init_values)
arr.set_parent(this, col_ndx.val + s_first_col_index);
arr.init_from_parent();
arr.insert(ndx, 0);
return false;
return IteratorControl::AdvanceToNext;
}

bool nullable = attr.test(col_attr_Nullable);
Expand Down Expand Up @@ -434,7 +434,7 @@ void Cluster::insert_row(size_t ndx, ObjKey k, const FieldValues& init_values)
REALM_ASSERT(false);
break;
}
return false;
return IteratorControl::AdvanceToNext;
};
m_tree_top.for_each_and_every_column(insert_in_column);
}
Expand Down Expand Up @@ -464,7 +464,7 @@ void Cluster::move(size_t ndx, ClusterNode* new_node, int64_t offset)

if (attr.test(col_attr_Collection)) {
do_move<ArrayRef>(ndx, col_key, new_leaf);
return false;
return IteratorControl::AdvanceToNext;
}

switch (type) {
Expand Down Expand Up @@ -523,7 +523,7 @@ void Cluster::move(size_t ndx, ClusterNode* new_node, int64_t offset)
REALM_ASSERT(false);
break;
}
return false;
return IteratorControl::AdvanceToNext;
};
m_tree_top.for_each_and_every_column(move_from_column);
for (size_t i = ndx; i < m_keys.size(); i++) {
Expand Down Expand Up @@ -878,7 +878,7 @@ size_t Cluster::erase(ObjKey key, CascadeState& state)

values.erase(ndx);

return false;
return IteratorControl::AdvanceToNext;
}

switch (col_type) {
Expand Down Expand Up @@ -942,7 +942,7 @@ size_t Cluster::erase(ObjKey key, CascadeState& state)
REALM_ASSERT(false);
break;
}
return false;
return IteratorControl::AdvanceToNext;
};
m_tree_top.for_each_and_every_column(erase_in_column);

Expand Down Expand Up @@ -1003,7 +1003,7 @@ void Cluster::nullify_incoming_links(ObjKey key, CascadeState& state)
values.copy_on_write();
values.nullify_fwd_links(ndx, state);

return false;
return IteratorControl::AdvanceToNext;
};

m_tree_top.get_owning_table()->for_each_backlink_column(nullify_fwd_links);
Expand Down Expand Up @@ -1165,7 +1165,7 @@ void Cluster::verify() const
// FIXME: Nullable primitives
break;
}
return false;
return IteratorControl::AdvanceToNext;
}
else if (attr.test(col_attr_Dictionary)) {
ArrayRef arr(get_alloc());
Expand All @@ -1186,7 +1186,7 @@ void Cluster::verify() const
cluster.verify();
}
}
return false;
return IteratorControl::AdvanceToNext;
}
else if (attr.test(col_attr_Set)) {
ArrayRef arr(get_alloc());
Expand Down Expand Up @@ -1242,7 +1242,7 @@ void Cluster::verify() const
// FIXME: Nullable primitives
break;
}
return false;
return IteratorControl::AdvanceToNext;
}

switch (col_type) {
Expand Down Expand Up @@ -1293,7 +1293,7 @@ void Cluster::verify() const
default:
break;
}
return false;
return IteratorControl::AdvanceToNext;
};

m_tree_top.for_each_and_every_column(verify_column);
Expand Down Expand Up @@ -1339,7 +1339,7 @@ void Cluster::dump_objects(int64_t key_offset, std::string lead) const
}
}
std::cout << "}";
return false;
return IteratorControl::AdvanceToNext;
}

switch (col.get_type()) {
Expand Down Expand Up @@ -1392,7 +1392,6 @@ void Cluster::dump_objects(int64_t key_offset, std::string lead) const
else
std::cout << ", null";
break;
break;
}
case col_type_String: {
ArrayString arr(m_alloc);
Expand Down Expand Up @@ -1477,7 +1476,7 @@ void Cluster::dump_objects(int64_t key_offset, std::string lead) const
std::cout << ", Error";
break;
}
return false;
return IteratorControl::AdvanceToNext;
});
std::cout << std::endl;
}
Expand Down
6 changes: 3 additions & 3 deletions src/realm/cluster_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ bool ClusterNodeInner::traverse(ClusterTree::TraverseFunction func, int64_t key_
if (child_is_leaf) {
Cluster leaf(offs, m_alloc, m_tree_top);
leaf.init(mem);
if (func(&leaf)) {
if (func(&leaf) == IteratorControl::Stop) {
return true;
}
}
Expand Down Expand Up @@ -994,7 +994,7 @@ bool ClusterTree::get_leaf(ObjKey key, ClusterNode::IteratorState& state) const
bool ClusterTree::traverse(TraverseFunction func) const
{
if (m_root->is_leaf()) {
return func(static_cast<Cluster*>(m_root.get()));
return func(static_cast<Cluster*>(m_root.get())) == IteratorControl::Stop;
}
else {
return static_cast<ClusterNodeInner*>(m_root.get())->traverse(func, 0);
Expand All @@ -1016,7 +1016,7 @@ void ClusterTree::verify() const
#ifdef REALM_DEBUG
traverse([](const Cluster* cluster) {
cluster->verify();
return false;
return IteratorControl::AdvanceToNext;
});
#endif
}
Expand Down
6 changes: 3 additions & 3 deletions src/realm/cluster_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Cluster;
class ClusterTree {
public:
class Iterator;
using TraverseFunction = util::FunctionRef<bool(const Cluster*)>;
using TraverseFunction = util::FunctionRef<IteratorControl(const Cluster*)>;
using UpdateFunction = util::FunctionRef<void(Cluster*)>;
using ColIterateFunction = util::FunctionRef<bool(ColKey)>;
using ColIterateFunction = util::FunctionRef<IteratorControl(ColKey)>;

ClusterTree(Allocator& alloc);
virtual ~ClusterTree();
Expand Down Expand Up @@ -146,7 +146,7 @@ class ClusterTree {
size_t get_ndx(ObjKey k) const noexcept;
// Find the leaf containing the requested object
bool get_leaf(ObjKey key, ClusterNode::IteratorState& state) const noexcept;
// Visit all leaves and call the supplied function. Stop when function returns true.
// Visit all leaves and call the supplied function. Stop when function returns IteratorControl::Stop.
// Not allowed to modify the tree
bool traverse(TraverseFunction func) const;
// Visit all leaves and call the supplied function. The function can modify the leaf.
Expand Down
6 changes: 3 additions & 3 deletions src/realm/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ size_t virtual2real(const BPlusTree<ObjKey>* tree, size_t ndx) noexcept
size_t sz = leaf->size();
for (size_t i = 0; i < sz; i++) {
if (i + offset == ndx) {
return true;
return IteratorControl::Stop;
}
auto k = leaf->get(i);
if (k.is_unresolved()) {
adjust++;
}
}
return false;
return IteratorControl::AdvanceToNext;
};

tree->traverse(func);
Expand Down Expand Up @@ -63,7 +63,7 @@ void update_unresolved(std::vector<size_t>& vec, const BPlusTree<ObjKey>* tree)
vec.push_back(i + offset);
}
}
return false;
return IteratorControl::AdvanceToNext;
};

tree->traverse(func);
Expand Down
8 changes: 3 additions & 5 deletions src/realm/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ void DictionaryClusterTree::do_accumulate(size_t* return_ndx, AggregateType& agg
}
}
start_ndx += e;
// Continue
return false;
return IteratorControl::AdvanceToNext;
});

if (return_ndx)
Expand Down Expand Up @@ -375,12 +374,11 @@ size_t Dictionary::find_any(Mixed value) const
for (size_t i = 0; i < e; i++) {
if (leaf.get(i) == value) {
ret = start_ndx + i;
return true;
return IteratorControl::Stop;
}
}
start_ndx += e;
// Continue
return false;
return IteratorControl::AdvanceToNext;
});
}

Expand Down
6 changes: 2 additions & 4 deletions src/realm/dictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ class Dictionary final : public CollectionBaseImpl<CollectionBase, Dictionary> {
for (size_t i = 0; i < e; i++) {
f(leaf.get(i));
}
// Continue
return false;
return IteratorControl::AdvanceToNext;
};
m_clusters->traverse(trv_func);
}
Expand All @@ -132,8 +131,7 @@ class Dictionary final : public CollectionBaseImpl<CollectionBase, Dictionary> {
for (size_t i = 0; i < e; i++) {
f(leaf.get(i));
}
// Continue
return false;
return IteratorControl::AdvanceToNext;
};
m_clusters->traverse(trv_func);
}
Expand Down
Loading

0 comments on commit c3379db

Please sign in to comment.