diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/storage/entitystore.cpp | 2 | ||||
-rw-r--r-- | common/storage_common.cpp | 8 | ||||
-rw-r--r-- | common/storage_lmdb.cpp | 14 | ||||
-rw-r--r-- | common/utils.cpp | 10 | ||||
-rw-r--r-- | common/utils.h | 5 |
5 files changed, 27 insertions, 12 deletions
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index 6231479..c11f49c 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp | |||
@@ -451,7 +451,7 @@ QVector<Identifier> EntityStore::fullScan(const QByteArray &type) | |||
451 | return true; | 451 | return true; |
452 | } | 452 | } |
453 | 453 | ||
454 | size_t revision = *reinterpret_cast<const size_t*>(key.constData()); | 454 | size_t revision = byteArrayToSizeT(key); |
455 | 455 | ||
456 | const auto metadata = flatbuffers::GetRoot<Metadata>(buffer.metadataBuffer()); | 456 | const auto metadata = flatbuffers::GetRoot<Metadata>(buffer.metadataBuffer()); |
457 | const QByteArray uid = DataStore::getUidFromRevision(d->getTransaction(), revision); | 457 | const QByteArray uid = DataStore::getUidFromRevision(d->getTransaction(), revision); |
diff --git a/common/storage_common.cpp b/common/storage_common.cpp index ac246b2..5b4d3ec 100644 --- a/common/storage_common.cpp +++ b/common/storage_common.cpp | |||
@@ -141,7 +141,7 @@ size_t DataStore::getLatestRevisionFromUid(DataStore::Transaction &t, const QByt | |||
141 | size_t revision; | 141 | size_t revision; |
142 | t.openDatabase("uidsToRevisions", {}, AllowDuplicates | IntegerValues) | 142 | t.openDatabase("uidsToRevisions", {}, AllowDuplicates | IntegerValues) |
143 | .findLatest(uid, [&revision](const QByteArray &key, const QByteArray &value) { | 143 | .findLatest(uid, [&revision](const QByteArray &key, const QByteArray &value) { |
144 | revision = *reinterpret_cast<const size_t *>(value.constData()); | 144 | revision = byteArrayToSizeT(value); |
145 | }); | 145 | }); |
146 | 146 | ||
147 | return revision; | 147 | return revision; |
@@ -152,7 +152,7 @@ QList<size_t> DataStore::getRevisionsUntilFromUid(DataStore::Transaction &t, con | |||
152 | QList<size_t> queriedRevisions; | 152 | QList<size_t> queriedRevisions; |
153 | t.openDatabase("uidsToRevisions", {}, AllowDuplicates | IntegerValues) | 153 | t.openDatabase("uidsToRevisions", {}, AllowDuplicates | IntegerValues) |
154 | .scan(uid, [&queriedRevisions, lastRevision](const QByteArray &, const QByteArray &value) { | 154 | .scan(uid, [&queriedRevisions, lastRevision](const QByteArray &, const QByteArray &value) { |
155 | size_t currentRevision = *reinterpret_cast<const size_t *>(value.constData()); | 155 | size_t currentRevision = byteArrayToSizeT(value); |
156 | if (currentRevision < lastRevision) { | 156 | if (currentRevision < lastRevision) { |
157 | queriedRevisions << currentRevision; | 157 | queriedRevisions << currentRevision; |
158 | return true; | 158 | return true; |
@@ -185,7 +185,7 @@ void DataStore::recordRevision(DataStore::Transaction &transaction, size_t revis | |||
185 | .openDatabase("revisions", /* errorHandler = */ {}, IntegerKeys) | 185 | .openDatabase("revisions", /* errorHandler = */ {}, IntegerKeys) |
186 | .write(revision, uid); | 186 | .write(revision, uid); |
187 | transaction.openDatabase("uidsToRevisions", /* errorHandler = */ {}, AllowDuplicates | IntegerValues) | 187 | transaction.openDatabase("uidsToRevisions", /* errorHandler = */ {}, AllowDuplicates | IntegerValues) |
188 | .write(uid, QByteArray::fromRawData(reinterpret_cast<const char *>(&revision), sizeof(revision))); | 188 | .write(uid, sizeTToByteArray(revision)); |
189 | transaction | 189 | transaction |
190 | .openDatabase("revisionType", /* errorHandler = */ {}, IntegerKeys) | 190 | .openDatabase("revisionType", /* errorHandler = */ {}, IntegerKeys) |
191 | .write(revision, type); | 191 | .write(revision, type); |
@@ -199,7 +199,7 @@ void DataStore::removeRevision(DataStore::Transaction &transaction, size_t revis | |||
199 | .openDatabase("revisions", /* errorHandler = */ {}, IntegerKeys) | 199 | .openDatabase("revisions", /* errorHandler = */ {}, IntegerKeys) |
200 | .remove(revision); | 200 | .remove(revision); |
201 | transaction.openDatabase("uidsToRevisions", /* errorHandler = */ {}, AllowDuplicates | IntegerValues) | 201 | transaction.openDatabase("uidsToRevisions", /* errorHandler = */ {}, AllowDuplicates | IntegerValues) |
202 | .remove(uid, QByteArray::fromRawData(reinterpret_cast<const char *>(&revision), sizeof(revision))); | 202 | .remove(uid, sizeTToByteArray(revision)); |
203 | transaction | 203 | transaction |
204 | .openDatabase("revisionType", /* errorHandler = */ {}, IntegerKeys) | 204 | .openDatabase("revisionType", /* errorHandler = */ {}, IntegerKeys) |
205 | .remove(revision); | 205 | .remove(revision); |
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index e3377b2..981d4ef 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -382,7 +382,7 @@ DataStore::NamedDatabase::~NamedDatabase() | |||
382 | bool DataStore::NamedDatabase::write(const size_t key, const QByteArray &value, | 382 | bool DataStore::NamedDatabase::write(const size_t key, const QByteArray &value, |
383 | const std::function<void(const DataStore::Error &error)> &errorHandler) | 383 | const std::function<void(const DataStore::Error &error)> &errorHandler) |
384 | { | 384 | { |
385 | auto baKey = QByteArray::fromRawData(reinterpret_cast<const char *>(&key), sizeof(key)); | 385 | auto baKey = sizeTToByteArray(key); |
386 | return write(baKey, value, errorHandler); | 386 | return write(baKey, value, errorHandler); |
387 | } | 387 | } |
388 | 388 | ||
@@ -425,7 +425,7 @@ bool DataStore::NamedDatabase::write(const QByteArray &sKey, const QByteArray &s | |||
425 | void DataStore::NamedDatabase::remove( | 425 | void DataStore::NamedDatabase::remove( |
426 | const size_t key, const std::function<void(const DataStore::Error &error)> &errorHandler) | 426 | const size_t key, const std::function<void(const DataStore::Error &error)> &errorHandler) |
427 | { | 427 | { |
428 | auto baKey = QByteArray::fromRawData(reinterpret_cast<const char *>(&key), sizeof(key)); | 428 | auto baKey = sizeTToByteArray(key); |
429 | return remove(baKey, errorHandler); | 429 | return remove(baKey, errorHandler); |
430 | } | 430 | } |
431 | 431 | ||
@@ -437,7 +437,7 @@ void DataStore::NamedDatabase::remove(const QByteArray &k, const std::function<v | |||
437 | void DataStore::NamedDatabase::remove(const size_t key, const QByteArray &value, | 437 | void DataStore::NamedDatabase::remove(const size_t key, const QByteArray &value, |
438 | const std::function<void(const DataStore::Error &error)> &errorHandler) | 438 | const std::function<void(const DataStore::Error &error)> &errorHandler) |
439 | { | 439 | { |
440 | auto baKey = QByteArray::fromRawData(reinterpret_cast<const char *>(&key), sizeof(key)); | 440 | auto baKey = sizeTToByteArray(key); |
441 | return remove(baKey, value, errorHandler); | 441 | return remove(baKey, value, errorHandler); |
442 | } | 442 | } |
443 | 443 | ||
@@ -478,10 +478,10 @@ int DataStore::NamedDatabase::scan(const size_t key, | |||
478 | const std::function<bool(size_t key, const QByteArray &value)> &resultHandler, | 478 | const std::function<bool(size_t key, const QByteArray &value)> &resultHandler, |
479 | const std::function<void(const DataStore::Error &error)> &errorHandler, bool skipInternalKeys) const | 479 | const std::function<void(const DataStore::Error &error)> &errorHandler, bool skipInternalKeys) const |
480 | { | 480 | { |
481 | auto baKey = QByteArray::fromRawData(reinterpret_cast<const char *>(&key), sizeof(key)); | 481 | auto baKey = sizeTToByteArray(key); |
482 | return scan(baKey, | 482 | return scan(baKey, |
483 | [&resultHandler](const QByteArray &key, const QByteArray &value) { | 483 | [&resultHandler](const QByteArray &key, const QByteArray &value) { |
484 | size_t integerKey = *reinterpret_cast<const size_t *>(key.constData()); | 484 | size_t integerKey = byteArrayToSizeT(value); |
485 | return resultHandler(integerKey, value); | 485 | return resultHandler(integerKey, value); |
486 | }, | 486 | }, |
487 | errorHandler, /* findSubstringKeys = */ false, skipInternalKeys); | 487 | errorHandler, /* findSubstringKeys = */ false, skipInternalKeys); |
@@ -650,8 +650,8 @@ int DataStore::NamedDatabase::findAllInRange(const size_t lowerBound, const size | |||
650 | const std::function<void(const QByteArray &key, const QByteArray &value)> &resultHandler, | 650 | const std::function<void(const QByteArray &key, const QByteArray &value)> &resultHandler, |
651 | const std::function<void(const DataStore::Error &error)> &errorHandler) const | 651 | const std::function<void(const DataStore::Error &error)> &errorHandler) const |
652 | { | 652 | { |
653 | auto baLowerBound = QByteArray::fromRawData(reinterpret_cast<const char *>(&lowerBound), sizeof(size_t)); | 653 | auto baLowerBound = sizeTToByteArray(lowerBound); |
654 | auto baUpperBound = QByteArray::fromRawData(reinterpret_cast<const char *>(&upperBound), sizeof(size_t)); | 654 | auto baUpperBound = sizeTToByteArray(upperBound); |
655 | return findAllInRange(baLowerBound, baUpperBound, resultHandler, errorHandler); | 655 | return findAllInRange(baLowerBound, baUpperBound, resultHandler, errorHandler); |
656 | } | 656 | } |
657 | 657 | ||
diff --git a/common/utils.cpp b/common/utils.cpp index 3c54db4..f6c6798 100644 --- a/common/utils.cpp +++ b/common/utils.cpp | |||
@@ -23,3 +23,13 @@ QByteArray Sink::createUuid() | |||
23 | { | 23 | { |
24 | return QUuid::createUuid().toByteArray(); | 24 | return QUuid::createUuid().toByteArray(); |
25 | } | 25 | } |
26 | |||
27 | const QByteArray Sink::sizeTToByteArray(const size_t &value) | ||
28 | { | ||
29 | return QByteArray::fromRawData(reinterpret_cast<const char *>(&value), sizeof(size_t)); | ||
30 | } | ||
31 | |||
32 | size_t Sink::byteArrayToSizeT(const QByteArray &value) | ||
33 | { | ||
34 | return *reinterpret_cast<const size_t *>(value.constData()); | ||
35 | } | ||
diff --git a/common/utils.h b/common/utils.h index ec1d5b1..8565f17 100644 --- a/common/utils.h +++ b/common/utils.h | |||
@@ -26,6 +26,11 @@ namespace Sink { | |||
26 | 26 | ||
27 | QByteArray createUuid(); | 27 | QByteArray createUuid(); |
28 | 28 | ||
29 | // No copy is done on this functions. Therefore, the caller must not use the | ||
30 | // returned QByteArray after the size_t has been destroyed. | ||
31 | const QByteArray sizeTToByteArray(const size_t &); | ||
32 | size_t byteArrayToSizeT(const QByteArray &); | ||
33 | |||
29 | template <typename T> | 34 | template <typename T> |
30 | static QByteArray padNumber(T number); | 35 | static QByteArray padNumber(T number); |
31 | 36 | ||