Parent: [0dde29] (diff)

Child: [15855b] (diff)

Download this file

rcldb.cpp    380 lines (331 with data), 9.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#ifndef lint
static char rcsid[] = "@(#$Id: rcldb.cpp,v 1.6 2005-01-24 13:17:58 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
#include <sys/stat.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include "rcldb.h"
#include "textsplit.h"
#include "transcode.h"
#include "unacpp.h"
#include "conftree.h"
#include "xapian.h"
// Data for a xapian database. There could actually be 2 different ones for
// indexing or query as there is not much in common.
class Native {
public:
bool isopen;
bool iswritable;
// Indexing
Xapian::WritableDatabase wdb;
vector<bool> updated;
// Querying
Xapian::Database db;
Xapian::Query query;
Native() : isopen(false), iswritable(false) {}
};
Rcl::Db::Db()
{
pdata = new Native;
}
Rcl::Db::~Db()
{
cerr << "Rcl::Db::~Db" << endl;
if (pdata == 0)
return;
Native *ndb = (Native *)pdata;
cerr << "Db::~Db: isopen " << ndb->isopen << " iswritable " <<
ndb->iswritable << endl;
try {
// There is nothing to do for an ro db.
if (ndb->isopen == false || ndb->iswritable == false) {
cerr << "Deleting native database" << endl;
delete ndb;
return;
}
ndb->wdb.flush();
delete ndb;
} catch (const Xapian::Error &e) {
cout << "Exception: " << e.get_msg() << endl;
} catch (const string &s) {
cout << "Exception: " << s << endl;
} catch (const char *s) {
cout << "Exception: " << s << endl;
} catch (...) {
cout << "Caught unknown exception" << endl;
}
}
bool Rcl::Db::open(const string& dir, OpenMode mode)
{
if (pdata == 0)
return false;
Native *ndb = (Native *)pdata;
cerr << "Db::open: isopen " << ndb->isopen << " iswritable " <<
ndb->iswritable << endl;
try {
switch (mode) {
case DbUpd:
ndb->wdb = Xapian::Auto::open(dir, Xapian::DB_CREATE_OR_OPEN);
ndb->updated.resize(ndb->wdb.get_lastdocid() + 1);
ndb->iswritable = true;
break;
case DbTrunc:
ndb->wdb = Xapian::Auto::open(dir, Xapian::DB_CREATE_OR_OVERWRITE);
ndb->iswritable = true;
break;
case DbRO:
default:
ndb->iswritable = false;
ndb->db = Xapian::Auto::open(dir, Xapian::DB_OPEN);
break;
}
ndb->isopen = true;
return true;
} catch (const Xapian::Error &e) {
cout << "Exception: " << e.get_msg() << endl;
} catch (const string &s) {
cout << "Exception: " << s << endl;
} catch (const char *s) {
cout << "Exception: " << s << endl;
} catch (...) {
cout << "Caught unknown exception" << endl;
}
return false;
}
bool Rcl::Db::close()
{
if (pdata == 0)
return false;
Native *ndb = (Native *)pdata;
cerr << "Db::open: isopen " << ndb->isopen << " iswritable " <<
ndb->iswritable << endl;
if (ndb->isopen == false)
return true;
try {
if (ndb->isopen == true && ndb->iswritable == true) {
ndb->wdb.flush();
}
delete ndb;
} catch (const Xapian::Error &e) {
cout << "Exception: " << e.get_msg() << endl;
return false;
} catch (const string &s) {
cout << "Exception: " << s << endl;
return false;
} catch (const char *s) {
cout << "Exception: " << s << endl;
return false;
} catch (...) {
cout << "Caught unknown exception" << endl;
return false;
}
pdata = new Native;
if (pdata)
return true;
return false;
}
// A small class to hold state while splitting text
class wsData {
public:
Xapian::Document &doc;
Xapian::termpos basepos; // Base for document section
Xapian::termpos curpos; // Last position sent to callback
wsData(Xapian::Document &d) : doc(d), basepos(1), curpos(0)
{}
};
// Callback for the document to word splitting class during indexation
static bool splitCb(void *cdata, const std::string &term, int pos)
{
wsData *data = (wsData*)cdata;
// cerr << "splitCb: term " << term << endl;
//string printable;
//transcode(term, printable, "UTF-8", "ISO8859-1");
//cerr << "Adding " << printable << endl;
try {
// 1 is the value for wdfinc in index_text when called from omindex
// TOBEDONE: check what this is used for
data->curpos = pos;
data->doc.add_posting(term, data->basepos + data->curpos, 1);
} catch (...) {
cerr << "Error occurred during add_posting" << endl;
return false;
}
return true;
}
// Unaccent and lowercase data: use unac
// for accents, and do it by hand for upper / lower. Note lowercasing is
// only for ascii letters anyway, so it's just A-Z -> a-z
bool dumb_string(const string &in, string &out)
{
string inter;
out.erase();
if (!unac_cpp(in, inter))
return false;
out.reserve(inter.length());
for (unsigned int i = 0; i < inter.length(); i++) {
if (inter[i] >= 'A' && inter[i] <= 'Z')
out += inter[i] + 'a' - 'A';
else
out += inter[i];
}
return true;
}
bool Rcl::Db::add(const string &fn, const Rcl::Doc &doc)
{
if (pdata == 0)
return false;
Native *ndb = (Native *)pdata;
Xapian::Document newdocument;
// Document data record. omindex has the following nl separated fields:
// - url
// - sample
// - caption (title limited to 100 chars)
// - mime type
string record = "url=file:/" + fn;
record += "\nmtime=" + doc.mtime;
record += "\nsample=";
record += "\ncaption=" + doc.title;
record += "\nmtype=" + doc.mimetype;
record += "\n";
newdocument.set_data(record);
wsData splitData(newdocument);
TextSplit splitter(splitCb, &splitData);
string noacc;
if (!unac_cpp(doc.title, noacc)) {
return false;
}
splitter.text_to_words(noacc);
splitData.basepos += splitData.curpos + 100;
if (!dumb_string(doc.text, noacc)) {
return false;
}
splitter.text_to_words(noacc);
splitData.basepos += splitData.curpos + 100;
if (!dumb_string(doc.keywords, noacc)) {
return false;
}
splitter.text_to_words(noacc);
splitData.basepos += splitData.curpos + 100;
if (!dumb_string(doc.abstract, noacc)) {
return false;
}
splitter.text_to_words(noacc);
newdocument.add_term("T" + doc.mimetype);
string pathterm = "P" + fn;
newdocument.add_term(pathterm);
if (1 /*dupes == DUPE_replace*/) {
// If this document has already been indexed, update the existing
// entry.
try {
#if 0
Xapian::docid did =
#endif
ndb->wdb.replace_document(pathterm, newdocument);
#if 0
if (did < updated.size()) {
updated[did] = true;
//cout << "updated." << endl;
} else {
//cout << "added." << endl;
}
#endif
} catch (...) {
// FIXME: is this ever actually needed?
ndb->wdb.add_document(newdocument);
//cout << "added (failed re-seek for duplicate)." << endl;
}
} else {
try {
ndb->wdb.add_document(newdocument);
// cout << "added." << endl;
} catch (...) {
cerr << "Got exception while adding doc" << endl;
return false;
}
}
return true;
}
bool Rcl::Db::needUpdate(const string &filename, const struct stat *stp)
{
if (pdata == 0)
return false;
Native *ndb = (Native *)pdata;
string pathterm = "P" + filename;
if (!ndb->wdb.term_exists(pathterm))
return true;
Xapian::PostingIterator doc;
try {
Xapian::PostingIterator did = ndb->wdb.postlist_begin(pathterm);
if (did == ndb->wdb.postlist_end(pathterm))
return true;
Xapian::Document doc = ndb->wdb.get_document(*did);
string data = doc.get_data();
//cout << "DOCUMENT EXISTS " << data << endl;
const char *cp = strstr(data.c_str(), "mtime=");
cp += 6;
long mtime = atol(cp);
if (mtime >= stp->st_mtime) {
// cerr << "DOCUMENT UP TO DATE" << endl;
return false;
}
} catch (...) {
return true;
}
return true;
}
#include <vector>
class wsQData {
public:
vector<string> terms;
};
// Callback for the document to word splitting class during indexation
static bool splitQCb(void *cdata, const std::string &term, int )
{
wsQData *data = (wsQData*)cdata;
cerr << "splitQCb: term '" << term << "'" << endl;
cerr << "splitQCb: term length: " << term.length() << endl;
//string printable;
//transcode(term, printable, "UTF-8", "ISO8859-1");
//cerr << "Adding " << printable << endl;
data->terms.push_back(term);
return true;
}
bool Rcl::Db::setQuery(const std::string &querystring)
{
wsQData splitData;
TextSplit splitter(splitQCb, &splitData);
string noacc;
if (!dumb_string(querystring, noacc)) {
return false;
}
// noacc = querystring;
splitter.text_to_words(noacc);
Native *ndb = (Native *)pdata;
// splitData.terms.resize(0);
// splitData.terms.push_back(string("le"));
ndb->query = Xapian::Query(Xapian::Query::OP_OR, splitData.terms.begin(),
splitData.terms.end());
return true;
}
bool Rcl::Db::getDoc(int i, Doc &doc)
{
// cerr << "Rcl::Db::getDoc: " << i << endl;
Native *ndb = (Native *)pdata;
Xapian::Enquire enquire(ndb->db);
enquire.set_query(ndb->query);
Xapian::MSet matches = enquire.get_mset(i, 1);
// cerr << "Query `" << ndb->query.get_description() << "'" <<
// "Estimated results: " << matches.get_matches_lower_bound() << endl;
if (matches.empty())
return false;
Xapian::Document xdoc = matches.begin().get_document();
// Parse xapian document's data and populate doc fields
string data = xdoc.get_data();
ConfSimple parms(&data);
parms.get(string("mtype"), doc.mimetype);
parms.get(string("mtime"), doc.mtime);
parms.get(string("url"), doc.url);
return true;
}