scriptindex: Fix weird error cases
[xapian.git] / xapian-core / matcher / andmaybepostlist.cc
blob8051ec3ddc59c0c5e547de3748b2b603d24d88d4
1 /** @file
2 * @brief Merged postlist; items from one list, weights from both
3 */
4 /* Copyright 1999,2000,2001 BrightStation PLC
5 * Copyright 2002 Ananova Ltd
6 * Copyright 2003,2004,2005,2008,2009,2011,2017 Olly Betts
7 * Copyright 2009 Lemur Consulting Ltd
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 * USA
25 #include <config.h>
26 #include "andmaybepostlist.h"
28 #include "debuglog.h"
29 #include "multiandpostlist.h"
30 #include "omassert.h"
32 PostList *
33 AndMaybePostList::process_next_or_skip_to(double w_min, PostList *ret)
35 LOGCALL(MATCH, PostList *, "AndMaybePostList::process_next_or_skip_to", w_min | ret);
36 handle_prune(l, ret);
37 if (l->at_end()) {
38 // once l is over, so is the AND MAYBE
39 lhead = 0;
40 RETURN(NULL);
43 lhead = l->get_docid();
44 if (lhead <= rhead) RETURN(NULL);
46 bool valid;
47 check_handling_prune(r, lhead, w_min - lmax, matcher, valid);
48 if (r->at_end()) {
49 PostList *tmp = l;
50 l = NULL;
51 RETURN(tmp);
53 if (valid) {
54 rhead = r->get_docid();
55 } else {
56 rhead = 0;
58 RETURN(NULL);
61 PostList *
62 AndMaybePostList::sync_rhs(double w_min)
64 LOGCALL(MATCH, PostList *, "AndMaybePostList::sync_rhs", w_min);
65 bool valid;
66 check_handling_prune(r, lhead, w_min - lmax, matcher, valid);
67 if (r->at_end()) {
68 PostList *tmp = l;
69 l = NULL;
70 RETURN(tmp);
72 if (valid) {
73 rhead = r->get_docid();
74 } else {
75 rhead = 0;
77 RETURN(NULL);
80 PostList *
81 AndMaybePostList::next(double w_min)
83 LOGCALL(MATCH, PostList *, "AndMaybePostList::next", w_min);
84 if (w_min > lmax) {
85 // we can replace the AND MAYBE with an AND
86 PostList *ret;
87 LOGLINE(MATCH, "AND MAYBE -> AND");
88 ret = new MultiAndPostList(l, r, lmax, rmax, matcher, dbsize);
89 l = r = NULL;
90 skip_to_handling_prune(ret, std::max(lhead, rhead) + 1, w_min, matcher);
91 RETURN(ret);
93 RETURN(process_next_or_skip_to(w_min, l->next(w_min - rmax)));
96 PostList *
97 AndMaybePostList::skip_to(Xapian::docid did, double w_min)
99 LOGCALL(MATCH, PostList *, "AndMaybePostList::skip_to", did | w_min);
100 if (w_min > lmax) {
101 // we can replace the AND MAYBE with an AND
102 PostList *ret;
103 LOGLINE(MATCH, "AND MAYBE -> AND (in skip_to)");
104 ret = new MultiAndPostList(l, r, lmax, rmax, matcher, dbsize);
105 did = std::max(did, std::max(lhead, rhead));
106 l = r = NULL;
107 skip_to_handling_prune(ret, did, w_min, matcher);
108 RETURN(ret);
111 // exit if we're already past the skip point (or at it)
112 if (did <= lhead) RETURN(NULL);
114 RETURN(process_next_or_skip_to(w_min, l->skip_to(did, w_min - rmax)));
117 Xapian::doccount
118 AndMaybePostList::get_termfreq_max() const
120 LOGCALL(MATCH, Xapian::doccount, "AndMaybePostList::get_termfreq_max", NO_ARGS);
121 // Termfreq is exactly that of left hand branch.
122 RETURN(l->get_termfreq_max());
125 Xapian::doccount
126 AndMaybePostList::get_termfreq_min() const
128 LOGCALL(MATCH, Xapian::doccount, "AndMaybePostList::get_termfreq_min", NO_ARGS);
129 // Termfreq is exactly that of left hand branch.
130 RETURN(l->get_termfreq_min());
133 Xapian::doccount
134 AndMaybePostList::get_termfreq_est() const
136 LOGCALL(MATCH, Xapian::doccount, "AndMaybePostList::get_termfreq_est", NO_ARGS);
137 // Termfreq is exactly that of left hand branch.
138 RETURN(l->get_termfreq_est());
141 TermFreqs
142 AndMaybePostList::get_termfreq_est_using_stats(
143 const Xapian::Weight::Internal & stats) const
145 LOGCALL(MATCH, TermFreqs, "AndMaybePostList::get_termfreq_est_using_stats", stats);
146 // Termfreq is exactly that of left hand branch.
147 RETURN(l->get_termfreq_est_using_stats(stats));
150 Xapian::docid
151 AndMaybePostList::get_docid() const
153 LOGCALL(MATCH, Xapian::docid, "AndMaybePostList::get_docid", NO_ARGS);
154 Assert(lhead != 0); // check we've started
155 RETURN(lhead);
158 // only called if we are doing a probabilistic AND MAYBE
159 double
160 AndMaybePostList::get_weight() const
162 LOGCALL(MATCH, double, "AndMaybePostList::get_weight", NO_ARGS);
163 Assert(lhead != 0); // check we've started
164 if (lhead == rhead) RETURN(l->get_weight() + r->get_weight());
165 RETURN(l->get_weight());
168 // only called if we are doing a probabilistic operation
169 double
170 AndMaybePostList::get_maxweight() const
172 LOGCALL(MATCH, double, "AndMaybePostList::get_maxweight", NO_ARGS);
173 RETURN(lmax + rmax);
176 double
177 AndMaybePostList::recalc_maxweight()
179 LOGCALL(MATCH, double, "AndMaybePostList::recalc_maxweight", NO_ARGS);
180 lmax = l->recalc_maxweight();
181 rmax = r->recalc_maxweight();
182 RETURN(AndMaybePostList::get_maxweight());
185 bool
186 AndMaybePostList::at_end() const
188 LOGCALL(MATCH, bool, "AndMaybePostList::at_end", NO_ARGS);
189 RETURN(lhead == 0);
192 std::string
193 AndMaybePostList::get_description() const
195 return "(" + l->get_description() + " AndMaybe " + r->get_description() +
196 ")";
199 Xapian::termcount
200 AndMaybePostList::get_doclength() const
202 LOGCALL(MATCH, Xapian::termcount, "AndMaybePostList::get_doclength", NO_ARGS);
203 Assert(lhead != 0); // check we've started
204 if (lhead == rhead) AssertEq(l->get_doclength(), r->get_doclength());
205 RETURN(l->get_doclength());
208 Xapian::termcount
209 AndMaybePostList::get_unique_terms() const
211 LOGCALL(MATCH, Xapian::termcount, "AndMaybePostList::get_unique_terms", NO_ARGS);
212 Assert(lhead != 0); // check we've started
213 if (lhead == rhead) AssertEq(l->get_unique_terms(), r->get_unique_terms());
214 RETURN(l->get_unique_terms());
217 Xapian::termcount
218 AndMaybePostList::get_wdf() const
220 LOGCALL(MATCH, Xapian::termcount, "AndMaybePostList::get_wdf", NO_ARGS);
221 if (lhead == rhead) RETURN(l->get_wdf() + r->get_wdf());
222 RETURN(l->get_wdf());
225 Xapian::termcount
226 AndMaybePostList::count_matching_subqs() const
228 LOGCALL(MATCH, Xapian::termcount, "AndMaybePostList::count_matching_subqs", NO_ARGS);
229 if (lhead == rhead)
230 RETURN(l->count_matching_subqs() + r->count_matching_subqs());
231 RETURN(l->count_matching_subqs());
234 void
235 AndMaybePostList::gather_position_lists(OrPositionList* orposlist)
237 l->gather_position_lists(orposlist);
238 if (lhead == rhead) r->gather_position_lists(orposlist);