Fix testcase unsupportedcheck1 for --disable-backend-remote
[xapian.git] / xapian-core / api / decvalwtsource.cc
blob4121f254755966dad22684c3e9803dec8197eae3
1 /** @file
2 * @brief A posting source which returns decreasing weights from a value.
3 */
4 /* Copyright (C) 2009 Lemur Consulting Ltd
5 * Copyright (C) 2011,2012,2015,2016,2024 Olly Betts
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <config.h>
24 #include "xapian/postingsource.h"
25 #include "pack.h"
27 using namespace Xapian;
29 DecreasingValueWeightPostingSource::DecreasingValueWeightPostingSource(
30 Xapian::valueno slot_,
31 Xapian::docid range_start_,
32 Xapian::docid range_end_)
33 : Xapian::ValueWeightPostingSource(slot_),
34 range_start(range_start_),
35 range_end(range_end_)
39 double
40 DecreasingValueWeightPostingSource::get_weight() const {
41 return curr_weight;
44 Xapian::DecreasingValueWeightPostingSource *
45 DecreasingValueWeightPostingSource::clone() const {
46 return new DecreasingValueWeightPostingSource(get_slot(), range_start,
47 range_end);
50 std::string
51 DecreasingValueWeightPostingSource::name() const {
52 return "Xapian::DecreasingValueWeightPostingSource";
55 std::string
56 DecreasingValueWeightPostingSource::serialise() const {
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;
64 Xapian::DecreasingValueWeightPostingSource *
65 DecreasingValueWeightPostingSource::unserialise(const std::string &s) const {
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)) {
73 unpack_throw_serialisation_error(pos);
75 return new DecreasingValueWeightPostingSource(new_slot, new_range_start,
76 new_range_end);
79 void
80 DecreasingValueWeightPostingSource::reset(const Xapian::Database& db_,
81 Xapian::doccount shard_index)
83 Xapian::ValueWeightPostingSource::reset(db_, shard_index);
84 if (range_end == 0 || get_database().get_doccount() <= range_end)
85 items_at_end = false;
86 else
87 items_at_end = true;
90 void
91 DecreasingValueWeightPostingSource::skip_if_in_range(double min_wt)
93 if (ValueWeightPostingSource::at_end()) return;
94 curr_weight = Xapian::ValueWeightPostingSource::get_weight();
95 Xapian::docid docid = Xapian::ValueWeightPostingSource::get_docid();
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.
100 ValueWeightPostingSource::skip_to(range_end + 1, min_wt);
101 if (!ValueWeightPostingSource::at_end())
102 curr_weight = Xapian::ValueWeightPostingSource::get_weight();
104 } else {
105 if (curr_weight < min_wt) {
106 // terminate early.
107 done();
108 } else {
109 // Update max_weight.
110 set_maxweight(curr_weight);
116 void
117 DecreasingValueWeightPostingSource::next(double min_wt) {
118 if (get_maxweight() < min_wt) {
119 done();
120 return;
122 Xapian::ValueWeightPostingSource::next(min_wt);
123 skip_if_in_range(min_wt);
126 void
127 DecreasingValueWeightPostingSource::skip_to(Xapian::docid min_docid,
128 double min_wt) {
129 if (get_maxweight() < min_wt) {
130 done();
131 return;
133 Xapian::ValueWeightPostingSource::skip_to(min_docid, min_wt);
134 skip_if_in_range(min_wt);
137 bool
138 DecreasingValueWeightPostingSource::check(Xapian::docid min_docid,
139 double min_wt) {
140 if (get_maxweight() < min_wt) {
141 done();
142 return true;
144 bool valid = Xapian::ValueWeightPostingSource::check(min_docid, min_wt);
145 if (valid) {
146 skip_if_in_range(min_wt);
148 return valid;
151 std::string
152 DecreasingValueWeightPostingSource::get_description() const {
153 return "DecreasingValueWeightPostingSource()";