File: api_replicate.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 (912 lines) | stat: -rw-r--r-- 26,855 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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/* api_replicate.cc: tests of replication functionality
 *
 * Copyright 2008 Lemur Consulting Ltd
 * Copyright 2009,2010,2011,2012,2013,2014,2015,2016 Olly Betts
 * Copyright 2010 Richard Boulton
 * Copyright 2011 Dan Colish
 *
 * 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_replicate.h"

#include <xapian.h>
#include "api/replication.h"

#include "apitest.h"
#include "dbcheck.h"
#include "fd.h"
#include "filetests.h"
#include "safedirent.h"
#include "safeerrno.h"
#include "safefcntl.h"
#include "safesysstat.h"
#include "safeunistd.h"
#include "testsuite.h"
#include "testutils.h"
#include "unixcmds.h"

#include <sys/types.h>

#include <cstdlib>
#include <string>

#include <stdlib.h> // For setenv() or putenv()

using namespace std;

static void rmtmpdir(const string & path) {
    rm_rf(path);
}

static void mktmpdir(const string & path) {
    rmtmpdir(path);
    if (mkdir(path.c_str(), 0700) == -1 && errno != EEXIST) {
	FAIL_TEST("Can't make temporary directory");
    }
}

static off_t get_file_size(const string & path) {
    off_t size = file_size(path);
    if (errno) {
	FAIL_TEST("Can't stat '" << path << "'");
    }
    return size;
}

static size_t do_read(int fd, char * p, size_t desired)
{
    size_t total = 0;
    while (desired) {
	ssize_t c = read(fd, p, desired);
	if (c == 0) return total;
	if (c < 0) {
	    if (errno == EINTR) continue;
	    FAIL_TEST("Error reading from file");
	}
	p += c;
	total += c;
	desired -= c;
    }
    return total;
}

static void do_write(int fd, const char * p, size_t n)
{
    while (n) {
	ssize_t c = write(fd, p, n);
	if (c < 0) {
	    if (errno == EINTR) continue;
	    FAIL_TEST("Error writing to file");
	}
	p += c;
	n -= c;
    }
}

// Make a truncated copy of a file.
static off_t
truncated_copy(const string & srcpath, const string & destpath, off_t tocopy)
{
    FD fdin(open(srcpath.c_str(), O_RDONLY | O_BINARY));
    if (fdin == -1) {
	FAIL_TEST("Open failed (when opening '" << srcpath << "')");
    }

    FD fdout(open(destpath.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
    if (fdout == -1) {
	FAIL_TEST("Open failed (when creating '" << destpath << "')");
    }

    const int BUFSIZE = 1024;
    char buf[BUFSIZE];
    size_t total_bytes = 0;
    while (tocopy > 0) {
	size_t thiscopy = tocopy > BUFSIZE ? BUFSIZE : tocopy;
	size_t bytes = do_read(fdin, buf, thiscopy);
	if (thiscopy != bytes) {
	    FAIL_TEST("Couldn't read desired number of bytes from changeset");
	}
	tocopy -= bytes;
	total_bytes += bytes;
	do_write(fdout, buf, bytes);
    }

    if (close(fdout) == -1)
	FAIL_TEST("Error closing file");

    return total_bytes;
}

static void
get_changeset(const string & changesetpath,
	      Xapian::DatabaseMaster & master,
	      Xapian::DatabaseReplica & replica,
	      int expected_changesets,
	      int expected_fullcopies,
	      bool expected_changed,
	      bool full_copy = false)
{
    FD fd(open(changesetpath.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
    if (fd == -1) {
	FAIL_TEST("Open failed (when creating a new changeset file at '"
		  << changesetpath << "')");
    }
    Xapian::ReplicationInfo info1;
    master.write_changesets_to_fd(fd,
				  full_copy ? "" : replica.get_revision_info(),
				  &info1);

    TEST_EQUAL(info1.changeset_count, expected_changesets);
    TEST_EQUAL(info1.fullcopy_count, expected_fullcopies);
    TEST_EQUAL(info1.changed, expected_changed);
}

static int
apply_changeset(const string & changesetpath,
		Xapian::DatabaseReplica & replica,
		int expected_changesets,
		int expected_fullcopies,
		bool expected_changed)
{
    FD fd(open(changesetpath.c_str(), O_RDONLY | O_BINARY));
    if (fd == -1) {
	FAIL_TEST("Open failed (when reading changeset file at '"
		  << changesetpath << "')");
    }

    int count = 1;
    replica.set_read_fd(fd);
    Xapian::ReplicationInfo info1;
    Xapian::ReplicationInfo info2;
    bool client_changed = false;
    while (replica.apply_next_changeset(&info2, 0)) {
	++count;
	info1.changeset_count += info2.changeset_count;
	info1.fullcopy_count += info2.fullcopy_count;
	if (info2.changed)
	    client_changed = true;
    }
    info1.changeset_count += info2.changeset_count;
    info1.fullcopy_count += info2.fullcopy_count;
    if (info2.changed)
	client_changed = true;

    TEST_EQUAL(info1.changeset_count, expected_changesets);
    TEST_EQUAL(info1.fullcopy_count, expected_fullcopies);
    TEST_EQUAL(client_changed, expected_changed);
    return count;
}

// Replicate from the master to the replica.
// Returns the number of changesets which were applied.
static int
replicate(Xapian::DatabaseMaster & master,
	  Xapian::DatabaseReplica & replica,
	  const string & tempdir,
	  int expected_changesets,
	  int expected_fullcopies,
	  bool expected_changed,
	  bool full_copy = false)
{
    string changesetpath = tempdir + "/changeset";
    get_changeset(changesetpath, master, replica,
		  expected_changesets,
		  expected_fullcopies,
		  expected_changed,
		  full_copy);
    return apply_changeset(changesetpath, replica,
			   expected_changesets,
			   expected_fullcopies,
			   expected_changed);
}

// Check that the databases held at the given path are identical.
static void
check_equal_dbs(const string & masterpath, const string & replicapath)
{
    Xapian::Database master(masterpath);
    Xapian::Database replica(replicapath);

    TEST_EQUAL(master.get_uuid(), master.get_uuid());
    dbcheck(replica, master.get_doccount(), master.get_lastdocid());

    for (Xapian::TermIterator t = master.allterms_begin();
	 t != master.allterms_end(); ++t) {
	TEST_EQUAL(postlist_to_string(master, *t),
		   postlist_to_string(replica, *t));
    }
}

#if 0 // Dynamic version which we don't currently need.
static void
set_max_changesets(int count) {
#ifdef HAVE__PUTENV_S
    _putenv_s("XAPIAN_MAX_CHANGESETS", str(count).c_str());
#elif defined HAVE_SETENV
    setenv("XAPIAN_MAX_CHANGESETS", str(count).c_str(), 1);
#else
    static char buf[64] = "XAPIAN_MAX_CHANGESETS=";
    sprintf(buf + CONST_STRLEN("XAPIAN_MAX_CHANGESETS="), "%d", count);
    putenv(buf);
#endif
}
#endif

#ifdef HAVE__PUTENV_S
# define set_max_changesets(N) _putenv_s("XAPIAN_MAX_CHANGESETS", #N)
#elif defined HAVE_SETENV
# define set_max_changesets(N) setenv("XAPIAN_MAX_CHANGESETS", #N, 1)
#else
# define set_max_changesets(N) putenv(const_cast<char*>("XAPIAN_MAX_CHANGESETS="#N))
#endif

struct unset_max_changesets_helper_ {
    unset_max_changesets_helper_() { }
    ~unset_max_changesets_helper_() { set_max_changesets(0); }
};

// Ensure that we don't leave generation of changesets on for the next
// testcase, even if this one exits with an exception.
#define UNSET_MAX_CHANGESETS_AFTERWARDS unset_max_changesets_helper_ ezlxq

// #######################################################################
// # Tests start here

// Basic test of replication functionality.
DEFINE_TESTCASE(replicate1, replicas) {
    UNSET_MAX_CHANGESETS_AFTERWARDS;
    string tempdir = ".replicatmp";
    mktmpdir(tempdir);
    string masterpath = get_named_writable_database_path("master");

    set_max_changesets(10);

    Xapian::Document doc1;
    doc1.set_data(string("doc1"));
    doc1.add_posting("doc", 1);
    doc1.add_posting("one", 1);

    Xapian::WritableDatabase orig(get_named_writable_database("master"));
    Xapian::DatabaseMaster master(masterpath);
    string replicapath = tempdir + "/replica";
    {
	Xapian::DatabaseReplica replica(replicapath);

	// Add a document to the original database.
	orig.add_document(doc1);
	orig.commit();

	// Apply the replication - we don't have changesets stored, so this
	// should just do a database copy, and return a count of 1.
	int count = replicate(master, replica, tempdir, 0, 1, true);
	TEST_EQUAL(count, 1);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}

	// Repeating the replication should return a count of 1, since no
	// further changes should need to be applied.
	count = replicate(master, replica, tempdir, 0, 0, false);
	TEST_EQUAL(count, 1);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}
    }
    {
	// Regression test - if the replica was reopened, a full copy always
	// used to occur, whether it was needed or not.  Fixed in revision
	// #10117.
	Xapian::DatabaseReplica replica(replicapath);
	int count = replicate(master, replica, tempdir, 0, 0, false);
	TEST_EQUAL(count, 1);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}

	orig.add_document(doc1);
	orig.commit();
	orig.add_document(doc1);
	orig.commit();

	count = replicate(master, replica, tempdir, 2, 0, true);
	TEST_EQUAL(count, 3);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}

	check_equal_dbs(masterpath, replicapath);

	// We need this inner scope to we close the replica before we remove
	// the temporary directory on Windows.
    }

    rmtmpdir(tempdir);
    return true;
}

// Test replication from a replicated copy.
DEFINE_TESTCASE(replicate2, replicas) {
    SKIP_TEST_FOR_BACKEND("glass"); // Glass doesn't currently support this.
    UNSET_MAX_CHANGESETS_AFTERWARDS;

    string tempdir = ".replicatmp";
    mktmpdir(tempdir);
    string masterpath = get_named_writable_database_path("master");

    set_max_changesets(10);

    {
	Xapian::WritableDatabase orig(get_named_writable_database("master"));
	Xapian::DatabaseMaster master(masterpath);
	string replicapath = tempdir + "/replica";
	Xapian::DatabaseReplica replica(replicapath);

	Xapian::DatabaseMaster master2(replicapath);
	string replica2path = tempdir + "/replica2";
	Xapian::DatabaseReplica replica2(replica2path);

	// Add a document to the original database.
	Xapian::Document doc1;
	doc1.set_data(string("doc1"));
	doc1.add_posting("doc", 1);
	doc1.add_posting("one", 1);
	orig.add_document(doc1);
	orig.commit();

	// Apply the replication - we don't have changesets stored, so this
	// should just do a database copy, and return a count of 1.
	TEST_EQUAL(replicate(master, replica, tempdir, 0, 1, true), 1);
	check_equal_dbs(masterpath, replicapath);

	// Replicate from the replica.
	TEST_EQUAL(replicate(master2, replica2, tempdir, 0, 1, true), 1);
	check_equal_dbs(masterpath, replica2path);

	orig.add_document(doc1);
	orig.commit();
	orig.add_document(doc1);
	orig.commit();

	// Replicate from the replica - should have no changes.
	TEST_EQUAL(replicate(master2, replica2, tempdir, 0, 0, false), 1);
	check_equal_dbs(replicapath, replica2path);

	// Replicate, and replicate from the replica - should have 2 changes.
	TEST_EQUAL(replicate(master, replica, tempdir, 2, 0, 1), 3);
	check_equal_dbs(masterpath, replicapath);
	TEST_EQUAL(replicate(master2, replica2, tempdir, 2, 0, 1), 3);
	check_equal_dbs(masterpath, replica2path);

	// Stop writing changesets, and make a modification
	set_max_changesets(0);
	orig.close();
	orig = get_writable_database_again();
	orig.add_document(doc1);
	orig.commit();

	// Replication should do a full copy.
	TEST_EQUAL(replicate(master, replica, tempdir, 0, 1, true), 1);
	check_equal_dbs(masterpath, replicapath);
	TEST_EQUAL(replicate(master2, replica2, tempdir, 0, 1, true), 1);
	check_equal_dbs(masterpath, replica2path);

	// Start writing changesets, but only keep 1 in history, and make a
	// modification.
	set_max_changesets(1);
	orig.close();
	orig = get_writable_database_again();
	orig.add_document(doc1);
	orig.commit();

	// Replicate, and replicate from the replica - should have 1 changes.
	TEST_EQUAL(replicate(master, replica, tempdir, 1, 0, 1), 2);
	check_equal_dbs(masterpath, replicapath);
	TEST_EQUAL(replicate(master2, replica2, tempdir, 1, 0, 1), 2);
	check_equal_dbs(masterpath, replica2path);

	// Make two changes - only one changeset should be preserved.
	orig.add_document(doc1);
	orig.commit();

	// Replication should do a full copy, since one of the needed
	// changesets is missing.

	//FIXME - the following tests are commented out because the backends
	//don't currently tidy up old changesets correctly.
	//TEST_EQUAL(replicate(master, replica, tempdir, 0, 1, true), 1);
	//check_equal_dbs(masterpath, replicapath);
	//TEST_EQUAL(replicate(master2, replica2, tempdir, 0, 1, true), 1);
	//check_equal_dbs(masterpath, replica2path);

	// We need this inner scope to we close the replicas before we remove
	// the temporary directory on Windows.
    }

    rmtmpdir(tempdir);
    return true;
}

static void
replicate_with_brokenness(Xapian::DatabaseMaster & master,
			  Xapian::DatabaseReplica & replica,
			  const string & tempdir,
			  int expected_changesets,
			  int expected_fullcopies,
			  bool expected_changed)
{
    string changesetpath = tempdir + "/changeset";
    get_changeset(changesetpath, master, replica,
		  1, 0, 1);

    // Try applying truncated changesets of various different lengths.
    string brokenchangesetpath = tempdir + "/changeset_broken";
    off_t filesize = get_file_size(changesetpath);
    off_t len = 10;
    off_t copylen;
    while (len < filesize) {
	copylen = truncated_copy(changesetpath, brokenchangesetpath, len);
	TEST_EQUAL(copylen, len);
	tout << "Trying replication with a changeset truncated to " << len <<
		" bytes, from " << filesize << " bytes\n";
	TEST_EXCEPTION(Xapian::NetworkError,
		       apply_changeset(brokenchangesetpath, replica,
				       expected_changesets, expected_fullcopies,
				       expected_changed));
	if (len < 30 || len >= filesize - 10) {
	    // For lengths near the beginning and end, increment size by 1
	    ++len;
	} else {
	    // Don't bother incrementing by small amounts in the middle of
	    // the changeset.
	    len += 1000;
	    if (len >= filesize - 10) {
		len = filesize - 10;
	    }
	}
    }
}

// Test changesets which are truncated (and therefore invalid).
DEFINE_TESTCASE(replicate3, replicas) {
    UNSET_MAX_CHANGESETS_AFTERWARDS;
    string tempdir = ".replicatmp";
    mktmpdir(tempdir);
    string masterpath = get_named_writable_database_path("master");

    set_max_changesets(10);

    {
	Xapian::WritableDatabase orig(get_named_writable_database("master"));
	Xapian::DatabaseMaster master(masterpath);
	string replicapath = tempdir + "/replica";
	Xapian::DatabaseReplica replica(replicapath);

	// Add a document to the original database.
	Xapian::Document doc1;
	doc1.set_data(string("doc1"));
	doc1.add_posting("doc", 1);
	doc1.add_posting("one", 1);
	orig.add_document(doc1);
	orig.commit();

	TEST_EQUAL(replicate(master, replica, tempdir, 0, 1, true), 1);
	check_equal_dbs(masterpath, replicapath);

	// Make a changeset.
	orig.add_document(doc1);
	orig.commit();

	replicate_with_brokenness(master, replica, tempdir, 1, 0, true);
	// Although it throws an error, the final replication in
	// replicate_with_brokenness() updates the database, since it's just
	// the end-of-replication message which is missing its body.
	check_equal_dbs(masterpath, replicapath);

	// Check that the earlier broken replications didn't cause any problems
	// for the next replication.
	orig.add_document(doc1);
	orig.commit();
	TEST_EQUAL(replicate(master, replica, tempdir, 1, 0, true), 2);

	// We need this inner scope to we close the replica before we remove
	// the temporary directory on Windows.
    }

    rmtmpdir(tempdir);
    return true;
}

// Tests for max_changesets
DEFINE_TESTCASE(replicate4, replicas) {
    UNSET_MAX_CHANGESETS_AFTERWARDS;
    string tempdir = ".replicatmp";
    mktmpdir(tempdir);
    string masterpath = get_named_writable_database_path("master");

    set_max_changesets(1);

    {
	Xapian::WritableDatabase orig(get_named_writable_database("master"));
	Xapian::DatabaseMaster master(masterpath);
	string replicapath = tempdir + "/replica";
	Xapian::DatabaseReplica replica(replicapath);

	// Add a document with no positions to the original database.
	Xapian::Document doc1;
	doc1.set_data(string("doc1"));
	doc1.add_term("nopos");
	orig.add_document(doc1);
	orig.commit();

	// Apply the replication - we don't have changesets stored, so this
	// should just do a database copy, and return a count of 1.
	int count = replicate(master, replica, tempdir, 0, 1, true);
	TEST_EQUAL(count, 1);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}

	// Add a document with positional information to the original database.
	doc1.add_posting("pos", 1);
	orig.add_document(doc1);
	orig.commit();

	// Replicate, and check that we have the positional information.
	count = replicate(master, replica, tempdir, 1, 0, true);
	TEST_EQUAL(count, 2);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}
	check_equal_dbs(masterpath, replicapath);

	// Add a document with no positions to the original database.
	Xapian::Document doc2;
	doc2.set_data(string("doc2"));
	doc2.add_term("nopos");
	orig.add_document(doc2);
	if (get_dbtype() != "chert") {
	    // FIXME: Needs to be pre-commit for new-glass
	    set_max_changesets(0);
	}
	orig.commit();

	// Replicate, and check that we have the positional information.
	count = replicate(master, replica, tempdir, 1, 0, true);
	TEST_EQUAL(count, 2);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}
	check_equal_dbs(masterpath, replicapath);
	TEST(!file_exists(masterpath + "/changes1"));

	// Turn off replication, make sure we dont write anything
	if (get_dbtype() == "chert") {
	    set_max_changesets(0);
	}

	// Add a document with no positions to the original database.
	Xapian::Document doc3;
	doc3.set_data(string("doc3"));
	doc3.add_term("nonopos");
	orig.add_document(doc3);
	orig.commit();

	// Replicate, and check that we have the positional information.
	count = replicate(master, replica, tempdir, 0, 1, true);
	TEST_EQUAL(count, 1);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}
	// Should have pulled a full copy
	check_equal_dbs(masterpath, replicapath);
	TEST(!file_exists(masterpath + "/changes3"));

	// We need this inner scope to we close the replica before we remove
	// the temporary directory on Windows.
    }

    rmtmpdir(tempdir);
    return true;
}


// Tests for max_changesets
DEFINE_TESTCASE(replicate5, replicas) {
    SKIP_TEST_FOR_BACKEND("chert");
    UNSET_MAX_CHANGESETS_AFTERWARDS;
    string tempdir = ".replicatmp";
    mktmpdir(tempdir);
    string masterpath = get_named_writable_database_path("master");

    set_max_changesets(2);

    {
	Xapian::WritableDatabase orig(get_named_writable_database("master"));
	Xapian::DatabaseMaster master(masterpath);
	string replicapath = tempdir + "/replica";
	Xapian::DatabaseReplica replica(replicapath);

	// Add a document with no positions to the original database.
	Xapian::Document doc1;
	doc1.set_data(string("doc1"));
	doc1.add_term("nopos");
	orig.add_document(doc1);
	orig.commit();

	// Apply the replication - we don't have changesets stored, so this
	// should just do a database copy, and return a count of 1.
	int count = replicate(master, replica, tempdir, 0, 1, true);
	TEST_EQUAL(count, 1);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}

	// Add a document with positional information to the original database.
	doc1.add_posting("pos", 1);
	orig.add_document(doc1);
	orig.commit();

	// Replicate, and check that we have the positional information.
	count = replicate(master, replica, tempdir, 1, 0, true);
	TEST_EQUAL(count, 2);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}
	check_equal_dbs(masterpath, replicapath);

	// Add a document with no positions to the original database.
	Xapian::Document doc2;
	doc2.set_data(string("doc2"));
	doc2.add_term("nopos");
	orig.add_document(doc2);
	orig.commit();

	// Replicate, and check that we have the positional information.
	count = replicate(master, replica, tempdir, 1, 0, true);
	TEST_EQUAL(count, 2);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}
	check_equal_dbs(masterpath, replicapath);

	// Add a document with no positions to the original database.
	Xapian::Document doc3;
	doc3.set_data(string("doc3"));
	doc3.add_term("nonopos");
	orig.add_document(doc3);
	orig.commit();

	// Replicate, and check that we have the positional information.
	count = replicate(master, replica, tempdir, 1, 0, true);
	TEST_EQUAL(count, 2);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}
	check_equal_dbs(masterpath, replicapath);

	// Ensure that only these changesets exists
	TEST(!file_exists(masterpath + "/changes1"));
	TEST(file_exists(masterpath + "/changes2"));
	TEST(file_exists(masterpath + "/changes3"));

	set_max_changesets(3);
	masterpath = get_named_writable_database_path("master");

	// Add a document with no positions to the original database.
	Xapian::Document doc4;
	doc4.set_data(string("doc4"));
	doc4.add_term("nononopos");
	orig.add_document(doc4);
	orig.commit();

	// Replicate, and check that we have the positional information.
	count = replicate(master, replica, tempdir, 1, 0, true);
	TEST_EQUAL(count, 2);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}
	check_equal_dbs(masterpath, replicapath);

	// Add a document with no positions to the original database.
	Xapian::Document doc5;
	doc5.set_data(string("doc5"));
	doc5.add_term("nonononopos");
	orig.add_document(doc5);
	orig.commit();

	// Replicate, and check that we have the positional information.
	count = replicate(master, replica, tempdir, 1, 0, true);
	TEST_EQUAL(count, 2);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}
	check_equal_dbs(masterpath, replicapath);

	TEST(!file_exists(masterpath + "/changes2"));
	TEST(file_exists(masterpath + "/changes3"));
	TEST(file_exists(masterpath + "/changes4"));
	TEST(file_exists(masterpath + "/changes5"));

	// We need this inner scope to we close the replica before we remove
	// the temporary directory on Windows.
    }

    rmtmpdir(tempdir);
    return true;
}

/// Test --full-copy option.
DEFINE_TESTCASE(replicate6, replicas) {
    UNSET_MAX_CHANGESETS_AFTERWARDS;
    string tempdir = ".replicatmp";
    mktmpdir(tempdir);
    string masterpath = get_named_writable_database_path("master");

    set_max_changesets(10);

    {
	Xapian::WritableDatabase orig(get_named_writable_database("master"));
	Xapian::DatabaseMaster master(masterpath);
	string replicapath = tempdir + "/replica";
	Xapian::DatabaseReplica replica(replicapath);

	// Add a document to the original database.
	Xapian::Document doc1;
	doc1.set_data(string("doc1"));
	doc1.add_posting("doc", 1);
	doc1.add_posting("one", 1);
	orig.add_document(doc1);
	orig.commit();

	rm_rf(masterpath + "1");
	cp_R(masterpath, masterpath + "1");

	orig.add_document(doc1);
	orig.commit();

	// Apply the replication - we don't have changesets stored, so this
	// should just do a database copy, and return a count of 1.
	int count = replicate(master, replica, tempdir, 0, 1, true);
	TEST_EQUAL(count, 1);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}

	Xapian::DatabaseMaster master1(masterpath + "1");

	// Try to replicate an older version of the master.
	count = replicate(master1, replica, tempdir, 0, 0, false);
	TEST_EQUAL(count, 1);

	// Force a full copy.
	count = replicate(master1, replica, tempdir, 0, 1, true, true);
	TEST_EQUAL(count, 1);

	// Test we can still replicate.
	orig.add_document(doc1);
	orig.commit();

	count = replicate(master, replica, tempdir, 2, 0, true);
	TEST_EQUAL(count, 3);

	check_equal_dbs(masterpath, replicapath);

	// We need this inner scope to we close the replica before we remove
	// the temporary directory on Windows.
    }

    rmtmpdir(tempdir);
    return true;
}

/// Test healing a corrupt replica (new in 1.3.5).
DEFINE_TESTCASE(replicate7, replicas) {
    UNSET_MAX_CHANGESETS_AFTERWARDS;
    string tempdir = ".replicatmp";
    mktmpdir(tempdir);
    string masterpath = get_named_writable_database_path("master");

    set_max_changesets(10);

    Xapian::WritableDatabase orig(get_named_writable_database("master"));
    Xapian::DatabaseMaster master(masterpath);
    string replicapath = tempdir + "/replica";
    {
	Xapian::DatabaseReplica replica(replicapath);

	// Add a document to the original database.
	Xapian::Document doc1;
	doc1.set_data(string("doc1"));
	doc1.add_posting("doc", 1);
	doc1.add_posting("one", 1);
	orig.add_document(doc1);
	orig.commit();

	orig.add_document(doc1);
	orig.commit();

	// Apply the replication - we don't have changesets stored, so this
	// should just do a database copy, and return a count of 1.
	int count = replicate(master, replica, tempdir, 0, 1, true);
	TEST_EQUAL(count, 1);
	{
	    Xapian::Database dbcopy(replicapath);
	    TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
	}
    }

    {
	// Corrupt replica by truncating all the files to size 0.
	string d = replicapath;
	d += "/replica_1";
	DIR * dir = opendir(d.c_str());
	TEST(dir != NULL);
	while (true) {
	    errno = 0;
	    struct dirent * entry = readdir(dir);
	    if (!entry) {
		if (errno == 0)
		    break;
		FAIL_TEST("readdir failed: " << strerror(errno));
	    }

	    // Skip '.' and '..'.
	    if (entry->d_name[0] == '.') continue;

	    string file = d;
	    file += '/';
	    file += entry->d_name;
	    int fd = open(file.c_str(), O_WRONLY|O_TRUNC, 0666);
	    TEST(fd != -1);
	    TEST(close(fd) == 0);
	}
	closedir(dir);
    }

    {
	Xapian::DatabaseReplica replica(replicapath);

	// Replication should succeed and perform a full copy.
	int count = replicate(master, replica, tempdir, 0, 1, true);
	TEST_EQUAL(count, 1);

	check_equal_dbs(masterpath, replicapath);

	// We need this inner scope to we close the replica before we remove
	// the temporary directory on Windows.
    }

    rmtmpdir(tempdir);
    return true;
}