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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/** @file registry.cc
* @brief Class for looking up user subclasses during unserialisation.
*/
/* Copyright (C) 2006,2007,2008,2009,2010,2016 Olly Betts
* Copyright (C) 2006,2007,2009 Lemur Consulting Ltd
*
* 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/registry.h"
#include "xapian/error.h"
#include "xapian/geospatial.h"
#include "xapian/intrusive_ptr.h"
#include "xapian/matchspy.h"
#include "xapian/postingsource.h"
#include "xapian/weight.h"
#include "debuglog.h"
#include <algorithm>
#include <map>
#include <string>
using namespace std;
class Xapian::Registry::Internal : public Xapian::Internal::intrusive_base {
friend class Xapian::Registry;
/// Registered weighting schemes.
std::map<std::string, Xapian::Weight *> wtschemes;
/// Registered external posting sources.
std::map<std::string, Xapian::PostingSource *> postingsources;
/// Registered match spies.
std::map<std::string, Xapian::MatchSpy *> matchspies;
/// Registered lat-long metrics.
std::map<std::string, Xapian::LatLongMetric *> lat_long_metrics;
/// Add the standard subclasses provided in the API.
void add_defaults();
/// Clear all registered weighting schemes.
void clear_weighting_schemes();
/// Clear all registered posting sources.
void clear_posting_sources();
/// Clear all registered match spies.
void clear_match_spies();
/// Clear all registered lat-long metrics.
void clear_lat_long_metrics();
public:
Internal();
~Internal();
};
template<class T>
static inline void
register_object(map<string, T*> & registry, const T & obj)
{
string name = obj.name();
if (rare(name.empty())) {
throw Xapian::InvalidOperationError("Unable to register object - name() method returned empty string");
}
pair<typename map<string, T *>::iterator, bool> r;
r = registry.insert(make_pair(name, static_cast<T*>(NULL)));
if (!r.second) {
// Existing element with this key, so replace the pointer with NULL
// and delete the existing pointer.
//
// If the delete throws, this will leave a NULL entry in the map, but
// that won't affect behaviour as we return NULL for "not found"
// anyway. The memory used will be leaked if the dtor throws, but
// throwing exceptions from the dtor is bad form, so that's not a big
// problem.
T * p = NULL;
swap(p, r.first->second);
delete p;
}
T * clone = obj.clone();
if (rare(!clone)) {
throw Xapian::InvalidOperationError("Unable to register object - clone() method returned NULL");
}
r.first->second = clone;
}
template<class T>
static inline const T *
lookup_object(map<string, T*> registry, const string & name)
{
typename map<string, T*>::const_iterator i = registry.find(name);
if (i == registry.end()) {
return NULL;
}
return i->second;
}
namespace Xapian {
Registry::Internal::Internal()
{
add_defaults();
}
Registry::Internal::~Internal()
{
clear_weighting_schemes();
clear_posting_sources();
clear_match_spies();
clear_lat_long_metrics();
}
void
Registry::Internal::add_defaults()
{
Xapian::Weight * weighting_scheme;
weighting_scheme = new Xapian::BB2Weight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::BM25Weight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::BM25PlusWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::BoolWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::CoordWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::TradWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::TfIdfWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::InL2Weight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::IfB2Weight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::IneB2Weight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::DLHWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::PL2PlusWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::PL2Weight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::DPHWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::LMWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
Xapian::PostingSource * source;
source = new Xapian::ValueWeightPostingSource(0);
postingsources[source->name()] = source;
source = new Xapian::DecreasingValueWeightPostingSource(0);
postingsources[source->name()] = source;
source = new Xapian::ValueMapPostingSource(0);
postingsources[source->name()] = source;
source = new Xapian::FixedWeightPostingSource(0.0);
postingsources[source->name()] = source;
source = new Xapian::LatLongDistancePostingSource(0,
Xapian::LatLongCoords(),
Xapian::GreatCircleMetric());
postingsources[source->name()] = source;
Xapian::MatchSpy * spy;
spy = new Xapian::ValueCountMatchSpy();
matchspies[spy->name()] = spy;
Xapian::LatLongMetric * metric;
metric = new Xapian::GreatCircleMetric();
lat_long_metrics[metric->name()] = metric;
}
void
Registry::Internal::clear_weighting_schemes()
{
map<string, Xapian::Weight*>::const_iterator i;
for (i = wtschemes.begin(); i != wtschemes.end(); ++i) {
delete i->second;
}
}
void
Registry::Internal::clear_posting_sources()
{
map<string, Xapian::PostingSource *>::const_iterator i;
for (i = postingsources.begin(); i != postingsources.end(); ++i) {
delete i->second;
}
}
void
Registry::Internal::clear_match_spies()
{
map<string, Xapian::MatchSpy *>::const_iterator i;
for (i = matchspies.begin(); i != matchspies.end(); ++i) {
delete i->second;
}
}
void
Registry::Internal::clear_lat_long_metrics()
{
map<string, Xapian::LatLongMetric *>::const_iterator i;
for (i = lat_long_metrics.begin(); i != lat_long_metrics.end(); ++i) {
delete i->second;
}
}
Registry::Registry(const Registry & other)
: internal(other.internal)
{
LOGCALL_CTOR(API, "Registry", other);
}
Registry &
Registry::operator=(const Registry & other)
{
LOGCALL(API, Xapian::Registry &, "Xapian::Registry::operator=", other);
internal = other.internal;
RETURN(*this);
}
Registry::Registry()
: internal(new Registry::Internal())
{
LOGCALL_CTOR(API, "Registry", NO_ARGS);
}
Registry::~Registry()
{
LOGCALL_DTOR(API, "Registry");
// Note - we don't need to do anything special in this destructor, but it
// does need to be explicitly defined because the definition of the
// internals is not visible externally, which results in an error if the
// compiler tries to generate a default destructor.
}
void
Registry::register_weighting_scheme(const Xapian::Weight &wt)
{
LOGCALL_VOID(API, "Xapian::Registry::register_weighting_scheme", wt.name());
register_object(internal->wtschemes, wt);
}
const Xapian::Weight *
Registry::get_weighting_scheme(const string & name) const
{
LOGCALL(API, const Xapian::Weight *, "Xapian::Registry::get_weighting_scheme", name);
RETURN(lookup_object(internal->wtschemes, name));
}
void
Registry::register_posting_source(const Xapian::PostingSource &source)
{
LOGCALL_VOID(API, "Xapian::Registry::register_posting_source", source.name());
register_object(internal->postingsources, source);
}
const Xapian::PostingSource *
Registry::get_posting_source(const string & name) const
{
LOGCALL(API, const Xapian::PostingSource *, "Xapian::Registry::get_posting_source", name);
RETURN(lookup_object(internal->postingsources, name));
}
void
Registry::register_match_spy(const Xapian::MatchSpy &spy)
{
LOGCALL_VOID(API, "Xapian::Registry::register_match_spy", spy.name());
register_object(internal->matchspies, spy);
}
const Xapian::MatchSpy *
Registry::get_match_spy(const string & name) const
{
LOGCALL(API, const Xapian::MatchSpy *, "Xapian::Registry::get_match_spy", name);
RETURN(lookup_object(internal->matchspies, name));
}
void
Registry::register_lat_long_metric(const Xapian::LatLongMetric &metric)
{
LOGCALL_VOID(API, "Xapian::Registry::register_lat_long_metric", metric.name());
register_object(internal->lat_long_metrics, metric);
}
const Xapian::LatLongMetric *
Registry::get_lat_long_metric(const string & name) const
{
LOGCALL(API, const Xapian::LatLongMetric *, "Xapian::Registry::get_lat_long_metric", name);
RETURN(lookup_object(internal->lat_long_metrics, name));
}
}
|