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

Refactor link tracing code #5796

Merged
merged 6 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
* Prevent migrations to an embedded object type when there are incoming links from Mixed/TypedLink properties until we can support them. ([#5796](https://github.com/realm/realm-core/pull/5796))
* Fixed undefined behaviour on queries involving a constant and an indexed column on some property types like UUID and Timestamp. ([#5753](https://github.com/realm/realm-core/issues/5753), since 12.5.0)
* Fix all UBSan failures hit by tests. It is unclear if any of these manifested as visible bugs. ([PR #5665](https://github.com/realm/realm-core/pull/5665))
* Fix sorting order for `realm_query_find_first` in the C API.([#5720](https://github.com/realm/realm-core/issues/5720))
* Upload completion callbacks may have called before the download message that completed them was fully integrated. ([#4865](https://github.com/realm/realm-core/issues/4865)).


### Breaking changes
* None.

Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ let notSyncServerSources: [String] = [
"realm/history.cpp",
"realm/impl",
"realm/index_string.cpp",
"realm/link_translator.cpp",
"realm/list.cpp",
"realm/mixed.cpp",
"realm/node.cpp",
Expand Down
1 change: 1 addition & 0 deletions src/realm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set(REALM_SOURCES
impl/simulated_failure.cpp
impl/transact_log.cpp
index_string.cpp
link_translator.cpp
list.cpp
node.cpp
mixed.cpp
Expand Down
94 changes: 94 additions & 0 deletions src/realm/link_translator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*************************************************************************
*
* Copyright 2022 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/

#include "realm/link_translator.hpp"

#include <realm/dictionary.hpp>
#include <realm/list.hpp>
#include <realm/set.hpp>

namespace realm {

LinkTranslator::LinkTranslator(Obj origin, ColKey origin_col_key)
: m_origin_obj(origin)
, m_origin_col_key(origin_col_key)
{
}

void LinkTranslator::run()
{
ColumnAttrMask attr = m_origin_col_key.get_attrs();
if (attr.test(col_attr_List)) {
if (m_origin_col_key.get_type() == col_type_LinkList) {
LnkLst link_list = m_origin_obj.get_linklist(m_origin_col_key);
on_list_of_links(link_list);
}
else if (m_origin_col_key.get_type() == col_type_Mixed) {
Lst<Mixed> list = m_origin_obj.get_list<Mixed>(m_origin_col_key);
on_list_of_mixed(list);
}
else if (m_origin_col_key.get_type() == col_type_TypedLink) {
Lst<ObjLink> list = m_origin_obj.get_list<ObjLink>(m_origin_col_key);
on_list_of_typedlink(list);
}
else {
throw std::runtime_error(
util::format("LinkTranslator unhandled list type: %1", m_origin_col_key.get_type()));
}
}
else if (attr.test(col_attr_Set)) {
if (m_origin_col_key.get_type() == col_type_Link) {
LnkSet set = m_origin_obj.get_linkset(m_origin_col_key);
on_set_of_links(set);
}
else if (m_origin_col_key.get_type() == col_type_Mixed) {
Set<Mixed> set = m_origin_obj.get_set<Mixed>(m_origin_col_key);
on_set_of_mixed(set);
}
else if (m_origin_col_key.get_type() == col_type_TypedLink) {
Set<ObjLink> set = m_origin_obj.get_set<ObjLink>(m_origin_col_key);
on_set_of_typedlink(set);
}
else {
throw std::runtime_error(
util::format("LinkTranslator unhandled set type: %1", m_origin_col_key.get_type()));
}
}
else if (attr.test(col_attr_Dictionary)) {
auto dict = m_origin_obj.get_dictionary(m_origin_col_key);
on_dictionary(dict);
}
else {
REALM_ASSERT_EX(!m_origin_col_key.is_collection(), m_origin_col_key);
if (m_origin_col_key.get_type() == col_type_Link) {
on_link_property(m_origin_col_key);
}
else if (m_origin_col_key.get_type() == col_type_Mixed) {
on_mixed_property(m_origin_col_key);
}
else if (m_origin_col_key.get_type() == col_type_TypedLink) {
on_typedlink_property(m_origin_col_key);
}
else {
throw std::runtime_error(
util::format("LinkTranslator unhandled property type: %1", m_origin_col_key.get_type()));
}
}
}

} // namespace realm
47 changes: 47 additions & 0 deletions src/realm/link_translator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*************************************************************************
*
* Copyright 2022 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/

#ifndef REALM_LINK_TRANSLATOR_HPP
#define REALM_LINK_TRANSLATOR_HPP

#include <realm/obj.hpp>

namespace realm {

struct LinkTranslator {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not class?

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment on how and where this class is supposed to be used

LinkTranslator(Obj origin, ColKey origin_col_key);
void run();
virtual void on_list_of_links(LnkLst list) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should use references for the collection types

virtual void on_list_of_mixed(Lst<Mixed> list) = 0;
virtual void on_list_of_typedlink(Lst<ObjLink> list) = 0;
virtual void on_set_of_links(LnkSet set) = 0;
virtual void on_set_of_mixed(Set<Mixed> set) = 0;
virtual void on_set_of_typedlink(Set<ObjLink> set) = 0;
virtual void on_dictionary(Dictionary dict) = 0;
virtual void on_link_property(ColKey col) = 0;
virtual void on_mixed_property(ColKey col) = 0;
virtual void on_typedlink_property(ColKey col) = 0;

protected:
Obj m_origin_obj;
ColKey m_origin_col_key;
};

} // namespace realm

#endif // REALM_LINK_TRANSLATOR_HPP
2 changes: 2 additions & 0 deletions src/realm/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ void Lst<ObjKey>::do_clear()
target_obj.remove_one_backlink(backlink_col, m_obj.get_key()); // Throws
size_t num_remaining = target_obj.get_backlink_count(*origin_table, m_col_key);
if (num_remaining == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps the above check can be removed?

// embedded objects should only have one incoming link
REALM_ASSERT_EX(target_obj.get_backlink_count() == 0, target_obj.get_backlink_count());
state.m_to_be_deleted.emplace_back(target_table_key, target_key);
}
}
Expand Down
Loading