-
Notifications
You must be signed in to change notification settings - Fork 163
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
Changes from 4 commits
0634e94
e30c1dc
be7f753
eeb942d
98201ca
b8cac8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not class?