diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-12-06 19:01:03 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-12-06 19:01:03 +0100 |
commit | 303a3cf5ba48dd9857a1fb9600cedd604c71de8d (patch) | |
tree | b7607eb58b7fbda5dbd49133bb316fe109edc44a /common/typeindex.cpp | |
parent | 46570dd9684990846cfd4c3dc5be71498c5a6278 (diff) | |
download | sink-303a3cf5ba48dd9857a1fb9600cedd604c71de8d.tar.gz sink-303a3cf5ba48dd9857a1fb9600cedd604c71de8d.zip |
Added TypeIndex
A central location for all types to specify what properties are
indexed, and how to query them.
Diffstat (limited to 'common/typeindex.cpp')
-rw-r--r-- | common/typeindex.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/common/typeindex.cpp b/common/typeindex.cpp new file mode 100644 index 0000000..0a0dc33 --- /dev/null +++ b/common/typeindex.cpp | |||
@@ -0,0 +1,105 @@ | |||
1 | /* | ||
2 | Copyright (c) 2015 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | #include "typeindex.h" | ||
20 | |||
21 | #include "log.h" | ||
22 | #include "index.h" | ||
23 | |||
24 | TypeIndex::TypeIndex(const QByteArray &type) | ||
25 | : mType(type) | ||
26 | { | ||
27 | |||
28 | } | ||
29 | |||
30 | template<> | ||
31 | void TypeIndex::addProperty<QByteArray>(const QByteArray &property) | ||
32 | { | ||
33 | auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Akonadi2::Storage::Transaction &transaction) { | ||
34 | // Trace() << "Indexing " << mType + ".index." + property << value.toByteArray(); | ||
35 | Index(mType + ".index." + property, transaction).add(value.toByteArray(), identifier); | ||
36 | }; | ||
37 | mIndexer.insert(property, indexer); | ||
38 | mProperties << property; | ||
39 | } | ||
40 | |||
41 | template<> | ||
42 | void TypeIndex::addProperty<QString>(const QByteArray &property) | ||
43 | { | ||
44 | auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Akonadi2::Storage::Transaction &transaction) { | ||
45 | // Trace() << "Indexing " << mType + ".index." + property << value.toByteArray(); | ||
46 | Index(mType + ".index." + property, transaction).add(value.toByteArray(), identifier); | ||
47 | }; | ||
48 | mIndexer.insert(property, indexer); | ||
49 | mProperties << property; | ||
50 | } | ||
51 | |||
52 | template<> | ||
53 | void TypeIndex::addProperty<QDateTime>(const QByteArray &property) | ||
54 | { | ||
55 | auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Akonadi2::Storage::Transaction &transaction) { | ||
56 | // Trace() << "Indexing " << mType + ".index." + property << value.toByteArray(); | ||
57 | Index(mType + ".index." + property, transaction).add(value.toByteArray(), identifier); | ||
58 | }; | ||
59 | mIndexer.insert(property, indexer); | ||
60 | mProperties << property; | ||
61 | } | ||
62 | |||
63 | void TypeIndex::add(const QByteArray &identifier, const Akonadi2::ApplicationDomain::BufferAdaptor &bufferAdaptor, Akonadi2::Storage::Transaction &transaction) | ||
64 | { | ||
65 | for (const auto &property : mProperties) { | ||
66 | const auto value = bufferAdaptor.getProperty(property); | ||
67 | if (value.isValid()) { | ||
68 | auto indexer = mIndexer.value(property); | ||
69 | indexer(identifier, value, transaction); | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | |||
74 | void TypeIndex::remove(const QByteArray &identifier, const Akonadi2::ApplicationDomain::BufferAdaptor &bufferAdaptor, Akonadi2::Storage::Transaction &transaction) | ||
75 | { | ||
76 | for (const auto &property : mProperties) { | ||
77 | const auto value = bufferAdaptor.getProperty(property); | ||
78 | if (value.isValid()) { | ||
79 | //FIXME don't always convert to byte array | ||
80 | Index(mType + ".index." + property, transaction).remove(value.toByteArray(), identifier); | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | |||
85 | ResultSet TypeIndex::query(const Akonadi2::Query &query, QSet<QByteArray> &appliedFilters, Akonadi2::Storage::Transaction &transaction) | ||
86 | { | ||
87 | QVector<QByteArray> keys; | ||
88 | for (const auto &property : mProperties) { | ||
89 | if (query.propertyFilter.contains(property)) { | ||
90 | Index index(mType + ".index." + property, transaction); | ||
91 | index.lookup(query.propertyFilter.value(property).toByteArray(), [&](const QByteArray &value) { | ||
92 | keys << value; | ||
93 | }, | ||
94 | [property](const Index::Error &error) { | ||
95 | Warning() << "Error in index: " << error.message << property; | ||
96 | }); | ||
97 | appliedFilters << property; | ||
98 | } | ||
99 | Trace() << "Index lookup found keys: " << keys.size(); | ||
100 | return ResultSet(keys); | ||
101 | } | ||
102 | Trace() << "No matching index"; | ||
103 | return ResultSet(keys); | ||
104 | } | ||
105 | |||