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/storage_common.cpp | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'common/storage_common.cpp') 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; } -- cgit v1.2.3