1 /** @file weight.cc
2  * @brief Xapian::Weight base class
3  */
4 /* Copyright (C) 2007,2008,2009 Olly Betts
5  * Copyright (C) 2009 Lemur Consulting Ltd
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <config.h>
23 
24 #include "xapian/weight.h"
25 
26 #include "weightinternal.h"
27 
28 #include "debuglog.h"
29 
30 #include "xapian/error.h"
31 
32 using namespace std;
33 
34 namespace Xapian {
35 
36 void
init_(const Internal & stats,Xapian::termcount query_length)37 Weight::init_(const Internal & stats, Xapian::termcount query_length)
38 {
39     LOGCALL_VOID(MATCH, "Weight::init_", stats | query_length);
40     collection_size_ = stats.collection_size;
41     rset_size_ = stats.rset_size;
42     if (stats_needed & AVERAGE_LENGTH)
43 	average_length_ = stats.get_average_length();
44     if (stats_needed & DOC_LENGTH_MAX)
45 	doclength_upper_bound_ = stats.db.get_doclength_upper_bound();
46     if (stats_needed & DOC_LENGTH_MIN)
47 	doclength_lower_bound_ = stats.db.get_doclength_lower_bound();
48     wdf_upper_bound_ = 0;
49     termfreq_ = 0;
50     reltermfreq_ = 0;
51     query_length_ = query_length;
52     wqf_ = 1;
53     init(0.0);
54 }
55 
56 void
init_(const Internal & stats,Xapian::termcount query_length,const string & term,Xapian::termcount wqf,double factor)57 Weight::init_(const Internal & stats, Xapian::termcount query_length,
58 	      const string & term, Xapian::termcount wqf, double factor)
59 {
60     LOGCALL_VOID(MATCH, "Weight::init_", stats | query_length | term | wqf | factor);
61     collection_size_ = stats.collection_size;
62     rset_size_ = stats.rset_size;
63     if (stats_needed & AVERAGE_LENGTH)
64 	average_length_ = stats.get_average_length();
65     if (stats_needed & DOC_LENGTH_MAX)
66 	doclength_upper_bound_ = stats.db.get_doclength_upper_bound();
67     if (stats_needed & DOC_LENGTH_MIN)
68 	doclength_lower_bound_ = stats.db.get_doclength_lower_bound();
69     if (stats_needed & WDF_MAX)
70 	wdf_upper_bound_ = stats.db.get_wdf_upper_bound(term);
71     if (stats_needed & TERMFREQ)
72 	termfreq_ = stats.get_termfreq(term);
73     if (stats_needed & RELTERMFREQ)
74 	reltermfreq_ = stats.get_reltermfreq(term);
75     query_length_ = query_length;
76     wqf_ = wqf;
77     init(factor);
78 }
79 
80 void
init_(const Internal & stats,Xapian::termcount query_length,double factor,Xapian::doccount termfreq,Xapian::doccount reltermfreq)81 Weight::init_(const Internal & stats, Xapian::termcount query_length,
82 	      double factor, Xapian::doccount termfreq,
83 	      Xapian::doccount reltermfreq)
84 {
85     LOGCALL_VOID(MATCH, "Weight::init_", stats | query_length | factor | termfreq | reltermfreq);
86     // Synonym case.
87     collection_size_ = stats.collection_size;
88     rset_size_ = stats.rset_size;
89     if (stats_needed & AVERAGE_LENGTH)
90 	average_length_ = stats.get_average_length();
91     if (stats_needed & DOC_LENGTH_MAX)
92 	doclength_upper_bound_ = stats.db.get_doclength_upper_bound();
93     if (stats_needed & DOC_LENGTH_MIN)
94 	doclength_lower_bound_ = stats.db.get_doclength_lower_bound();
95 
96     // The doclength is an upper bound on the wdf.  This is obviously true for
97     // normal terms, but SynonymPostList ensures that it is also true for
98     // synonym terms by clamping the wdf values returned to the doclength.
99     //
100     // (This clamping is only actually necessary in cases where a constituent
101     // term of the synonym is repeated.)
102     if (stats_needed & WDF_MAX)
103 	wdf_upper_bound_ = stats.db.get_doclength_upper_bound();
104 
105     termfreq_ = termfreq;
106     reltermfreq_ = reltermfreq;
107     query_length_ = query_length;
108     wqf_ = 1;
109     init(factor);
110 }
111 
~Weight()112 Weight::~Weight() { }
113 
114 string
name() const115 Weight::name() const
116 {
117     return string();
118 }
119 
120 string
serialise() const121 Weight::serialise() const
122 {
123     throw Xapian::UnimplementedError("serialise() not supported for this Xapian::Weight subclass");
124 }
125 
126 Weight *
unserialise(const string &) const127 Weight::unserialise(const string &) const
128 {
129     throw Xapian::UnimplementedError("unserialise() not supported for this Xapian::Weight subclass");
130 }
131 
132 }
133