File: api_replacedoc.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 (264 lines) | stat: -rw-r--r-- 8,799 bytes parent folder | download | duplicates (5)
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
/* api_replacedoc.cc: tests of document replacing.
 *
 * Copyright 2009 Richard Boulton
 * Copyright 2015,2016 Olly Betts
 *
 * 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_replacedoc.h"

#include <string>
#include <map>

using namespace std;

#include <xapian.h>
#include "testsuite.h"
#include "testutils.h"

#include "apitest.h"
#include "dbcheck.h"

// Test that positionlists are updated correctly.
DEFINE_TESTCASE(poslistupdate1, positional && writable) {
    Xapian::WritableDatabase db = get_writable_database();

    Xapian::Document doc;
    doc.add_posting("pos", 2);
    doc.add_posting("pos", 3);
    db.add_document(doc);
    db.commit();

    TEST_EQUAL(docterms_to_string(db, 1), "Term(pos, wdf=2, pos=[2, 3])");

    doc = db.get_document(1);
    doc.add_term("pos2");
    db.replace_document(1, doc);
    db.commit();
    TEST_EQUAL(docterms_to_string(db, 1),
	       "Term(pos, wdf=2, pos=[2, 3]), "
	       "Term(pos2, wdf=1)");

    doc = db.get_document(1);
    doc.add_posting("pos3", 1);
    doc.add_posting("pos3", 5);
    db.replace_document(1, doc);
    db.commit();
    TEST_EQUAL(docterms_to_string(db, 1),
	       "Term(pos, wdf=2, pos=[2, 3]), "
	       "Term(pos2, wdf=1), "
	       "Term(pos3, wdf=2, pos=[1, 5])");

    doc = db.get_document(1);
    doc.remove_term("pos");
    db.replace_document(1, doc);
    db.commit();
    TEST_EQUAL(docterms_to_string(db, 1),
	       "Term(pos2, wdf=1), "
	       "Term(pos3, wdf=2, pos=[1, 5])");

    // Regression test: the old positionlist fragment used to be left lying
    // around here.
    Xapian::PositionIterator posit(db.positionlist_begin(1, "pos"));
    TEST(posit == db.positionlist_end(1, "pos"));

    doc = db.get_document(1);
    doc.remove_term("pos3");
    db.replace_document(1, doc);
    db.commit();
    TEST_EQUAL(docterms_to_string(db, 1),
	       "Term(pos2, wdf=1)");

    // Regression test: the old positionlist fragment used to be left lying
    // around here.
    Xapian::PositionIterator posit2(db.positionlist_begin(1, "pos3"));
    TEST(posit2 == db.positionlist_end(1, "pos3"));

    doc = db.get_document(1);
    doc.add_term("pos");
    db.replace_document(1, doc);
    db.commit();
    TEST_EQUAL(docterms_to_string(db, 1),
	       "Term(pos, wdf=1), "
	       "Term(pos2, wdf=1)");

    return true;
}

static Xapian::Document
basic_doc() {
    Xapian::Document doc;
    doc.add_term("z0", 0);
    doc.add_term("z1", 1);
    return doc;
}

static string
basic_docterms() {
    return ", Term(z0, wdf=0), Term(z1, wdf=1)";
}

/** Check that changing the wdf of a term in a document works.
 */
