diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-06-07 19:26:21 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-06-07 19:26:21 +0200 |
commit | 810543a625074fb8685c7c59e36123c055e8fda6 (patch) | |
tree | 3ea4d74e26d74d022398447a9ede000c3023b181 /examples/dummyresource/facade.cpp | |
parent | 235a85211c769e190754d2fdb172069d68a889b9 (diff) | |
download | sink-810543a625074fb8685c7c59e36123c055e8fda6.tar.gz sink-810543a625074fb8685c7c59e36123c055e8fda6.zip |
Use type-specific index implementations.
Diffstat (limited to 'examples/dummyresource/facade.cpp')
-rw-r--r-- | examples/dummyresource/facade.cpp | 57 |
1 files changed, 6 insertions, 51 deletions
diff --git a/examples/dummyresource/facade.cpp b/examples/dummyresource/facade.cpp index e678214..9849a92 100644 --- a/examples/dummyresource/facade.cpp +++ b/examples/dummyresource/facade.cpp | |||
@@ -48,46 +48,6 @@ DummyResourceFacade::~DummyResourceFacade() | |||
48 | { | 48 | { |
49 | } | 49 | } |
50 | 50 | ||
51 | static std::function<bool(const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)> prepareQuery(const Akonadi2::Query &query) | ||
52 | { | ||
53 | //Compose some functions to make query matching fast. | ||
54 | //This way we can process the query once, and convert all values into something that can be compared quickly | ||
55 | std::function<bool(const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)> preparedQuery; | ||
56 | if (!query.ids.isEmpty()) { | ||
57 | //Match by id | ||
58 | //TODO: for id's a direct lookup would be way faster | ||
59 | |||
60 | //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) | ||
61 | //Probably a premature optimization, but perhaps a useful technique to be investigated. | ||
62 | QVector<std::string> ids; | ||
63 | for (const auto &id : query.ids) { | ||
64 | ids << id.toStdString(); | ||
65 | } | ||
66 | preparedQuery = [ids](const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local) { | ||
67 | if (ids.contains(key)) { | ||
68 | return true; | ||
69 | } | ||
70 | return false; | ||
71 | }; | ||
72 | } else if (!query.propertyFilter.isEmpty()) { | ||
73 | if (query.propertyFilter.contains("uid")) { | ||
74 | const QByteArray uid = query.propertyFilter.value("uid").toByteArray(); | ||
75 | preparedQuery = [uid](const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local) { | ||
76 | if (local && local->uid() && (QByteArray::fromRawData(local->uid()->c_str(), local->uid()->size()) == uid)) { | ||
77 | return true; | ||
78 | } | ||
79 | return false; | ||
80 | }; | ||
81 | } | ||
82 | } else { | ||
83 | //Match everything | ||
84 | preparedQuery = [](const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local) { | ||
85 | return true; | ||
86 | }; | ||
87 | } | ||
88 | return preparedQuery; | ||
89 | } | ||
90 | |||
91 | static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadata)> callback) | 51 | static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadata)> callback) |
92 | { | 52 | { |
93 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | 53 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { |
@@ -130,22 +90,17 @@ void DummyResourceFacade::readValue(const QSharedPointer<Akonadi2::Storage> &sto | |||
130 | 90 | ||
131 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage) | 91 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage) |
132 | { | 92 | { |
133 | auto resultSet = Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::queryIndexes(query, "org.kde.dummy"); | 93 | QSet<QByteArray> appliedFilters; |
94 | ResultSet resultSet = Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::queryIndexes(query, "org.kde.dummy", appliedFilters); | ||
95 | const auto remainingFilters = query.propertyFilter.keys().toSet() - appliedFilters; | ||
134 | 96 | ||
135 | //Scan for where we don't have an index | ||
136 | //TODO: we may want a way for queryIndexes to indicate that the resultSet is not final, and that a scan over the remaining set is required | ||
137 | //TODO: the prepared query should be generalized in TypeImplementation on top of domain adaptors | ||
138 | if (resultSet.isEmpty()) { | 97 | if (resultSet.isEmpty()) { |
139 | QVector<QByteArray> keys; | 98 | QVector<QByteArray> keys; |
140 | const auto preparedQuery = prepareQuery(query); | 99 | scan(storage, QByteArray(), [=, &keys](const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadataBuffer) { |
141 | scan(storage, QByteArray(), [preparedQuery, &keys](const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadataBuffer) { | 100 | keys << key; |
142 | //TODO use adapter for query and scan? | ||
143 | if (preparedQuery && preparedQuery(std::string(key.constData(), key.size()), buffer, local)) { | ||
144 | keys << key; | ||
145 | } | ||
146 | return true; | 101 | return true; |
147 | }); | 102 | }); |
148 | return ResultSet(keys); | 103 | resultSet = ResultSet(keys); |
149 | } | 104 | } |
150 | 105 | ||
151 | return resultSet; | 106 | return resultSet; |