summaryrefslogtreecommitdiffstats
path: root/common/typeindex.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/typeindex.cpp')
-rw-r--r--common/typeindex.cpp39
1 files changed, 14 insertions, 25 deletions
diff --git a/common/typeindex.cpp b/common/typeindex.cpp
index ddf5df5..1321469 100644
--- a/common/typeindex.cpp
+++ b/common/typeindex.cpp
@@ -30,13 +30,13 @@ static QByteArray getByteArray(const QVariant &value)
30 if (value.isValid() && !value.toByteArray().isEmpty()) { 30 if (value.isValid() && !value.toByteArray().isEmpty()) {
31 return value.toByteArray(); 31 return value.toByteArray();
32 } 32 }
33 //LMDB can't handle empty keys, so use something different 33 // LMDB can't handle empty keys, so use something different
34 return "toplevel"; 34 return "toplevel";
35} 35}
36 36
37static QByteArray toSortableByteArray(const QDateTime &date) 37static QByteArray toSortableByteArray(const QDateTime &date)
38{ 38{
39 //Sort invalid last 39 // Sort invalid last
40 if (!date.isValid()) { 40 if (!date.isValid()) {
41 return QByteArray::number(std::numeric_limits<unsigned int>::max()); 41 return QByteArray::number(std::numeric_limits<unsigned int>::max());
42 } 42 }
@@ -44,10 +44,8 @@ static QByteArray toSortableByteArray(const QDateTime &date)
44} 44}
45 45
46 46
47TypeIndex::TypeIndex(const QByteArray &type) 47TypeIndex::TypeIndex(const QByteArray &type) : mType(type)
48 : mType(type)
49{ 48{
50
51} 49}
52 50
53QByteArray TypeIndex::indexName(const QByteArray &property, const QByteArray &sortProperty) const 51QByteArray TypeIndex::indexName(const QByteArray &property, const QByteArray &sortProperty) const
@@ -58,7 +56,7 @@ QByteArray TypeIndex::indexName(const QByteArray &property, const QByteArray &so
58 return mType + ".index." + property + ".sort." + sortProperty; 56 return mType + ".index." + property + ".sort." + sortProperty;
59} 57}
60 58
61template<> 59template <>
62void TypeIndex::addProperty<QByteArray>(const QByteArray &property) 60void TypeIndex::addProperty<QByteArray>(const QByteArray &property)
63{ 61{
64 auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Sink::Storage::Transaction &transaction) { 62 auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Sink::Storage::Transaction &transaction) {
@@ -69,7 +67,7 @@ void TypeIndex::addProperty<QByteArray>(const QByteArray &property)
69 mProperties << property; 67 mProperties << property;
70} 68}
71 69
72template<> 70template <>
73void TypeIndex::addProperty<QString>(const QByteArray &property) 71void TypeIndex::addProperty<QString>(const QByteArray &property)
74{ 72{
75 auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Sink::Storage::Transaction &transaction) { 73 auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Sink::Storage::Transaction &transaction) {
@@ -80,7 +78,7 @@ void TypeIndex::addProperty<QString>(const QByteArray &property)
80 mProperties << property; 78 mProperties << property;
81} 79}
82 80
83template<> 81template <>
84void TypeIndex::addProperty<QDateTime>(const QByteArray &property) 82void TypeIndex::addProperty<QDateTime>(const QByteArray &property)
85{ 83{
86 auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Sink::Storage::Transaction &transaction) { 84 auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Sink::Storage::Transaction &transaction) {
@@ -94,7 +92,7 @@ void TypeIndex::addProperty<QDateTime>(const QByteArray &property)
94 mProperties << property; 92 mProperties << property;
95} 93}
96 94
97template<> 95template <>
98void TypeIndex::addPropertyWithSorting<QByteArray, QDateTime>(const QByteArray &property, const QByteArray &sortProperty) 96void TypeIndex::addPropertyWithSorting<QByteArray, QDateTime>(const QByteArray &property, const QByteArray &sortProperty)
99{ 97{
100 auto indexer = [=](const QByteArray &identifier, const QVariant &value, const QVariant &sortValue, Sink::Storage::Transaction &transaction) { 98 auto indexer = [=](const QByteArray &identifier, const QVariant &value, const QVariant &sortValue, Sink::Storage::Transaction &transaction) {
@@ -102,7 +100,7 @@ void TypeIndex::addPropertyWithSorting<QByteArray, QDateTime>(const QByteArray &
102 const auto propertyValue = getByteArray(value); 100 const auto propertyValue = getByteArray(value);
103 Index(indexName(property, sortProperty), transaction).add(propertyValue + toSortableByteArray(date), identifier); 101 Index(indexName(property, sortProperty), transaction).add(propertyValue + toSortableByteArray(date), identifier);
104 }; 102 };
105 mSortIndexer.insert(property+sortProperty, indexer); 103 mSortIndexer.insert(property + sortProperty, indexer);
106 mSortedProperties.insert(property, sortProperty); 104 mSortedProperties.insert(property, sortProperty);
107} 105}
108 106
@@ -116,7 +114,7 @@ void TypeIndex::add(const QByteArray &identifier, const Sink::ApplicationDomain:
116 for (auto it = mSortedProperties.constBegin(); it != mSortedProperties.constEnd(); it++) { 114 for (auto it = mSortedProperties.constBegin(); it != mSortedProperties.constEnd(); it++) {
117 const auto value = bufferAdaptor.getProperty(it.key()); 115 const auto value = bufferAdaptor.getProperty(it.key());
118 const auto sortValue = bufferAdaptor.getProperty(it.value()); 116 const auto sortValue = bufferAdaptor.getProperty(it.value());
119 auto indexer = mSortIndexer.value(it.key()+it.value()); 117 auto indexer = mSortIndexer.value(it.key() + it.value());
120 indexer(identifier, value, sortValue, transaction); 118 indexer(identifier, value, sortValue, transaction);
121 } 119 }
122} 120}
@@ -125,7 +123,7 @@ void TypeIndex::remove(const QByteArray &identifier, const Sink::ApplicationDoma
125{ 123{
126 for (const auto &property : mProperties) { 124 for (const auto &property : mProperties) {
127 const auto value = bufferAdaptor.getProperty(property); 125 const auto value = bufferAdaptor.getProperty(property);
128 //FIXME don't always convert to byte array 126 // FIXME don't always convert to byte array
129 Index(indexName(property), transaction).remove(getByteArray(value), identifier); 127 Index(indexName(property), transaction).remove(getByteArray(value), identifier);
130 } 128 }
131 for (auto it = mSortedProperties.constBegin(); it != mSortedProperties.constEnd(); it++) { 129 for (auto it = mSortedProperties.constBegin(); it != mSortedProperties.constEnd(); it++) {
@@ -147,12 +145,8 @@ ResultSet TypeIndex::query(const Sink::Query &query, QSet<QByteArray> &appliedFi
147 Index index(indexName(it.key(), it.value()), transaction); 145 Index index(indexName(it.key(), it.value()), transaction);
148 const auto lookupKey = getByteArray(query.propertyFilter.value(it.key())); 146 const auto lookupKey = getByteArray(query.propertyFilter.value(it.key()));
149 Trace() << "looking for " << lookupKey; 147 Trace() << "looking for " << lookupKey;
150 index.lookup(lookupKey, [&](const QByteArray &value) { 148 index.lookup(lookupKey, [&](const QByteArray &value) { keys << value; },
151 keys << value; 149 [it](const Index::Error &error) { Warning() << "Error in index: " << error.message << it.key() << it.value(); }, true);
152 },
153 [it](const Index::Error &error) {
154 Warning() << "Error in index: " << error.message << it.key() << it.value();
155 }, true);
156 appliedFilters << it.key(); 150 appliedFilters << it.key();
157 appliedSorting = it.value(); 151 appliedSorting = it.value();
158 Trace() << "Index lookup on " << it.key() << it.value() << " found " << keys.size() << " keys."; 152 Trace() << "Index lookup on " << it.key() << it.value() << " found " << keys.size() << " keys.";
@@ -163,12 +157,8 @@ ResultSet TypeIndex::query(const Sink::Query &query, QSet<QByteArray> &appliedFi
163 if (query.propertyFilter.contains(property)) { 157 if (query.propertyFilter.contains(property)) {
164 Index index(indexName(property), transaction); 158 Index index(indexName(property), transaction);
165 const auto lookupKey = getByteArray(query.propertyFilter.value(property)); 159 const auto lookupKey = getByteArray(query.propertyFilter.value(property));
166 index.lookup(lookupKey, [&](const QByteArray &value) { 160 index.lookup(
167 keys << value; 161 lookupKey, [&](const QByteArray &value) { keys << value; }, [property](const Index::Error &error) { Warning() << "Error in index: " << error.message << property; });
168 },
169 [property](const Index::Error &error) {
170 Warning() << "Error in index: " << error.message << property;
171 });
172 appliedFilters << property; 162 appliedFilters << property;
173 Trace() << "Index lookup on " << property << " found " << keys.size() << " keys."; 163 Trace() << "Index lookup on " << property << " found " << keys.size() << " keys.";
174 return ResultSet(keys); 164 return ResultSet(keys);
@@ -177,4 +167,3 @@ ResultSet TypeIndex::query(const Sink::Query &query, QSet<QByteArray> &appliedFi
177 Trace() << "No matching index"; 167 Trace() << "No matching index";
178 return ResultSet(keys); 168 return ResultSet(keys);
179} 169}
180