File: multiandpostlist.cc

package info (click to toggle)
xapian-core 1.4.3-2+deb9u3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,412 kB
  • sloc: cpp: 113,868; ansic: 8,723; sh: 4,433; perl: 836; makefile: 566; tcl: 317; python: 40
file content (288 lines) | stat: -rw-r--r-- 7,377 bytes parent folder | download | duplicates (3)
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
/** @file multiandpostlist.cc
 * @brief N-way AND postlist
 */
/* Copyright (C) 2007,2009,2011,2012,2015,2017 Olly Betts
 * Copyright (C) 2009 Lemur Consulting Ltd
 *
 * 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 "multiandpostlist.h"
#include "omassert.h"
#include "debuglog.h"

using namespace std;

void
MultiAndPostList::allocate_plist_and_max_wt()
{
    plist = new PostList * [n_kids];
    try {
	max_wt = new double [n_kids]();
    } catch (...) {
	delete [] plist;
	plist = NULL;
	throw;
    }
}

MultiAndPostList::~MultiAndPostList()
{
    if (plist) {
	for (size_t i = 0; i < n_kids; ++i) {
	    delete plist[i];
	}
	delete [] plist;
    }
    delete [] max_wt;
}

Xapian::doccount
MultiAndPostList::get_termfreq_min() const
{
    // The number of matching documents is minimised when we have the minimum
    // number of matching documents from each sub-postlist, and these are
    // maximally disjoint.
    Xapian::doccount sum = plist[0]->get_termfreq_min();
    if (sum) {
	for (size_t i = 1; i < n_kids; ++i) {
	    Xapian::doccount sum_old = sum;
	    sum += plist[i]->get_termfreq_min();
	    // If sum < sum_old, the calculation overflowed and the true sum
	    // must be > db_size.  Since we added a value <= db_size,
	    // subtracting db_size must un-overflow us.
	    if (sum >= sum_old && sum <= db_size) {
		// It's possible there's no overlap.
		return 0;
	    }
	    sum -= db_size;
	}
	AssertRelParanoid(sum,<=,MultiAndPostList::get_termfreq_est());
    }
    return sum;
}

Xapian::doccount
MultiAndPostList::get_termfreq_max() const
{
    // We can't match more documents than the least of our sub-postlists.
    Xapian::doccount result = plist[0]->get_termfreq_max();
    for (size_t i = 1; i < n_kids; ++i) {
	Xapian::doccount tf = plist[i]->get_termfreq_max();
	if (tf < result) result = tf;
    }
    return result;
}

Xapian::doccount
MultiAndPostList::get_termfreq_est() const
{
    LOGCALL(MATCH, Xapian::doccount, "MultiAndPostList::get_termfreq_est", NO_ARGS);
    if (rare(db_size == 0))
	RETURN(0);
    // We calculate the estimate assuming independence.  With this assumption,
    // the estimate is the product of the estimates for the sub-postlists
    // divided by db_size (n_kids - 1) times.
    double result = plist[0]->get_termfreq_est();
    for (size_t i = 1; i < n_kids; ++i) {
	result = (result * plist[i]->get_termfreq_est()) / db_size;
    }
    return static_cast<Xapian::doccount>(result + 0.5);
}

TermFreqs
MultiAndPostList::get_termfreq_est_using_stats(
	const Xapian::Weight::Internal & stats) const
{
    LOGCALL(MATCH, TermFreqs, "MultiAndPostList::get_termfreq_est_using_stats", stats);
    // We calculate the estimate assuming independence.  With this assumption,
    // the estimate is the product of the estimates for the sub-postlists
    // divided by db_size (n_kids - 1) times.
    TermFreqs freqs(plist[0]->get_termfreq_est_using_stats(stats));

    double freqest = double(freqs.termfreq);
    double relfreqest = double(freqs.reltermfreq);
    double collfreqest = double(freqs.collfreq);

    // Our caller should have ensured this.
    Assert(stats.collection_size);

    for (size_t i = 1; i < n_kids; ++i) {
	freqs = plist[i]->get_termfreq_est_using_stats(stats);

	// If the collection is empty, freqest should be 0 already, so leave
	// it alone.
	freqest = (freqest * freqs.termfreq) / stats.collection_size;
	collfreqest = (collfreqest * freqs.collfreq) / stats.total_term_count;

	// If the rset is empty, relfreqest should be 0 already, so leave
	// it alone.
	if (stats.rset_size != 0)
	    relfreqest = (relfreqest * freqs.reltermfreq) / stats.rset_size;
    }

    RETURN(TermFreqs(static_cast<Xapian::doccount>(freqest + 0.5),
		     static_cast<Xapian::doccount>(relfreqest + 0.5),
		     static_cast<Xapian::termcount>(collfreqest + 0.5)));
}

double
MultiAndPostList::get_maxweight() const
{
    return max_total;
}

Xapian::docid
MultiAndPostList::get_docid() const
{
    return did;
}

Xapian::termcount
MultiAndPostList::get_doclength() const
{
    Assert(did);
    Xapian::termcount doclength = plist[0]->get_doclength();
    for (size_t i = 1; i < n_kids; ++i) {
	AssertEq(doclength, plist[i]->get_doclength());
    }
    return doclength;
}

Xapian::termcount
MultiAndPostList::get_unique_terms() const
{
    Assert(did);
    Xapian::termcount unique_terms = plist[0]->get_unique_terms();
    for (size_t i = 1; i < n_kids; ++i) {
	AssertEq(unique_terms, plist[i]->get_unique_terms());
    }
    return unique_terms;
}

double
MultiAndPostList::get_weight() const
{
    Assert(did);
    double result = 0;
    for (size_t i = 0; i < n_kids; ++i) {
	result += plist[i]->get_weight();
    }
    return result;
}

bool
MultiAndPostList::at_end() const
{
    return (did == 0);
}

double
MultiAndPostList::recalc_maxweight()
{
    max_total = 0.0;
    for (size_t i = 0; i < n_kids; ++i) {
	double new_max = plist[i]->recalc_maxweight();
	max_wt[i] = new_max;
	max_total += new_max;
    }
    return max_total;
}

PostList *
MultiAndPostList::find_next_match(double w_min)
{
advanced_plist0:
    if (plist[0]->at_end()) {
	did = 0;
	return NULL;
    }
    did = plist[0]->get_docid();
    for (size_t i = 1; i < n_kids; ++i) {
	bool valid;
	check_helper(i, did, w_min, valid);
	if (!valid) {
	    next_helper(0, w_min);
	    goto advanced_plist0;
	}
	if (plist[i]->at_end()) {
	    did = 0;
	    return NULL;
	}
	Xapian::docid new_did = plist[i]->get_docid();
	if (new_did != did) {
	    skip_to_helper(0, new_did, w_min);
	    goto advanced_plist0;
	}
    }
    return NULL;
}

PostList *
MultiAndPostList::next(double w_min)
{
    next_helper(0, w_min);
    return find_next_match(w_min);
}

PostList *
MultiAndPostList::skip_to(Xapian::docid did_min, double w_min)
{
    skip_to_helper(0, did_min, w_min);
    return find_next_match(w_min);
}

std::string
MultiAndPostList::get_description() const
{
    string desc("(");
    desc += plist[0]->get_description();
    for (size_t i = 1; i < n_kids; ++i) {
	desc += " AND ";
	desc += plist[i]->get_description();
    }
    desc += ')';
    return desc;
}

Xapian::termcount
MultiAndPostList::get_wdf() const
{
    Xapian::termcount totwdf = 0;
    for (size_t i = 0; i < n_kids; ++i) {
	totwdf += plist[i]->get_wdf();
    }
    return totwdf;
}

Xapian::termcount
MultiAndPostList::count_matching_subqs() const
{
    Xapian::termcount total = 0;
    for (size_t i = 0; i < n_kids; ++i) {
	total += plist[i]->count_matching_subqs();
    }
    return total;
}

void
MultiAndPostList::gather_position_lists(OrPositionList* orposlist)
{
    for (size_t i = 0; i < n_kids; ++i) {
	plist[i]->gather_position_lists(orposlist);
    }
}