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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513 | // *****************************************************************************
/*!
\file src/db.cpp
\copyright 2022-2025 J. Bakosi,
All rights reserved. See the LICENSE file for details.
\brief Piac database functionality
*/
// *****************************************************************************
#include <string>
#include "string_util.hpp"
#include "logging_util.hpp"
#include "crypto_util.hpp"
#include "db.hpp"
#include "document.hpp"
Xapian::doccount
piac::get_doccount( const std::string db_name )<--- Function parameter 'db_name' should be passed by const reference. [+]Parameter 'db_name' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
// *****************************************************************************
// Get number of documents in Xapian database
//! \param[in] db_name Name of Xapian db to operate on
//! \return Number of documents in database
// *****************************************************************************
{
try {
Xapian::Database db( db_name );
return db.get_doccount();
} catch ( const Xapian::Error &e ) {
MWARNING( e.get_description() );
}
return {};
}
std::string
piac::add_document( const std::string& author,
Xapian::TermGenerator& indexer,
Xapian::WritableDatabase& db,
Document& ndoc )
// ****************************************************************************
// Add document to Xapian database
//! \param[in] author Author of the database document
//! \param[in,out] indexer Xapian indexer to use for database indexing
//! \param[in,out] db Xapian database object to add document to
//! \param[in,out] ndoc Json document to add
//! \return Hash of the document added
// ****************************************************************************
{
assert( not author.empty() );
Xapian::Document doc;
indexer.set_document( doc );
// Index each field with a suitable prefix
indexer.index_text( ndoc.title(), 1, "S" );
indexer.index_text( ndoc.description(), 1, "XD" );
indexer.index_text( ndoc.category(), 1, "XC" );
indexer.index_text( ndoc.condition(), 1, "XO" );
indexer.index_text( ndoc.shipping(), 1, "XS" );
indexer.index_text( ndoc.format(), 1, "XF" );
indexer.index_text( ndoc.location(), 1, "XL" );
indexer.index_text( ndoc.keywords(), 1, "K" );
// Index fields without prefixes for general search
indexer.index_text( ndoc.title() );
indexer.increase_termpos();
indexer.index_text( ndoc.description() );
indexer.increase_termpos();
indexer.index_text( ndoc.category() );
indexer.increase_termpos();
indexer.index_text( ndoc.condition() );
indexer.increase_termpos();
indexer.index_text( ndoc.shipping() );
indexer.increase_termpos();
indexer.index_text( ndoc.format() );
indexer.increase_termpos();
indexer.index_text( ndoc.location() );
indexer.increase_termpos();
indexer.index_text( ndoc.keywords() );
// Add value fields
doc.add_value( 1, std::to_string( ndoc.price() ) );
// Generate a hash of the doc fields and store it in the document
ndoc.author( author );
auto entry = ndoc.serialize();
ndoc.sha( sha256( entry ) );
auto sha = ndoc.sha();
// Ensure each object ends up in the database only once no matter how
// many times we run the indexer
doc.add_boolean_term( std::to_string( ndoc.id() ) );
doc.set_data( entry );
doc.add_term( 'Q' + sha );
// Add Xapian doc to db
db.replace_document( 'Q' + sha, doc );
return sha;
}
std::string
piac::index_db( const std::string& author,
const std::string& db_name,
const std::string& input_filename,
const std::unordered_set< std::string >& my_hashes )
// ****************************************************************************
// Index Xapian database
//! \param[in] author Author of the database document
//! \param[in] db_name Name of the Xapian database object
//! \param[in] input_filename File to read JSON data from
//! \param[in] my_hashes Hashes to check for duplicates when adding documents
//! \return Info string showing how many documents have been added
// ****************************************************************************
{
assert( not author.empty() );
std::ifstream f( input_filename );
if (not f.good()) {
return "Cannot open database input file: " + input_filename;
}
MDEBUG( "Indexing " << input_filename );
Xapian::WritableDatabase db( db_name, Xapian::DB_CREATE_OR_OPEN );
Xapian::TermGenerator indexer;
Xapian::Stem stemmer( "english" );
indexer.set_stemmer( stemmer );
indexer.set_stemming_strategy( indexer.STEM_SOME_FULL_POS );
std::size_t numins = 0;
try {
// Read json db from file
Documents ndoc;
ndoc.deserializeFromFile( input_filename );
// Insert documents we do not yet have from json into xapian db
for (auto& d : ndoc.documents()) {
d.author( author );
auto entry = d.serialize();
if (my_hashes.find( sha256( entry ) ) == end(my_hashes)) {
add_document( author, indexer, db, d );
++numins;
}
}
MDEBUG( "Indexed " << numins << " entries" );
// Explicitly commit so that we get to see any errors. WritableDatabase's
// destructor will commit implicitly (unless we're in a transaction) but
// will swallow any exceptions produced.
db.commit();
} catch ( const Xapian::Error &e ) {
MERROR( e.get_description() );
}
return "Added " + std::to_string( numins ) + " entries";
}
[[nodiscard]] std::string
piac::db_query( const std::string& db_name, std::string&& cmd )
// *****************************************************************************
// Query Xapian database
//! \param[in] db_name Name of the Xapian database object
//! \param[in,out] cmd Query command
//! \return Result of the database query
// *****************************************************************************
{
try {
MDEBUG( "db query: '" << cmd << "'" );
// Open the database for searching
Xapian::Database db( db_name );
// Start an enquire session
Xapian::Enquire enquire( db );
// Parse the query string to produce a Xapian::Query object
Xapian::QueryParser qp;
Xapian::Stem stemmer("english");
qp.set_stemmer( stemmer );
qp.set_database( db );
qp.set_stemming_strategy( Xapian::QueryParser::STEM_SOME );
Xapian::Query query = qp.parse_query( cmd );
MDEBUG( "parsed query: '" << query.get_description() << "'" );
// Find the top 10 results for the query
enquire.set_query( query );
MDEBUG( "set query: '" << query.get_description() << "'" );
Xapian::MSet matches = enquire.get_mset( 0, 10 );
MDEBUG( "got matches" );
// Construct the results
auto nr = matches.get_matches_estimated();
MDEBUG( "got estimated matches: " << nr );
std::stringstream result;
result << nr << " results found.";
if (nr) {
result << "\nmatches 1-" << matches.size() << ":\n\n";
for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) {
MDEBUG( "getting match: " << i.get_rank() );
result << i.get_rank() + 1 << ": " << i.get_weight() << " docid=" << *i
<< " [" << i.get_document().get_data() << "]\n";
}
}
//MDEBUG( "results: " + result.str() );
return result.str();
} catch ( const Xapian::Error &e ) {
MERROR( e.get_description() );
}
return {};
}
[[nodiscard]] std::vector< std::string >
piac::db_get_docs( const std::string& db_name,
const std::vector< std::string >& hashes )
// *****************************************************************************
// Get documents from Xapian database
//! \param[in] db_name Name of the Xapian database object
//! \param[in] hashes Hashes of database documents to get retrieve
//! \return Result of the database query
// *****************************************************************************
{
std::vector< std::string > docs;
try {
Xapian::Database db( db_name );
Xapian::doccount dbsize = db.get_doccount();
if (dbsize == 0) return {};
for (const auto& h : hashes) {
assert( h.size() == 32 );
auto p = db.postlist_begin( 'Q' + h );
if (p != db.postlist_end( 'Q' + h ))
docs.push_back( db.get_document( *p ).get_data() );
else
MWARNING( "Document not found: " << hex(h) );
}
} catch ( const Xapian::Error &e ) {
if (e.get_description().find("No such file") == std::string::npos)
MERROR( e.get_description() );
}
return docs;
}
std::size_t
piac::db_put_docs( const std::string& db_name,
const std::vector< std::string >& docs )
// *****************************************************************************
// Put documents to Xapian database
//! \param[in] db_name Name of the Xapian database object
//! \param[in] docs Documents to insert to Xapian database
//! \return Number of documents inserted
// *****************************************************************************
{
try {
MDEBUG( "Inserting & indexing " << docs.size() << " new entries" );
Xapian::WritableDatabase db( db_name, Xapian::DB_CREATE_OR_OPEN );
Xapian::TermGenerator indexer;
Xapian::Stem stemmer( "english" );
indexer.set_stemmer( stemmer );
indexer.set_stemming_strategy( indexer.STEM_SOME_FULL_POS );
// Insert all documents into xapian db
for (const auto& d : docs) {
Document ndoc;
ndoc.deserialize( d );
// refuse doc without author
auto author = ndoc.author();
if (not author.empty()) add_document( author, indexer, db, ndoc );
}
MDEBUG( "Finished indexing " << docs.size() <<
" new entries, commit to db" );
// Explicitly commit so that we get to see any errors. WritableDatabase's
// destructor will commit implicitly (unless we're in a transaction) but
// will swallow any exceptions produced.
db.commit();
return docs.size();
} catch ( const Xapian::Error &e ) {
MERROR( e.get_description() );
}
return 0;
}
std::string
piac::db_rm_docs( const std::string& author,
const std::string& db_name,
const std::unordered_set< std::string >& hashes_to_delete,
const std::unordered_set< std::string >& my_hashes )
// *****************************************************************************
// Remove documents from Xapian database
//! \param[in] author Author of the database document
//! \param[in] db_name Name of the Xapian database object
//! \param[in] hashes_to_delete Hashes of documents to delete
//! \param[in] my_hashes Hashes to check for duplicates when removing documents
//! \return Info on number of documents removed
// *****************************************************************************
{
std::size_t numrm = 0;
try {
Xapian::WritableDatabase db( db_name );
Xapian::doccount dbsize = db.get_doccount();
if (dbsize == 0) return "no docs";
for (const auto& h : my_hashes) {
assert( h.size() == 32 );
auto p = db.postlist_begin( 'Q' + h );
if ( p != db.postlist_end( 'Q' + h ) &&
hashes_to_delete.find(hex(h)) != end(hashes_to_delete) )
{
auto entry = db.get_document( *p ).get_data();
Document ndoc;
ndoc.deserialize( entry );
if (author == ndoc.author()) {
MDEBUG( "db rm" + sha256(h) );
db.delete_document( 'Q' + h );
++numrm;
} else {
MDEBUG( "db rm auth: " + hex(author) + " != " + hex(ndoc.author()) );
return "db rm: author != user";
}
}
}
} catch ( const Xapian::Error &e ) {
if (e.get_description().find("No such file") == std::string::npos)
MERROR( e.get_description() );
}
return "Removed " + std::to_string( numrm ) + " entries";
}
[[nodiscard]] std::vector< std::string >
piac::db_list_hash( const std::string& db_name, bool inhex )
// *****************************************************************************
// List hashes from Xapian database
//! \param[in] db_name Name of the Xapian database object
//! \param[in] inhex True to list hashes hex-encoded
//! \return List of hashes
// *****************************************************************************
{
std::vector< std::string > hashes;
try {
Xapian::Database db( db_name );
Xapian::doccount dbsize = db.get_doccount();
if (dbsize == 0) return {};
for (auto it = db.postlist_begin({}); it != db.postlist_end({}); ++it) {
auto entry = db.get_document( *it ).get_data();
auto digest = sha256( entry );
hashes.emplace_back( inhex ? hex(digest) : digest );
}
} catch ( const Xapian::Error &e ) {
if (e.get_description().find("No such file") == std::string::npos)
MERROR( e.get_description() );
}
return hashes;
}
[[nodiscard]] std::vector< std::string >
piac::db_list_doc( const std::string& db_name )
// *****************************************************************************
// List documents from Xapian database
//! \param[in] db_name Name of the Xapian database object
//! \return List of documents
// *****************************************************************************
{
std::vector< std::string > docs;
try {
Xapian::Database db( db_name );
Xapian::doccount dbsize = db.get_doccount();
if (dbsize == 0) return {};
for (auto it = db.postlist_begin({}); it != db.postlist_end({}); ++it) {
auto entry = db.get_document( *it ).get_data();
auto digest = sha256( entry );
Document d;
d.deserialize( entry );
d.author( hex( d.author() ) );
docs.emplace_back( hex( digest ) + ": " + d.serialize() );
}
} catch ( const Xapian::Error &e ) {
if (e.get_description().find("No such file") == std::string::npos)
MERROR( e.get_description() );
}
return docs;
}
[[nodiscard]] std::size_t
piac::db_list_numuser( const std::string& db_name )
// *****************************************************************************
// List number of unique users in Xapian database
//! \param[in] db_name Name of the Xapian database object
//! \return Number of unique users created documents in database
// *****************************************************************************
{
try {
Xapian::Database db( db_name );
Xapian::doccount dbsize = db.get_doccount();
if (dbsize == 0) return {};
std::unordered_set< std::string > user;
for (auto it = db.postlist_begin({}); it != db.postlist_end({}); ++it) {
auto entry = db.get_document( *it ).get_data();
Document d;
d.deserialize( entry );
user.insert( d.author() );
}
return user.size();
} catch ( const Xapian::Error &e ) {
if (e.get_description().find("No such file") == std::string::npos)
MERROR( e.get_description() );
}
return {};
}
std::string
piac::db_add( const std::string& author,
const std::string& db_name,
std::string&& cmd,
const std::unordered_set< std::string >& my_hashes )
// *****************************************************************************
// Add documents to Xapian database
//! \param[in] author Author of the database document
//! \param[in] db_name Name of the Xapian database object
//! \param[in,out] cmd Add command
//! \param[in] my_hashes Hashes to check for duplicates when adding documents
//! \return Info string after add database operation
// *****************************************************************************
{
trim( cmd );
MDEBUG( "db add " + cmd );
assert( not author.empty() );
if (cmd[0]=='j' && cmd[1]=='s' && cmd[2]=='o' && cmd[3]=='n') {
cmd.erase( 0, 5 );
MDEBUG( "Add json file: '" << cmd << "' to db" );
return index_db( author, db_name, cmd, my_hashes );
}
return "unknown cmd";
}
std::string
piac::db_rm( const std::string& author,
const std::string& db_name,
std::string&& cmd,
const std::unordered_set< std::string >& my_hashes )
// *****************************************************************************
// Remove documents from Xapian database
//! \param[in] author Author of the database document
//! \param[in] db_name Name of the Xapian database object
//! \param[in,out] cmd Remove command
//! \param[in] my_hashes Hashes to check for duplicates when removing documents
//! \return Info string after remove database operation
// *****************************************************************************
{
trim( cmd );
MDEBUG( "db rm " + cmd );
assert( not author.empty() );
if (not cmd.empty()) {
auto h = tokenize( cmd );
std::unordered_set< std::string > hashes_to_delete( begin(h), end(h) );
return db_rm_docs( author, db_name, hashes_to_delete, my_hashes );
}
return "unknown cmd";
}
std::string
piac::db_list( const std::string& db_name, std::string&& cmd )
// *****************************************************************************
// List Xapian database
//! \param[in] db_name Name of the Xapian database object
//! \param[in,out] cmd List command
//! \return List of items queried from database
// *****************************************************************************
{
trim( cmd );
MDEBUG( "db list " + cmd );
if (cmd.empty()) {
auto docs = db_list_doc( db_name );
std::string result( "Number of documents: " +
std::to_string( docs.size() ) + '\n' );
for (auto&& d : docs) result += std::move(d) + '\n';
result.pop_back();
return result;
} else if (cmd[0]=='n' && cmd[1]=='u' && cmd[2]=='m' && cmd[3]=='d' &&
cmd[4]=='o' && cmd[5]=='c')
{
return "Number of documents: " + std::to_string( get_doccount( db_name ) );
} else if (cmd[0]=='n' && cmd[1]=='u' && cmd[2]=='m' && cmd[3]=='u' &&
cmd[4]=='s' && cmd[5]=='r')
{
return "Number of users: " + std::to_string( db_list_numuser( db_name ) );
} else if (cmd[0]=='h' && cmd[1]=='a' && cmd[2]=='s' && cmd[3]=='h') {
cmd.erase( 0, 5 );
auto hashes = db_list_hash( db_name, /* inhex = */ true );
std::string result( "Number of documents: " +
std::to_string( hashes.size() ) + '\n' );
for (auto&& h : hashes) result += std::move(h) + '\n';
result.pop_back();
return result;
}
return "unknown cmd";
}
|