summaryrefslogtreecommitdiffstats
path: root/common/storage_common.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-03-30 23:38:45 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-03-31 11:11:08 +0200
commit42f32ea5865c95028c577000e15e8a8631d16e74 (patch)
tree2d9e8a77ccccf088a8807f35f87e4264163d6cdd /common/storage_common.cpp
parent34851314d39307f22df01a4b711e6fd3c5618e23 (diff)
downloadsink-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.cpp40
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
38std::function<void(const Storage::Error &error)> Storage::basicErrorHandler() 39std::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
43void Storage::read(const std::string &sKey, const std::function<bool(const std::string &value)> &resultHandler) 44void 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
48void Storage::read(const std::string &sKey, const std::function<bool(void *ptr, int size)> &resultHandler) 49std::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
57int 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
53void Storage::scan(const std::string &sKey, const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler) 65bool 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
58void Storage::setMaxRevision(qint64 revision) 70void 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
63qint64 Storage::maxRevision() 75qint64 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}