From 0a6229931ac90fa8914dbdafef51adf5ca92a206 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 27 Jul 2018 12:18:06 +0200 Subject: All xapian stuff in a central place --- common/fulltextindex.cpp | 34 +++++++++++++++++++++++++++++++ common/fulltextindex.h | 7 +++++++ sinksh/syntax_modules/sink_inspect.cpp | 37 ++++++++++------------------------ 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/common/fulltextindex.cpp b/common/fulltextindex.cpp index 164a5b9..b1c6178 100644 --- a/common/fulltextindex.cpp +++ b/common/fulltextindex.cpp @@ -182,3 +182,37 @@ QVector FulltextIndex::lookup(const QString &searchTerm) return results; } +qint64 FulltextIndex::getDoccount() const +{ + if (!mDb) { + return -1; + } + try { + return mDb->get_doccount(); + } catch (const Xapian::Error &) { + // Nothing to do, move along + } + return -1; +} + +FulltextIndex::Result FulltextIndex::getIndexContent(const QByteArray &identifier) const +{ + if (!mDb) { + {}; + } + try { + auto id = "Q" + identifier.toStdString(); + Xapian::PostingIterator p = mDb->postlist_begin(id); + if (p != mDb->postlist_end(id)) { + auto document = mDb->get_document(*p); + QStringList terms; + for (auto it = document.termlist_begin(); it != document.termlist_end(); it++) { + terms << QString::fromStdString(*it); + } + return {true, terms}; + } + } catch (const Xapian::Error &) { + // Nothing to do, move along + } + return {}; +} diff --git a/common/fulltextindex.h b/common/fulltextindex.h index e06f29d..f24af3b 100644 --- a/common/fulltextindex.h +++ b/common/fulltextindex.h @@ -29,6 +29,13 @@ public: QVector lookup(const QString &key); + qint64 getDoccount() const; + struct Result { + bool found{false}; + QStringList terms; + }; + Result getIndexContent(const QByteArray &identifier) const; + private: Xapian::WritableDatabase* writableDatabase(); Q_DISABLE_COPY(FulltextIndex); diff --git a/sinksh/syntax_modules/sink_inspect.cpp b/sinksh/syntax_modules/sink_inspect.cpp index 1a964cf..e0e30c7 100644 --- a/sinksh/syntax_modules/sink_inspect.cpp +++ b/sinksh/syntax_modules/sink_inspect.cpp @@ -17,9 +17,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -//xapian.h needs to be included first to build -#include - #include #include // tr() #include @@ -33,6 +30,7 @@ #include "common/entitybuffer.h" #include "common/metadata_generated.h" #include "common/bufferutils.h" +#include "common/fulltextindex.h" #include "sinksh_utils.h" #include "state.h" @@ -120,32 +118,19 @@ bool inspect(const QStringList &args, State &state) return false; } if (options.options.contains("fulltext")) { - try { - Xapian::Database db(QFile::encodeName(Sink::resourceStorageLocation(resource) + '/' + "fulltext").toStdString(), Xapian::DB_OPEN); - if (options.options.value("fulltext").isEmpty()) { - state.printLine(QString("Total document count: ") + QString::number(db.get_doccount())); + FulltextIndex index(resource, Sink::Storage::DataStore::ReadOnly); + if (options.options.value("fulltext").isEmpty()) { + state.printLine(QString("Total document count: ") + QString::number(index.getDoccount())); + } else { + const auto entityId = SinkshUtils::parseUid(options.options.value("fulltext").first().toUtf8()); + const auto content = index.getIndexContent(entityId); + if (!content.found) { + state.printLine(QString("Failed to find the document with the id: ") + entityId); } else { - auto entityId = SinkshUtils::parseUid(options.options.value("fulltext").first().toUtf8()); - auto id = "Q" + entityId.toStdString(); - Xapian::PostingIterator p = db.postlist_begin(id); - if (p == db.postlist_end(id)) { - state.printLine(QString("Failed to find the document with the id: ") + QString::fromStdString(id)); - } else { - state.printLine(QString("Found the document: ")); - auto document = db.get_document(*p); - - QStringList terms; - for (auto it = document.termlist_begin(); it != document.termlist_end(); it++) { - terms << QString::fromStdString(*it); - } - state.printLine(QString("Terms: ") + terms.join(", "), 1); - } - + state.printLine(QString("Found document with terms: ") + content.terms.join(", "), 1); } - } catch (const Xapian::Error &) { - // Nothing to do, move along - } + } return false; } -- cgit v1.2.3