From 42f32ea5865c95028c577000e15e8a8631d16e74 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 30 Mar 2015 23:38:45 +0200 Subject: Storage: API cleanup/use QByteArray instead of std::string --- common/index.cpp | 4 +- common/index.h | 6 +- common/messagequeue.cpp | 4 +- common/messagequeue.h | 6 +- common/pipeline.cpp | 2 +- common/storage.h | 90 ++++++++++++++++++++--------- common/storage_common.cpp | 40 ++++++++----- common/storage_lmdb.cpp | 137 +++++++++++++++++++++++++-------------------- dummyresource/facade.cpp | 8 +-- tests/hawd/dataset.cpp | 4 +- tests/storagebenchmark.cpp | 10 ++-- tests/storagetest.cpp | 20 ++++--- 12 files changed, 198 insertions(+), 133 deletions(-) diff --git a/common/index.cpp b/common/index.cpp index 7b0c682..9bb467b 100644 --- a/common/index.cpp +++ b/common/index.cpp @@ -17,12 +17,12 @@ void Index::add(const QByteArray &key, const QByteArray &value) void Index::lookup(const QByteArray &key, const std::function &resultHandler, const std::function &errorHandler) { - mStorage.scan(key.data(), key.size(), [this, resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { + mStorage.scan(key, [this, resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { resultHandler(QByteArray(static_cast(valuePtr), valueSize)); return true; }, [errorHandler](const Akonadi2::Storage::Error &error) { - qDebug() << "Error while retrieving value" << QString::fromStdString(error.message); + qDebug() << "Error while retrieving value" << error.message; errorHandler(Error(error.store, error.code, error.message)); } ); diff --git a/common/index.h b/common/index.h index aea654a..3cd7cc8 100644 --- a/common/index.h +++ b/common/index.h @@ -14,10 +14,10 @@ public: class Error { public: - Error(const std::string &s, int c, const std::string &m) + Error(const QByteArray &s, int c, const QByteArray &m) : store(s), message(m), code(c) {} - std::string store; - std::string message; + QByteArray store; + QByteArray message; int code; }; diff --git a/common/messagequeue.cpp b/common/messagequeue.cpp index 76f8162..add4759 100644 --- a/common/messagequeue.cpp +++ b/common/messagequeue.cpp @@ -23,7 +23,7 @@ void MessageQueue::dequeue(const std::function &errorHandler) { bool readValue = false; - mStorage.scan("", 0, [this, resultHandler, &readValue](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { + mStorage.scan("", [this, resultHandler, &readValue](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { const auto key = QByteArray::fromRawData(static_cast(keyPtr), keySize); if (Akonadi2::Storage::isInternalKey(key)) { return true; @@ -42,7 +42,7 @@ void MessageQueue::dequeue(const std::functionfilterIt.next(); - d->pipeline->storage().scan(d->key.toStdString(), [this, preprocessor](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { + d->pipeline->storage().scan(d->key, [this, preprocessor](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { auto entity = Akonadi2::GetEntity(dataValue); preprocessor->process(*this, *entity); return false; diff --git a/common/storage.h b/common/storage.h index 099b45f..78faac0 100644 --- a/common/storage.h +++ b/common/storage.h @@ -33,45 +33,80 @@ class AKONADI2COMMON_EXPORT Storage { public: enum AccessMode { ReadOnly, ReadWrite }; + enum ErrorCodes { + GenericError, + NotOpen, + ReadOnlyError, + TransactionError, + NotFound + }; + class Error { public: - Error(const std::string &s, int c, const std::string &m) + Error(const QByteArray &s, int c, const QByteArray &m) : store(s), message(m), code(c) {} - std::string store; - std::string message; + QByteArray store; + QByteArray message; int code; }; Storage(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly, bool allowDuplicates = false); ~Storage(); bool isInTransaction() const; - bool startTransaction(AccessMode mode = ReadWrite); - bool commitTransaction(); + bool startTransaction(AccessMode mode = ReadWrite, const std::function &errorHandler = std::function()); + bool commitTransaction(const std::function &errorHandler = std::function()); void abortTransaction(); - //TODO: row removal - //TODO: cursor based read - //TODO: query? - bool write(const void *key, size_t keySize, const void *value, size_t valueSize); - bool write(const std::string &sKey, const std::string &sValue); - void read(const std::string &sKey, - const std::function &resultHandler); - void read(const std::string &sKey, - const std::function &resultHandler, - const std::function &errorHandler); - void read(const std::string &sKey, const std::function &resultHandler); - void read(const std::string &sKey, - const std::function & resultHandler, - const std::function &errorHandler); - void scan(const std::string &sKey, const std::function &resultHandler); - void scan(const char *keyData, uint keySize, - const std::function &resultHandler, - const std::function &errorHandler); - void remove(void const *keyData, uint keySize); - void remove(void const *keyData, uint keySize, - const std::function &errorHandler); + /** + * Write values. + */ + bool write(const void *key, size_t keySize, const void *value, size_t valueSize, const std::function &errorHandler = std::function()); + + /** + * Convenience API + */ + bool write(const QByteArray &key, const QByteArray &value, const std::function &errorHandler = std::function()); + + /** + * Read values with a give key. + * + * * An empty @param key results in a full scan + * * If duplicates are existing (revisions), all values are returned. + * * The pointers of the returned values are valid during the execution of the @param resultHandler + * + * @return The number of values retrieved. + */ + int scan(const QByteArray &key, const std::function &resultHandler, const std::function &errorHandler = std::function()); + + /** + * Convenience API + */ + int scan(const QByteArray &key, const std::function &resultHandler, const std::function &errorHandler = std::function()); + + /** + * Remove a value + */ + void remove(void const *key, uint keySize, const std::function &errorHandler = std::function()); + + /** + * Convenience API + */ + void remove(const QByteArray &key, const std::function &errorHandler = std::function()); + + /** + * Set the default error handler. + */ + void setDefaultErrorHandler(const std::function &errorHandler); + std::function defaultErrorHandler(); + + /** + * A basic error handler that writes to std::cerr. + * + * Used if nothing else is configured. + */ static std::function basicErrorHandler(); + qint64 diskUsage() const; void removeFromDisk() const; @@ -84,6 +119,9 @@ public: static bool isInternalKey(void *key, int keySize); static bool isInternalKey(const QByteArray &key); +private: + std::function mErrorHandler; + private: class Private; Private * const d; diff --git a/common/storage_common.cpp b/common/storage_common.cpp index ff2a2cd..5728096 100644 --- a/common/storage_common.cpp +++ b/common/storage_common.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2014 Aaron Seigo + * Copyright (C) 2014 Christian Mollekopf * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -32,7 +33,7 @@ void errorHandler(const Storage::Error &error) { //TODO: allow this to be turned on / off globally //TODO: log $SOMEWHERE $SOMEHOW rather than just spit to stderr - std::cerr << "Read error in " << error.store << ", code " << error.code << ", message: " << error.message << std::endl; + std::cout << "Read error in " << error.store.toStdString() << ", code " << error.code << ", message: " << error.message.toStdString() << std::endl; } std::function Storage::basicErrorHandler() @@ -40,36 +41,47 @@ std::function Storage::basicErrorHandler() return errorHandler; } -void Storage::read(const std::string &sKey, const std::function &resultHandler) +void Storage::setDefaultErrorHandler(const std::function &errorHandler) { - read(sKey, resultHandler, &errorHandler); + mErrorHandler = errorHandler; } -void Storage::read(const std::string &sKey, const std::function &resultHandler) +std::function Storage::defaultErrorHandler() { - read(sKey, resultHandler, &errorHandler); + if (mErrorHandler) { + return mErrorHandler; + } + return basicErrorHandler(); +} + +int Storage::scan(const QByteArray &key, const std::function &resultHandler, const std::function &errorHandler) +{ + return scan(key, [&resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) { + return resultHandler(QByteArray::fromRawData((char*)(valuePtr), valueSize)); + }, + errorHandler); } -void Storage::scan(const std::string &sKey, const std::function &resultHandler) +bool Storage::write(const QByteArray &sKey, const QByteArray &sValue, const std::function &errorHandler) { - scan(sKey.data(), sKey.size(), resultHandler, &errorHandler); + return write(const_cast(sKey.data()), sKey.size(), const_cast(sValue.data()), sValue.size(), errorHandler); } void Storage::setMaxRevision(qint64 revision) { - write("__internal_maxRevision", QString::number(revision).toStdString()); + write("__internal_maxRevision", QByteArray::number(revision)); } qint64 Storage::maxRevision() { qint64 r = 0; - read(std::string("__internal_maxRevision"), [&](const std::string &revision) -> bool { - r = QString::fromStdString(revision).toLongLong(); + scan("__internal_maxRevision", [&](const QByteArray &revision) -> bool { + r = revision.toLongLong(); return false; - }, - [](const Storage::Error &error) { - //Ignore the error in case we don't find the value - //TODO only ignore value not found errors + }, [this](const Error &error){ + if (error.code != ErrorCodes::NotFound) { + defaultErrorHandler()(error); + } }); return r; } diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index c97f9ce..4fcb11f 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp @@ -36,6 +36,17 @@ namespace Akonadi2 { +int getErrorCode(int e) +{ + switch (e) { + case MDB_NOTFOUND: + return Storage::ErrorCodes::NotFound; + default: + break; + } + return -1; +} + class Storage::Private { public: @@ -130,12 +141,14 @@ bool Storage::exists() const { return (d->env != 0); } + bool Storage::isInTransaction() const { return d->transaction; } -bool Storage::startTransaction(AccessMode type) +bool Storage::startTransaction(AccessMode type, + const std::function &errorHandler) { if (!d->env) { return false; @@ -144,9 +157,12 @@ bool Storage::startTransaction(AccessMode type) bool requestedRead = type == ReadOnly; if (d->mode == ReadOnly && !requestedRead) { + Error error(d->name.toLatin1(), ErrorCodes::GenericError, "Requested read/write transaction in read-only mode."); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); return false; } + //We already have a transaction if (d->transaction && (!d->readTransaction || requestedRead)) { return true; } @@ -172,11 +188,13 @@ bool Storage::startTransaction(AccessMode type) if (!rc) { rc = mdb_dbi_open(d->transaction, NULL, d->allowDuplicates ? MDB_DUPSORT : 0, &d->dbi); if (rc) { - qWarning() << "Error while opening transaction: " << mdb_strerror(rc); + Error error(d->name.toLatin1(), ErrorCodes::GenericError, "Error while opening transaction: " + QByteArray(mdb_strerror(rc))); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); } } else { if (rc) { - qWarning() << "Error while beginning transaction: " << mdb_strerror(rc); + Error error(d->name.toLatin1(), ErrorCodes::GenericError, "Error while beginning transaction: " + QByteArray(mdb_strerror(rc))); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); } } @@ -185,7 +203,7 @@ bool Storage::startTransaction(AccessMode type) return !rc; } -bool Storage::commitTransaction() +bool Storage::commitTransaction(const std::function &errorHandler) { if (!d->env) { return false; @@ -200,7 +218,8 @@ bool Storage::commitTransaction() d->transaction = 0; if (rc) { - std::cerr << "mdb_txn_commit: " << rc << " " << mdb_strerror(rc) << std::endl; + Error error(d->name.toLatin1(), ErrorCodes::GenericError, "Error during transaction commit: " + QByteArray(mdb_strerror(rc))); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); } return !rc; @@ -216,25 +235,32 @@ void Storage::abortTransaction() d->transaction = 0; } -bool Storage::write(const void *keyPtr, size_t keySize, const void *valuePtr, size_t valueSize) +bool Storage::write(const void *keyPtr, size_t keySize, const void *valuePtr, size_t valueSize, + const std::function &errorHandler) { if (!d->env) { + Error error(d->name.toLatin1(), ErrorCodes::NotOpen, "Not open"); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); return false; } if (d->mode == ReadOnly) { - std::cerr << "tried to write in read-only mode." << std::endl; + Error error(d->name.toLatin1(), ErrorCodes::GenericError, "Tried to write in read-only mode."); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); return false; } if (!keyPtr || keySize == 0) { - std::cerr << "tried to write empty key." << std::endl; + Error error(d->name.toLatin1(), ErrorCodes::GenericError, "Tried to write empty key."); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); return false; } const bool implicitTransaction = !d->transaction || d->readTransaction; if (implicitTransaction) { if (!startTransaction()) { + Error error(d->name.toLatin1(), ErrorCodes::TransactionError, "Failed to start transaction."); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); return false; } } @@ -248,11 +274,14 @@ bool Storage::write(const void *keyPtr, size_t keySize, const void *valuePtr, si rc = mdb_put(d->transaction, d->dbi, &key, &data, 0); if (rc) { - std::cerr << "mdb_put: " << rc << " " << mdb_strerror(rc) << std::endl; + Error error(d->name.toLatin1(), ErrorCodes::GenericError, "mdb_put: " + QByteArray(mdb_strerror(rc))); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); } if (implicitTransaction) { if (rc) { + Error error(d->name.toLatin1(), ErrorCodes::GenericError, "aborting transaction"); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); abortTransaction(); } else { rc = commitTransaction(); @@ -262,39 +291,14 @@ bool Storage::write(const void *keyPtr, size_t keySize, const void *valuePtr, si return !rc; } -bool Storage::write(const std::string &sKey, const std::string &sValue) -{ - return write(const_cast(sKey.data()), sKey.size(), const_cast(sValue.data()), sValue.size()); -} - -void Storage::read(const std::string &sKey, - const std::function &resultHandler, - const std::function &errorHandler) -{ - read(sKey, - [&](void *ptr, int size) -> bool { - const std::string resultValue(static_cast(ptr), size); - return resultHandler(resultValue); - }, errorHandler); -} - -void Storage::read(const std::string &sKey, - const std::function &resultHandler, - const std::function &errorHandler) -{ - scan(sKey.data(), sKey.size(), [resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) { - return resultHandler(valuePtr, valueSize); - }, errorHandler); -} - -void Storage::scan(const char *keyData, uint keySize, - const std::function &resultHandler, - const std::function &errorHandler) +int Storage::scan(const QByteArray &k, + const std::function &resultHandler, + const std::function &errorHandler) { if (!d->env) { - Error error(d->name.toStdString(), -1, "Not open"); - errorHandler(error); - return; + Error error(d->name.toLatin1(), ErrorCodes::NotOpen, "Not open"); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); + return 0; } int rc; @@ -302,29 +306,33 @@ void Storage::scan(const char *keyData, uint keySize, MDB_val data; MDB_cursor *cursor; - key.mv_data = (void*)keyData; - key.mv_size = keySize; + key.mv_data = (void*)k.constData(); + key.mv_size = k.size(); const bool implicitTransaction = !d->transaction; if (implicitTransaction) { if (!startTransaction(ReadOnly)) { - Error error(d->name.toStdString(), -2, "Could not start transaction"); - errorHandler(error); - return; + Error error(d->name.toLatin1(), ErrorCodes::TransactionError, "Could not start transaction"); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); + return 0; } } rc = mdb_cursor_open(d->transaction, d->dbi, &cursor); if (rc) { - Error error(d->name.toStdString(), rc, std::string("Error during mdb_cursor open: ") + mdb_strerror(rc)); - errorHandler(error); - return; + Error error(d->name.toLatin1(), getErrorCode(rc), QByteArray("Error during mdb_cursor open: ") + QByteArray(mdb_strerror(rc))); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); + return 0; } - if (!keyData || keySize == 0 || d->allowDuplicates) { + int numberOfRetrievedValues = 0; + + if (k.isEmpty() || d->allowDuplicates) { if ((rc = mdb_cursor_get(cursor, &key, &data, d->allowDuplicates ? MDB_SET_RANGE : MDB_FIRST)) == 0) { + numberOfRetrievedValues++; if (resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { while ((rc = mdb_cursor_get(cursor, &key, &data, d->allowDuplicates ? MDB_NEXT_DUP : MDB_NEXT)) == 0) { + numberOfRetrievedValues++; if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { break; } @@ -338,6 +346,7 @@ void Storage::scan(const char *keyData, uint keySize, } } else { if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_SET)) == 0) { + numberOfRetrievedValues++; resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size); } } @@ -345,39 +354,42 @@ void Storage::scan(const char *keyData, uint keySize, mdb_cursor_close(cursor); if (rc) { - Error error(d->name.toStdString(), rc, std::string("Key: ") + std::string(keyData, keySize) + " : " + mdb_strerror(rc)); - errorHandler(error); + Error error(d->name.toLatin1(), getErrorCode(rc), QByteArray("Key: ") + k + " : " + QByteArray(mdb_strerror(rc))); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); } if (implicitTransaction) { abortTransaction(); } + return numberOfRetrievedValues; } -void Storage::remove(const void *keyData, uint keySize) +void Storage::remove(const QByteArray &key, + const std::function &errorHandler) { - remove(keyData, keySize, basicErrorHandler()); + remove(key.data(), key.size(), errorHandler); } -void Storage::remove(const void *keyData, uint keySize, const std::function &errorHandler) +void Storage::remove(const void *keyData, uint keySize, + const std::function &errorHandler) { if (!d->env) { - Error error(d->name.toStdString(), -1, "Not open"); - errorHandler(error); + Error error(d->name.toLatin1(), ErrorCodes::GenericError, "Not open"); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); return; } if (d->mode == ReadOnly) { - Error error(d->name.toStdString(), -3, "Tried to write in read-only mode"); - errorHandler(error); + Error error(d->name.toLatin1(), ErrorCodes::ReadOnlyError, "Tried to write in read-only mode"); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); return; } const bool implicitTransaction = !d->transaction || d->readTransaction; if (implicitTransaction) { if (!startTransaction()) { - Error error(d->name.toStdString(), -2, "Could not start transaction"); - errorHandler(error); + Error error(d->name.toLatin1(), ErrorCodes::TransactionError, "Could not start transaction"); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); return; } } @@ -389,8 +401,8 @@ void Storage::remove(const void *keyData, uint keySize, const std::functiontransaction, d->dbi, &key, 0); if (rc) { - Error error(d->name.toStdString(), -1, QString("Error on mdb_del: %1 %2").arg(rc).arg(mdb_strerror(rc)).toStdString()); - errorHandler(error); + Error error(d->name.toLatin1(), ErrorCodes::GenericError, QString("Error on mdb_del: %1 %2").arg(rc).arg(mdb_strerror(rc)).toLatin1()); + errorHandler ? errorHandler(error) : defaultErrorHandler()(error); } if (implicitTransaction) { @@ -413,6 +425,7 @@ qint64 Storage::diskUsage() const void Storage::removeFromDisk() const { const QString fullPath(d->storageRoot + '/' + d->name); + qDebug() << "removing " << fullPath; QMutexLocker locker(&d->sMutex); QDir dir(fullPath); if (!dir.removeRecursively()) { diff --git a/dummyresource/facade.cpp b/dummyresource/facade.cpp index e0d27dc..893b585 100644 --- a/dummyresource/facade.cpp +++ b/dummyresource/facade.cpp @@ -135,7 +135,7 @@ Async::Job DummyResourceFacade::synchronizeResource(bool sync, bool proces void DummyResourceFacade::readValue(QSharedPointer storage, const QByteArray &key, const std::function &resultCallback, std::function preparedQuery) { - storage->scan(key.data(), key.size(), [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { + storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { //Skip internals if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { @@ -170,7 +170,7 @@ void DummyResourceFacade::readValue(QSharedPointer storage, c } if (!resourceBuffer || !metadataBuffer) { - qWarning() << "invalid buffer " << QString::fromStdString(std::string(static_cast(keyValue), keySize)); + qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast(keyValue), keySize); return true; } @@ -189,7 +189,7 @@ void DummyResourceFacade::readValue(QSharedPointer storage, c return true; }, [](const Akonadi2::Storage::Error &error) { - qWarning() << "Error during query: " << QString::fromStdString(error.message); + qWarning() << "Error during query: " << error.message; }); } @@ -208,7 +208,7 @@ Async::Job DummyResourceFacade::load(const Akonadi2::Query &query, const s keys << value; }, [](const Index::Error &error) { - qWarning() << "Error in index: " << QString::fromStdString(error.message); + qWarning() << "Error in index: " << error.message; }); } diff --git a/tests/hawd/dataset.cpp b/tests/hawd/dataset.cpp index e1f9700..9f1d307 100644 --- a/tests/hawd/dataset.cpp +++ b/tests/hawd/dataset.cpp @@ -253,7 +253,7 @@ void Dataset::eachRow(const std::function &resultHandler) } Row row(*this); - m_storage.scan(nullptr, 0, + m_storage.scan("", [&](void *key, int keySize, void *data, int dataSize) -> bool { if (keySize != sizeof(qint64)) { return true; @@ -277,7 +277,7 @@ Dataset::Row Dataset::row(qint64 key) } Row row(*this, key); - m_storage.scan((const char *)&key, sizeof(qint64), + m_storage.scan(QByteArray::fromRawData((const char *)&key, sizeof(qint64)), [&row](void *keyPtr, int keyLength, void *valuePtr, int valueSize) -> bool { QByteArray array((const char*)valuePtr, valueSize); row.fromBinary(array); diff --git a/tests/storagebenchmark.cpp b/tests/storagebenchmark.cpp index 9cf9a71..0233466 100644 --- a/tests/storagebenchmark.cpp +++ b/tests/storagebenchmark.cpp @@ -15,7 +15,7 @@ using namespace Calendar; using namespace flatbuffers; -static std::string createEvent() +static QByteArray createEvent() { static const size_t attachmentSize = 1024*2; // 2KB static uint8_t rawData[attachmentSize]; @@ -33,7 +33,7 @@ static std::string createEvent() memcpy((void*)Calendar::GetEvent(fbb.GetBufferPointer())->attachment()->Data(), rawData, attachmentSize); } - return std::string(reinterpret_cast(fbb.GetBufferPointer()), fbb.GetSize()); + return QByteArray::fromRawData(reinterpret_cast(fbb.GetBufferPointer()), fbb.GetSize()); } // static void readEvent(const std::string &data) @@ -103,9 +103,9 @@ private Q_SLOTS: store->startTransaction(); } - store->write(keyPrefix + std::to_string(i), event); + store->write(keyPrefix + QByteArray::number(i), event); } else { - myfile << event; + myfile << event.toStdString(); } } @@ -122,7 +122,7 @@ private Q_SLOTS: { for (int i = 0; i < count; i++) { if (store) { - store->read(keyPrefix + std::to_string(i), [](std::string value) -> bool { return true; }); + store->scan(keyPrefix + QByteArray::number(i), [](const QByteArray &value) -> bool { return true; }); } } } diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp index 5b4a0ba..e339c87 100644 --- a/tests/storagetest.cpp +++ b/tests/storagetest.cpp @@ -28,7 +28,7 @@ private: } storage.startTransaction(); } - storage.write(keyPrefix + std::to_string(i), keyPrefix + std::to_string(i)); + storage.write(keyPrefix + QByteArray::number(i), keyPrefix + QByteArray::number(i)); } storage.commitTransaction(); } @@ -37,9 +37,9 @@ private: { bool success = true; bool keyMatch = true; - const auto reference = keyPrefix + std::to_string(i); - storage.read(keyPrefix + std::to_string(i), - [&keyMatch, &reference](const std::string &value) -> bool { + const auto reference = keyPrefix + QByteArray::number(i); + storage.scan(keyPrefix + QByteArray::number(i), + [&keyMatch, &reference](const QByteArray &value) -> bool { if (value != reference) { qDebug() << "Mismatch while reading"; keyMatch = false; @@ -47,7 +47,7 @@ private: return keyMatch; }, [&success](const Akonadi2::Storage::Error &error) { - qDebug() << QString::fromStdString(error.message); + qDebug() << error.message; success = false; } ); @@ -133,7 +133,7 @@ private Q_SLOTS: populate(3); Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite); store.scan("key1", [&](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { - store.remove(keyValue, keySize, [](const Akonadi2::Storage::Error &) { + store.remove(QByteArray::fromRawData(static_cast(keyValue), keySize), [](const Akonadi2::Storage::Error &) { QVERIFY(false); }); return false; @@ -144,14 +144,16 @@ private Q_SLOTS: { bool gotResult = false; bool gotError = false; - Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadOnly); - store.scan(0, 0, [&](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { + Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite); + int numValues = store.scan("", [&](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { gotResult = true; return false; }, - [&](Akonadi2::Storage::Error) { + [&](const Akonadi2::Storage::Error &error) { + qDebug() << error.message; gotError = true; }); + QCOMPARE(numValues, 0); QVERIFY(!gotResult); QVERIFY(!gotError); } -- cgit v1.2.3