diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-03-30 23:38:45 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-03-31 11:11:08 +0200 |
commit | 42f32ea5865c95028c577000e15e8a8631d16e74 (patch) | |
tree | 2d9e8a77ccccf088a8807f35f87e4264163d6cdd /common/storage_common.cpp | |
parent | 34851314d39307f22df01a4b711e6fd3c5618e23 (diff) | |
download | sink-42f32ea5865c95028c577000e15e8a8631d16e74.tar.gz sink-42f32ea5865c95028c577000e15e8a8631d16e74.zip |
Storage: API cleanup/use QByteArray instead of std::string
Diffstat (limited to 'common/storage_common.cpp')
-rw-r--r-- | common/storage_common.cpp | 40 |
1 files changed, 26 insertions, 14 deletions
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 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | 2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> |
3 | * Copyright (C) 2014 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | * | 4 | * |
4 | * This library is free software; you can redistribute it and/or | 5 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public | 6 | * modify it under the terms of the GNU Lesser General Public |
@@ -32,7 +33,7 @@ void errorHandler(const Storage::Error &error) | |||
32 | { | 33 | { |
33 | //TODO: allow this to be turned on / off globally | 34 | //TODO: allow this to be turned on / off globally |
34 | //TODO: log $SOMEWHERE $SOMEHOW rather than just spit to stderr | 35 | //TODO: log $SOMEWHERE $SOMEHOW rather than just spit to stderr |
35 | std::cerr << "Read error in " << error.store << ", code " << error.code << ", message: " << error.message << std::endl; | 36 | std::cout << "Read error in " << error.store.toStdString() << ", code " << error.code << ", message: " << error.message.toStdString() << std::endl; |
36 | } | 37 | } |
37 | 38 | ||
38 | std::function<void(const Storage::Error &error)> Storage::basicErrorHandler() | 39 | std::function<void(const Storage::Error &error)> Storage::basicErrorHandler() |
@@ -40,36 +41,47 @@ std::function<void(const Storage::Error &error)> Storage::basicErrorHandler() | |||
40 | return errorHandler; | 41 | return errorHandler; |
41 | } | 42 | } |
42 | 43 | ||
43 | void Storage::read(const std::string &sKey, const std::function<bool(const std::string &value)> &resultHandler) | 44 | void Storage::setDefaultErrorHandler(const std::function<void(const Storage::Error &error)> &errorHandler) |
44 | { | 45 | { |
45 | read(sKey, resultHandler, &errorHandler); | 46 | mErrorHandler = errorHandler; |
46 | } | 47 | } |
47 | 48 | ||
48 | void Storage::read(const std::string &sKey, const std::function<bool(void *ptr, int size)> &resultHandler) | 49 | std::function<void(const Storage::Error &error)> Storage::defaultErrorHandler() |
49 | { | 50 | { |
50 | read(sKey, resultHandler, &errorHandler); | 51 | if (mErrorHandler) { |
52 | return mErrorHandler; | ||
53 | } | ||
54 | return basicErrorHandler(); | ||
55 | } | ||
56 | |||
57 | int Storage::scan(const QByteArray &key, const std::function<bool(const QByteArray &value)> &resultHandler, const std::function<void(const Storage::Error &error)> &errorHandler) | ||
58 | { | ||
59 | return scan(key, [&resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) { | ||
60 | return resultHandler(QByteArray::fromRawData((char*)(valuePtr), valueSize)); | ||
61 | }, | ||
62 | errorHandler); | ||
51 | } | 63 | } |
52 | 64 | ||
53 | void Storage::scan(const std::string &sKey, const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler) | 65 | bool Storage::write(const QByteArray &sKey, const QByteArray &sValue, const std::function<void(const Storage::Error &error)> &errorHandler) |
54 | { | 66 | { |
55 | scan(sKey.data(), sKey.size(), resultHandler, &errorHandler); | 67 | return write(const_cast<char*>(sKey.data()), sKey.size(), const_cast<char*>(sValue.data()), sValue.size(), errorHandler); |
56 | } | 68 | } |
57 | 69 | ||
58 | void Storage::setMaxRevision(qint64 revision) | 70 | void Storage::setMaxRevision(qint64 revision) |
59 | { | 71 | { |
60 | write("__internal_maxRevision", QString::number(revision).toStdString()); | 72 | write("__internal_maxRevision", QByteArray::number(revision)); |
61 | } | 73 | } |
62 | 74 | ||
63 | qint64 Storage::maxRevision() | 75 | qint64 Storage::maxRevision() |
64 | { | 76 | { |
65 | qint64 r = 0; | 77 | qint64 r = 0; |
66 | read(std::string("__internal_maxRevision"), [&](const std::string &revision) -> bool { | 78 | scan("__internal_maxRevision", [&](const QByteArray &revision) -> bool { |
67 | r = QString::fromStdString(revision).toLongLong(); | 79 | r = revision.toLongLong(); |
68 | return false; | 80 | return false; |
69 | }, | 81 | }, [this](const Error &error){ |
70 | [](const Storage::Error &error) { | 82 | if (error.code != ErrorCodes::NotFound) { |
71 | //Ignore the error in case we don't find the value | 83 | defaultErrorHandler()(error); |
72 | //TODO only ignore value not found errors | 84 | } |
73 | }); | 85 | }); |
74 | return r; | 86 | return r; |
75 | } | 87 | } |