Skip to content

Commit

Permalink
RCORE-2115 Fix multiple backlinks created by dictionary (#7677)
Browse files Browse the repository at this point in the history
* fix inserting a link multiple times to the same key in a dictionary of mixed creating multiple backlinks
  • Loading branch information
ironage authored and jedelbo committed May 6, 2024
1 parent 77ce9b3 commit 62e617b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/realm/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,10 @@ std::pair<Dictionary::Iterator, bool> Dictionary::insert(Mixed key, Mixed value)
ObjLink old_link;
if (old_entry) {
Mixed old_value = m_values->get(ndx);
if (old_value.is_type(type_TypedLink)) {
old_link = old_value.get<ObjLink>();
}
if (!value.is_same_type(old_value) || value != old_value) {
if (old_value.is_type(type_TypedLink)) {
old_link = old_value.get<ObjLink>();
}
m_values->set(ndx, value);
}
else {
Expand Down
6 changes: 6 additions & 0 deletions test/test_dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,22 @@ TEST(Dictionary_TypedLinks)
CHECK_EQUAL(lady.get_backlink_count(*persons, col_dict), 1);
CHECK_EQUAL(lady.get_backlink(*persons, col_dict, 0), adam.get_key());
CHECK_EQUAL(lady.get_backlink_count(), 1);
// make sure that inserting the same link again does nothing
CHECK_NOT(dict.insert("Pet", lady).second);
CHECK_EQUAL(lady.get_backlink_count(), 1);
lady.remove();
cmp(dict["Pet"], Mixed());

// Reinsert lady
lady = dogs->create_object_with_primary_key("lady");
dict.insert("Pet", lady);
dict.insert("Pet", lady);
CHECK_EQUAL(lady.get_backlink_count(), 1);
lady.invalidate(); // Make lady a tombstone :-(
cmp(dict["Pet"], Mixed());
lady = dogs->create_object_with_primary_key("lady");
cmp(dict["Pet"], Mixed(lady.get_link()));
CHECK_EQUAL(lady.get_backlink_count(), 1);

auto invalid_link = pluto.get_link();
pluto.remove();
Expand Down

0 comments on commit 62e617b

Please sign in to comment.