Ditto 4.11.0
 
Loading...
Searching...
No Matches
Store.hpp
1#ifndef _DITTO_STORE_
2#define _DITTO_STORE_
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 {
27class DittoHandleWrapper;
28// clang-format off
62// clang-format on
63class Store {
64 friend class AttachmentFetcher;
65 friend class Ditto;
66 friend class StoreObserver;
67 friend class Transaction;
68
69 static bool finalize_attachment_fetcher(
70 std::shared_ptr<AttachmentFetcher> const &attachment_fetcher,
71 std::shared_ptr<std::set<std::shared_ptr<AttachmentFetcher>>> const
72 &attachment_fetchers,
73 std::lock_guard<std::mutex> const &lock);
74
75 std::shared_ptr<DittoHandleWrapper> ditto_handle_wrapper;
76 DiskUsage disk_usage;
77 Store(std::shared_ptr<DittoHandleWrapper> ditto_handle_wrapper);
78 void set_weak_ditto_fields(std::weak_ptr<Ditto::Fields> weak_ditto_fields);
79 std::weak_ptr<Ditto::Fields> weak_ditto_fields;
80
81 // Lock responsible for synchronizing access to the attachment_fetchers set.
82 std::shared_ptr<std::mutex> attachment_fetchers_lock;
83 mutable std::shared_ptr<std::set<std::shared_ptr<AttachmentFetcher>>>
84 attachment_fetchers;
85
86 bool unregister_observer(std::shared_ptr<StoreObserver>);
87
88#ifdef DITTO_INTERNAL_TESTING
89public:
90#endif
91 std::vector<TransactionInfo> get_transactions() const;
92
93private:
94public:
95 Store();
96
100 std::set<std::shared_ptr<StoreObserver>> observers;
101
105 std::set<std::shared_ptr<AttachmentFetcher>> get_attachment_fetchers() const;
106
118 Collection collection(std::string name) const;
119
129 std::vector<std::unique_ptr<WriteTransactionResult>>
130 write(std::function<void(WriteTransaction &)> fn) const;
131
140
147
161 QueryResult execute(std::string query, nlohmann::json query_args = nullptr);
162
179 std::shared_ptr<StoreObserver>
180 register_observer(std::string query, StoreObservationHandler change_handler);
181
202 std::shared_ptr<StoreObserver>
203 register_observer(std::string query, nlohmann::json query_args,
204 StoreObservationHandler change_handler);
205
220 std::shared_ptr<StoreObserver>
221 register_observer(std::string query,
222 StoreObservationHandlerWithNextSignal change_handler);
223
241 std::shared_ptr<StoreObserver>
242 register_observer(std::string query, nlohmann::json query_args,
243 StoreObservationHandlerWithNextSignal change_handler);
244
280 Attachment new_attachment(std::string path,
281 std::map<std::string, std::string> metadata =
282 std::map<std::string, std::string>()) const;
283
314 std::shared_ptr<AttachmentFetcher>
315 fetch_attachment(std::shared_ptr<AttachmentToken> token,
316 AttachmentFetcherEventHandler event_handler) const;
317
348 std::shared_ptr<AttachmentFetcher>
349 fetch_attachment(std::unordered_map<std::string, any> map,
350 AttachmentFetcherEventHandler event_handler) const;
351
352 // clang-format off
407 // clang-format on
408 TransactionCompletionAction transaction(
409 const TransactionOptions &options,
410 const std::function<TransactionCompletionAction(Transaction &)> &fn);
411
417 TransactionCompletionAction transaction(
418 const std::function<TransactionCompletionAction(Transaction &)> &fn);
419
428 void execute_transaction(const TransactionOptions &options,
429 const std::function<void(Transaction &)> &fn);
430
438 void execute_transaction(const std::function<void(Transaction &)> &fn);
439
450 template <typename T>
452 const std::function<T(Transaction &)> &fn) {
453 std::promise<T> promise;
454 auto future = promise.get_future();
455 transaction(options,
456 [&promise,
457 &fn](Transaction &transaction) -> TransactionCompletionAction {
458 try {
459 promise.set_value(fn(transaction));
460 return TransactionCompletionAction::commit;
461 } catch (const std::exception &e) {
462 promise.set_exception(std::current_exception());
463 throw;
464 }
465 });
466 return future.get();
467 }
468
479 template <typename T>
480 T transaction_returning(const std::function<T(Transaction &)> &fn) {
482 }
483};
484
485} // namespace ditto
486
487#endif
Represents an attachment and can be used to insert the associated attachment into a document at a spe...
Definition Attachment.hpp:22
A reference to a collection in a Store.
Definition Collection.hpp:27
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:58
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.
Definition Store.cpp:113
T transaction_returning(const std::function< T(Transaction &)> &fn)
Convenience method, same as transaction(std::function<TransactionCompletionAction(Transaction &)>,...
Definition Store.hpp:480
TransactionCompletionAction transaction(const TransactionOptions &options, const std::function< TransactionCompletionAction(Transaction &)> &fn)
Executes multiple DQL queries within a single atomic transaction.
Definition Store.cpp:224
DiskUsage get_disk_usage() const
Provides access to the Store's disk usage.
Definition Store.cpp:48
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...
Definition Store.cpp:51
std::set< std::shared_ptr< AttachmentFetcher > > get_attachment_fetchers() const
Returns a copy of all currently active attachment fetchers.
Definition Store.cpp:189
PendingCollectionsOperation collections() const
Returns an object that lets you fetch or observe the collections in the store.
Definition Store.cpp:44
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...
Definition Store.cpp:82
void execute_transaction(const TransactionOptions &options, const std::function< void(Transaction &)> &fn)
Convenience function, same as transaction(TransactionOptions, std::function<TransactionCompletionActi...
Definition Store.cpp:235
std::set< std::shared_ptr< StoreObserver > > observers
Returns a copy of all currently active store observers.
Definition Store.hpp:100
QueryResult execute(std::string query, nlohmann::json query_args=nullptr)
Executes a DQL query and returns matching items as a query result.
Definition Store.cpp:63
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.
Definition Store.cpp:139
Collection collection(std::string name) const
Returns a Collection with the provided name.
Definition Store.cpp:40
T transaction_returning(const TransactionOptions &options, const std::function< T(Transaction &)> &fn)
Convenience method, same as transaction(TransactionOptions, std::function<TransactionCompletionAction...
Definition Store.hpp:451
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:25
A thin wrapper around a function that will get called when there are updates relating to an attempt t...
Definition AttachmentFetcher.hpp:19