1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2017 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/primitive_list_notifier.hpp"
21 #include "shared_realm.hpp"
23 #include <realm/link_view.hpp>
25 using namespace realm;
26 using namespace realm::_impl;
28 PrimitiveListNotifier::PrimitiveListNotifier(TableRef table, std::shared_ptr<Realm> realm)
29 : CollectionNotifier(std::move(realm))
30 , m_prev_size(table->size())
32 set_table(*table->get_parent_table());
33 m_table_handover = source_shared_group().export_table_for_handover(table);
36 void PrimitiveListNotifier::release_data() noexcept
41 void PrimitiveListNotifier::do_attach_to(SharedGroup& sg)
43 REALM_ASSERT(m_table_handover);
44 REALM_ASSERT(!m_table);
45 m_table = sg.import_table_from_handover(std::move(m_table_handover));
48 void PrimitiveListNotifier::do_detach_from(SharedGroup& sg)
50 REALM_ASSERT(!m_table_handover);
52 m_table_handover = sg.export_table_for_handover(m_table);
57 bool PrimitiveListNotifier::do_add_required_change_info(TransactionChangeInfo& info)
59 REALM_ASSERT(!m_table_handover);
60 if (!m_table || !m_table->is_attached()) {
61 return false; // origin row was deleted after the notification was added
64 auto& table = *m_table->get_parent_table();
65 size_t row_ndx = m_table->get_parent_row_index();
66 size_t col_ndx = find_container_column(table, row_ndx, m_table, type_Table, &Table::get_subtable);
67 info.lists.push_back({table.get_index_in_group(), row_ndx, col_ndx, &m_change});
73 void PrimitiveListNotifier::run()
75 if (!m_table || !m_table->is_attached()) {
76 // Table was deleted entirely, so report all of the rows being removed
77 // if this is the first run after that
79 m_change.deletions.set(m_prev_size);
88 if (!m_change.deletions.empty() && m_change.deletions.begin()->second == std::numeric_limits<size_t>::max()) {
89 // Table was cleared, so set the deletions to the actual previous size
90 m_change.deletions.set(m_prev_size);
93 m_prev_size = m_table->size();
96 void PrimitiveListNotifier::do_prepare_handover(SharedGroup&)
98 add_changes(std::move(m_change));