Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RCORE-2108: Clear backlinks when object containing nested collections is deleted #7680

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
### Fixed
* Fixed a bug when running a IN query on a String/Int/UUID/ObjectId property that was indexed. ([7642](/~https://github.com/realm/realm-core/issues/7642) since v14.6.0)
* Fixed a bug when running a IN query on a integer property where double/float parameters were ignored. ([7642](/~https://github.com/realm/realm-core/issues/7642) since v14.6.0)
* Having links in a nested collections would leave the file inconsistent if the top object is removed. ([#7657](/~https://github.com/realm/realm-core/issues/7657), since 14.0.0)

### Breaking changes
* None.
Expand Down
15 changes: 14 additions & 1 deletion src/realm/array_backlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,20 @@ void ArrayBacklink::verify() const
REALM_ASSERT(src_obj.get_collection_ptr(src_col_key)->find_any(target_link) != npos);
}
else {
REALM_ASSERT(src_obj.get<Mixed>(src_col_key).get_link() == target_link);
auto val = src_obj.get<Mixed>(src_col_key);
if (val.is_type(type_TypedLink)) {
REALM_ASSERT(src_obj.get<Mixed>(src_col_key).get_link() == target_link);
}
else if (val.is_type(type_List)) {
DummyParent parent(src_table, val.get_ref());
Lst<Mixed> list(parent, 0);
REALM_ASSERT(list.find_any(target_link) != npos);
}
else if (val.is_type(type_Dictionary)) {
DummyParent parent(src_table, val.get_ref());
Dictionary dict(parent, 0);
REALM_ASSERT(dict.find_any(target_link) != npos);
}
}
continue;
}
Expand Down
54 changes: 39 additions & 15 deletions src/realm/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,24 +761,48 @@ inline void Cluster::do_erase(size_t ndx, ColKey col_key)
values.set_parent(this, col_ndx.val + s_first_col_index);
set_spec<T>(values, col_ndx);
values.init_from_parent();
ObjLink link;
if constexpr (std::is_same_v<T, ArrayTypedLink>) {
link = values.get(ndx);
}
if constexpr (std::is_same_v<T, ArrayMixed>) {
Mixed value = values.get(ndx);
if (value.is_type(type_TypedLink)) {
link = value.get<ObjLink>();
if (ObjLink link = values.get(ndx)) {
if (const Table* origin_table = m_tree_top.get_owning_table()) {
auto target_obj = origin_table->get_parent_group()->get_object(link);

ColKey backlink_col_key =
target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
REALM_ASSERT(backlink_col_key);
target_obj.remove_one_backlink(backlink_col_key, get_real_key(ndx)); // Throws
}
}
}
if (link) {
if (const Table* origin_table = m_tree_top.get_owning_table()) {
auto target_obj = origin_table->get_parent_group()->get_object(link);
values.erase(ndx);
}

ColKey backlink_col_key = target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
REALM_ASSERT(backlink_col_key);
target_obj.remove_one_backlink(backlink_col_key, get_real_key(ndx)); // Throws
}
inline void Cluster::do_erase_mixed(size_t ndx, ColKey col_key, ObjKey key, CascadeState& state)
{
const Table* origin_table = m_tree_top.get_owning_table();
auto col_ndx = col_key.get_index();

ArrayMixed values(m_alloc);
values.set_parent(this, col_ndx.val + s_first_col_index);
values.init_from_parent();

Mixed value = values.get(ndx);
if (value.is_type(type_TypedLink)) {
ObjLink link = value.get<ObjLink>();
auto target_obj = origin_table->get_parent_group()->get_object(link);

ColKey backlink_col_key = target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
REALM_ASSERT(backlink_col_key);
target_obj.remove_one_backlink(backlink_col_key, get_real_key(ndx)); // Throws
}
if (value.is_type(type_List)) {
Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
Lst<Mixed> list(obj, col_key);
list.remove_backlinks(state);
}
if (value.is_type(type_Dictionary)) {
Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
Dictionary dict(obj, col_key);
dict.remove_backlinks(state);
}
values.erase(ndx);
}
Expand Down Expand Up @@ -897,7 +921,7 @@ size_t Cluster::erase(ObjKey key, CascadeState& state)
do_erase<ArrayBinary>(ndx, col_key);
break;
case col_type_Mixed:
do_erase<ArrayMixed>(ndx, col_key);
do_erase_mixed(ndx, col_key, key, state);
break;
case col_type_Timestamp:
do_erase<ArrayTimestamp>(ndx, col_key);
Expand Down
1 change: 1 addition & 0 deletions src/realm/cluster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class Cluster : public ClusterNode {
remove_backlinks(get_owning_table(), origin_key, col, links, state);
}
void do_erase_key(size_t ndx, ColKey col, CascadeState& state);
void do_erase_mixed(size_t ndx, ColKey col, ObjKey key, CascadeState& state);
void do_insert_key(size_t ndx, ColKey col, Mixed init_val, ObjKey origin_key);
void do_insert_link(size_t ndx, ColKey col, Mixed init_val, ObjKey origin_key);
void do_insert_mixed(size_t ndx, ColKey col_key, Mixed init_value, ObjKey origin_key);
Expand Down
1 change: 1 addition & 0 deletions src/realm/replication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ void Replication::select_obj(ObjKey key)
}
}
m_selected_obj = key;
m_selected_list = CollectionId();
}

void Replication::do_set(const Table* t, ColKey col_key, ObjKey key, _impl::Instruction variant)
Expand Down
14 changes: 7 additions & 7 deletions src/realm/replication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ class Replication {
return m_logger;
}

util::Logger* would_log(util::Logger::Level level) const noexcept
{
if (m_logger && m_logger->would_log(level))
return m_logger;
return nullptr;
}

protected:
Replication() = default;

Expand Down Expand Up @@ -439,13 +446,6 @@ class Replication {
Mixed index) const;
Path get_prop_name(Path&&) const;
size_t transact_log_size();

util::Logger* would_log(util::Logger::Level level) const noexcept
{
if (m_logger && m_logger->would_log(level))
return m_logger;
return nullptr;
}
};

class Replication::Interrupted : public std::exception {
Expand Down
14 changes: 14 additions & 0 deletions src/realm/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,13 @@ Obj Table::create_object_with_primary_key(const Mixed& primary_key, FieldValues&

// Check if unresolved exists
if (unres_key) {
if (Replication* repl = get_repl()) {
if (auto logger = repl->would_log(util::Logger::Level::debug)) {
logger->log(LogCategory::object, util::Logger::Level::debug, "Cancel tombstone on '%1': %2",
get_class_name(), unres_key);
}
}

auto tombstone = m_tombstones->get(unres_key);
ret.assign_pk_and_backlinks(tombstone);
// If tombstones had no links to it, it may still be alive
Expand Down Expand Up @@ -2638,6 +2645,13 @@ Obj Table::get_or_create_tombstone(ObjKey key, ColKey pk_col, Mixed pk_val)
}
return tombstone;
}
if (Replication* repl = get_repl()) {
if (auto logger = repl->would_log(util::Logger::Level::debug)) {
logger->log(LogCategory::object, util::Logger::Level::debug,
"Create tombstone for object '%1' with primary key %2 : %3", get_class_name(), pk_val,
unres_key);
}
}
return m_tombstones->insert(unres_key, {{pk_col, pk_val}});
}

Expand Down
20 changes: 19 additions & 1 deletion test/test_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,13 @@ TEST(List_NestedCollection_Links)
auto origin = tr->add_table("origin");
auto list_col = origin->add_column_list(type_Mixed, "any_list");
auto any_col = origin->add_column(type_Mixed, "any");
auto any1_col = origin->add_column(type_Mixed, "any1");
auto embedded_col = origin->add_column(*embedded, "sub");

Obj target_obj1 = target->create_object();
Obj target_obj2 = target->create_object();
Obj target_obj3 = target->create_object();
Obj target_obj4 = target->create_object();
Obj parent = origin->create_object();
parent.create_and_set_linked_object(embedded_col);
auto child_obj = parent.get_linked_object(embedded_col);
Expand Down Expand Up @@ -877,6 +879,8 @@ TEST(List_NestedCollection_Links)
dict_any = o.get_dictionary(any_col);
dict_any.insert("Godbye", target_obj1.get_link());
CHECK_THROW_ANY(dict_any.insert("Wrong", child_obj.get_link()));
o.set_collection(any1_col, CollectionType::List);
o.get_list<Mixed>(any1_col).add(target_obj4.get_link());

// Create link from a list nested in a collection nested in a Mixed property
dict_any.insert_collection("List", CollectionType::List);
Expand All @@ -887,6 +891,7 @@ TEST(List_NestedCollection_Links)
CHECK_EQUAL(target_obj1.get_backlink_count(), 2);
CHECK_EQUAL(target_obj2.get_backlink_count(), 1);
CHECK_EQUAL(target_obj3.get_backlink_count(), 1);
CHECK_EQUAL(target_obj4.get_backlink_count(), 1);
};

