1 /** @file
2  * @brief Xapian::Weight base class
3  */
4 /* Copyright (C) 2007,2008,2009,2014,2017,2019 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 "omassert.h"
29 #include "debuglog.h"
30 
31 #include "xapian/error.h"
32 
33 using namespace std;
34 
35 namespace Xapian {
36 
37 void
init_(const Internal & stats,Xapian::termcount query_length)38 Weight::init_(const Internal & stats, Xapian::termcount query_length)
39 {
40     LOGCALL_VOID(MATCH, "Weight::init_", stats | query_length);
41     collection_size_ = stats.collection_size;
42     rset_size_ = stats.rset_size;
43     if (stats_needed & AVERAGE_LENGTH)
44 	average_length_ = stats.get_average_length();
45     if (stats_needed & DOC_LENGTH_MAX)
46 	doclength_upper_bound_ = stats.db.get_doclength_upper_bound();
47     if (stats_needed & DOC_LENGTH_MIN)
48 	doclength_lower_bound_ = stats.db.get_doclength_lower_bound();
49     collectionfreq_ = 0;
50     wdf_upper_bound_ = 0;
51     termfreq_ = 0;
52     reltermfreq_ = 0;
53     query_length_ = query_length;
54     wqf_ = 1;
55     init(0.0);
56 }
57 
58 void
init_(const Internal & stats,Xapian::termcount query_length,const string & term,Xapian::termcount wqf,double factor)59 Weight::init_(const Internal & stats, Xapian::termcount query_length,
60 	      const string & term, Xapian::termcount wqf, double factor)
61 {
62     LOGCALL_VOID(MATCH, "Weight::init_", stats | query_length | term | wqf | factor);
63     collection_size_ = stats.collection_size;
64     rset_size_ = stats.rset_size;
65     if (stats_needed & AVERAGE_LENGTH)
66 	average_length_ = stats.get_average_length();
67     if (stats_needed & DOC_LENGTH_MAX)
68 	doclength_upper_bound_ = stats.db.get_doclength_upper_bound();
69     if (stats_needed & DOC_LENGTH_MIN)
70 	doclength_lower_bound_ = stats.db.get_doclength_lower_bound();
71     if (stats_needed & WDF_MAX)
72 	wdf_upper_bound_ = stats.db.get_wdf_upper_bound(term);
73     if (stats_needed & (TERMFREQ | RELTERMFREQ | COLLECTION_FREQ)) {
74 	bool ok = stats.get_stats(term,
75 				  termfreq_, reltermfreq_, collectionfreq_);
76 	(void)ok;
77 	Assert(ok);
78     }
79     query_length_ = query_length;
80     wqf_ = wqf;
81     init(factor);
82 }
83 
84 void
init_(const Internal & stats,Xapian::termcount query_length,double factor,Xapian::doccount termfreq,Xapian::doccount reltermfreq,Xapian::termcount collection_freq)85 Weight::init_(const Internal & stats, Xapian::termcount query_length,
86 	      double factor, Xapian::doccount termfreq,
87 	      Xapian::doccount reltermfreq, Xapian::termcount collection_freq)
88 {
89     LOGCALL_VOID(MATCH, "Weight::init_", stats | query_length | factor | termfreq | reltermfreq | collection_freq);
90     // Synonym case.
91     collection_size_ = stats.collection_size;
92     rset_size_ = stats.rset_size;
93     if (stats_needed & AVERAGE_LENGTH)
94 	average_length_ = stats.get_average_length();
95     if (stats_needed & (DOC_LENGTH_MAX | WDF_MAX)) {
96 	doclength_upper_bound_ = stats.db.get_doclength_upper_bound();
97 	// The doclength is an upper bound on the wdf.  This is obviously true
98 	// for normal terms, but SynonymPostList ensures that it is also true
99 	// for synonym terms by clamping the wdf values returned to the
100 	// doclength.
101 	//
102 	// (This clamping is only actually necessary in cases where a constituent
103 	// term of the synonym is repeated.)
104 	wdf_upper_bound_ = doclength_upper_bound_;
105     }
106     if (stats_needed & DOC_LENGTH_MIN)
107 	doclength_lower_bound_ = stats.db.get_doclength_lower_bound();
108 
109     termfreq_ = termfreq;
110     reltermfreq_ = reltermfreq;
111     query_length_ = query_length;
112     collectionfreq_ = collection_freq;
113     wqf_ = 1;
114     init(factor);
115 }
116 
~Weight()117 Weight::~Weight() { }
118 
119 string
name() const120 Weight::name() const
121 {
122     return string();
123 }
124 
125 string
serialise() const126 Weight::serialise() const
127 {
128     throw Xapian::UnimplementedError("serialise() not supported for this Xapian::Weight subclass");
129 }
130 
131 Weight *
unserialise(const string &) const132 Weight::unserialise(const string &) const
133 {
134     throw Xapian::UnimplementedError("unserialise() not supported for this Xapian::Weight subclass");
135 }
136 
137 }
138