summaryrefslogtreecommitdiffstats
path: root/common/storage.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-03-03 09:01:05 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-03-03 09:01:05 +0100
commit4d9746c828558c9f872e0aed52442863affb25d5 (patch)
tree507d7c2ba67f47d3cbbcf01a722236ff1b48426b /common/storage.h
parent9cea920b7dd51867a0be0fed2f461b6be73c103e (diff)
downloadsink-4d9746c828558c9f872e0aed52442863affb25d5.tar.gz
sink-4d9746c828558c9f872e0aed52442863affb25d5.zip
Fromatted the whole codebase with clang-format.
clang-format -i */**{.cpp,.h}
Diffstat (limited to 'common/storage.h')
-rw-r--r--common/storage.h77
1 files changed, 41 insertions, 36 deletions
diff --git a/common/storage.h b/common/storage.h
index 663d192..b051daa 100644
--- a/common/storage.h
+++ b/common/storage.h
@@ -26,14 +26,19 @@
26#include <functional> 26#include <functional>
27#include <QString> 27#include <QString>
28 28
29namespace Sink 29namespace Sink {
30{
31 30
32class SINK_EXPORT Storage { 31class SINK_EXPORT Storage
32{
33public: 33public:
34 enum AccessMode { ReadOnly, ReadWrite }; 34 enum AccessMode
35 {
36 ReadOnly,
37 ReadWrite
38 };
35 39
36 enum ErrorCodes { 40 enum ErrorCodes
41 {
37 GenericError, 42 GenericError,
38 NotOpen, 43 NotOpen,
39 ReadOnlyError, 44 ReadOnlyError,
@@ -44,8 +49,9 @@ public:
44 class Error 49 class Error
45 { 50 {
46 public: 51 public:
47 Error(const QByteArray &s, int c, const QByteArray &m) 52 Error(const QByteArray &s, int c, const QByteArray &m) : store(s), message(m), code(c)
48 : store(s), message(m), code(c) {} 53 {
54 }
49 QByteArray store; 55 QByteArray store;
50 QByteArray message; 56 QByteArray message;
51 int code; 57 int code;
@@ -65,13 +71,11 @@ public:
65 /** 71 /**
66 * Remove a key 72 * Remove a key
67 */ 73 */
68 void remove(const QByteArray &key, 74 void remove(const QByteArray &key, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
69 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
70 /** 75 /**
71 * Remove a key-value pair 76 * Remove a key-value pair
72 */ 77 */
73 void remove(const QByteArray &key, const QByteArray &value, 78 void remove(const QByteArray &key, const QByteArray &value, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
74 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
75 79
76 /** 80 /**
77 * Read values with a given key. 81 * Read values with a given key.
@@ -82,9 +86,8 @@ public:
82 * 86 *
83 * @return The number of values retrieved. 87 * @return The number of values retrieved.
84 */ 88 */
85 int scan(const QByteArray &key, 89 int scan(const QByteArray &key, const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler,
86 const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, 90 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>(), bool findSubstringKeys = false) const;
87 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>(), bool findSubstringKeys = false) const;
88 91
89 /** 92 /**
90 * Finds the last value in a series matched by prefix. 93 * Finds the last value in a series matched by prefix.
@@ -92,28 +95,29 @@ public:
92 * This is used to match by uid prefix and find the highest revision. 95 * This is used to match by uid prefix and find the highest revision.
93 * Note that this relies on a key scheme like $uid$revision. 96 * Note that this relies on a key scheme like $uid$revision.
94 */ 97 */
95 void findLatest(const QByteArray &uid, 98 void findLatest(const QByteArray &uid, const std::function<void(const QByteArray &key, const QByteArray &value)> &resultHandler,
96 const std::function<void(const QByteArray &key, const QByteArray &value)> &resultHandler, 99 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()) const;
97 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()) const;
98 100
99 /** 101 /**
100 * Returns true if the database contains the substring key. 102 * Returns true if the database contains the substring key.
101 */ 103 */
102 bool contains(const QByteArray &uid); 104 bool contains(const QByteArray &uid);
103 105
104 NamedDatabase(NamedDatabase&& other) : d(other.d) 106 NamedDatabase(NamedDatabase &&other) : d(other.d)
105 { 107 {
106 d = other.d; 108 d = other.d;
107 other.d = nullptr; 109 other.d = nullptr;
108 } 110 }
109 111
110 NamedDatabase& operator=(NamedDatabase&& other) { 112 NamedDatabase &operator=(NamedDatabase &&other)
113 {
111 d = other.d; 114 d = other.d;
112 other.d = nullptr; 115 other.d = nullptr;
113 return *this; 116 return *this;
114 } 117 }
115 118
116 operator bool() const { 119 operator bool() const
120 {
117 return (d != nullptr); 121 return (d != nullptr);
118 } 122 }
119 123
@@ -121,10 +125,10 @@ public:
121 125
122 private: 126 private:
123 friend Transaction; 127 friend Transaction;
124 NamedDatabase(NamedDatabase& other); 128 NamedDatabase(NamedDatabase &other);
125 NamedDatabase& operator=(NamedDatabase& other); 129 NamedDatabase &operator=(NamedDatabase &other);
126 class Private; 130 class Private;
127 NamedDatabase(Private*); 131 NamedDatabase(Private *);
128 Private *d; 132 Private *d;
129 }; 133 };
130 134
@@ -138,37 +142,39 @@ public:
138 142
139 QList<QByteArray> getDatabaseNames() const; 143 QList<QByteArray> getDatabaseNames() const;
140 144
141 NamedDatabase openDatabase(const QByteArray &name = QByteArray("default"), const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>(), bool allowDuplicates = false) const; 145 NamedDatabase openDatabase(const QByteArray &name = QByteArray("default"),
146 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>(), bool allowDuplicates = false) const;
142 147
143 Transaction(Transaction&& other) : d(other.d) 148 Transaction(Transaction &&other) : d(other.d)
144 { 149 {
145 d = other.d; 150 d = other.d;
146 other.d = nullptr; 151 other.d = nullptr;
147 } 152 }
148 Transaction& operator=(Transaction&& other) { 153 Transaction &operator=(Transaction &&other)
154 {
149 d = other.d; 155 d = other.d;
150 other.d = nullptr; 156 other.d = nullptr;
151 return *this; 157 return *this;
152 } 158 }
153 159
154 operator bool() const { 160 operator bool() const
161 {
155 return (d != nullptr); 162 return (d != nullptr);
156 } 163 }
157 164
158 private: 165 private:
159 Transaction(Transaction& other); 166 Transaction(Transaction &other);
160 Transaction& operator=(Transaction& other); 167 Transaction &operator=(Transaction &other);
161 friend Storage; 168 friend Storage;
162 class Private; 169 class Private;
163 Transaction(Private*); 170 Transaction(Private *);
164 Private *d; 171 Private *d;
165 }; 172 };
166 173
167 Storage(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly); 174 Storage(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly);
168 ~Storage(); 175 ~Storage();
169 176
170 Transaction createTransaction(AccessMode mode = ReadWrite, 177 Transaction createTransaction(AccessMode mode = ReadWrite, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
171 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
172 178
173 /** 179 /**
174 * Set the default error handler. 180 * Set the default error handler.
@@ -178,7 +184,7 @@ public:
178 184
179 /** 185 /**
180 * A basic error handler that writes to std::cerr. 186 * A basic error handler that writes to std::cerr.
181 * 187 *
182 * Used if nothing else is configured. 188 * Used if nothing else is configured.
183 */ 189 */
184 static std::function<void(const Storage::Error &error)> basicErrorHandler(); 190 static std::function<void(const Storage::Error &error)> basicErrorHandler();
@@ -188,7 +194,7 @@ public:
188 194
189 /** 195 /**
190 * Clears all cached environments. 196 * Clears all cached environments.
191 * 197 *
192 * This only ever has to be called if a database was removed from another process. 198 * This only ever has to be called if a database was removed from another process.
193 */ 199 */
194 static void clearEnv(); 200 static void clearEnv();
@@ -220,8 +226,7 @@ private:
220 226
221private: 227private:
222 class Private; 228 class Private;
223 Private * const d; 229 Private *const d;
224}; 230};
225 231
226} // namespace Sink 232} // namespace Sink
227