diff options
Diffstat (limited to 'common/storage_lmdb.cpp')
-rw-r--r-- | common/storage_lmdb.cpp | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index 983d0e2..86d76a0 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -52,14 +52,17 @@ int getErrorCode(int e) | |||
52 | class Storage::Transaction::Private | 52 | class Storage::Transaction::Private |
53 | { | 53 | { |
54 | public: | 54 | public: |
55 | Private(MDB_txn *_txn, MDB_dbi _dbi, bool _allowDuplicates, const std::function<void(const Storage::Error &error)> &_defaultErrorHandler, const QString &_name) | 55 | Private(MDB_txn *_txn, MDB_dbi _dbi, bool _allowDuplicates, const std::function<void(const Storage::Error &error)> &_defaultErrorHandler, const QString &_name, MDB_env *_env) |
56 | : transaction(_txn), | 56 | : env(_env), |
57 | transaction(_txn), | ||
57 | dbi(_dbi), | 58 | dbi(_dbi), |
58 | allowDuplicates(_allowDuplicates), | 59 | allowDuplicates(_allowDuplicates), |
59 | defaultErrorHandler(_defaultErrorHandler), | 60 | defaultErrorHandler(_defaultErrorHandler), |
60 | name(_name), | 61 | name(_name), |
61 | implicitCommit(false), | 62 | implicitCommit(false), |
62 | error(false) | 63 | error(false), |
64 | autoCommitInterval(0), | ||
65 | modificationCounter(0) | ||
63 | { | 66 | { |
64 | 67 | ||
65 | } | 68 | } |
@@ -68,6 +71,7 @@ public: | |||
68 | 71 | ||
69 | } | 72 | } |
70 | 73 | ||
74 | MDB_env *env; | ||
71 | MDB_txn *transaction; | 75 | MDB_txn *transaction; |
72 | MDB_dbi dbi; | 76 | MDB_dbi dbi; |
73 | bool allowDuplicates; | 77 | bool allowDuplicates; |
@@ -75,6 +79,8 @@ public: | |||
75 | QString name; | 79 | QString name; |
76 | bool implicitCommit; | 80 | bool implicitCommit; |
77 | bool error; | 81 | bool error; |
82 | int autoCommitInterval; | ||
83 | int modificationCounter; | ||
78 | }; | 84 | }; |
79 | 85 | ||
80 | Storage::Transaction::Transaction() | 86 | Storage::Transaction::Transaction() |
@@ -128,6 +134,13 @@ void Storage::Transaction::abort() | |||
128 | d->transaction = nullptr; | 134 | d->transaction = nullptr; |
129 | } | 135 | } |
130 | 136 | ||
137 | void Storage::Transaction::setAutocommit(int interval) | ||
138 | { | ||
139 | if (d) { | ||
140 | d->autoCommitInterval = interval; | ||
141 | } | ||
142 | } | ||
143 | |||
131 | bool Storage::Transaction::write(const QByteArray &sKey, const QByteArray &sValue, const std::function<void(const Storage::Error &error)> &errorHandler) | 144 | bool Storage::Transaction::write(const QByteArray &sKey, const QByteArray &sValue, const std::function<void(const Storage::Error &error)> &errorHandler) |
132 | { | 145 | { |
133 | if (!d || !d->transaction) { | 146 | if (!d || !d->transaction) { |
@@ -160,6 +173,29 @@ bool Storage::Transaction::write(const QByteArray &sKey, const QByteArray &sValu | |||
160 | d->implicitCommit = true; | 173 | d->implicitCommit = true; |
161 | } | 174 | } |
162 | 175 | ||
176 | if (d->autoCommitInterval > 0) { | ||
177 | d->modificationCounter++; | ||
178 | if (d->modificationCounter >= d->autoCommitInterval) { | ||
179 | commit(); | ||
180 | |||
181 | MDB_txn *txn; | ||
182 | rc = mdb_txn_begin(d->env, NULL, 0, &txn); | ||
183 | if (!rc) { | ||
184 | //TODO: Move opening of dbi into Transaction for different named databases | ||
185 | MDB_dbi dbi; | ||
186 | rc = mdb_dbi_open(txn, NULL, d->allowDuplicates ? MDB_DUPSORT : 0, &dbi); | ||
187 | if (rc) { | ||
188 | errorHandler(Error(d->name.toLatin1(), ErrorCodes::GenericError, "Error while opening transaction: " + QByteArray(mdb_strerror(rc)))); | ||
189 | } else { | ||
190 | d->transaction = txn; | ||
191 | d->dbi = dbi; | ||
192 | } | ||
193 | } | ||
194 | |||
195 | d->modificationCounter = 0; | ||
196 | } | ||
197 | } | ||
198 | |||
163 | return !rc; | 199 | return !rc; |
164 | } | 200 | } |
165 | 201 | ||
@@ -387,7 +423,7 @@ Storage::Transaction Storage::createTransaction(AccessMode type, const std::func | |||
387 | errorHandler(Error(d->name.toLatin1(), ErrorCodes::GenericError, "Error while opening transaction: " + QByteArray(mdb_strerror(rc)))); | 423 | errorHandler(Error(d->name.toLatin1(), ErrorCodes::GenericError, "Error while opening transaction: " + QByteArray(mdb_strerror(rc)))); |
388 | return Transaction(); | 424 | return Transaction(); |
389 | } | 425 | } |
390 | return Transaction(new Transaction::Private(txn, dbi, d->allowDuplicates, defaultErrorHandler(), d->name)); | 426 | return Transaction(new Transaction::Private(txn, dbi, d->allowDuplicates, defaultErrorHandler(), d->name, d->env)); |
391 | } else { | 427 | } else { |
392 | if (rc) { | 428 | if (rc) { |
393 | errorHandler(Error(d->name.toLatin1(), ErrorCodes::GenericError, "Error while beginning transaction: " + QByteArray(mdb_strerror(rc)))); | 429 | errorHandler(Error(d->name.toLatin1(), ErrorCodes::GenericError, "Error while beginning transaction: " + QByteArray(mdb_strerror(rc)))); |