Skip to content

Commit

Permalink
add more test coverage to verify single link removal
Browse files Browse the repository at this point in the history
  • Loading branch information
ironage committed Jun 26, 2024
1 parent 8b65927 commit 6304527
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/realm/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ inline void Cluster::do_erase_key(size_t ndx, ColKey col_key, CascadeState& stat
values.set_parent(this, col_ndx.val + s_first_col_index);
values.init_from_parent();

ObjKey key = values.get(ndx); // FIXME: check if this needs get_real_key()
ObjKey key = values.get(ndx);
if (key != null_key) {
do_remove_backlinks(get_real_key(ndx), col_key, std::vector<ObjKey>{key}, state);
}
Expand Down
82 changes: 82 additions & 0 deletions test/test_mixed_null_assertions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,33 @@ struct NestedDictOfDicts {
ColKey m_col_key;
};

struct SingleLink {
void init_table(TableRef) {}
void set_links(Obj from, std::vector<ObjLink> to)
{
REALM_ASSERT(to.size() == 1);
TableRef src_table = from.get_table();
if (!m_col_key) {
TableRef dest_table = src_table->get_parent_group()->get_table(to[0].get_table_key());
m_col_key = src_table->add_column(*dest_table, "link");
}
TableKey dest_table_key = src_table->get_opposite_table_key(m_col_key);
REALM_ASSERT(to[0].get_table_key() == dest_table_key);
from.set<ObjKey>(m_col_key, to[0].get_obj_key());
}
void get_links(Obj from, std::vector<ObjLink>& links)
{
if (!m_col_key) {
return;
}
TableRef src_table = from.get_table();
TableKey dest_table_key = src_table->get_opposite_table_key(m_col_key);
ObjKey link = from.get<ObjKey>(m_col_key);
links.push_back(ObjLink{dest_table_key, link});
}
ColKey m_col_key;
};

TEST_TYPES(Mixed_ContainerOfLinksFromLargeCluster, ListOfMixedLinks, DictionaryOfMixedLinks, NestedDictionary,
NestedList, NestedListOfLists, NestedDictOfDicts)
{
Expand Down Expand Up @@ -802,3 +829,58 @@ TEST_TYPES(Mixed_ContainerOfLinksFromLargeCluster, ListOfMixedLinks, DictionaryO
remove_one_object(rnd);
}
}

TEST_TYPES(Mixed_RemoveRecursiveSingleLink, SingleLink, ListOfMixedLinks, DictionaryOfMixedLinks, NestedDictionary,
NestedList, NestedListOfLists, NestedDictOfDicts)
{
Group g;
auto top1 = g.add_table_with_primary_key("top1", type_String, "_id");
auto top2 = g.add_table_with_primary_key("top2", type_String, "_id");
TEST_TYPE type;
type.init_table(top1);

constexpr size_t num_src_objects = 4000; // more than BPNODE_SIZE
constexpr size_t num_dst_objects = 1000;
for (size_t i = 0; i < num_src_objects; ++i) {
auto top1_obj = top1->create_object_with_primary_key(util::format("top1_%1", i));

// get or create
auto top2_obj = top2->create_object_with_primary_key(util::format("top2_%1", i % num_dst_objects));

std::vector<ObjLink> dest_links = {ObjLink(top2->get_key(), top2_obj.get_key())};
type.set_links(top1_obj, dest_links);
}

auto remove_one_object = [&](size_t ndx) {
Obj obj_to_remove = top1->get_object(ndx);
std::vector<ObjLink> links;
type.get_links(obj_to_remove, links);
CHECK_EQUAL(links.size(), 1);
ObjLink link_1 = links[0];
CHECK_EQUAL(link_1.get_table_key(), top2->get_key());

Obj obj_linked = top2->get_object(link_1.get_obj_key());
size_t previous_backlink_count = obj_linked.get_backlink_count();
CHECK_NOT_EQUAL(previous_backlink_count, 0);

top1->remove_object_recursive(obj_to_remove.get_key());
CHECK_NOT(obj_to_remove.is_valid());
if (previous_backlink_count == 1) {
CHECK_NOT(obj_linked.is_valid());
}
else {
CHECK(obj_linked.is_valid());
size_t new_backlink_count = obj_linked.get_backlink_count();
CHECK_EQUAL(new_backlink_count, previous_backlink_count - 1);
}
};

// erase at random, to exercise the collapse/join of cluster leafs
Random random(test_util::random_int<unsigned long>()); // Seed from slow global generator
while (!top1->is_empty()) {
size_t rnd = random.draw_int_mod(top1->size());
remove_one_object(rnd);
}
CHECK_EQUAL(top1->size(), 0);
CHECK_EQUAL(top2->size(), 0);
}

0 comments on commit 6304527

Please sign in to comment.