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 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"
25 #include "xapian/error.h"
26 #include "net/length.h"
27 #include "serialise-double.h"
30 using namespace Xapian
;
32 DecreasingValueWeightPostingSource::DecreasingValueWeightPostingSource(
33 Xapian::valueno slot_
,
34 Xapian::docid range_start_
,
35 Xapian::docid range_end_
)
36 : Xapian::ValueWeightPostingSource(slot_
),
37 range_start(range_start_
),
43 DecreasingValueWeightPostingSource::get_weight() const {
47 Xapian::DecreasingValueWeightPostingSource
*
48 DecreasingValueWeightPostingSource::clone() const {
49 return new DecreasingValueWeightPostingSource(get_slot(), range_start
,
54 DecreasingValueWeightPostingSource::name() const {
55 return "Xapian::DecreasingValueWeightPostingSource";
59 DecreasingValueWeightPostingSource::serialise() const {
61 result
+= encode_length(get_slot());
62 result
+= encode_length(range_start
);
63 result
+= encode_length(range_end
);
67 Xapian::DecreasingValueWeightPostingSource
*
68 DecreasingValueWeightPostingSource::unserialise(const std::string
&s
) const {
69 const char * pos
= s
.data();
70 const char * end
= pos
+ s
.size();
71 Xapian::valueno new_slot
;
72 Xapian::docid new_range_start
, new_range_end
;
73 decode_length(&pos
, end
, new_slot
);
74 decode_length(&pos
, end
, new_range_start
);
75 decode_length(&pos
, end
, new_range_end
);
77 throw Xapian::NetworkError("Junk at end of serialised "
78 "DecreasingValueWeightPostingSource");
79 return new DecreasingValueWeightPostingSource(new_slot
, new_range_start
,
84 DecreasingValueWeightPostingSource::init(const Xapian::Database
& db_
) {
85 Xapian::ValueWeightPostingSource::init(db_
);
86 if (range_end
== 0 || get_database().get_doccount() <= range_end
)
93 DecreasingValueWeightPostingSource::skip_if_in_range(double min_wt
)
95 if (ValueWeightPostingSource::at_end()) return;
96 curr_weight
= Xapian::ValueWeightPostingSource::get_weight();
97 Xapian::docid docid
= Xapian::ValueWeightPostingSource::get_docid();
98 if (docid
>= range_start
&& (range_end
== 0 || docid
<= range_end
)) {
100 if (curr_weight
< min_wt
) {
101 // skip to end of range.
102 ValueWeightPostingSource::skip_to(range_end
+ 1, min_wt
);
103 if (!ValueWeightPostingSource::at_end())
104 curr_weight
= Xapian::ValueWeightPostingSource::get_weight();
107 if (curr_weight
< min_wt
) {
111 // Update max_weight.
112 set_maxweight(curr_weight
);
119 DecreasingValueWeightPostingSource::next(double min_wt
) {
120 if (get_maxweight() < min_wt
) {
124 Xapian::ValueWeightPostingSource::next(min_wt
);
125 skip_if_in_range(min_wt
);
129 DecreasingValueWeightPostingSource::skip_to(Xapian::docid min_docid
,
131 if (get_maxweight() < min_wt
) {
135 Xapian::ValueWeightPostingSource::skip_to(min_docid
, min_wt
);
136 skip_if_in_range(min_wt
);
140 DecreasingValueWeightPostingSource::check(Xapian::docid min_docid
,
142 if (get_maxweight() < min_wt
) {
146 bool valid
= Xapian::ValueWeightPostingSource::check(min_docid
, min_wt
);
148 skip_if_in_range(min_wt
);
154 DecreasingValueWeightPostingSource::get_description() const {
155 return "DecreasingValueWeightPostingSource()";