diff options
Diffstat (limited to 'dummyresource/facade.cpp')
-rw-r--r-- | dummyresource/facade.cpp | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/dummyresource/facade.cpp b/dummyresource/facade.cpp index bfe1de4..44056f8 100644 --- a/dummyresource/facade.cpp +++ b/dummyresource/facade.cpp | |||
@@ -10,8 +10,7 @@ using namespace flatbuffers; | |||
10 | 10 | ||
11 | DummyResourceFacade::DummyResourceFacade() | 11 | DummyResourceFacade::DummyResourceFacade() |
12 | : Akonadi2::StoreFacade<Akonadi2::Domain::Event>(), | 12 | : Akonadi2::StoreFacade<Akonadi2::Domain::Event>(), |
13 | mResourceAccess(new ResourceAccess("dummyresource")), | 13 | mResourceAccess(/* new ResourceAccess("dummyresource") */) |
14 | mStorage(new Storage(Akonadi2::Store::storageLocation(), "dummyresource")) | ||
15 | { | 14 | { |
16 | // connect(mResourceAccess.data(), &ResourceAccess::ready, this, onReadyChanged); | 15 | // connect(mResourceAccess.data(), &ResourceAccess::ready, this, onReadyChanged); |
17 | } | 16 | } |
@@ -47,7 +46,10 @@ void DummyResourceFacade::remove(const Akonadi2::Domain::Event &domainObject) | |||
47 | //=> perhaps do heap allocate and use smart pointer? | 46 | //=> perhaps do heap allocate and use smart pointer? |
48 | class DummyEventAdaptor : public Akonadi2::Domain::Event { | 47 | class DummyEventAdaptor : public Akonadi2::Domain::Event { |
49 | public: | 48 | public: |
50 | DummyEventAdaptor(const QString &resource, const QString &identifier, qint64 revision):Akonadi2::Domain::Event(resource, identifier, revision){}; | 49 | DummyEventAdaptor(const QString &resource, const QString &identifier, qint64 revision) |
50 | :Akonadi2::Domain::Event(resource, identifier, revision) | ||
51 | { | ||
52 | } | ||
51 | 53 | ||
52 | //TODO | 54 | //TODO |
53 | // void setProperty(const QString &key, const QVariant &value) | 55 | // void setProperty(const QString &key, const QVariant &value) |
@@ -75,15 +77,45 @@ public: | |||
75 | void DummyResourceFacade::load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::Domain::Event::Ptr &)> &resultCallback) | 77 | void DummyResourceFacade::load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::Domain::Event::Ptr &)> &resultCallback) |
76 | { | 78 | { |
77 | qDebug() << "load called"; | 79 | qDebug() << "load called"; |
78 | //TODO only read values matching the query | ||
79 | auto storage = QSharedPointer<Storage>::create(Akonadi2::Store::storageLocation(), "dummyresource"); | 80 | auto storage = QSharedPointer<Storage>::create(Akonadi2::Store::storageLocation(), "dummyresource"); |
80 | storage->read("", [resultCallback, storage](void *data, int size) -> bool { | 81 | |
82 | //Compose some functions to make query matching fast. | ||
83 | //This way we can process the query once, and convert all values into something that can be compared quickly | ||
84 | std::function<bool(const std::string &key, DummyEvent const *buffer)> preparedQuery; | ||
85 | if (!query.ids.isEmpty()) { | ||
86 | //Match by id | ||
87 | //TODO: for id's a direct lookup would be way faster | ||
88 | |||
89 | //We convert the id's to std::string so we don't have to convert each key during the scan. (This runs only once, and the query will be run for every key) | ||
90 | //Probably a premature optimization, but perhaps a useful technique to be investigated. | ||
91 | QVector<std::string> ids; | ||
92 | for (const auto &id : query.ids) { | ||
93 | ids << id.toStdString(); | ||
94 | } | ||
95 | preparedQuery = [ids](const std::string &key, DummyEvent const *buffer) { | ||
96 | if (ids.contains(key)) { | ||
97 | return true; | ||
98 | } | ||
99 | return false; | ||
100 | }; | ||
101 | } else { | ||
102 | //Match everything | ||
103 | preparedQuery = [](const std::string &key, DummyEvent const *buffer) { | ||
104 | return true; | ||
105 | }; | ||
106 | } | ||
107 | |||
108 | //Because we have no indexes yet, we always do a full scan | ||
109 | storage->scan("", [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | ||
81 | //TODO read second buffer as well | 110 | //TODO read second buffer as well |
82 | auto eventBuffer = GetDummyEvent(data); | 111 | auto eventBuffer = GetDummyEvent(dataValue); |
83 | auto event = QSharedPointer<DummyEventAdaptor>::create("dummyresource", "key", 0); | 112 | if (preparedQuery && preparedQuery(std::string(static_cast<char*>(keyValue), keySize), eventBuffer)) { |
84 | event->buffer = eventBuffer; | 113 | //TODO read the revision from the generic portion of the buffer |
85 | event->storage = storage; | 114 | auto event = QSharedPointer<DummyEventAdaptor>::create("dummyresource", QString::fromUtf8(static_cast<char*>(keyValue), keySize), 0); |
86 | resultCallback(event); | 115 | event->buffer = eventBuffer; |
116 | event->storage = storage; | ||
117 | resultCallback(event); | ||
118 | } | ||
87 | return true; | 119 | return true; |
88 | }); | 120 | }); |
89 | } | 121 | } |