1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #include "impl/list_notifier.hpp"
21 #include "shared_realm.hpp"
23 #include <realm/link_view.hpp>
25 using namespace realm;
26 using namespace realm::_impl;
28 ListNotifier::ListNotifier(LinkViewRef lv, std::shared_ptr<Realm> realm)
29 : CollectionNotifier(std::move(realm))
30 , m_prev_size(lv->size())
32 set_table(lv->get_target_table());
33 m_lv_handover = source_shared_group().export_linkview_for_handover(lv);
36 void ListNotifier::release_data() noexcept
41 void ListNotifier::do_attach_to(SharedGroup& sg)
43 REALM_ASSERT(m_lv_handover);
45 m_lv = sg.import_linkview_from_handover(std::move(m_lv_handover));
48 void ListNotifier::do_detach_from(SharedGroup& sg)
50 REALM_ASSERT(!m_lv_handover);
52 m_lv_handover = sg.export_linkview_for_handover(m_lv);
57 bool ListNotifier::do_add_required_change_info(TransactionChangeInfo& info)
59 REALM_ASSERT(!m_lv_handover);
60 if (!m_lv || !m_lv->is_attached()) {
61 return false; // origin row was deleted after the notification was added
64 auto& table = m_lv->get_origin_table();
65 size_t row_ndx = m_lv->get_origin_row_index();
66 size_t col_ndx = find_container_column(table, row_ndx, m_lv, type_LinkList, &Table::get_linklist);
67 info.lists.push_back({table.get_index_in_group(), row_ndx, col_ndx, &m_change});
73 void ListNotifier::run()
75 if (!m_lv || !m_lv->is_attached()) {
76 // LV was deleted, so report all of the rows being removed if this is
77 // the first run after that
79 m_change.deletions.set(m_prev_size);
88 auto row_did_change = get_modification_checker(*m_info, m_lv->get_target_table());
89 for (size_t i = 0; i < m_lv->size(); ++i) {
90 if (m_change.modifications.contains(i))
92 if (row_did_change(m_lv->get(i).get_index()))
93 m_change.modifications.add(i);
96 for (auto const& move : m_change.moves) {
97 if (m_change.modifications.contains(move.to))
99 if (row_did_change(m_lv->get(move.to).get_index()))
100 m_change.modifications.add(move.to);
103 m_prev_size = m_lv->size();
106 void ListNotifier::do_prepare_handover(SharedGroup&)
108 add_changes(std::move(m_change));