xapian-core  2.0.0
andmaybepostlist.cc
Go to the documentation of this file.
1 
4 /* Copyright 2017 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "andmaybepostlist.h"
24 
25 #include "andpostlist.h"
26 
27 using namespace std;
28 
29 PostList*
31  double w_min,
32  bool* valid_ptr)
33 {
34  pl = new AndPostList(pl, r, pl_max, r_max, pltree, termfreq);
35  r = NULL;
36  PostList* result;
37  if (valid_ptr) {
38  result = pl->check(did, w_min, *valid_ptr);
39  } else {
40  result = pl->skip_to(did, w_min);
41  }
42  if (!result) {
43  result = pl;
44  pl = NULL;
45  }
46  pltree->force_recalc();
47  return result;
48 }
49 
52 {
53  return pl_did;
54 }
55 
56 double
58  Xapian::termcount unique_terms,
59  Xapian::termcount wdfdocmax) const
60 {
61  auto res = pl->get_weight(doclen, unique_terms, wdfdocmax);
62  if (maybe_matches())
63  res += r->get_weight(doclen, unique_terms, wdfdocmax);
64  return res;
65 }
66 
67 double
69 {
70  pl_max = pl->recalc_maxweight();
71  r_max = r->recalc_maxweight();
72  return pl_max + r_max;
73 }
74 
75 PostList*
77 {
78  if (w_min > pl_max)
79  return decay_to_and(max(pl_did, r_did) + 1, w_min);
80 
81  PostList* result = pl->next(w_min - r_max);
82  if (result) {
83  delete pl;
84  pl = result;
85  }
86  if (pl->at_end()) {
87  result = pl;
88  pl = NULL;
89  pltree->force_recalc();
90  return result;
91  }
92 
93  pl_did = pl->get_docid();
94  if (pl_did > r_did) {
95  bool r_valid;
96  result = r->check(pl_did, w_min - pl_max, r_valid);
97  if (result) {
98  delete r;
99  r = result;
100  }
101  if (!r_valid)
102  return NULL;
103  if (r->at_end()) {
104  result = pl;
105  pl = NULL;
106  pltree->force_recalc();
107  return result;
108  }
109  r_did = r->get_docid();
110  }
111  return NULL;
112 }
113 
114 PostList*
116 {
117  // skip_to(pl_did) happens after decay from OR
118  if (did < pl_did)
119  return NULL;
120 
121  if (w_min > pl_max) {
122  // We dealt with did <= pl_did just above.
123  return decay_to_and(max(did, r_did), w_min);
124  }
125 
126  PostList* result = pl->skip_to(did, w_min - r_max);
127  if (result) {
128  delete pl;
129  pl = result;
130  }
131  if (pl->at_end()) {
132  result = pl;
133  pl = NULL;
134  pltree->force_recalc();
135  return result;
136  }
137  pl_did = pl->get_docid();
138  if (pl_did > r_did) {
139  bool r_valid;
140  result = r->check(pl_did, 0, r_valid);
141  if (result) {
142  delete r;
143  r = result;
144  }
145  if (!r_valid)
146  return NULL;
147  if (r->at_end()) {
148  result = pl;
149  pl = NULL;
150  pltree->force_recalc();
151  return result;
152  }
153  r_did = r->get_docid();
154  }
155  return NULL;
156 }
157 
158 PostList*
159 AndMaybePostList::check(Xapian::docid did, double w_min, bool& valid)
160 {
161  if (w_min > pl_max)
162  return decay_to_and(max({did, pl_did, r_did}), w_min, &valid);
163 
164  PostList* result = pl->check(did, w_min - r_max, valid);
165  if (result) {
166  delete pl;
167  pl = result;
168  }
169  if (valid) {
170  if (pl->at_end()) {
171  result = pl;
172  pl = NULL;
173  pltree->force_recalc();
174  return result;
175  }
176  pl_did = pl->get_docid();
177  if (pl_did > r_did) {
178  bool r_valid;
179  result = r->check(pl_did, 0, r_valid);
180  if (result) {
181  delete r;
182  r = result;
183  }
184  if (!r_valid)
185  return NULL;
186  if (r->at_end()) {
187  result = pl;
188  pl = NULL;
189  pltree->force_recalc();
190  return result;
191  }
192  r_did = r->get_docid();
193  }
194  }
195  return NULL;
196 }
197 
198 string
200 {
201  string desc = "AndMaybePostList(";
202  desc += pl->get_description();
203  desc += ", ";
204  desc += r->get_description();
205  desc += ')';
206  return desc;
207 }
208 
211 {
212  auto res = pl->get_wdf();
213  if (maybe_matches())
214  res += r->get_wdf();
215  return res;
216 }
217 
220 {
221  auto res = pl->count_matching_subqs();
222  if (maybe_matches())
223  res += r->count_matching_subqs();
224  return res;
225 }
226 
227 void
229 {
230  pl->gather_position_lists(orposlist);
231  if (maybe_matches())
232  r->gather_position_lists(orposlist);
233 }
PostList class implementing Query::OP_AND_MAYBE.
N-way AND postlist.
Xapian::termcount get_wdf() const
Return the wdf for the document at the current position.
PostList * skip_to(Xapian::docid did, double w_min)
Skip forward to the specified docid.
PostList * decay_to_and(Xapian::docid did, double w_min, bool *valid_ptr=NULL)
double recalc_maxweight()
Recalculate the upper bound on what get_weight() can return.
Xapian::docid get_docid() const
Return the current docid.
double get_weight(Xapian::termcount doclen, Xapian::termcount unique_terms, Xapian::termcount wdfdocmax) const
Return the weight contribution for the current position.
void gather_position_lists(OrPositionList *orposlist)
Gather PositionList* objects for a subtree.
Xapian::termcount count_matching_subqs() const
Count the number of leaf subqueries which match at the current position.
std::string get_description() const
Return a string description of this object.
PostList * check(Xapian::docid did, double w_min, bool &valid)
Check if the specified docid occurs in this postlist.
N-way AND postlist.
Definition: andpostlist.h:32
Abstract base class for postlists.
Definition: postlist.h:40
virtual PostList * skip_to(Xapian::docid did, double w_min)=0
Skip forward to the specified docid.
virtual PostList * next(double w_min)=0
Advance the current position to the next document in the postlist.
virtual Xapian::docid get_docid() const =0
Return the current docid.
PostList * next()
Advance the current position to the next document in the postlist.
Definition: postlist.h:168
virtual PostList * check(Xapian::docid did, double w_min, bool &valid)
Check if the specified docid occurs in this postlist.
Definition: postlist.cc:52
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:64
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51