Branch data Line data Source code
1 : : /** @file decvalwtsource.cc
2 : : * @brief A posting source which returns decreasing weights from a value.
3 : : */
4 : : /* Copyright (C) 2009 Lemur Consulting Ltd
5 : : *
6 : : * This program is free software; you can redistribute it and/or modify
7 : : * it under the terms of the GNU General Public License as published by
8 : : * the Free Software Foundation; either version 2 of the License, or
9 : : * (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, write to the Free Software
18 : : * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 : : */
20 : :
21 : : #include <config.h>
22 : :
23 : : #include "xapian/postingsource.h"
24 : : #include "xapian/error.h"
25 : : #include "serialise.h"
26 : : #include "serialise-double.h"
27 : : #include <cmath>
28 : :
29 : : using namespace Xapian;
30 : :
31 : 1953 : DecreasingValueWeightPostingSource::DecreasingValueWeightPostingSource(
32 : : Xapian::valueno slot_,
33 : : Xapian::docid range_start_,
34 : : Xapian::docid range_end_)
35 : : : Xapian::ValueWeightPostingSource(slot_),
36 : : range_start(range_start_),
37 : 1953 : range_end(range_end_)
38 : : {
39 : 1953 : }
40 : :
41 : : Xapian::weight
42 : 350 : DecreasingValueWeightPostingSource::get_weight() const {
43 : 350 : return curr_weight;
44 : : }
45 : :
46 : : Xapian::DecreasingValueWeightPostingSource *
47 : 160 : DecreasingValueWeightPostingSource::clone() const {
48 : : return new DecreasingValueWeightPostingSource(slot, range_start,
49 : 160 : range_end);
50 : : }
51 : :
52 : : std::string
53 : 1642 : DecreasingValueWeightPostingSource::name() const {
54 : 1642 : return "Xapian::DecreasingValueWeightPostingSource";
55 : : }
56 : :
57 : : std::string
58 : 66 : DecreasingValueWeightPostingSource::serialise() const {
59 : 66 : std::string result;
60 : 66 : result += encode_length(slot);
61 : 66 : result += encode_length(range_start);
62 : 66 : result += encode_length(range_end);
63 : 0 : return result;
64 : : }
65 : :
66 : : Xapian::DecreasingValueWeightPostingSource *
67 : 66 : DecreasingValueWeightPostingSource::unserialise(const std::string &s) const {
68 : 66 : const char * pos = s.data();
69 : 66 : const char * end = pos + s.size();
70 : 66 : Xapian::valueno new_slot = decode_length(&pos, end, false);
71 : 66 : Xapian::docid new_range_start = decode_length(&pos, end, false);
72 : 66 : Xapian::docid new_range_end = decode_length(&pos, end, false);
73 [ - + ]: 66 : if (pos != end)
74 : : throw Xapian::NetworkError("Junk at end of serialised "
75 : 0 : "DecreasingValueWeightPostingSource");
76 : : return new DecreasingValueWeightPostingSource(new_slot, new_range_start,
77 : 66 : new_range_end);
78 : : }
79 : :
80 : : void
81 : 210 : DecreasingValueWeightPostingSource::init(const Xapian::Database & db_) {
82 : 210 : Xapian::ValueWeightPostingSource::init(db_);
83 [ + + + + ]: 210 : if (range_end == 0 || db.get_doccount() <= range_end)
[ + + ]
84 : 110 : items_at_end = false;
85 : : else
86 : 100 : items_at_end = true;
87 : 210 : }
88 : :
89 : : void
90 : 850 : DecreasingValueWeightPostingSource::skip_if_in_range(Xapian::weight min_wt)
91 : : {
92 [ + + ]: 850 : if (value_it == db.valuestream_end(slot)) return;
93 : 680 : curr_weight = Xapian::ValueWeightPostingSource::get_weight();
94 : 680 : Xapian::docid docid = Xapian::ValueWeightPostingSource::get_docid();
95 [ + + + + ]: 680 : if (docid >= range_start && (range_end == 0 || docid <= range_end)) {
[ + + ]
96 [ + + ]: 550 : if (items_at_end) {
97 [ + + ]: 270 : if (curr_weight < min_wt) {
98 : : // skip to end of range.
99 : 90 : value_it.skip_to(range_end + 1);
100 [ + - ]: 90 : if (value_it != db.valuestream_end(slot))
101 : 90 : curr_weight = Xapian::ValueWeightPostingSource::get_weight();
102 : : }
103 : : } else {
104 [ + + ]: 280 : if (curr_weight < min_wt) {
105 : : // terminate early.
106 : 40 : value_it = db.valuestream_end(slot);
107 : : } else {
108 : : // Update max_weight.
109 : 850 : set_maxweight(curr_weight);
110 : : }
111 : : }
112 : : }
113 : : }
114 : :
115 : : void
116 : 790 : DecreasingValueWeightPostingSource::next(Xapian::weight min_wt) {
117 [ - + ]: 790 : if (get_maxweight() < min_wt) {
118 : 0 : value_it = db.valuestream_end(slot);
119 : 0 : started = true;
120 : 0 : return;
121 : : }
122 : 790 : Xapian::ValueWeightPostingSource::next(min_wt);
123 : 790 : skip_if_in_range(min_wt);
124 : : }
125 : :
126 : : void
127 : 20 : DecreasingValueWeightPostingSource::skip_to(Xapian::docid min_docid,
128 : : Xapian::weight min_wt) {
129 [ - + ]: 20 : if (get_maxweight() < min_wt) {
130 : 0 : value_it = db.valuestream_end(slot);
131 : 0 : started = true;
132 : 0 : return;
133 : : }
134 : 20 : Xapian::ValueWeightPostingSource::skip_to(min_docid, min_wt);
135 : 20 : skip_if_in_range(min_wt);
136 : : }
137 : :
138 : : bool
139 : 40 : DecreasingValueWeightPostingSource::check(Xapian::docid min_docid,
140 : : Xapian::weight min_wt) {
141 [ - + ]: 40 : if (get_maxweight() < min_wt) {
142 : 0 : value_it = db.valuestream_end(slot);
143 : 0 : started = true;
144 : 0 : return true;
145 : : }
146 : 40 : bool valid = Xapian::ValueWeightPostingSource::check(min_docid, min_wt);
147 [ + - ]: 40 : if (valid) {
148 : 40 : skip_if_in_range(min_wt);
149 : : }
150 : 40 : return valid;
151 : : }
152 : :
153 : : std::string
154 : 1 : DecreasingValueWeightPostingSource::get_description() const {
155 : 1 : return "DecreasingValueWeightPostingSource()";
156 : : }
|