File: api_valuestats.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 (322 lines) | stat: -rw-r--r-- 12,328 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
/* api_valuestats.cc: tests of the value statistics functions.
 *
 * Copyright 2008 Lemur Consulting Ltd
 * Copyright 2008,2009,2011 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_valuestats.h"

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

#include "apitest.h"

using namespace std;

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

/// Test of value statistics methods.
DEFINE_TESTCASE(valuestats1, writable && valuestats) {
    Xapian::WritableDatabase db_w = get_writable_database();

    // Check that counts are initially zero.
    TEST_EQUAL(db_w.get_value_freq(0), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "");
    TEST_EQUAL(db_w.get_value_freq(1), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "");

    Xapian::Document doc;
    doc.add_value(0, "hello");

    // Check that statistics for the correct value slot increase when document
    // is added.  (Check slot 1 first, so that cache invalidation of the last
    // slot read also gets checked.)
    db_w.add_document(doc);
    TEST_EQUAL(db_w.get_value_freq(1), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "");
    TEST_EQUAL(db_w.get_value_freq(0), 1);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "hello");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "hello");

    // Check that statistics work correctly when second document is added.
    doc = Xapian::Document();
    doc.add_value(0, "world");
    doc.add_value(1, "cheese");
    db_w.replace_document(2, doc);
    TEST_EQUAL(db_w.get_value_freq(0), 2);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "hello");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "world");
    TEST_EQUAL(db_w.get_value_freq(1), 1);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "cheese");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "cheese");

    // Deleting a document affects the count, but not the bounds.
    db_w.delete_document(1);
    TEST_EQUAL(db_w.get_value_freq(1), 1);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "cheese");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "cheese");
    TEST_EQUAL(db_w.get_value_freq(0), 1);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "hello");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "world");

    // Deleting all the documents returns the bounds to their original value.
    db_w.delete_document(2);
    TEST_EQUAL(db_w.get_value_freq(0), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "");
    TEST_EQUAL(db_w.get_value_freq(1), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "");

    // Adding a document with a value in one of the old slots should still
    // end up with tight bounds on it.
    doc = Xapian::Document();
    doc.add_value(1, "newval");
    db_w.replace_document(2, doc);
    TEST_EQUAL(db_w.get_value_freq(1), 1);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "newval");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "newval");
    TEST_EQUAL(db_w.get_value_freq(0), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "");

    return true;
}

/// Test that value statistics stuff obeys transactions.
DEFINE_TESTCASE(valuestats2, transactions && valuestats) {
    Xapian::WritableDatabase db_w = get_writable_database();
    Xapian::Database db = get_writable_database_as_database();

    // Check that counts are initially zero.
    TEST_EQUAL(db_w.get_value_freq(0), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "");
    TEST_EQUAL(db_w.get_value_freq(1), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "");
    TEST_EQUAL(db.get_value_freq(0), 0);
    TEST_EQUAL(db.get_value_lower_bound(0), "");
    TEST_EQUAL(db.get_value_upper_bound(0), "");
    TEST_EQUAL(db.get_value_freq(1), 0);
    TEST_EQUAL(db.get_value_lower_bound(1), "");
    TEST_EQUAL(db.get_value_upper_bound(1), "");

    Xapian::Document doc;
    doc.add_value(0, "hello");

    // Check that statistics for the correct value slot increase when document
    // is added.  (Check slot 1 first, so that cache invalidation of the last
    // slot read also gets checked.)
    db_w.add_document(doc);
    TEST_EQUAL(db_w.get_value_freq(1), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "");
    TEST_EQUAL(db_w.get_value_freq(0), 1);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "hello");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "hello");

    // The readonly database shouldn't change, though.
    TEST_EQUAL(db.get_value_freq(1), 0);
    TEST_EQUAL(db.get_value_lower_bound(1), "");
    TEST_EQUAL(db.get_value_upper_bound(1), "");
    TEST_EQUAL(db.get_value_freq(0), 0);
    TEST_EQUAL(db.get_value_lower_bound(0), "");
    TEST_EQUAL(db.get_value_upper_bound(0), "");

    // Check that statistics work correctly when second document is added.
    doc = Xapian::Document();
    doc.add_value(0, "world");
    doc.add_value(1, "cheese");
    db_w.replace_document(2, doc);
    TEST_EQUAL(db_w.get_value_freq(0), 2);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "hello");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "world");
    TEST_EQUAL(db_w.get_value_freq(1), 1);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "cheese");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "cheese");

    // The readonly database shouldn't change, though.
    TEST_EQUAL(db.get_value_freq(0), 0);
    TEST_EQUAL(db.get_value_lower_bound(0), "");
    TEST_EQUAL(db.get_value_upper_bound(0), "");
    TEST_EQUAL(db.get_value_freq(1), 0);
    TEST_EQUAL(db.get_value_lower_bound(1), "");
    TEST_EQUAL(db.get_value_upper_bound(1), "");

    // Check that readonly database catches up when a commit is done.
    db_w.commit();
    TEST(db.reopen());
    TEST_EQUAL(db.get_value_freq(1), 1);
    TEST_EQUAL(db.get_value_lower_bound(1), "cheese");
    TEST_EQUAL(db.get_value_upper_bound(1), "cheese");
    TEST_EQUAL(db.get_value_freq(0), 2);
    TEST_EQUAL(db.get_value_lower_bound(0), "hello");
    TEST_EQUAL(db.get_value_upper_bound(0), "world");

    // Deleting a document affects the count, but not the bounds.
    db_w.delete_document(1);
    TEST_EQUAL(db_w.get_value_freq(1), 1);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "cheese");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "cheese");
    TEST_EQUAL(db_w.get_value_freq(0), 1);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "hello");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "world");

    // Deleting all the documents returns the bounds to their original value.
    db_w.delete_document(2);
    TEST_EQUAL(db_w.get_value_freq(0), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "");
    TEST_EQUAL(db_w.get_value_freq(1), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "");

    // Adding a document with a value in one of the old slots should still
    // end up with tight bounds on it.
    doc = Xapian::Document();
    doc.add_value(1, "newval");
    db_w.replace_document(2, doc);
    TEST_EQUAL(db_w.get_value_freq(1), 1);
    TEST_EQUAL(db_w.get_value_lower_bound(1), "newval");
    TEST_EQUAL(db_w.get_value_upper_bound(1), "newval");
    TEST_EQUAL(db_w.get_value_freq(0), 0);
    TEST_EQUAL(db_w.get_value_lower_bound(0), "");
    TEST_EQUAL(db_w.get_value_upper_bound(0), "");

    // Check that a readonly database gets the right statistics, too.
    db_w.commit();
    TEST(db.reopen());
    TEST_EQUAL(db.get_value_freq(0), 0);
    TEST_EQUAL(db.get_value_lower_bound(0), "");
    TEST_EQUAL(db.get_value_upper_bound(0), "");
    TEST_EQUAL(db.get_value_freq(1), 1);
    TEST_EQUAL(db.get_value_lower_bound(1), "newval");
    TEST_EQUAL(db.get_value_upper_bound(1), "newval");

    return true;
}

/// Test reading value statistics from prebuilt databases.
DEFINE_TESTCASE(valuestats3, valuestats) {
    Xapian::Database db = get_database("apitest_simpledata");

    TEST_EQUAL(db.get_value_freq(1), 6);
    TEST_EQUAL(db.get_value_lower_bound(1), "h");
    TEST_EQUAL(db.get_value_upper_bound(1), "n");
    TEST_EQUAL(db.get_value_freq(2), 6);
    TEST_EQUAL(db.get_value_lower_bound(2), "d");
    TEST_EQUAL(db.get_value_upper_bound(2), "i");
    TEST_EQUAL(db.get_value_freq(3), 6);
    TEST_EQUAL(db.get_value_lower_bound(3), " ");
    TEST_EQUAL(db.get_value_upper_bound(3), "s");
    TEST_EQUAL(db.get_value_freq(4), 6);
    TEST_EQUAL(db.get_value_lower_bound(4), " ");
    TEST_EQUAL(db.get_value_upper_bound(4), "y");
    TEST_EQUAL(db.get_value_freq(5), 6);
    TEST_EQUAL(db.get_value_lower_bound(5), "e");
    TEST_EQUAL(db.get_value_upper_bound(5), "p");
    TEST_EQUAL(db.get_value_freq(6), 6);
    TEST_EQUAL(db.get_value_lower_bound(6), "a");
    TEST_EQUAL(db.get_value_upper_bound(6), "t");
    TEST_EQUAL(db.get_value_freq(7), 6);
    TEST_EQUAL(db.get_value_lower_bound(7), " ");
    TEST_EQUAL(db.get_value_upper_bound(7), "r");
    TEST_EQUAL(db.get_value_freq(8), 6);
    TEST_EQUAL(db.get_value_lower_bound(8), "a");
    TEST_EQUAL(db.get_value_upper_bound(8), "t");
    TEST_EQUAL(db.get_value_freq(9), 6);
    TEST_EQUAL(db.get_value_lower_bound(9), " ");
    TEST_EQUAL(db.get_value_upper_bound(9), "n");
    TEST_EQUAL(db.get_value_freq(10), 6);
    TEST_EQUAL(db.get_value_lower_bound(10), "e");
    TEST_EQUAL(db.get_value_upper_bound(10), "w");
    TEST_EQUAL(db.get_value_freq(11), 6);
    TEST_EQUAL(db.get_value_lower_bound(11), "\xb9P");
    TEST_EQUAL(db.get_value_upper_bound(11), "\xc7\x04");

    return true;
}

DEFINE_TESTCASE(valuestats4, transactions && valuestats) {
    const size_t FLUSH_THRESHOLD = 10000;
    {
	Xapian::WritableDatabase db_w = get_writable_database();
	Xapian::Document doc;
	doc.add_value(1, "test");
	for (size_t i = 0; i < FLUSH_THRESHOLD; ++i) {
	    db_w.add_document(doc);
	}

	Xapian::Database db = get_writable_database_as_database();
	// Check that we had an automatic-commit.
	TEST_EQUAL(db.get_doccount(), FLUSH_THRESHOLD);
	// Check that the value stats are there.
	TEST_EQUAL(db.get_value_freq(1), FLUSH_THRESHOLD);
	TEST_EQUAL(db.get_value_lower_bound(1), "test");
	TEST_EQUAL(db.get_value_upper_bound(1), "test");

	db_w.begin_transaction();
	doc.add_value(1, "umbrella");
	db_w.cancel_transaction();
    }

    {
	Xapian::Database db = get_writable_database_as_database();
	// Check that we had an automatic-commit.
	TEST_EQUAL(db.get_doccount(), FLUSH_THRESHOLD);
	// Check that the value stats are there.
	TEST_EQUAL(db.get_value_freq(1), FLUSH_THRESHOLD);
	TEST_EQUAL(db.get_value_lower_bound(1), "test");
	TEST_EQUAL(db.get_value_upper_bound(1), "test");
    }

    return true;
}

/// Regression test for bug fixed in 1.1.1 which led to incorrect valuestats.
DEFINE_TESTCASE(valuestats5, !backend) {
    Xapian::Document doc;
    doc.add_value(0, "zero");
    doc.add_value(1, "one");
    doc.add_value(2, "two");
    doc.add_value(3, "three");
    doc.add_value(4, "");
    doc.add_value(5, "five");
    doc.remove_value(3);
    doc.add_value(1, "");

    // Check that we don't have any empty values reported.
    size_t c = 0;
    Xapian::ValueIterator v = doc.values_begin();
    while (v != doc.values_end()) {
	TEST(!(*v).empty());
	++c;
	++v;
    }
    TEST_EQUAL(c, 3); // 0, 2, 5

    return true;
}