Changeset View
Changeset View
Standalone View
Standalone View
src/lib/searchstore.cpp
Show All 40 Lines | |||||
41 | #include <KFileMetaData/TypeInfo> | 41 | #include <KFileMetaData/TypeInfo> | ||
42 | #include <KFileMetaData/Types> | 42 | #include <KFileMetaData/Types> | ||
43 | 43 | | |||
44 | #include <algorithm> | 44 | #include <algorithm> | ||
45 | #include <tuple> | 45 | #include <tuple> | ||
46 | 46 | | |||
47 | using namespace Baloo; | 47 | using namespace Baloo; | ||
48 | 48 | | |||
49 | namespace { | ||||
50 | QPair<quint32, quint32> calculateTimeRange(const QDateTime& dt, Term::Comparator com) | ||||
51 | { | ||||
52 | Q_ASSERT(dt.isValid()); | ||||
53 | quint32 timet = dt.toSecsSinceEpoch(); | ||||
54 | | ||||
55 | if (com == Term::LessEqual) { | ||||
56 | return {0, timet}; | ||||
57 | } | ||||
58 | if (com == Term::Less) { | ||||
59 | return {0, timet - 1}; | ||||
60 | } | ||||
61 | if (com == Term::GreaterEqual) { | ||||
62 | return {timet, std::numeric_limits<quint32>::max()}; | ||||
63 | } | ||||
64 | if (com == Term::Greater) { | ||||
65 | return {timet + 1, std::numeric_limits<quint32>::max()}; | ||||
66 | } | ||||
67 | if (com == Term::Equal) { | ||||
68 | timet = QDateTime(dt.date()).toSecsSinceEpoch(); | ||||
69 | return {timet, timet + 24 * 60 * 60 - 1}; | ||||
70 | } | ||||
71 | | ||||
72 | Q_ASSERT_X(0, __FUNC__, "mtime query must contain a valid comparator"); | ||||
73 | return {0, 0}; | ||||
74 | } | ||||
75 | } | ||||
76 | | ||||
49 | SearchStore::SearchStore() | 77 | SearchStore::SearchStore() | ||
50 | : m_db(nullptr) | 78 | : m_db(nullptr) | ||
51 | { | 79 | { | ||
52 | m_db = globalDatabaseInstance(); | 80 | m_db = globalDatabaseInstance(); | ||
53 | if (!m_db->open(Database::ReadOnlyDatabase)) { | 81 | if (!m_db->open(Database::ReadOnlyDatabase)) { | ||
54 | m_db = nullptr; | 82 | m_db = nullptr; | ||
55 | } | 83 | } | ||
56 | 84 | | |||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Line(s) | 175 | else { | |||
148 | KFileMetaData::PropertyInfo pi = KFileMetaData::PropertyInfo::fromName(property); | 176 | KFileMetaData::PropertyInfo pi = KFileMetaData::PropertyInfo::fromName(property); | ||
149 | if (pi.property() == KFileMetaData::Property::Empty) { | 177 | if (pi.property() == KFileMetaData::Property::Empty) { | ||
150 | qDebug() << "Property" << property << "not found"; | 178 | qDebug() << "Property" << property << "not found"; | ||
151 | return QByteArray(); | 179 | return QByteArray(); | ||
152 | } | 180 | } | ||
153 | int propPrefix = static_cast<int>(pi.property()); | 181 | int propPrefix = static_cast<int>(pi.property()); | ||
154 | return 'X' + QByteArray::number(propPrefix) + '-'; | 182 | return 'X' + QByteArray::number(propPrefix) + '-'; | ||
155 | } | 183 | } | ||
156 | | ||||
157 | } | 184 | } | ||
158 | 185 | | |||
159 | PostingIterator* SearchStore::constructQuery(Transaction* tr, const Term& term) | 186 | PostingIterator* SearchStore::constructQuery(Transaction* tr, const Term& term) | ||
160 | { | 187 | { | ||
161 | Q_ASSERT(tr); | 188 | Q_ASSERT(tr); | ||
162 | 189 | | |||
163 | if (term.operation() == Term::And || term.operation() == Term::Or) { | 190 | if (term.operation() == Term::And || term.operation() == Term::Or) { | ||
164 | const QList<Term> subTerms = term.subTerms(); | 191 | const QList<Term> subTerms = term.subTerms(); | ||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Line(s) | 251 | if (value.type() == QVariant::ByteArray) { | |||
243 | } else if (day == 0) { | 270 | } else if (day == 0) { | ||
244 | endDate.setDate(endDate.year(), endDate.month(), endDate.daysInMonth()); | 271 | endDate.setDate(endDate.year(), endDate.month(), endDate.daysInMonth()); | ||
245 | } | 272 | } | ||
246 | 273 | | |||
247 | return tr->mTimeRangeIter(QDateTime(startDate).toSecsSinceEpoch(), QDateTime(endDate, QTime(23, 59, 59)).toSecsSinceEpoch()); | 274 | return tr->mTimeRangeIter(QDateTime(startDate).toSecsSinceEpoch(), QDateTime(endDate, QTime(23, 59, 59)).toSecsSinceEpoch()); | ||
248 | } | 275 | } | ||
249 | else if (value.type() == QVariant::Date || value.type() == QVariant::DateTime) { | 276 | else if (value.type() == QVariant::Date || value.type() == QVariant::DateTime) { | ||
250 | const QDateTime dt = value.toDateTime(); | 277 | const QDateTime dt = value.toDateTime(); | ||
251 | return constructMTimeQuery(tr, dt, term.comparator()); | 278 | QPair<quint32, quint32> timerange = calculateTimeRange(dt, term.comparator()); | ||
279 | if ((timerange.first == 0) && (timerange.second == 0)) { | ||||
280 | return nullptr; | ||||
281 | } | ||||
282 | return tr->mTimeRangeIter(timerange.first, timerange.second); | ||||
252 | } | 283 | } | ||
253 | else { | 284 | else { | ||
254 | Q_ASSERT_X(0, "SearchStore::constructQuery", "modified property must contain date/datetime values"); | 285 | Q_ASSERT_X(0, "SearchStore::constructQuery", "modified property must contain date/datetime values"); | ||
286 | return nullptr; | ||||
255 | } | 287 | } | ||
256 | } else if (property == "tag") { | 288 | } else if (property == "tag") { | ||
257 | if (term.comparator() == Term::Equal) { | 289 | if (term.comparator() == Term::Equal) { | ||
258 | const QByteArray prefix = "TAG-"; | 290 | const QByteArray prefix = "TAG-"; | ||
259 | EngineQuery q = EngineQuery(prefix + value.toByteArray()); | 291 | EngineQuery q = EngineQuery(prefix + value.toByteArray()); | ||
260 | return tr->postingIterator(q); | 292 | return tr->postingIterator(q); | ||
261 | } else if (term.comparator() == Term::Contains) { | 293 | } else if (term.comparator() == Term::Contains) { | ||
262 | const QByteArray prefix = "TA"; | 294 | const QByteArray prefix = "TA"; | ||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Line(s) | 390 | if (ti == KFileMetaData::Type::Empty) { | |||
359 | qDebug() << "Type" << value << "does not exist"; | 391 | qDebug() << "Type" << value << "does not exist"; | ||
360 | return EngineQuery(); | 392 | return EngineQuery(); | ||
361 | } | 393 | } | ||
362 | int num = static_cast<int>(ti.type()); | 394 | int num = static_cast<int>(ti.type()); | ||
363 | 395 | | |||
364 | return EngineQuery('T' + QByteArray::number(num)); | 396 | return EngineQuery('T' + QByteArray::number(num)); | ||
365 | } | 397 | } | ||
366 | 398 | | |||
367 | PostingIterator* SearchStore::constructMTimeQuery(Transaction* tr, const QDateTime& dt, Term::Comparator com) | | |||
368 | { | | |||
369 | Q_ASSERT(dt.isValid()); | | |||
370 | quint32 timet = dt.toSecsSinceEpoch(); | | |||
371 | | ||||
372 | MTimeDB::Comparator mtimeCom; | | |||
373 | if (com == Term::Equal) { | | |||
374 | mtimeCom = MTimeDB::Equal; | | |||
375 | quint32 end = QDateTime(dt.date().addDays(1)).toSecsSinceEpoch() - 1; | | |||
376 | | ||||
377 | return tr->mTimeRangeIter(timet, end); | | |||
378 | } | | |||
379 | else if (com == Term::GreaterEqual) { | | |||
380 | mtimeCom = MTimeDB::GreaterEqual; | | |||
381 | } else if (com == Term::Greater) { | | |||
382 | timet++; | | |||
383 | mtimeCom = MTimeDB::GreaterEqual; | | |||
384 | } else if (com == Term::LessEqual) { | | |||
385 | mtimeCom = MTimeDB::LessEqual; | | |||
386 | } else if (com == Term::Less) { | | |||
387 | mtimeCom = MTimeDB::LessEqual; | | |||
388 | timet--; | | |||
389 | } else { | | |||
390 | Q_ASSERT_X(0, "SearchStore::constructQuery", "mtime query must contain a valid comparator"); | | |||
391 | return nullptr; | | |||
392 | } | | |||
393 | | ||||
394 | return tr->mTimeIter(timet, mtimeCom); | | |||
395 | } | |