File: api_compact.cc

package info (click to toggle)
xapian-core 1.4.3-2~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 21,224 kB
  • sloc: cpp: 113,806; ansic: 8,723; sh: 4,433; perl: 836; makefile: 567; tcl: 317; python: 40
file content (665 lines) | stat: -rw-r--r-- 17,751 bytes parent folder | download | duplicates (2)
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/** @file api_compact.cc
 * @brief Tests of Database::compact()
 */
/* Copyright (C) 2009,2010,2011,2012,2013,2015,2016 Olly Betts
 * Copyright (C) 2010 Richard Boulton
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <config.h>

#include "api_compact.h"

#include <xapian.h>

#include "apitest.h"
#include "dbcheck.h"
#include "filetests.h"
#include "str.h"
#include "testsuite.h"
#include "testutils.h"

#include <cstdlib>
#include <fstream>

#include <sys/types.h>
#include "safesysstat.h"
#include "safefcntl.h"
#include "safeunistd.h"

#include "unixcmds.h"

using namespace std;

static void
make_sparse_db(Xapian::WritableDatabase &db, const string & s)
{
    // Need non-const pointer for strtoul(), but data isn't modified.
    char * p = const_cast<char *>(s.c_str());

    while (*p) {
	bool del = (*p == '!');
	if (del) ++p;
	Xapian::docid first = strtoul(p, &p, 10);
	Xapian::docid last = first;
	if (*p == '-') {
	    last = strtoul(p + 1, &p, 10);
	}
	if (*p && *p != ' ') {
	    tout << p - s.c_str() << endl;
	    FAIL_TEST("Bad sparse db spec (expected space): " << s);
	}
	if (first > last) {
	    FAIL_TEST("Bad sparse db spec (first > last): " << s);
	}

	do {
	    if (del) {
		db.delete_document(first);
	    } else {
		Xapian::Document doc;
		string id = str(first);
		doc.set_data(id);
		doc.add_term("Q" + str(first));
		doc.add_term(string(first % 7 + 1, char((first % 26) + 'a')));
		db.replace_document(first, doc);
	    }
	} while (first++ < last);

	if (*p == '\0') break;
	++p;
    }

    db.commit();
}

static void
check_sparse_uid_terms(const string & path)
{
    Xapian::Database db(path);
    Xapian::TermIterator t;
    for (t = db.allterms_begin("Q"); t != db.allterms_end("Q"); ++t) {
	Xapian::docid did = atoi((*t).c_str() + 1);
	Xapian::PostingIterator p = db.postlist_begin(*t);
	TEST_EQUAL(*p, did);
    }
}

DEFINE_TESTCASE(compactnorenumber1, generated) {
    string a = get_database_path("compactnorenumber1a", make_sparse_db,
				 "5-7 24 76 987 1023-1027 9999 !9999");
    string a_uuid;
    {
	Xapian::Database db(a);
	a_uuid = db.get_uuid();
    }
    string b = get_database_path("compactnorenumber1b", make_sparse_db,
				 "1027-1030");
    string c = get_database_path("compactnorenumber1c", make_sparse_db,
				 "1028-1040");
    string d = get_database_path("compactnorenumber1d", make_sparse_db,
				 "3000 999999 !999999");

    string out = get_named_writable_database_path("compactnorenumber1out");

    rm_rf(out);
    {
	Xapian::Database db(a);
	db.compact(out, Xapian::DBCOMPACT_NO_RENUMBER);
    }

    check_sparse_uid_terms(out);

    {
	TEST(!dir_exists(out + "/donor"));
	Xapian::Database db(out);
	// xapian-compact should change the UUID of the database, but didn't
	// prior to 1.0.18/1.1.4.
	string out_uuid = db.get_uuid();
	TEST_NOT_EQUAL(a_uuid, out_uuid);
	TEST_EQUAL(out_uuid.size(), 36);
	TEST_NOT_EQUAL(out_uuid, "00000000-0000-0000-0000-000000000000");

	// White box test - ensure that the donor database is removed.
	TEST(!dir_exists(out + "/donor"));
    }

    rm_rf(out);
    {
	Xapian::Database db;
	db.add_database(Xapian::Database(a));
	db.add_database(Xapian::Database(c));
	db.compact(out, Xapian::DBCOMPACT_NO_RENUMBER);
    }
    check_sparse_uid_terms(out);
    {
	// Check that xapian-compact is producing a consistent database.  Also,
	// regression test - xapian 1.1.4 set lastdocid to 0 in the output
	// database.
	Xapian::Database outdb(out);
	dbcheck(outdb, 24, 9999);
    }

    rm_rf(out);
    {
	Xapian::Database db;
	db.add_database(Xapian::Database(d));
	db.add_database(Xapian::Database(a));
	db.add_database(Xapian::Database(c));
	db.compact(out, Xapian::DBCOMPACT_NO_RENUMBER);
    }
    check_sparse_uid_terms(out);

    rm_rf(out);
    {
	Xapian::Database db;
	db.add_database(Xapian::Database(c));
	db.add_database(Xapian::Database(a));
	db.add_database(Xapian::Database(d));
	db.compact(out, Xapian::DBCOMPACT_NO_RENUMBER);
    }
    check_sparse_uid_terms(out);

    // Should fail.
    rm_rf(out);
    {
	Xapian::Database db;
	db.add_database(Xapian::Database(a));
	db.add_database(Xapian::Database(b));
	TEST_EXCEPTION(Xapian::InvalidOperationError,
	    db.compact(out, Xapian::DBCOMPACT_NO_RENUMBER)
	);
    }

    // Should fail.
    rm_rf(out);
    {
	Xapian::Database db;
	db.add_database(Xapian::Database(b));
	db.add_database(Xapian::Database(a));
	TEST_EXCEPTION(Xapian::InvalidOperationError,
	    db.compact(out, Xapian::DBCOMPACT_NO_RENUMBER)
	);
    }

    // Should fail.
    rm_rf(out);
    {
	Xapian::Database db;
	db.add_database(Xapian::Database(a));
	db.add_database(Xapian::Database(b));
	db.add_database(Xapian::Database(d));
	TEST_EXCEPTION(Xapian::InvalidOperationError,
	    db.compact(out, Xapian::DBCOMPACT_NO_RENUMBER)
	);
    }

    // Should fail.
    rm_rf(out);
    {
	Xapian::Database db;
	db.add_database(Xapian::Database(d));
	db.add_database(Xapian::Database(b));
	db.add_database(Xapian::Database(a));
	TEST_EXCEPTION(Xapian::InvalidOperationError,
	    db.compact(out, Xapian::DBCOMPACT_NO_RENUMBER)
	);
    }

    // Should fail.
    rm_rf(out);
    {
	Xapian::Database db;
	db.add_database(Xapian::Database(b));
	db.add_database(Xapian::Database(a));
	db.add_database(Xapian::Database(d));
	TEST_EXCEPTION(Xapian::InvalidOperationError,
	    db.compact(out, Xapian::DBCOMPACT_NO_RENUMBER)
	);
    }

    return true;
}

// Test use of compact to merge two databases.
DEFINE_TESTCASE(compactmerge1, chert || glass) {
    string indbpath = get_database_path("apitest_simpledata");
    string outdbpath = get_named_writable_database_path("compactmerge1out");
    rm_rf(outdbpath);

    {
	Xapian::Database db;
	db.add_database(Xapian::Database(indbpath));
	db.add_database(Xapian::Database(indbpath));
	db.compact(outdbpath);
    }

    Xapian::Database indb(get_database("apitest_simpledata"));
    Xapian::Database outdb(outdbpath);

    TEST_EQUAL(indb.get_doccount() * 2, outdb.get_doccount());
    dbcheck(outdb, outdb.get_doccount(), outdb.get_doccount());

    return true;
}

static void
make_multichunk_db(Xapian::WritableDatabase &db, const string &)
{
    int count = 10000;

    Xapian::Document doc;
    doc.add_term("a");
    while (count) {
	db.add_document(doc);
	--count;
    }

    db.commit();
}

// Test use of compact on a database which has multiple chunks for a term.
// This is a regression test for ticket #427
DEFINE_TESTCASE(compactmultichunks1, generated) {
    string indbpath = get_database_path("compactmultichunks1in",
					make_multichunk_db, "");
    string outdbpath = get_named_writable_database_path("compactmultichunks1out");
    rm_rf(outdbpath);

    {
	Xapian::Database db(indbpath);
	db.compact(outdbpath);
    }

    Xapian::Database indb(indbpath);
    Xapian::Database outdb(outdbpath);

    TEST_EQUAL(indb.get_doccount(), outdb.get_doccount());
    dbcheck(outdb, outdb.get_doccount(), outdb.get_doccount());

    return true;
}

// Test compacting from a stub database directory.
DEFINE_TESTCASE(compactstub1, chert || glass) {
    const char * stubpath = ".stub/compactstub1";
    const char * stubpathfile = ".stub/compactstub1/XAPIANDB";
    mkdir(".stub", 0755);
    mkdir(stubpath, 0755);
    ofstream stub(stubpathfile);
    TEST(stub.is_open());
    stub << "auto ../../" << get_database_path("apitest_simpledata") << endl;
    stub << "auto ../../" << get_database_path("apitest_simpledata2") << endl;
    stub.close();

    string outdbpath = get_named_writable_database_path("compactstub1out");
    rm_rf(outdbpath);

    {
	Xapian::Database db(stubpath);
	db.compact(outdbpath);
    }

    Xapian::Database indb(stubpath);
    Xapian::Database outdb(outdbpath);

    TEST_EQUAL(indb.get_doccount(), outdb.get_doccount());
    dbcheck(outdb, outdb.get_doccount(), outdb.get_doccount());

    return true;
}

// Test compacting from a stub database file.
DEFINE_TESTCASE(compactstub2, chert || glass) {
    const char * stubpath = ".stub/compactstub2";
    mkdir(".stub", 0755);
    ofstream stub(stubpath);
    TEST(stub.is_open());
    stub << "auto ../" << get_database_path("apitest_simpledata") << endl;
    stub << "auto ../" << get_database_path("apitest_simpledata2") << endl;
    stub.close();

    string outdbpath = get_named_writable_database_path("compactstub2out");
    rm_rf(outdbpath);

    {
	Xapian::Database db(stubpath);
	db.compact(outdbpath);
    }

    Xapian::Database indb(stubpath);
    Xapian::Database outdb(outdbpath);

    TEST_EQUAL(indb.get_doccount(), outdb.get_doccount());
    dbcheck(outdb, outdb.get_doccount(), outdb.get_doccount());

    return true;
}

// Test compacting a stub database file to itself.
DEFINE_TESTCASE(compactstub3, chert || glass) {
    const char * stubpath = ".stub/compactstub3";
    mkdir(".stub", 0755);
    ofstream stub(stubpath);
    TEST(stub.is_open());
    stub << "auto ../" << get_database_path("apitest_simpledata") << endl;
    stub << "auto ../" << get_database_path("apitest_simpledata2") << endl;
    stub.close();

    Xapian::doccount in_docs;
    {
	Xapian::Database indb(stubpath);
	in_docs = indb.get_doccount();
	indb.compact(stubpath);
    }

    Xapian::Database outdb(stubpath);

    TEST_EQUAL(in_docs, outdb.get_doccount());
    dbcheck(outdb, outdb.get_doccount(), outdb.get_doccount());

    return true;
}

// Test compacting a stub database directory to itself.
DEFINE_TESTCASE(compactstub4, chert || glass) {
    const char * stubpath = ".stub/compactstub4";
    const char * stubpathfile = ".stub/compactstub4/XAPIANDB";
    mkdir(".stub", 0755);
    mkdir(stubpath, 0755);
    ofstream stub(stubpathfile);
    TEST(stub.is_open());
    stub << "auto ../../" << get_database_path("apitest_simpledata") << endl;
    stub << "auto ../../" << get_database_path("apitest_simpledata2") << endl;
    stub.close();

    Xapian::doccount in_docs;
    {
	Xapian::Database indb(stubpath);
	in_docs = indb.get_doccount();
	indb.compact(stubpath);
    }

    Xapian::Database outdb(stubpath);

    TEST_EQUAL(in_docs, outdb.get_doccount());
    dbcheck(outdb, outdb.get_doccount(), outdb.get_doccount());

    return true;
}

static void
make_all_tables(Xapian::WritableDatabase &db, const string &)
{
    Xapian::Document doc;
    doc.add_term("foo");
    db.add_document(doc);
    db.add_spelling("foo");
    db.add_synonym("bar", "pub");
    db.add_synonym("foobar", "foo");

    db.commit();
}

static void
make_missing_tables(Xapian::WritableDatabase &db, const string &)
{
    Xapian::Document doc;
    doc.add_term("foo");
    db.add_document(doc);

    db.commit();
}

DEFINE_TESTCASE(compactmissingtables1, generated) {
    string a = get_database_path("compactmissingtables1a",
				 make_all_tables);
    string b = get_database_path("compactmissingtables1b",
				 make_missing_tables);

    string out = get_named_writable_database_path("compactmissingtables1out");
    rm_rf(out);

    {
	Xapian::Database db;
	db.add_database(Xapian::Database(a));
	db.add_database(Xapian::Database(b));
	db.compact(out);
    }

    {
	Xapian::Database db(out);
	TEST_NOT_EQUAL(db.spellings_begin(), db.spellings_end());
	TEST_NOT_EQUAL(db.synonym_keys_begin(), db.synonym_keys_end());
	// FIXME: arrange for input b to not have a termlist table.
//	TEST_EXCEPTION(Xapian::FeatureUnavailableError, db.termlist_begin(1));
    }

    return true;
}

static void
make_all_tables2(Xapian::WritableDatabase &db, const string &)
{
    Xapian::Document doc;
    doc.add_term("bar");
    db.add_document(doc);
    db.add_spelling("bar");
    db.add_synonym("bar", "baa");
    db.add_synonym("barfoo", "barbar");
    db.add_synonym("foofoo", "barfoo");

    db.commit();
}

/// Adds coverage for merging synonym table.
DEFINE_TESTCASE(compactmergesynonym1, generated) {
    string a = get_database_path("compactmergesynonym1a",
				 make_all_tables);
    string b = get_database_path("compactmergesynonym1b",
				 make_all_tables2);

    string out = get_named_writable_database_path("compactmergesynonym1out");
    rm_rf(out);

    {
	Xapian::Database db;
	db.add_database(Xapian::Database(a));
	db.add_database(Xapian::Database(b));
	db.compact(out);
    }

    {
	Xapian::Database db(out);

	Xapian::TermIterator i = db.spellings_begin();
	TEST_NOT_EQUAL(i, db.spellings_end());
	TEST_EQUAL(*i, "bar");
	++i;
	TEST_NOT_EQUAL(i, db.spellings_end());
	TEST_EQUAL(*i, "foo");
	++i;
	TEST_EQUAL(i, db.spellings_end());

	i = db.synonym_keys_begin();
	TEST_NOT_EQUAL(i, db.synonym_keys_end());
	TEST_EQUAL(*i, "bar");
	++i;
	TEST_NOT_EQUAL(i, db.synonym_keys_end());
	TEST_EQUAL(*i, "barfoo");
	++i;
	TEST_NOT_EQUAL(i, db.synonym_keys_end());
	TEST_EQUAL(*i, "foobar");
	++i;
	TEST_NOT_EQUAL(i, db.synonym_keys_end());
	TEST_EQUAL(*i, "foofoo");
	++i;
	TEST_EQUAL(i, db.synonym_keys_end());
    }

    return true;
}

DEFINE_TESTCASE(compactempty1, chert || glass) {
    string empty_dbpath = get_database_path(string());
    string outdbpath = get_named_writable_database_path("compactempty1out");
    rm_rf(outdbpath);

    {
	// Compacting an empty database tried to divide by zero in 1.3.0.
	Xapian::Database db;
	db.add_database(Xapian::Database(empty_dbpath));
	db.compact(outdbpath);

	Xapian::Database outdb(outdbpath);
	TEST_EQUAL(outdb.get_doccount(), 0);
	dbcheck(outdb, 0, 0);
    }

    {
	// Check compacting two empty databases together.
	Xapian::Database db;
	db.add_database(Xapian::Database(empty_dbpath));
	db.add_database(Xapian::Database(empty_dbpath));
	db.compact(outdbpath);

	Xapian::Database outdb(outdbpath);
	TEST_EQUAL(outdb.get_doccount(), 0);
	dbcheck(outdb, 0, 0);
    }

    return true;
}

DEFINE_TESTCASE(compactmultipass1, chert || glass) {
    string outdbpath = get_named_writable_database_path("compactmultipass1");
    rm_rf(outdbpath);

    string a = get_database_path("compactnorenumber1a", make_sparse_db,
				 "5-7 24 76 987 1023-1027 9999 !9999");
    string b = get_database_path("compactnorenumber1b", make_sparse_db,
				 "1027-1030");
    string c = get_database_path("compactnorenumber1c", make_sparse_db,
				 "1028-1040");
    string d = get_database_path("compactnorenumber1d", make_sparse_db,
				 "3000 999999 !999999");

    {
	Xapian::Database db;
	db.add_database(Xapian::Database(a));
	db.add_database(Xapian::Database(b));
	db.add_database(Xapian::Database(c));
	db.add_database(Xapian::Database(d));
	db.compact(outdbpath, Xapian::DBCOMPACT_MULTIPASS);
    }

    Xapian::Database outdb(outdbpath);
    dbcheck(outdb, 29, 1041);

    return true;
}

// Test compacting to an fd.
DEFINE_TESTCASE(compacttofd1, glass) {
    Xapian::Database indb(get_database("apitest_simpledata"));
    string outdbpath = get_named_writable_database_path("compacttofd1out");
    rm_rf(outdbpath);

    int fd = open(outdbpath.c_str(), O_CREAT|O_RDWR, 0666);
    TEST(fd != -1);
    indb.compact(fd);

    // Confirm that the fd was closed by Xapian.
    TEST(close(fd) == -1);
    TEST(errno == EBADF);

    Xapian::Database outdb(outdbpath);

    TEST_EQUAL(indb.get_doccount(), outdb.get_doccount());
    dbcheck(outdb, outdb.get_doccount(), outdb.get_doccount());

    return true;
}

// Test compacting to an fd at at offset.
DEFINE_TESTCASE(compacttofd2, glass) {
    Xapian::Database indb(get_database("apitest_simpledata"));
    string outdbpath = get_named_writable_database_path("compacttofd2out");
    rm_rf(outdbpath);

    int fd = open(outdbpath.c_str(), O_CREAT|O_RDWR, 0666);
    TEST(fd != -1);
    TEST(lseek(fd, 8192, SEEK_SET) == 8192);
    indb.compact(fd);

    // Confirm that the fd was closed by Xapian.
    TEST(close(fd) == -1);
    TEST(errno == EBADF);

    fd = open(outdbpath.c_str(), O_RDONLY, 0666);
    TEST(fd != -1);

    // Test that the database wasn't just written to the start of the file.
    char buf[8192];
    size_t n = sizeof(buf);
    while (n) {
	ssize_t c = read(fd, buf, n);
	TEST(c > 0);
	for (const char * p = buf; p != buf + c; ++p) {
	    TEST(*p == 0);
	}
	n -= c;
    }

    TEST(lseek(fd, 8192, SEEK_SET) == 8192);
    Xapian::Database outdb(fd);

    TEST_EQUAL(indb.get_doccount(), outdb.get_doccount());
    dbcheck(outdb, outdb.get_doccount(), outdb.get_doccount());

    return true;
}

// Regression test for bug fixed in 1.3.5.  If you commit a WritableDatabase
// with uncommitted changes, you get an inconsistent output.
DEFINE_TESTCASE(compactsingle1, glass) {
    Xapian::WritableDatabase db = get_writable_database();
    Xapian::Document doc;
    doc.add_term("foo");
    doc.add_term("bar");
    doc.add_term("baz");
    db.add_document(doc);

    string output = ".glass/db__compactsingle1-out";
    // In 1.3.4, we would hang if the output file already existed, so check
    // that works.
    touch(output);

    TEST_EXCEPTION(Xapian::InvalidOperationError,
	db.compact(output, Xapian::DBCOMPACT_SINGLE_FILE));

    // Check the file wasn't removed by the failed attempt.
    TEST(file_exists(output));

    db.commit();
    db.compact(output, Xapian::DBCOMPACT_SINGLE_FILE);
    db.close();

    TEST_EQUAL(Xapian::Database::check(output, 0, &tout), 0);

    return true;
}