1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2015 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 #ifndef REALM_RESULTS_NOTIFIER_HPP
20 #define REALM_RESULTS_NOTIFIER_HPP
22 #include "collection_notifier.hpp"
23 #include "results.hpp"
25 #include <realm/group_shared.hpp>
29 class ResultsNotifier : public CollectionNotifier {
31 ResultsNotifier(Results& target);
33 void target_results_moved(Results& old_target, Results& new_target);
36 // Target Results to update
37 // Can only be used with lock_target() held
38 Results* m_target_results;
40 // The source Query, in handover form iff m_sg is null
41 std::unique_ptr<SharedGroup::Handover<Query>> m_query_handover;
42 std::unique_ptr<Query> m_query;
44 DescriptorOrdering::HandoverPatch m_ordering_handover;
45 DescriptorOrdering m_descriptor_ordering;
46 bool m_target_is_in_table_order;
48 // The TableView resulting from running the query. Will be detached unless
49 // the query was (re)run since the last time the handover object was created
51 std::unique_ptr<SharedGroup::Handover<TableView>> m_tv_handover;
52 std::unique_ptr<SharedGroup::Handover<TableView>> m_tv_to_deliver;
54 // The table version from the last time the query was run. Used to avoid
55 // rerunning the query when there's no chance of it changing.
56 uint_fast64_t m_last_seen_version = -1;
58 // The rows from the previous run of the query, for calculating diffs
59 std::vector<size_t> m_previous_rows;
61 // The changeset calculated during run() and delivered in do_prepare_handover()
62 CollectionChangeBuilder m_changes;
63 TransactionChangeInfo* m_info = nullptr;
66 void calculate_changes();
67 void deliver(SharedGroup&) override;
70 void do_prepare_handover(SharedGroup&) override;
71 bool do_add_required_change_info(TransactionChangeInfo& info) override;
72 bool prepare_to_deliver() override;
74 void release_data() noexcept override;
75 void do_attach_to(SharedGroup& sg) override;
76 void do_detach_from(SharedGroup& sg) override;
82 #endif /* REALM_RESULTS_NOTIFIER_HPP */