1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/** @file latlong_posting_source.cc
* @brief LatLongPostingSource implementation.
*/
/* Copyright 2008 Lemur Consulting Ltd
* Copyright 2010,2011 Richard Boulton
* Copyright 2012,2015 Olly Betts
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include <config.h>
#include "xapian/geospatial.h"
#include "xapian/error.h"
#include "xapian/registry.h"
#include "net/length.h"
#include "serialise-double.h"
#include "str.h"
#include <cmath>
using namespace Xapian;
using namespace std;
static double
weight_from_distance(double dist, double k1, double k2)
{
return k1 * pow(dist + k1, -k2);
}
void
LatLongDistancePostingSource::calc_distance()
{
dist = (*metric)(centre, get_value());
}
/// Validate the parameters supplied to LatLongDistancePostingSource.
static void
validate_postingsource_params(double k1, double k2) {
if (k1 <= 0) {
string msg("k1 parameter to LatLongDistancePostingSource must be "
"greater than 0; was ");
msg += str(k1);
throw InvalidArgumentError(msg);
}
if (k2 <= 0) {
string msg("k2 parameter to LatLongDistancePostingSource must be "
"greater than 0; was ");
msg += str(k2);
throw InvalidArgumentError(msg);
}
}
LatLongDistancePostingSource::LatLongDistancePostingSource(
valueno slot_,
const LatLongCoords & centre_,
const LatLongMetric * metric_,
double max_range_,
double k1_,
double k2_)
: ValuePostingSource(slot_),
centre(centre_),
metric(metric_),
max_range(max_range_),
k1(k1_),
k2(k2_)
{
validate_postingsource_params(k1, k2);
set_maxweight(weight_from_distance(0, k1, k2));
}
LatLongDistancePostingSource::LatLongDistancePostingSource(
valueno slot_,
const LatLongCoords & centre_,
const LatLongMetric & metric_,
double max_range_,
double k1_,
double k2_)
: ValuePostingSource(slot_),
centre(centre_),
metric(metric_.clone()),
max_range(max_range_),
k1(k1_),
k2(k2_)
{
validate_postingsource_params(k1, k2);
set_maxweight(weight_from_distance(0, k1, k2));
}
LatLongDistancePostingSource::LatLongDistancePostingSource(
valueno slot_,
const LatLongCoords & centre_,
double max_range_,
double k1_,
double k2_)
: ValuePostingSource(slot_),
centre(centre_),
metric(new Xapian::GreatCircleMetric()),
max_range(max_range_),
k1(k1_),
k2(k2_)
{
validate_postingsource_params(k1, k2);
set_maxweight(weight_from_distance(0, k1, k2));
}
LatLongDistancePostingSource::~LatLongDistancePostingSource()
{
delete metric;
}
void
LatLongDistancePostingSource::next(double min_wt)
{
ValuePostingSource::next(min_wt);
while (!ValuePostingSource::at_end()) {
calc_distance();
if (max_range == 0 || dist <= max_range)
break;
ValuePostingSource::next(min_wt);
}
}
void
LatLongDistancePostingSource::skip_to(docid min_docid,
double min_wt)
{
ValuePostingSource::skip_to(min_docid, min_wt);
while (!ValuePostingSource::at_end()) {
calc_distance();
if (max_range == 0 || dist <= max_range)
break;
ValuePostingSource::next(min_wt);
}
}
bool
LatLongDistancePostingSource::check(docid min_docid,
double min_wt)
{
if (!ValuePostingSource::check(min_docid, min_wt)) {
// check returned false, so we know the document is not in the source.
return false;
}
if (ValuePostingSource::at_end()) {
// return true, since we're definitely at the end of the list.
return true;
}
calc_distance();
if (max_range > 0 && dist > max_range) {
return false;
}
return true;
}
double
LatLongDistancePostingSource::get_weight() const
{
return weight_from_distance(dist, k1, k2);
}
LatLongDistancePostingSource *
LatLongDistancePostingSource::clone() const
{
return new LatLongDistancePostingSource(get_slot(), centre,
metric->clone(),
max_range, k1, k2);
}
string
LatLongDistancePostingSource::name() const
{
return "Xapian::LatLongDistancePostingSource";
}
string
LatLongDistancePostingSource::serialise() const
{
string serialised_centre = centre.serialise();
string metric_name = metric->name();
string serialised_metric = metric->serialise();
string result = encode_length(get_slot());
result += encode_length(serialised_centre.size());
result += serialised_centre;
result += encode_length(metric_name.size());
result += metric_name;
result += encode_length(serialised_metric.size());
result += serialised_metric;
result += serialise_double(max_range);
result += serialise_double(k1);
result += serialise_double(k2);
return result;
}
LatLongDistancePostingSource *
LatLongDistancePostingSource::unserialise_with_registry(const string &s,
const Registry & registry) const
{
const char * p = s.data();
const char * end = p + s.size();
valueno new_slot;
decode_length(&p, end, new_slot);
size_t len;
decode_length_and_check(&p, end, len);
string new_serialised_centre(p, len);
p += len;
decode_length_and_check(&p, end, len);
string new_metric_name(p, len);
p += len;
decode_length_and_check(&p, end, len);
string new_serialised_metric(p, len);
p += len;
double new_max_range = unserialise_double(&p, end);
double new_k1 = unserialise_double(&p, end);
double new_k2 = unserialise_double(&p, end);
if (p != end) {
throw NetworkError("Bad serialised LatLongDistancePostingSource - junk at end");
}
LatLongCoords new_centre;
new_centre.unserialise(new_serialised_centre);
const Xapian::LatLongMetric * metric_type =
registry.get_lat_long_metric(new_metric_name);
if (metric_type == NULL) {
string msg("LatLongMetric ");
msg += new_metric_name;
msg += " not registered";
throw InvalidArgumentError(msg);
}
LatLongMetric * new_metric =
metric_type->unserialise(new_serialised_metric);
return new LatLongDistancePostingSource(new_slot, new_centre,
new_metric,
new_max_range, new_k1, new_k2);
}
void
LatLongDistancePostingSource::init(const Database & db_)
{
ValuePostingSource::init(db_);
if (max_range > 0.0) {
// Possible that no documents are in range.
set_termfreq_min(0);
// Note - would be good to improve termfreq_est here, too, but
// I can't think of anything we can do with the information
// available.
}
}
string
LatLongDistancePostingSource::get_description() const
{
string result("Xapian::LatLongDistancePostingSource(slot=");
result += str(get_slot());
result += ")";
return result;
}
|