2 * @brief A posting source which returns decreasing weights from a value.
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
24 #include "xapian/postingsource.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_
),
40 DecreasingValueWeightPostingSource::get_weight() const {
44 Xapian::DecreasingValueWeightPostingSource
*
45 DecreasingValueWeightPostingSource::clone() const {
46 return new DecreasingValueWeightPostingSource(get_slot(), range_start
,
51 DecreasingValueWeightPostingSource::name() const {
52 return "Xapian::DecreasingValueWeightPostingSource";
56 DecreasingValueWeightPostingSource::serialise() const {
58 pack_uint(result
, get_slot());
59 pack_uint(result
, range_start
);
60 pack_uint_last(result
, range_end
);
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
,
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
)
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
)) {
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();
105 if (curr_weight
< min_wt
) {
109 // Update max_weight.
110 set_maxweight(curr_weight
);
117 DecreasingValueWeightPostingSource::next(double min_wt
) {
118 if (get_maxweight() < min_wt
) {
122 Xapian::ValueWeightPostingSource::next(min_wt
);
123 skip_if_in_range(min_wt
);
127 DecreasingValueWeightPostingSource::skip_to(Xapian::docid min_docid
,
129 if (get_maxweight() < min_wt
) {
133 Xapian::ValueWeightPostingSource::skip_to(min_docid
, min_wt
);
134 skip_if_in_range(min_wt
);
138 DecreasingValueWeightPostingSource::check(Xapian::docid min_docid
,
140 if (get_maxweight() < min_wt
) {
144 bool valid
= Xapian::ValueWeightPostingSource::check(min_docid
, min_wt
);
146 skip_if_in_range(min_wt
);
152 DecreasingValueWeightPostingSource::get_description() const {
153 return "DecreasingValueWeightPostingSource()";