From b093ffda77ee716763496be552b06cee5cb1b4e5 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 13 Feb 2018 12:04:52 +0100 Subject: Added some fulltext index inspection --- sinksh/syntax_modules/sink_inspect.cpp | 38 +++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'sinksh/syntax_modules') diff --git a/sinksh/syntax_modules/sink_inspect.cpp b/sinksh/syntax_modules/sink_inspect.cpp index f75b59f..efa5bb4 100644 --- a/sinksh/syntax_modules/sink_inspect.cpp +++ b/sinksh/syntax_modules/sink_inspect.cpp @@ -17,8 +17,12 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +//xapian.h needs to be included first to build +#include + #include #include // tr() +#include #include "common/resource.h" #include "common/storage.h" @@ -40,7 +44,7 @@ namespace SinkInspect bool inspect(const QStringList &args, State &state) { if (args.isEmpty()) { - state.printError(QObject::tr("Options: [--resource $resource] ([--db $db] [--filter $id] [--showinternal] | [--validaterids $type])")); + state.printError(QObject::tr("Options: [--resource $resource] ([--db $db] [--filter $id] [--showinternal] | [--validaterids $type] | [--fulltext [$id]])")); } auto options = SyntaxTree::parseOptions(args); auto resource = SinkshUtils::parseUid(options.options.value("resource").value(0).toUtf8()); @@ -48,8 +52,7 @@ bool inspect(const QStringList &args, State &state) Sink::Storage::DataStore storage(Sink::storageLocation(), resource, Sink::Storage::DataStore::ReadOnly); auto transaction = storage.createTransaction(Sink::Storage::DataStore::ReadOnly); - bool validateRids = options.options.contains("validaterids"); - if (validateRids) { + if (options.options.contains("validaterids")) { if (options.options.value("validaterids").isEmpty()) { state.printError(QObject::tr("Specify a type to validate.")); return false; @@ -116,6 +119,35 @@ 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())); + } 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); + } + + } + } catch (const Xapian::Error &error) { + // Nothing to do, move along + } + + return false; + } auto dbs = options.options.value("db"); auto idFilter = options.options.value("filter"); -- cgit v1.2.3