diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-08-22 11:25:26 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-08-22 11:25:26 +0200 |
commit | 6746247a49f09287ae4924c5c3df791f9bf61cbc (patch) | |
tree | b9df15f1e6413a8995704cf9f0c5f425c9bb9558 /common/storage.h | |
parent | fc3a5df884b25d5e624027b1fb017f42986980b2 (diff) | |
download | sink-6746247a49f09287ae4924c5c3df791f9bf61cbc.tar.gz sink-6746247a49f09287ae4924c5c3df791f9bf61cbc.zip |
Use named databases in storage.
This will allow us to create indexes in the same store.
Diffstat (limited to 'common/storage.h')
-rw-r--r-- | common/storage.h | 61 |
1 files changed, 50 insertions, 11 deletions
diff --git a/common/storage.h b/common/storage.h index a7241a7..2f7a2df 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -51,16 +51,12 @@ public: | |||
51 | int code; | 51 | int code; |
52 | }; | 52 | }; |
53 | 53 | ||
54 | class Transaction | 54 | class Transaction; |
55 | class NamedDatabase | ||
55 | { | 56 | { |
56 | public: | 57 | public: |
57 | Transaction(); | 58 | NamedDatabase(); |
58 | ~Transaction(); | 59 | ~NamedDatabase(); |
59 | bool commit(const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); | ||
60 | void abort(); | ||
61 | |||
62 | void setAutocommit(int interval); | ||
63 | |||
64 | /** | 60 | /** |
65 | * Write a value | 61 | * Write a value |
66 | */ | 62 | */ |
@@ -73,22 +69,57 @@ public: | |||
73 | 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)>()); |
74 | /** | 70 | /** |
75 | * Read values with a given key. | 71 | * Read values with a given key. |
76 | * | 72 | * |
77 | * * An empty @param key results in a full scan | 73 | * * An empty @param key results in a full scan |
78 | * * If duplicates are existing (revisions), all values are returned. | 74 | * * If duplicates are existing (revisions), all values are returned. |
79 | * * The pointers of the returned values are valid during the execution of the @param resultHandler | 75 | * * The pointers of the returned values are valid during the execution of the @param resultHandler |
80 | * | 76 | * |
81 | * @return The number of values retrieved. | 77 | * @return The number of values retrieved. |
82 | */ | 78 | */ |
83 | int scan(const QByteArray &k, | 79 | int scan(const QByteArray &k, |
84 | const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, | 80 | const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, |
85 | const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()) const; | 81 | const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()) const; |
86 | 82 | ||
83 | NamedDatabase(NamedDatabase&& other) : d(other.d) | ||
84 | { | ||
85 | d = other.d; | ||
86 | other.d = nullptr; | ||
87 | } | ||
88 | |||
89 | NamedDatabase& operator=(NamedDatabase&& other) { | ||
90 | d = other.d; | ||
91 | other.d = nullptr; | ||
92 | return *this; | ||
93 | } | ||
94 | |||
95 | operator bool() const { | ||
96 | return (d != nullptr); | ||
97 | } | ||
98 | |||
99 | private: | ||
100 | friend Transaction; | ||
101 | NamedDatabase(NamedDatabase& other); | ||
102 | NamedDatabase& operator=(NamedDatabase& other); | ||
103 | class Private; | ||
104 | NamedDatabase(Private*); | ||
105 | Private *d; | ||
106 | }; | ||
107 | |||
108 | class Transaction | ||
109 | { | ||
110 | public: | ||
111 | Transaction(); | ||
112 | ~Transaction(); | ||
113 | bool commit(const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); | ||
114 | void abort(); | ||
115 | |||
116 | NamedDatabase openDatabase(const QByteArray &name = QByteArray("default"), const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()) const; | ||
117 | |||
87 | Transaction(Transaction&& other) : d(other.d) | 118 | Transaction(Transaction&& other) : d(other.d) |
88 | { | 119 | { |
89 | d = other.d; | 120 | d = other.d; |
90 | other.d = nullptr; | 121 | other.d = nullptr; |
91 | } | 122 | } |
92 | Transaction& operator=(Transaction&& other) { | 123 | Transaction& operator=(Transaction&& other) { |
93 | d = other.d; | 124 | d = other.d; |
94 | other.d = nullptr; | 125 | other.d = nullptr; |
@@ -98,6 +129,14 @@ public: | |||
98 | operator bool() const { | 129 | operator bool() const { |
99 | return (d != nullptr); | 130 | return (d != nullptr); |
100 | } | 131 | } |
132 | |||
133 | bool write(const QByteArray &key, const QByteArray &value, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); | ||
134 | |||
135 | void remove(const QByteArray &key, | ||
136 | const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); | ||
137 | int scan(const QByteArray &k, | ||
138 | const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, | ||
139 | const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()) const; | ||
101 | private: | 140 | private: |
102 | Transaction(Transaction& other); | 141 | Transaction(Transaction& other); |
103 | Transaction& operator=(Transaction& other); | 142 | Transaction& operator=(Transaction& other); |