diff --git a/src/engine/enginequery.cpp b/src/engine/enginequery.cpp index 11faea5c..8d1b6e34 100644 --- a/src/engine/enginequery.cpp +++ b/src/engine/enginequery.cpp @@ -1,50 +1,50 @@ /* * This file is part of the KDE Baloo Project * Copyright (C) 2015 Vishesh Handa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ #include "enginequery.h" using namespace Baloo; EngineQuery::EngineQuery() : m_pos(0) , m_op(Equal) { } EngineQuery::EngineQuery(const QByteArray& term, int pos) : m_term(term) , m_pos(pos) , m_op(Equal) { } EngineQuery::EngineQuery(const QByteArray& term, EngineQuery::Operation op, int pos) : m_term(term) , m_pos(pos) , m_op(op) { } -EngineQuery::EngineQuery(const QVector subQueries, Operation op) +EngineQuery::EngineQuery(const QVector &subQueries, Operation op) : m_pos(0) , m_op(op) , m_subQueries(subQueries) { } diff --git a/src/engine/enginequery.h b/src/engine/enginequery.h index 9d2ab934..693360c3 100644 --- a/src/engine/enginequery.h +++ b/src/engine/enginequery.h @@ -1,105 +1,105 @@ /* * This file is part of the KDE Baloo Project * Copyright (C) 2015 Vishesh Handa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ #ifndef BALOO_ENGINEQUERY_H #define BALOO_ENGINEQUERY_H #include "engine_export.h" #include #include #include namespace Baloo { class BALOO_ENGINE_EXPORT EngineQuery { public: enum Operation { Equal, StartsWith, And, Or, Phrase }; EngineQuery(); EngineQuery(const QByteArray& term, int pos = 0); EngineQuery(const QByteArray& term, Operation op, int pos = 0); - EngineQuery(const QVector subQueries, Operation op); + EngineQuery(const QVector &subQueries, Operation op); QByteArray term() const { return m_term; } int pos() const { return m_pos; } Operation op() const { return m_op; } void setOp(const Operation& op) { m_op = op; } bool leaf() const { return !m_term.isEmpty(); } bool empty() { return m_subQueries.isEmpty() && m_term.isEmpty(); } QVector subQueries() const { return m_subQueries; } bool operator ==(const EngineQuery& q) const { return m_term == q.m_term && m_pos == q.m_pos && m_op == q.m_op && m_subQueries == q.m_subQueries; } private: QByteArray m_term; int m_pos; Operation m_op; QVector m_subQueries; }; } // namespace Baloo inline QDebug operator << (QDebug d, const Baloo::EngineQuery& q) { QDebugStateSaver state(d); d.setAutoInsertSpaces(false); if (q.op() == Baloo::EngineQuery::And) { d << "[AND " << q.subQueries() << "]"; } else if (q.op() == Baloo::EngineQuery::Or) { d << "[OR " << q.subQueries() << "]"; } else if (q.op() == Baloo::EngineQuery::Phrase) { d << "[PHRASE " << q.subQueries() << "]"; } else { Q_ASSERT(q.subQueries().isEmpty()); d << "(" << q.term() << "," << q.pos() << "," << q.op() << ")"; } return d; } #endif diff --git a/src/engine/idtreedb.cpp b/src/engine/idtreedb.cpp index 1a2c99a8..1d90629d 100644 --- a/src/engine/idtreedb.cpp +++ b/src/engine/idtreedb.cpp @@ -1,196 +1,196 @@ /* This file is part of the KDE Baloo project. * Copyright (C) 2015 Vishesh Handa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ #include "idtreedb.h" #include "enginedebug.h" #include "postingiterator.h" #include using namespace Baloo; IdTreeDB::IdTreeDB(MDB_dbi dbi, MDB_txn* txn) : m_txn(txn) , m_dbi(dbi) { Q_ASSERT(txn != nullptr); Q_ASSERT(dbi != 0); } MDB_dbi IdTreeDB::create(MDB_txn* txn) { MDB_dbi dbi = 0; int rc = mdb_dbi_open(txn, "idtree", MDB_CREATE | MDB_INTEGERKEY, &dbi); if (rc) { qCWarning(ENGINE) << "IdTreeDB::create" << mdb_strerror(rc); return 0; } return dbi; } MDB_dbi IdTreeDB::open(MDB_txn* txn) { MDB_dbi dbi = 0; int rc = mdb_dbi_open(txn, "idtree", MDB_INTEGERKEY, &dbi); if (rc) { qCWarning(ENGINE) << "IdTreeDB::open" << mdb_strerror(rc); return 0; } return dbi; } -void IdTreeDB::put(quint64 docId, const QVector subDocIds) +void IdTreeDB::put(quint64 docId, const QVector &subDocIds) { Q_ASSERT(!subDocIds.isEmpty()); Q_ASSERT(!subDocIds.contains(0)); MDB_val key; key.mv_size = sizeof(quint64); key.mv_data = static_cast(&docId); MDB_val val; val.mv_size = subDocIds.size() * sizeof(quint64); val.mv_data = static_cast(const_cast(subDocIds.constData())); int rc = mdb_put(m_txn, m_dbi, &key, &val, 0); if (rc) { qCWarning(ENGINE) << "IdTreeDB::put" << mdb_strerror(rc); } } QVector IdTreeDB::get(quint64 docId) { MDB_val key; key.mv_size = sizeof(quint64); key.mv_data = static_cast(&docId); MDB_val val{0, nullptr}; int rc = mdb_get(m_txn, m_dbi, &key, &val); if (rc) { if (rc != MDB_NOTFOUND) { qCDebug(ENGINE) << "IdTreeDB::get" << docId << mdb_strerror(rc); } return QVector(); } // FIXME: This still makes a copy of the data. Perhaps we can avoid that? QVector list(val.mv_size / sizeof(quint64)); memcpy(list.data(), val.mv_data, val.mv_size); return list; } void IdTreeDB::del(quint64 docId) { MDB_val key; key.mv_size = sizeof(quint64); key.mv_data = static_cast(&docId); int rc = mdb_del(m_txn, m_dbi, &key, nullptr); if (rc != 0 && rc != MDB_NOTFOUND) { qCDebug(ENGINE) << "IdTreeDB::del" << mdb_strerror(rc); } } // // Iter // class IdTreePostingIterator : public PostingIterator { public: IdTreePostingIterator(const IdTreeDB& db, const QVector list) : m_db(db), m_pos(-1), m_idList(list) {} quint64 docId() const override { if (m_pos >= 0 && m_pos < m_resultList.size()) return m_resultList[m_pos]; return 0; } quint64 next() override { if (m_resultList.isEmpty() && m_idList.isEmpty()) { return 0; } if (m_resultList.isEmpty()) { while (!m_idList.isEmpty()) { quint64 id = m_idList.takeLast(); m_idList << m_db.get(id); m_resultList << id; } std::sort(m_resultList.begin(), m_resultList.end()); m_pos = 0; } else { if (m_pos < m_resultList.size()) m_pos++; else m_resultList.clear(); } if (m_pos < m_resultList.size()) return m_resultList[m_pos]; else return 0; } private: IdTreeDB m_db; int m_pos; QVector m_idList; QVector m_resultList; }; PostingIterator* IdTreeDB::iter(quint64 docId) { Q_ASSERT(docId > 0); QVector list = {docId}; return new IdTreePostingIterator(*this, list); } QMap> IdTreeDB::toTestMap() const { MDB_cursor* cursor; mdb_cursor_open(m_txn, m_dbi, &cursor); MDB_val key = {0, nullptr}; MDB_val val; QMap> map; while (1) { int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); if (rc) { qCDebug(ENGINE) << "PostingDB::toTestMap" << mdb_strerror(rc); break; } const quint64 id = *(static_cast(key.mv_data)); QVector list(val.mv_size / sizeof(quint64)); memcpy(list.data(), val.mv_data, val.mv_size); map.insert(id, list); } mdb_cursor_close(cursor); return map; } diff --git a/src/engine/idtreedb.h b/src/engine/idtreedb.h index 8b6e5f02..66217a43 100644 --- a/src/engine/idtreedb.h +++ b/src/engine/idtreedb.h @@ -1,58 +1,58 @@ /* This file is part of the KDE Baloo project. * Copyright (C) 2015 Vishesh Handa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ #ifndef BALOO_IDTREEDB_H #define BALOO_IDTREEDB_H #include "engine_export.h" #include #include #include namespace Baloo { class PostingIterator; class BALOO_ENGINE_EXPORT IdTreeDB { public: IdTreeDB(MDB_dbi dbi, MDB_txn* txn); static MDB_dbi create(MDB_txn* txn); static MDB_dbi open(MDB_txn* txn); - void put(quint64 docId, const QVector subDocIds); + void put(quint64 docId, const QVector &subDocIds); QVector get(quint64 docId); void del(quint64 docId); /** * Returns an iterator which will return all the docIds which use \p docId * are the parent docID. */ PostingIterator* iter(quint64 docId); QMap> toTestMap() const; private: MDB_txn* m_txn; MDB_dbi m_dbi; }; } #endif // BALOO_IDTREEDB_H