create_links();
Expand Down Expand Up @@ -917,15 +922,28 @@ TEST(List_NestedCollection_Links)
CHECK_EQUAL(target_obj2.get_backlink_count(), 1);
o.remove();
CHECK_EQUAL(target_obj2.get_backlink_count(), 0);
CHECK_EQUAL(target_obj4.get_backlink_count(), 0);
tr->commit_and_continue_as_read();

create_links();
// Clearing dictionary should remove links
tr->promote_to_write();
dict_any.clear();
tr->commit_and_continue_as_read();
CHECK_EQUAL(target_obj1.get_backlink_count(), 1);
CHECK_EQUAL(target_obj3.get_backlink_count(), 0);
o.remove();
tr->commit_and_continue_as_read();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like a comment explaining the scenario added here

create_links();
tr->promote_to_write();
// Removing the top object should remove all backlinks.
// This includes the links contained in the collections
// held by the any (dictionary) and any1 (list) properties.
o.remove();
CHECK_EQUAL(target_obj1.get_backlink_count(), 0);
CHECK_EQUAL(target_obj2.get_backlink_count(), 0);
CHECK_EQUAL(target_obj3.get_backlink_count(), 0);
CHECK_EQUAL(target_obj4.get_backlink_count(), 0);
}

TEST(List_NestedCollection_Unresolved)
Expand Down
Loading