Ditto 4.13.1
 
Loading...
Searching...
No Matches
Store.hpp
1#ifndef DITTO_STORE_H
2#define DITTO_STORE_H
3
4#include "Attachment.hpp"
5#include "AttachmentFetchEvent.hpp"
6#include "AttachmentFetcher.hpp"
7#include "AttachmentToken.hpp"
8#include "Collection.hpp"
9#include "DiskUsage.hpp"
10#include "Ditto.hpp"
11#include "PendingCollectionsOperation.hpp"
12#include "QueryResult.hpp"
13#include "StoreObserver.hpp"
14#include "Transaction.hpp"
15#include "WriteTransaction.hpp"
16#include "WriteTransactionResult.hpp"
17
18#include <functional>
19#include <future>
20#include <memory>
21#include <mutex>
22#include <set>
23#include <string>
24#include <vector>
25
26namespace ditto {
27
28class DittoHandleWrapper;
29
30// clang-format off
64// clang-format on
65class Store {
66 friend class AttachmentFetcher;
67 friend class Ditto;
68 friend class StoreObserver;
69 friend class Transaction;
70
71 static bool finalize_attachment_fetcher(
72 std::shared_ptr<AttachmentFetcher> const &attachment_fetcher,
73 std::shared_ptr<std::set<std::shared_ptr<AttachmentFetcher>>> const
74 &attachment_fetchers,
75 std::lock_guard<std::mutex> const &lock);
76
77 std::shared_ptr<DittoHandleWrapper> ditto_handle_wrapper;
78 DiskUsage disk_usage;
79 explicit Store(std::shared_ptr<DittoHandleWrapper> ditto_handle_wrapper);
80 void set_weak_ditto_fields(std::weak_ptr<Ditto::Fields> weak_ditto_fields);
81 std::weak_ptr<Ditto::Fields> weak_ditto_fields;
82
83 // Lock responsible for synchronizing access to the attachment_fetchers set.
84 std::shared_ptr<std::mutex> attachment_fetchers_lock;
85 mutable std::shared_ptr<std::set<std::shared_ptr<AttachmentFetcher>>>
86 attachment_fetchers;
87
88 // Lock responsible for synchronizing access to the observers set.
89 std::shared_ptr<std::mutex> observers_lock;
90 bool unregister_observer(std::shared_ptr<StoreObserver>);
91
92#ifdef DITTO_INTERNAL_TESTING
93public:
94#endif
95 std::vector<TransactionInfo> get_transactions() const;
96
97private:
98public:
99 // TODO(v5): remove this unused public constructor
100 DITTO_DEPRECATED_BECAUSE(
101 "Use `Ditto::get_store()` to access its `Store` instance.")
107 Store();
108
109 DITTO_DISABLE_DEPRECATED_WARNINGS("`observers` is deprecated")
110 Store(const Store &other) = default;
111 Store(Store &&other) noexcept = default;
112 Store &operator=(const Store &other) = default;
113 Store &operator=(Store &&other) noexcept = default;
114 ~Store() = default;
115 DITTO_REENABLE_WARNINGS
116
122 std::set<std::shared_ptr<StoreObserver>> get_observers() const;
123
124 // NOLINTBEGIN
125 // TODO(v5): make this data member private. When it becomes private,
126 // the deprecation marker can be removed.
127 DITTO_DEPRECATED_BECAUSE("Use `Store::get_observers()` instead")
137 std::set<std::shared_ptr<StoreObserver>> observers;
138 // NOLINTEND
139
143
144 std::set<std::shared_ptr<AttachmentFetcher>> get_attachment_fetchers() const;
145
146 DITTO_DISABLE_DEPRECATED_WARNINGS("`Collection` is deprecated")
161 DITTO_DEPRECATED_BECAUSE(
162 "Use DQL (Ditto Query Language) instead. For more information see: "
163 "https://ditto.com/link/legacy-to-dql-guide")
164 Collection collection(std::string name) const;
165 DITTO_REENABLE_WARNINGS
166
167 DITTO_DISABLE_DEPRECATED_WARNINGS("WriteTransaction is deprecated")
180 DITTO_DEPRECATED_BECAUSE(
181 "Use DQL (Ditto Query Language) instead. For more information see: "
182 "https://ditto.com/link/legacy-to-dql-guide")
183 std::vector<std::unique_ptr<WriteTransactionResult>>
184 write(std::function<void(WriteTransaction &)> fn) const;
185 DITTO_REENABLE_WARNINGS
186
187 DITTO_DISABLE_DEPRECATED_WARNINGS("PendingCollectionsOperation is deprecated")
198 DITTO_DEPRECATED_BECAUSE(
199 "Use DQL \"SELECT * from system::collections\" instead.For more "
200 "information see: https://ditto.com/link/legacy-to-dql-guide")
202 DITTO_REENABLE_WARNINGS
203
210
224 QueryResult execute(std::string query, nlohmann::json query_args = nullptr);
225
242 std::shared_ptr<StoreObserver>
243 register_observer(std::string query, StoreObservationHandler change_handler);
244
265 std::shared_ptr<StoreObserver>
266 register_observer(std::string query, nlohmann::json query_args,
267 StoreObservationHandler change_handler);
268
283 std::shared_ptr<StoreObserver>
284 register_observer(std::string query,
286
304 std::shared_ptr<StoreObserver>
305 register_observer(std::string query, nlohmann::json query_args,
307
343 Attachment new_attachment(std::string path,
344 std::map<std::string, std::string> metadata =
345 std::map<std::string, std::string>()) const;
346
377 std::shared_ptr<AttachmentFetcher>
378 fetch_attachment(std::shared_ptr<AttachmentToken> token,
379 AttachmentFetcherEventHandler event_handler) const;
380
411 std::shared_ptr<AttachmentFetcher>
412 fetch_attachment(std::unordered_map<std::string, any> map,
413 AttachmentFetcherEventHandler event_handler) const;
414
415 // clang-format off
470 // clang-format on
472 const TransactionOptions &options,
473 const std::function<TransactionCompletionAction(Transaction &)> &fn);
474
481 const std::function<TransactionCompletionAction(Transaction &)> &fn);
482
492 const std::function<void(Transaction &)> &fn);
493
501 void execute_transaction(const std::function<void(Transaction &)> &fn);
502
513 template <typename T>
515 const std::function<T(Transaction &)> &fn) {
516 std::promise<T> promise;
517 auto future = promise.get_future();
518 transaction(options,
519 [&promise,
520 &fn](Transaction &transaction) -> TransactionCompletionAction {
521 try {
522 promise.set_value(fn(transaction));
524 } catch (const std::exception &e) {
525 promise.set_exception(std::current_exception());
526 throw;
527 }
528 });
529 return future.get();
530 }
531
542 template <typename T>
543 T transaction_returning(const std::function<T(Transaction &)> &fn) {
545 }
546};
547
548} // namespace ditto
549
550#endif
Represents an attachment and can be used to insert the associated attachment into a document at a spe...
Definition Attachment.hpp:22
Serves as a token for a specific attachment that you can pass to a call to Store::fetch_attachment.
Definition AttachmentToken.hpp:18
A reference to a collection in a Store.
Definition Collection.hpp:31
Provides an interface to be able to monitor local files.
Definition DiskUsage.hpp:66
These objects are returned when calling collections() on Store objects.
Definition PendingCollectionsOperation.hpp:87
Represents the returned results when executing a DQL query, containing a QueryResultItem for each mat...
Definition QueryResult.hpp:121
Attachment new_attachment(std::string path, std::map< std::string, std::string > metadata=std::map< std::string, std::string >()) const
Creates a new attachment, which can then be inserted into a document.
T transaction_returning(const std::function< T(Transaction &)> &fn)
Convenience method, same as transaction(std::function<TransactionCompletionAction(Transaction &)>,...
Definition Store.hpp:543
TransactionCompletionAction transaction(const TransactionOptions &options, const std::function< TransactionCompletionAction(Transaction &)> &fn)
Executes multiple DQL queries within a single atomic transaction.
DiskUsage get_disk_usage() const
Provides access to the Store's disk usage.
std::set< std::shared_ptr< StoreObserver > > get_observers() const
Return a copy of all currently active store observers.
std::vector< std::unique_ptr< WriteTransactionResult > > write(std::function< void(WriteTransaction &)> fn) const
Allows you to group multiple operations together that affect multiple documents, potentially across m...
std::set< std::shared_ptr< AttachmentFetcher > > get_attachment_fetchers() const
Returns a copy of all currently active attachment fetchers.
PendingCollectionsOperation collections() const
Returns an object that lets you fetch or observe the collections in the store.
std::shared_ptr< StoreObserver > register_observer(std::string query, StoreObservationHandler change_handler)
Installs and returns a change observer for a query, configuring Ditto to call the provided change han...
void execute_transaction(const TransactionOptions &options, const std::function< void(Transaction &)> &fn)
Convenience function, same as transaction(TransactionOptions, std::function<TransactionCompletionActi...
std::set< std::shared_ptr< StoreObserver > > observers
Returns a copy of all currently active store observers.
Definition Store.hpp:137
QueryResult execute(std::string query, nlohmann::json query_args=nullptr)
Executes a DQL query and returns matching items as a query result.
Store()
Default constructor for Store.
std::shared_ptr< AttachmentFetcher > fetch_attachment(std::shared_ptr< AttachmentToken > token, AttachmentFetcherEventHandler event_handler) const
Trigger an attachment to be downloaded locally to the device and observe its progress as it does so.
Collection collection(std::string name) const
Returns a Collection with the provided name.
T transaction_returning(const TransactionOptions &options, const std::function< T(Transaction &)> &fn)
Convenience method, same as transaction(TransactionOptions, std::function<TransactionCompletionAction...
Definition Store.hpp:514
Options for configuring a transaction.
Definition Transaction.hpp:25
WriteTransaction exposes functionality that allows you to perform multiple operations on the store wi...
Definition WriteTransaction.hpp:30
Polyfill of std::any for C++11.
Definition any.hpp:18
Namespace for the Ditto C++ SDK types and functions.
Definition AbstractDocumentPath.hpp:19
std::function< void(QueryResult)> StoreObservationHandler
A function that will get called when there are updates relating to a store observer.
Definition StoreObserver.hpp:25
std::function< void(QueryResult, NextSignal)> StoreObservationHandlerWithNextSignal
A function that will get called when there are updates relating to a store observer,...
Definition StoreObserver.hpp:33
TransactionCompletionAction
Represents an action that completes a transaction, by either committing it or rolling it back.
Definition Transaction.hpp:80
@ commit
Represents the action of committing a transaction.
Definition Transaction.hpp:82
A thin wrapper around a function that will get called when there are updates relating to an attempt t...
Definition AttachmentFetcher.hpp:19
Provides information about the result of an operation on a document that was part of a write transact...
Definition WriteTransactionResult.hpp:35