xapian-core  2.0.0
decvalwtsource.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2009 Lemur Consulting Ltd
5  * Copyright (C) 2011,2012,2015,2016,2024 Olly Betts
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 
24 #include "xapian/postingsource.h"
25 #include "pack.h"
26 
27 using namespace Xapian;
28 
30  Xapian::valueno slot_,
31  Xapian::docid range_start_,
32  Xapian::docid range_end_)
34  range_start(range_start_),
35  range_end(range_end_)
36 {
37 }
38 
39 double
41  return curr_weight;
42 }
43 
47  range_end);
48 }
49 
50 std::string
52  return "Xapian::DecreasingValueWeightPostingSource";
53 }
54 
55 std::string
57  std::string result;
58  pack_uint(result, get_slot());
59  pack_uint(result, range_start);
60  pack_uint_last(result, range_end);
61  return result;
62 }
63 
66  const char * pos = s.data();
67  const char * end = pos + s.size();
68  Xapian::valueno new_slot;
69  Xapian::docid new_range_start, new_range_end;
70  if (!unpack_uint(&pos, end, &new_slot) ||
71  !unpack_uint(&pos, end, &new_range_start) ||
72  !unpack_uint_last(&pos, end, &new_range_end)) {
74  }
75  return new DecreasingValueWeightPostingSource(new_slot, new_range_start,
76  new_range_end);
77 }
78 
79 void
81  Xapian::doccount shard_index)
82 {
84  if (range_end == 0 || get_database().get_doccount() <= range_end)
85  items_at_end = false;
86  else
87  items_at_end = true;
88 }
89 
90 void
92 {
96  if (docid >= range_start && (range_end == 0 || docid <= range_end)) {
97  if (items_at_end) {
98  if (curr_weight < min_wt) {
99  // skip to end of range.
103  }
104  } else {
105  if (curr_weight < min_wt) {
106  // terminate early.
107  done();
108  } else {
109  // Update max_weight.
111  }
112  }
113  }
114 }
115 
116 void
118  if (get_maxweight() < min_wt) {
119  done();
120  return;
121  }
123  skip_if_in_range(min_wt);
124 }
125 
126 void
128  double min_wt) {
129  if (get_maxweight() < min_wt) {
130  done();
131  return;
132  }
134  skip_if_in_range(min_wt);
135 }
136 
137 bool
139  double min_wt) {
140  if (get_maxweight() < min_wt) {
141  done();
142  return true;
143  }
144  bool valid = Xapian::ValueWeightPostingSource::check(min_docid, min_wt);
145  if (valid) {
146  skip_if_in_range(min_wt);
147  }
148  return valid;
149 }
150 
151 std::string
153  return "DecreasingValueWeightPostingSource()";
154 }
An indexed database of documents.
Definition: database.h:75
Read weights from a value which is known to decrease as docid increases.
Xapian::docid range_end
End of range of docids for which weights are known to be decreasing.
double get_weight() const override
Return the weight contribution for the current document.
std::string name() const override
Name of the posting source class.
bool check(Xapian::docid min_docid, double min_wt) override
Check if the specified docid occurs.
double curr_weight
Weight at current position.
void reset(const Database &db_, Xapian::doccount shard_index) override
Set this PostingSource to the start of the list of postings.
void skip_to(Xapian::docid min_docid, double min_wt) override
Advance to the specified docid.
DecreasingValueWeightPostingSource(Xapian::valueno slot_, Xapian::docid range_start_=0, Xapian::docid range_end_=0)
Construct a DecreasingValueWeightPostingSource.
void skip_if_in_range(double min_wt)
Skip the iterator forward if in the decreasing range, and weight is low.
std::string get_description() const override
Return a string describing this object.
Xapian::docid range_start
Start of range of docids for which weights are known to be decreasing.
bool items_at_end
Flag, set to true if there are docs after the end of the range.
DecreasingValueWeightPostingSource * unserialise(const std::string &serialised) const override
Create object given string serialisation returned by serialise().
std::string serialise() const override
Serialise object parameters into a string.
void next(double min_wt) override
Advance the current position to the next matching document.
DecreasingValueWeightPostingSource * clone() const override
Clone the posting source.
void set_maxweight(double max_weight)
Specify an upper bound on what get_weight() will return from now on.
double get_maxweight() const noexcept
Return the currently set upper bound on what get_weight() can return.
void skip_to(Xapian::docid min_docid, double min_wt)
Advance to the specified docid.
bool at_end() const
Return true if the current position is past the last entry in this list.
void next(double min_wt)
Advance the current position to the next matching document.
Xapian::Database get_database() const
The database we're reading values from.
bool check(Xapian::docid min_docid, double min_wt)
Check if the specified docid occurs.
Xapian::docid get_docid() const
Return the current docid.
Xapian::valueno get_slot() const
The slot we're reading values from.
void done()
End the iteration.
A posting source which reads weights from a value slot.
void reset(const Database &db_, Xapian::doccount shard_index)
Set this PostingSource to the start of the list of postings.
double get_weight() const
Return the weight contribution for the current document.
Xapian::termpos pos
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
unsigned valueno
The number for a value slot in a document.
Definition: types.h:90
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:37
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51
void unpack_throw_serialisation_error(const char *p)
Throw appropriate SerialisationError.
Definition: pack.cc:29
Pack types into strings and unpack them again.
bool unpack_uint_last(const char **p, const char *end, U *result)
Decode an unsigned integer as the last item in a string.
Definition: pack.h:118
void pack_uint_last(std::string &s, U value)
Append an encoded unsigned integer to a string as the last item.
Definition: pack.h:100
bool unpack_uint(const char **p, const char *end, U *result)
Decode an unsigned integer from a string.
Definition: pack.h:346
void pack_uint(std::string &s, U value)
Append an encoded unsigned integer to a string.
Definition: pack.h:315
External sources of posting information.