DEFINE_TESTCASE(modtermwdf1, writable) {
    Xapian::WritableDatabase db(get_writable_database());

    string bdt(basic_docterms());

    // Add a simple document.
    Xapian::Document doc1(basic_doc());
    doc1.add_term("takeaway", 1);
    db.add_document(doc1);

    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=1)" + bdt);

    // Modify the wdf of an existing document, checking stats before commit.
    Xapian::Document doc2(basic_doc());
    doc2.add_term("takeaway", 2);
    db.replace_document(1, doc2);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=2)" + bdt);

    // Remove a term, and then put it back again.
    Xapian::Document doc0(basic_doc());
    db.replace_document(1, doc0);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), bdt.substr(2));
    db.replace_document(1, doc1);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=1)" + bdt);

    // Remove a term, commit, then put it back, remove it, and put it back.
    // This is to test the handling of items in the change cache.
    db.replace_document(1, doc0);
    db.commit();
    db.replace_document(1, doc2);
    db.replace_document(1, doc0);
    db.replace_document(1, doc2);
    db.commit();
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=2)" + bdt);

    // Remove a term, and then put it back again without checking stats.
    db.replace_document(1, doc0);
    db.replace_document(1, doc2);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=2)" + bdt);

    // Modify a term, and then put it back again without checking stats.
    db.replace_document(1, doc1);
    db.replace_document(1, doc2);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=2)" + bdt);

    // Modify the wdf of an existing document, checking stats after commit.
    Xapian::Document doc3(basic_doc());
    doc3.add_term("takeaway", 3);
    db.replace_document(1, doc3);
    db.commit();
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=3)" + bdt);

    // Change a document, without changing its length.
    Xapian::Document doc3_diff(basic_doc());
    doc3_diff.add_term("takeaways", 3);
    db.replace_document(1, doc3_diff);
    db.commit();
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaways, wdf=3)" + bdt);

    // Put it back.
    db.replace_document(1, doc3);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=3)" + bdt);

    // Modify a document taken from the database.
    Xapian::Document doc4(db.get_document(1));
    Xapian::Document doc3a(db.get_document(1)); // need this one later
    doc3a.termlist_count(); // Pull the document termlist into memory.
    doc4.add_term("takeaway", 1);
    db.replace_document(1, doc4);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=4)" + bdt);

    // Add a document which was previously added and then modified.
    doc1.add_term("takeaway", 1);
    db.replace_document(1, doc1);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=2)" + bdt);

    // Add back a document which was taken from the database, but never modified.
    db.replace_document(1, doc3a);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=3)" + bdt);

    // Add a position to the document.
    Xapian::Document doc5(db.get_document(1));
    doc5.add_posting("takeaway", 1, 2);
    db.replace_document(1, doc5);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=5, pos=[1])" + bdt);

    // Add a position without changing the wdf.
    Xapian::Document doc5b(db.get_document(1));
    doc5b.add_posting("takeaway", 2, 0);
    db.replace_document(1, doc5b);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=5, pos=[1, 2])" + bdt);

    // Delete a position without changing the wdf.
    Xapian::Document doc5c(basic_doc());
    doc5c.add_posting("takeaway", 2, 5);
    db.replace_document(1, doc5c);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=5, pos=[2])" + bdt);

    // Delete the other position without changing the wdf.
    Xapian::Document doc5d(basic_doc());
    doc5d.add_term("takeaway", 5);
    db.replace_document(1, doc5d);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=5)" + bdt);

    // Reduce the wdf to 0, but don't delete the term.
    Xapian::Document doc0freq(basic_doc());
    doc0freq.add_term("takeaway", 0);
    db.replace_document(1, doc0freq);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=0)" + bdt);

    // Reduce the wdf to 0, and delete the term.
    db.replace_document(1, doc0);
    dbcheck(db, 1, 1);
    TEST_EQUAL(docterms_to_string(db, 1), bdt.substr(2));

    // Delete the document.
    db.delete_document(1);
    dbcheck(db, 0, 1);
    TEST_EXCEPTION(Xapian::DocNotFoundError, docterms_to_string(db, 1));
    TEST_EQUAL(postlist_to_string(db, "takeaway"), "");
    TEST_EXCEPTION(Xapian::DocNotFoundError, docstats_to_string(db, 1));
    TEST_EQUAL(termstats_to_string(db, "takeaway"), "tf=0,cf=0");
    TEST_EQUAL(db.get_doccount(), 0);
    TEST_EQUAL(db.get_avlength(), 0);
    TEST_EQUAL(db.get_lastdocid(), 1);

    return true;
}