Realm C++ SDK Version v2.2.0

flex_sync.hpp

1
2//
3// Copyright 2022 Realm Inc.
4//
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
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
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.
16//
18
19#ifndef CPPREALM_FLEXIBLE_SYNC_HPP
20#define CPPREALM_FLEXIBLE_SYNC_HPP
21
22#include <future>
23#include <type_traits>
24
25#include <cpprealm/internal/bridge/obj.hpp>
26#include <cpprealm/internal/bridge/query.hpp>
27#include <cpprealm/internal/bridge/schema.hpp>
28#include <cpprealm/internal/bridge/realm.hpp>
29#include <cpprealm/internal/bridge/utils.hpp>
30
31#include <cpprealm/macros.hpp>
32#include <cpprealm/results.hpp>
33#include <cpprealm/rbool.hpp>
34
35namespace realm {
36 template <typename>
37 struct object;
38 class rbool;
39
40 namespace sync {
41 class MutableSubscriptionSet;
42 class SubscriptionSet;
43 class SubscriptionStore;
44 class Subscription;
45 }
46
47// A SyncSubscription represents a single query that may be OR'd with other queries on the same object class to be
48// send to the server in a QUERY or IDENT message.
50 // Returns the unique ID for this subscription.
51 std::string identifier;
52 // The name representing this subscription.
53 std::optional<std::string> name;
54 // Returns the timestamp of when this subscription was originally created.
55 std::chrono::time_point<std::chrono::system_clock> created_at;
56 // Returns the timestamp of the last time this subscription was updated by calling update_query.
57 std::chrono::time_point<std::chrono::system_clock> updated_at;
58 // Returns a stringified version of the query associated with this subscription.
59 std::string query_string;
60 // Returns the name of the object class of the query for this subscription.
61 std::string object_class_name;
62 private:
63 sync_subscription(const sync::Subscription&);
64
65 friend struct sync_subscription_set;
67 };
68
69// A MutableSyncSubscriptionSet represents a single query that may be OR'd with other queries on the same object class to be
70// send to the server in a QUERY or IDENT message.
72 private:
73 void insert_or_assign(const std::string& name, const internal::bridge::query&);
74 public:
81
82 // Inserts a new subscription into the set if one does not exist already.
83 // If the `query_fn` parameter is left empty, the subscription will sync *all* objects
84 // for the templated class type.
85 template<typename T>
86 void add(const std::string &name ,
87 std::optional<std::function<rbool(managed<T> &)>> &&query_fn = std::nullopt) {
88 static_assert(sizeof(managed<T>), "Must declare schema for T");
89
90 auto schema = m_realm.get().schema().find(managed<T>::schema.name);
91 auto group = m_realm.get().read_group();
92 auto table_ref = group.get_table(schema.table_key());
93 auto root_query = internal::bridge::query(table_ref);
94
95 if (query_fn) {
96 rbool query = rbool(std::move(root_query));
97 auto query_object = managed<T>::prepare_for_query(m_realm, &query);
98 auto full_query = (*query_fn)(query_object).q;
99 insert_or_assign(name, full_query);
100 } else {
101 insert_or_assign(name, root_query);
102 }
103 }
104
105 // Removes a subscription for a given name. Will throw if subscription does
106 // not exist.
107 void remove(const std::string& name);
108
109 // Finds a subscription for a given name. Will return `std::nullopt` is subscription does
110 // not exist.
111 std::optional<sync_subscription> find(const std::string& name);
112
113 // Updates a subscription for a given name.
114 // Will throw if subscription does not exist.
115 // If the `query_fn` parameter is left empty, the subscription will sync *all* objects
116 // for the templated class type.
117 template <typename T>
118 std::enable_if_t<std::is_base_of_v<object<T>, T>>
119 update_subscription(const std::string& name, std::optional<std::function<rbool(T&)>>&& query_fn = std::nullopt) {
120 remove(name);
121 add(name, std::move(query_fn));
122 }
123
124 // Updates a subscription for a given name.
125 // Will throw if subscription does not exist.
126 // If the `query_fn` parameter is left empty, the subscription will sync *all* objects
127 // for the templated class type.
128 template <typename T>
129 void update_subscription(const std::string& name,
130 std::optional<std::function<rbool(managed<T>&)>>&& query_fn = std::nullopt) {
131 remove(name);
132 add(name, std::move(query_fn));
133 }
134
135 // Removes all subscriptions.
136 void clear();
137
138 private:
139 mutable_sync_subscription_set(internal::bridge::realm&, const sync::MutableSubscriptionSet& subscription_set);
140#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES
141 internal::bridge::storage::MutableSyncSubscriptionSet m_subscription_set[1];
142#else
143 std::shared_ptr<sync::MutableSubscriptionSet> m_subscription_set;
144#endif
145 std::reference_wrapper<internal::bridge::realm> m_realm;
146 friend struct sync_subscription_set;
147 sync::MutableSubscriptionSet get_subscription_set();
148 };
149
151 public:
152 sync_subscription_set() = delete;
153 sync_subscription_set(const sync_subscription_set& other) = delete;
154 sync_subscription_set& operator=(const sync_subscription_set& other) ;
158
160 [[nodiscard]] size_t size() const;
161
162 // Finds a subscription for a given name. Will return `std::nullopt` is subscription does
163 // not exist.
164 std::optional<sync_subscription> find(const std::string& name);
165
166 std::future<bool> update(std::function<void(mutable_sync_subscription_set&)>&& fn);
167
169 private:
170 friend struct db;
171#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES
172 internal::bridge::storage::SyncSubscriptionSet m_subscription_set[1];
173#else
174 std::shared_ptr<sync::SubscriptionSet> m_subscription_set;
175#endif
176 std::reference_wrapper<internal::bridge::realm> m_realm;
177 };
178
179} // namespace realm
180
181#endif /* CPPREALM_FLEXIBLE_SYNC_HPP */
Definition: rbool.hpp:36
Definition: db.hpp:63
Definition: query.hpp:131
Definition: realm.hpp:67
Definition: flex_sync.hpp:71
Definition: flex_sync.hpp:37
Definition: flex_sync.hpp:150
size_t size() const
The total number of subscriptions in the set.
Definition: flex_sync.cpp:134
Definition: flex_sync.hpp:49