summaryrefslogtreecommitdiffstats
path: root/common/storage.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-08-09 23:17:42 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-08-09 23:17:42 +0200
commita8263a28f5d3a74581e289289d0807e6b656104b (patch)
tree66d3033df141b0a73d1b89b8345dbcad7ba6eb7f /common/storage.h
parentb6e7be78f3e13275a8f217a4e01b304d97538641 (diff)
downloadsink-a8263a28f5d3a74581e289289d0807e6b656104b.tar.gz
sink-a8263a28f5d3a74581e289289d0807e6b656104b.zip
Transaction class for storage
The beginning of a cleaner and less bare-bones API for the storage. The lifetime of transactions is now handled in (movable) transaction objects.
Diffstat (limited to 'common/storage.h')
-rw-r--r--common/storage.h56
1 files changed, 56 insertions, 0 deletions
diff --git a/common/storage.h b/common/storage.h
index 09365b0..77f11f9 100644
--- a/common/storage.h
+++ b/common/storage.h
@@ -51,8 +51,64 @@ public:
51 int code; 51 int code;
52 }; 52 };
53 53
54 class Transaction
55 {
56 public:
57 ~Transaction();
58 bool commit(const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
59 void abort();
60
61 /**
62 * Write a value
63 */
64 bool write(const QByteArray &key, const QByteArray &value, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
65
66 /**
67 * Remove a value
68 */
69 void remove(const QByteArray &key,
70 const std::function<void(const Storage::Error &error)> &errorHandler);
71 /**
72 * Read values with a given key.
73 *
74 * * An empty @param key results in a full scan
75 * * If duplicates are existing (revisions), all values are returned.
76 * * The pointers of the returned values are valid during the execution of the @param resultHandler
77 *
78 * @return The number of values retrieved.
79 */
80 int scan(const QByteArray &k,
81 const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler,
82 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
83
84 Transaction(Transaction&& other) : d(other.d)
85 {
86 d = other.d;
87 other.d = nullptr;
88 }
89 Transaction& operator=(Transaction&& other) {
90 d = other.d;
91 other.d = nullptr;
92 return *this;
93 }
94 private:
95 Transaction(Transaction& other);
96 Transaction& operator=(Transaction& other);
97 friend Storage;
98 class Private;
99 Transaction();
100 Transaction(Private*);
101 Private *d;
102 };
103
54 Storage(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly, bool allowDuplicates = false); 104 Storage(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly, bool allowDuplicates = false);
55 ~Storage(); 105 ~Storage();
106
107 Transaction createTransaction(AccessMode mode = ReadWrite,
108 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
109
110
111 //Old API
56 bool isInTransaction() const; 112 bool isInTransaction() const;
57 bool startTransaction(AccessMode mode = ReadWrite, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); 113 bool startTransaction(AccessMode mode = ReadWrite, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
58 bool commitTransaction(const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); 114 bool commitTransaction(const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());