summaryrefslogtreecommitdiffstats
path: root/common/storage.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-10-16 14:55:20 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-10-21 09:02:21 +0200
commit237b9ae4113e7a9f489632296941becb71afdb45 (patch)
tree01cde58f495944f01cad9d282391d4efd2897141 /common/storage.h
parent95d11bf0be98a4e3c08502fe23417b800233ce14 (diff)
downloadsink-237b9ae4113e7a9f489632296941becb71afdb45.tar.gz
sink-237b9ae4113e7a9f489632296941becb71afdb45.zip
Refactor how the storage is used.
This is the initial refactoring to improve how we deal with the storage. It does a couple of things: * Rename Sink::Storage to Sink::Storage::DataStore to free up the Sink::Storage namespace * Introduce a Sink::ResourceContext to have a single object that can be passed around containing everything that is necessary to operate on a resource. This is a lot better than the multiple separate parameters that we used to pass around all over the place, while still allowing for dependency injection for tests. * Tie storage access together using the new EntityStore that directly works with ApplicationDomainTypes. This gives us a central place where main storage, indexes and buffer adaptors are tied together, which will also give us a place to implement external indexes, such as a fulltextindex using xapian. * Use ApplicationDomainTypes as the default way to pass around entities. Instead of using various ways to pass around entities (buffers, buffer adaptors, ApplicationDomainTypes), only use a single way. The old approach was confusing, and was only done as: * optimization; really shouldn't be necessary and otherwise I'm sure we can find better ways to optimize ApplicationDomainType itself. * a way to account for entities that have multiple buffers, a concept that I no longer deem relevant. While this commit does the bulk of the work to get there, the following commits will refactor more stuff to get things back to normal.
Diffstat (limited to 'common/storage.h')
-rw-r--r--common/storage.h52
1 files changed, 27 insertions, 25 deletions
diff --git a/common/storage.h b/common/storage.h
index 4ef20d5..e368b05 100644
--- a/common/storage.h
+++ b/common/storage.h
@@ -27,8 +27,9 @@
27#include <QString> 27#include <QString>
28 28
29namespace Sink { 29namespace Sink {
30namespace Storage {
30 31
31class SINK_EXPORT Storage 32class SINK_EXPORT DataStore
32{ 33{
33public: 34public:
34 enum AccessMode 35 enum AccessMode
@@ -66,16 +67,16 @@ public:
66 /** 67 /**
67 * Write a value 68 * Write a value
68 */ 69 */
69 bool write(const QByteArray &key, const QByteArray &value, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); 70 bool write(const QByteArray &key, const QByteArray &value, const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>());
70 71
71 /** 72 /**
72 * Remove a key 73 * Remove a key
73 */ 74 */
74 void remove(const QByteArray &key, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); 75 void remove(const QByteArray &key, const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>());
75 /** 76 /**
76 * Remove a key-value pair 77 * Remove a key-value pair
77 */ 78 */
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)>()); 79 void remove(const QByteArray &key, const QByteArray &value, const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>());
79 80
80 /** 81 /**
81 * Read values with a given key. 82 * Read values with a given key.
@@ -87,7 +88,7 @@ public:
87 * @return The number of values retrieved. 88 * @return The number of values retrieved.
88 */ 89 */
89 int scan(const QByteArray &key, const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, 90 int scan(const QByteArray &key, 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, bool skipInternalKeys = true) const; 91 const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>(), bool findSubstringKeys = false, bool skipInternalKeys = true) const;
91 92
92 /** 93 /**
93 * Finds the last value in a series matched by prefix. 94 * Finds the last value in a series matched by prefix.
@@ -96,7 +97,7 @@ public:
96 * Note that this relies on a key scheme like $uid$revision. 97 * Note that this relies on a key scheme like $uid$revision.
97 */ 98 */
98 void findLatest(const QByteArray &uid, const std::function<void(const QByteArray &key, const QByteArray &value)> &resultHandler, 99 void findLatest(const QByteArray &uid, 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; 100 const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>()) const;
100 101
101 /** 102 /**
102 * Returns true if the database contains the substring key. 103 * Returns true if the database contains the substring key.
@@ -127,14 +128,14 @@ public:
127 public: 128 public:
128 Transaction(); 129 Transaction();
129 ~Transaction(); 130 ~Transaction();
130 bool commit(const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); 131 bool commit(const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>());
131 void abort(); 132 void abort();
132 133
133 QList<QByteArray> getDatabaseNames() const; 134 QList<QByteArray> getDatabaseNames() const;
134 bool validateNamedDatabases(); 135 bool validateNamedDatabases();
135 136
136 NamedDatabase openDatabase(const QByteArray &name = QByteArray("default"), 137 NamedDatabase openDatabase(const QByteArray &name = QByteArray("default"),
137 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>(), bool allowDuplicates = false) const; 138 const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>(), bool allowDuplicates = false) const;
138 139
139 Transaction(Transaction &&other); 140 Transaction(Transaction &&other);
140 Transaction &operator=(Transaction &&other); 141 Transaction &operator=(Transaction &&other);
@@ -144,29 +145,29 @@ public:
144 private: 145 private:
145 Transaction(Transaction &other); 146 Transaction(Transaction &other);
146 Transaction &operator=(Transaction &other); 147 Transaction &operator=(Transaction &other);
147 friend Storage; 148 friend DataStore;
148 class Private; 149 class Private;
149 Transaction(Private *); 150 Transaction(Private *);
150 Private *d; 151 Private *d;
151 }; 152 };
152 153
153 Storage(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly); 154 DataStore(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly);
154 ~Storage(); 155 ~DataStore();
155 156
156 Transaction createTransaction(AccessMode mode = ReadWrite, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); 157 Transaction createTransaction(AccessMode mode = ReadWrite, const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>());
157 158
158 /** 159 /**
159 * Set the default error handler. 160 * Set the default error handler.
160 */ 161 */
161 void setDefaultErrorHandler(const std::function<void(const Storage::Error &error)> &errorHandler); 162 void setDefaultErrorHandler(const std::function<void(const DataStore::Error &error)> &errorHandler);
162 std::function<void(const Storage::Error &error)> defaultErrorHandler() const; 163 std::function<void(const DataStore::Error &error)> defaultErrorHandler() const;
163 164
164 /** 165 /**
165 * A basic error handler that writes to std::cerr. 166 * A basic error handler that writes to std::cerr.
166 * 167 *
167 * Used if nothing else is configured. 168 * Used if nothing else is configured.
168 */ 169 */
169 static std::function<void(const Storage::Error &error)> basicErrorHandler(); 170 static std::function<void(const DataStore::Error &error)> basicErrorHandler();
170 171
171 qint64 diskUsage() const; 172 qint64 diskUsage() const;
172 void removeFromDisk() const; 173 void removeFromDisk() const;
@@ -178,16 +179,16 @@ public:
178 */ 179 */
179 static void clearEnv(); 180 static void clearEnv();
180 181
181 static qint64 maxRevision(const Sink::Storage::Transaction &); 182 static qint64 maxRevision(const Transaction &);
182 static void setMaxRevision(Sink::Storage::Transaction &, qint64 revision); 183 static void setMaxRevision(Transaction &, qint64 revision);
183 184
184 static qint64 cleanedUpRevision(const Sink::Storage::Transaction &); 185 static qint64 cleanedUpRevision(const Transaction &);
185 static void setCleanedUpRevision(Sink::Storage::Transaction &, qint64 revision); 186 static void setCleanedUpRevision(Transaction &, qint64 revision);
186 187
187 static QByteArray getUidFromRevision(const Sink::Storage::Transaction &, qint64 revision); 188 static QByteArray getUidFromRevision(const Transaction &, qint64 revision);
188 static QByteArray getTypeFromRevision(const Sink::Storage::Transaction &, qint64 revision); 189 static QByteArray getTypeFromRevision(const Transaction &, qint64 revision);
189 static void recordRevision(Sink::Storage::Transaction &, qint64 revision, const QByteArray &uid, const QByteArray &type); 190 static void recordRevision(Transaction &, qint64 revision, const QByteArray &uid, const QByteArray &type);
190 static void removeRevision(Sink::Storage::Transaction &, qint64 revision); 191 static void removeRevision(Transaction &, qint64 revision);
191 192
192 bool exists() const; 193 bool exists() const;
193 194
@@ -199,16 +200,17 @@ public:
199 static QByteArray uidFromKey(const QByteArray &key); 200 static QByteArray uidFromKey(const QByteArray &key);
200 static qint64 revisionFromKey(const QByteArray &key); 201 static qint64 revisionFromKey(const QByteArray &key);
201 202
202 static NamedDatabase mainDatabase(const Sink::Storage::Transaction &, const QByteArray &type); 203 static NamedDatabase mainDatabase(const Transaction &, const QByteArray &type);
203 204
204 static QByteArray generateUid(); 205 static QByteArray generateUid();
205 206
206private: 207private:
207 std::function<void(const Storage::Error &error)> mErrorHandler; 208 std::function<void(const DataStore::Error &error)> mErrorHandler;
208 209
209private: 210private:
210 class Private; 211 class Private;
211 Private *const d; 212 Private *const d;
212}; 213};
213 214
215}
214} // namespace Sink 216} // namespace Sink