diff options
author | Minijackson <minijackson@riseup.net> | 2018-08-21 13:40:59 +0200 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2018-08-21 13:40:59 +0200 |
commit | 91f0b273fc2072a4d7c96ba83b37fe45b3cda1ef (patch) | |
tree | ad44891a5bb90cef9eed398f074cea979f057440 | |
parent | 6ef0a29d8e468de50c9dcf260db45957d028a083 (diff) | |
download | sink-91f0b273fc2072a4d7c96ba83b37fe45b3cda1ef.tar.gz sink-91f0b273fc2072a4d7c96ba83b37fe45b3cda1ef.zip |
Refactor QByteArray ↔ size_t conversions into utils
-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 | ||||
-rw-r--r-- | sinksh/syntax_modules/sink_inspect.cpp | 2 | ||||
-rw-r--r-- | tests/pipelinetest.cpp | 2 | ||||
-rw-r--r-- | tests/storagetest.cpp | 4 |
8 files changed, 31 insertions, 16 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 | ||
diff --git a/sinksh/syntax_modules/sink_inspect.cpp b/sinksh/syntax_modules/sink_inspect.cpp index 956431a..1ab68f5 100644 --- a/sinksh/syntax_modules/sink_inspect.cpp +++ b/sinksh/syntax_modules/sink_inspect.cpp | |||
@@ -108,7 +108,7 @@ bool inspect(const QStringList &args, State &state) | |||
108 | 108 | ||
109 | QSet<QByteArray> uids; | 109 | QSet<QByteArray> uids; |
110 | db.scan("", [&] (const QByteArray &key, const QByteArray &data) { | 110 | db.scan("", [&] (const QByteArray &key, const QByteArray &data) { |
111 | size_t revision = *reinterpret_cast<const char*>(key.constData()); | 111 | size_t revision = Sink::byteArrayToSizeT(key); |
112 | uids.insert(Sink::Storage::DataStore::getUidFromRevision(transaction, revision)); | 112 | uids.insert(Sink::Storage::DataStore::getUidFromRevision(transaction, revision)); |
113 | return true; | 113 | return true; |
114 | }, | 114 | }, |
diff --git a/tests/pipelinetest.cpp b/tests/pipelinetest.cpp index 47d443f..34ee3a5 100644 --- a/tests/pipelinetest.cpp +++ b/tests/pipelinetest.cpp | |||
@@ -35,7 +35,7 @@ static QList<Sink::Storage::Key> getKeys(const QByteArray &dbEnv, const QByteArr | |||
35 | auto db = transaction.openDatabase(name, nullptr, false); | 35 | auto db = transaction.openDatabase(name, nullptr, false); |
36 | QList<Sink::Storage::Key> result; | 36 | QList<Sink::Storage::Key> result; |
37 | db.scan("", [&](const QByteArray &key, const QByteArray &value) { | 37 | db.scan("", [&](const QByteArray &key, const QByteArray &value) { |
38 | size_t revision = *reinterpret_cast<const char *>(key.constData()); | 38 | size_t revision = Sink::byteArrayToSizeT(value); |
39 | result << Sink::Storage::Key(Sink::Storage::Identifier::fromDisplayByteArray( | 39 | result << Sink::Storage::Key(Sink::Storage::Identifier::fromDisplayByteArray( |
40 | Sink::Storage::DataStore::getUidFromRevision(transaction, revision)), | 40 | Sink::Storage::DataStore::getUidFromRevision(transaction, revision)), |
41 | revision); | 41 | revision); |
diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp index 39fd380..925d1d9 100644 --- a/tests/storagetest.cpp +++ b/tests/storagetest.cpp | |||
@@ -838,8 +838,8 @@ private slots: | |||
838 | const size_t number1 = 1; | 838 | const size_t number1 = 1; |
839 | const size_t number2 = 2; | 839 | const size_t number2 = 2; |
840 | 840 | ||
841 | const QByteArray number1BA = QByteArray::fromRawData(reinterpret_cast<const char*>(&number1), sizeof(size_t)); | 841 | const QByteArray number1BA = Sink::sizeTToByteArray(number1); |
842 | const QByteArray number2BA = QByteArray::fromRawData(reinterpret_cast<const char*>(&number2), sizeof(size_t)); | 842 | const QByteArray number2BA = Sink::sizeTToByteArray(number2); |
843 | 843 | ||
844 | db.write(0, number1BA); | 844 | db.write(0, number1BA); |
845 | db.write(1, number2BA); | 845 | db.write(1, number2BA); |