summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/fulltextindex.cpp34
-rw-r--r--common/fulltextindex.h7
-rw-r--r--sinksh/syntax_modules/sink_inspect.cpp37
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<QByteArray> FulltextIndex::lookup(const QString &searchTerm)
182 return results; 182 return results;
183} 183}
184 184
185qint64 FulltextIndex::getDoccount() const
186{
187 if (!mDb) {
188 return -1;
189 }
190 try {
191 return mDb->get_doccount();
192 } catch (const Xapian::Error &) {
193 // Nothing to do, move along
194 }
195 return -1;
196}
197
198FulltextIndex::Result FulltextIndex::getIndexContent(const QByteArray &identifier) const
199{
200 if (!mDb) {
201 {};
202 }
203 try {
204 auto id = "Q" + identifier.toStdString();
205 Xapian::PostingIterator p = mDb->postlist_begin(id);
206 if (p != mDb->postlist_end(id)) {
207 auto document = mDb->get_document(*p);
208 QStringList terms;
209 for (auto it = document.termlist_begin(); it != document.termlist_end(); it++) {
210 terms << QString::fromStdString(*it);
211 }
212 return {true, terms};
213 }
214 } catch (const Xapian::Error &) {
215 // Nothing to do, move along
216 }
217 return {};
218}
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:
29 29
30 QVector<QByteArray> lookup(const QString &key); 30 QVector<QByteArray> lookup(const QString &key);
31 31
32 qint64 getDoccount() const;
33 struct Result {
34 bool found{false};
35 QStringList terms;
36 };
37 Result getIndexContent(const QByteArray &identifier) const;
38
32private: 39private:
33 Xapian::WritableDatabase* writableDatabase(); 40 Xapian::WritableDatabase* writableDatabase();
34 Q_DISABLE_COPY(FulltextIndex); 41 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 @@
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */ 18 */
19 19
20//xapian.h needs to be included first to build
21#include <xapian.h>
22
23#include <QDebug> 20#include <QDebug>
24#include <QObject> // tr() 21#include <QObject> // tr()
25#include <QFile> 22#include <QFile>
@@ -33,6 +30,7 @@
33#include "common/entitybuffer.h" 30#include "common/entitybuffer.h"
34#include "common/metadata_generated.h" 31#include "common/metadata_generated.h"
35#include "common/bufferutils.h" 32#include "common/bufferutils.h"
33#include "common/fulltextindex.h"
36 34
37#include "sinksh_utils.h" 35#include "sinksh_utils.h"
38#include "state.h" 36#include "state.h"
@@ -120,32 +118,19 @@ bool inspect(const QStringList &args, State &state)
120 return false; 118 return false;
121 } 119 }
122 if (options.options.contains("fulltext")) { 120 if (options.options.contains("fulltext")) {
123 try { 121 FulltextIndex index(resource, Sink::Storage::DataStore::ReadOnly);
124 Xapian::Database db(QFile::encodeName(Sink::resourceStorageLocation(resource) + '/' + "fulltext").toStdString(), Xapian::DB_OPEN); 122 if (options.options.value("fulltext").isEmpty()) {
125 if (options.options.value("fulltext").isEmpty()) { 123 state.printLine(QString("Total document count: ") + QString::number(index.getDoccount()));
126 state.printLine(QString("Total document count: ") + QString::number(db.get_doccount())); 124 } else {
125 const auto entityId = SinkshUtils::parseUid(options.options.value("fulltext").first().toUtf8());
126 const auto content = index.getIndexContent(entityId);
127 if (!content.found) {
128 state.printLine(QString("Failed to find the document with the id: ") + entityId);
127 } else { 129 } else {
128 auto entityId = SinkshUtils::parseUid(options.options.value("fulltext").first().toUtf8()); 130 state.printLine(QString("Found document with terms: ") + content.terms.join(", "), 1);
129 auto id = "Q" + entityId.toStdString();
130 Xapian::PostingIterator p = db.postlist_begin(id);
131 if (p == db.postlist_end(id)) {
132 state.printLine(QString("Failed to find the document with the id: ") + QString::fromStdString(id));
133 } else {
134 state.printLine(QString("Found the document: "));
135 auto document = db.get_document(*p);
136
137 QStringList terms;
138 for (auto it = document.termlist_begin(); it != document.termlist_end(); it++) {
139 terms << QString::fromStdString(*it);
140 }
141 state.printLine(QString("Terms: ") + terms.join(", "), 1);
142 }
143
144 } 131 }
145 } catch (const Xapian::Error &) {
146 // Nothing to do, move along
147 }
148 132
133 }
149 return false; 134 return false;
150 } 135 }
151 136