From d320030c53cf9fd4338ca3f6da00a9b876c13397 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Tue, 21 Aug 2018 13:40:59 +0200 Subject: =?UTF-8?q?Refactor=20QByteArray=20=E2=86=94=20size=5Ft=20conversi?= =?UTF-8?q?ons=20into=20utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/storage/entitystore.cpp | 2 +- common/storage_common.cpp | 8 ++++---- common/storage_lmdb.cpp | 14 +++++++------- common/utils.cpp | 10 ++++++++++ common/utils.h | 5 +++++ 5 files changed, 27 insertions(+), 12 deletions(-) (limited to 'common') diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index 0640f1c..daabf1f 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp @@ -451,7 +451,7 @@ QVector EntityStore::fullScan(const QByteArray &type) return true; } - size_t revision = *reinterpret_cast(key.constData()); + size_t revision = byteArrayToSizeT(key); const auto metadata = flatbuffers::GetRoot(buffer.metadataBuffer()); 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 size_t revision; t.openDatabase("uidsToRevisions", {}, AllowDuplicates | IntegerValues) .findLatest(uid, [&revision](const QByteArray &key, const QByteArray &value) { - revision = *reinterpret_cast(value.constData()); + revision = byteArrayToSizeT(value); }); return revision; @@ -152,7 +152,7 @@ QList DataStore::getRevisionsUntilFromUid(DataStore::Transaction &t, con QList queriedRevisions; t.openDatabase("uidsToRevisions", {}, AllowDuplicates | IntegerValues) .scan(uid, [&queriedRevisions, lastRevision](const QByteArray &, const QByteArray &value) { - size_t currentRevision = *reinterpret_cast(value.constData()); + size_t currentRevision = byteArrayToSizeT(value); if (currentRevision < lastRevision) { queriedRevisions << currentRevision; return true; @@ -185,7 +185,7 @@ void DataStore::recordRevision(DataStore::Transaction &transaction, size_t revis .openDatabase("revisions", /* errorHandler = */ {}, IntegerKeys) .write(revision, uid); transaction.openDatabase("uidsToRevisions", /* errorHandler = */ {}, AllowDuplicates | IntegerValues) - .write(uid, QByteArray::fromRawData(reinterpret_cast(&revision), sizeof(revision))); + .write(uid, sizeTToByteArray(revision)); transaction .openDatabase("revisionType", /* errorHandler = */ {}, IntegerKeys) .write(revision, type); @@ -199,7 +199,7 @@ void DataStore::removeRevision(DataStore::Transaction &transaction, size_t revis .openDatabase("revisions", /* errorHandler = */ {}, IntegerKeys) .remove(revision); transaction.openDatabase("uidsToRevisions", /* errorHandler = */ {}, AllowDuplicates | IntegerValues) - .remove(uid, QByteArray::fromRawData(reinterpret_cast(&revision), sizeof(revision))); + .remove(uid, sizeTToByteArray(revision)); transaction .openDatabase("revisionType", /* errorHandler = */ {}, IntegerKeys) .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() bool DataStore::NamedDatabase::write(const size_t key, const QByteArray &value, const std::function &errorHandler) { - auto baKey = QByteArray::fromRawData(reinterpret_cast(&key), sizeof(key)); + auto baKey = sizeTToByteArray(key); return write(baKey, value, errorHandler); } @@ -425,7 +425,7 @@ bool DataStore::NamedDatabase::write(const QByteArray &sKey, const QByteArray &s void DataStore::NamedDatabase::remove( const size_t key, const std::function &errorHandler) { - auto baKey = QByteArray::fromRawData(reinterpret_cast(&key), sizeof(key)); + auto baKey = sizeTToByteArray(key); return remove(baKey, errorHandler); } @@ -437,7 +437,7 @@ void DataStore::NamedDatabase::remove(const QByteArray &k, const std::function &errorHandler) { - auto baKey = QByteArray::fromRawData(reinterpret_cast(&key), sizeof(key)); + auto baKey = sizeTToByteArray(key); return remove(baKey, value, errorHandler); } @@ -478,10 +478,10 @@ int DataStore::NamedDatabase::scan(const size_t key, const std::function &resultHandler, const std::function &errorHandler, bool skipInternalKeys) const { - auto baKey = QByteArray::fromRawData(reinterpret_cast(&key), sizeof(key)); + auto baKey = sizeTToByteArray(key); return scan(baKey, [&resultHandler](const QByteArray &key, const QByteArray &value) { - size_t integerKey = *reinterpret_cast(key.constData()); + size_t integerKey = byteArrayToSizeT(value); return resultHandler(integerKey, value); }, errorHandler, /* findSubstringKeys = */ false, skipInternalKeys); @@ -650,8 +650,8 @@ int DataStore::NamedDatabase::findAllInRange(const size_t lowerBound, const size const std::function &resultHandler, const std::function &errorHandler) const { - auto baLowerBound = QByteArray::fromRawData(reinterpret_cast(&lowerBound), sizeof(size_t)); - auto baUpperBound = QByteArray::fromRawData(reinterpret_cast(&upperBound), sizeof(size_t)); + auto baLowerBound = sizeTToByteArray(lowerBound); + auto baUpperBound = sizeTToByteArray(upperBound); return findAllInRange(baLowerBound, baUpperBound, resultHandler, errorHandler); } 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() { return QUuid::createUuid().toByteArray(); } + +const QByteArray Sink::sizeTToByteArray(const size_t &value) +{ + return QByteArray::fromRawData(reinterpret_cast(&value), sizeof(size_t)); +} + +size_t Sink::byteArrayToSizeT(const QByteArray &value) +{ + return *reinterpret_cast(value.constData()); +} 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 { QByteArray createUuid(); +// No copy is done on this functions. Therefore, the caller must not use the +// returned QByteArray after the size_t has been destroyed. +const QByteArray sizeTToByteArray(const size_t &); +size_t byteArrayToSizeT(const QByteArray &); + template static QByteArray padNumber(T number); -- cgit v1.2.3