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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
/* omdocument.cc: class for performing a match
*
* Copyright 1999,2000,2001 BrightStation PLC
* Copyright 2002 Ananova Ltd
* Copyright 2003,2004,2006,2007,2008,2009,2011,2013,2014 Olly Betts
* Copyright 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/document.h>
#include "backends/document.h"
#include "documentvaluelist.h"
#include "maptermlist.h"
#include "net/serialise.h"
#include "str.h"
#include "unicode/description_append.h"
#include <xapian/error.h>
#include <xapian/types.h>
#include <xapian/valueiterator.h>
#include <algorithm>
#include <string>
using namespace std;
namespace Xapian {
// implementation of Document
Document::Document(Document::Internal *internal_) : internal(internal_)
{
}
Document::Document() : internal(new Xapian::Document::Internal)
{
}
string
Document::get_value(Xapian::valueno slot) const
{
LOGCALL(API, string, "Document::get_value", slot);
RETURN(internal->get_value(slot));
}
string
Document::get_data() const
{
LOGCALL(API, string, "Document::get_data", NO_ARGS);
RETURN(internal->get_data());
}
void
Document::set_data(const string &data)
{
LOGCALL_VOID(API, "Document::set_data", data);
internal->set_data(data);
}
void
Document::operator=(const Document &other)
{
// pointers are reference counted.
internal = other.internal;
}
Document::Document(const Document &other)
: internal(other.internal)
{
}
Document::~Document()
{
}
string
Document::get_description() const
{
return internal->get_description();
}
void
Document::add_value(Xapian::valueno slot, const string &value)
{
LOGCALL_VOID(API, "Document::add_value", slot | value);
internal->add_value(slot, value);
}
void
Document::remove_value(Xapian::valueno slot)
{
LOGCALL_VOID(API, "Document::remove_value", slot);
internal->remove_value(slot);
}
void
Document::clear_values()
{
LOGCALL_VOID(API, "Document::clear_values", NO_ARGS);
internal->clear_values();
}
void
Document::add_posting(const string & tname,
Xapian::termpos tpos,
Xapian::termcount wdfinc)
{
LOGCALL_VOID(API, "Document::add_posting", tname | tpos | wdfinc);
if (tname.empty()) {
throw InvalidArgumentError("Empty termnames aren't allowed.");
}
internal->add_posting(tname, tpos, wdfinc);
}
void
Document::add_term(const string & tname, Xapian::termcount wdfinc)
{
LOGCALL_VOID(API, "Document::add_term", tname | wdfinc);
if (tname.empty()) {
throw InvalidArgumentError("Empty termnames aren't allowed.");
}
internal->add_term(tname, wdfinc);
}
void
Document::remove_posting(const string & tname, Xapian::termpos tpos,
Xapian::termcount wdfdec)
{
LOGCALL_VOID(API, "Document::remove_posting", tname | tpos | wdfdec);
if (tname.empty()) {
throw InvalidArgumentError("Empty termnames aren't allowed.");
}
internal->remove_posting(tname, tpos, wdfdec);
}
void
Document::remove_term(const string & tname)
{
LOGCALL_VOID(API, "Document::remove_term", tname);
internal->remove_term(tname);
}
void
Document::clear_terms()
{
LOGCALL_VOID(API, "Document::clear_terms", NO_ARGS);
internal->clear_terms();
}
Xapian::termcount
Document::termlist_count() const {
LOGCALL(API, Xapian::termcount, "Document::termlist_count", NO_ARGS);
RETURN(internal->termlist_count());
}
TermIterator
Document::termlist_begin() const
{
LOGCALL(API, TermIterator, "Document::termlist_begin", NO_ARGS);
RETURN(TermIterator(internal->open_term_list()));
}
Xapian::termcount
Document::values_count() const {
LOGCALL(API, Xapian::termcount, "Document::values_count", NO_ARGS);
RETURN(internal->values_count());
}
ValueIterator
Document::values_begin() const
{
LOGCALL(API, ValueIterator, "Document::values_begin", NO_ARGS);
// Calling values_count() has the side effect of making sure that they have
// been read into the std::map "values" member of internal.
if (internal->values_count() == 0) RETURN(ValueIterator());
RETURN(ValueIterator(new DocumentValueList(internal)));
}
docid
Document::get_docid() const
{
LOGCALL(API, docid, "Document::get_docid", NO_ARGS);
RETURN(internal->get_docid());
}
std::string
Document::serialise() const
{
LOGCALL(API, std::string, "Document::serialise", NO_ARGS);
RETURN(serialise_document(*this));
}
Document
Document::unserialise(const std::string &s)
{
LOGCALL_STATIC(API, Document, "Document::unserialise", s);
RETURN(unserialise_document(s));
}
}
/////////////////////////////////////////////////////////////////////////////
void
OmDocumentTerm::add_position(Xapian::termpos tpos)
{
LOGCALL_VOID(DB, "OmDocumentTerm::add_position", tpos);
// We generally expect term positions to be added in approximately
// increasing order, so check the end first
if (positions.empty() || tpos > positions.back()) {
positions.push_back(tpos);
return;
}
// Search for the position the term occurs at. Use binary chop to
// search, since this is a sorted list.
vector<Xapian::termpos>::iterator i;
i = lower_bound(positions.begin(), positions.end(), tpos);
if (i == positions.end() || *i != tpos) {
positions.insert(i, tpos);
}
}
void
OmDocumentTerm::remove_position(Xapian::termpos tpos)
{
LOGCALL_VOID(DB, "OmDocumentTerm::remove_position", tpos);
// Search for the position the term occurs at. Use binary chop to
// search, since this is a sorted list.
vector<Xapian::termpos>::iterator i;
i = lower_bound(positions.begin(), positions.end(), tpos);
if (i == positions.end() || *i != tpos) {
throw Xapian::InvalidArgumentError("Position " + str(tpos) +
" not in list, can't remove");
}
positions.erase(i);
}
string
OmDocumentTerm::get_description() const
{
string description;
description = "OmDocumentTerm(wdf = ";
description += str(wdf);
description += ", positions[";
description += str(positions.size());
description += "])";
return description;
}
string
Xapian::Document::Internal::get_value(Xapian::valueno slot) const
{
if (values_here) {
map<Xapian::valueno, string>::const_iterator i;
i = values.find(slot);
if (i == values.end()) return string();
return i->second;
}
if (!database.get()) return string();
return do_get_value(slot);
}
string
Xapian::Document::Internal::get_data() const
{
LOGCALL(DB, string, "Xapian::Document::Internal::get_data", NO_ARGS);
if (data_here) RETURN(data);
if (!database.get()) RETURN(string());
RETURN(do_get_data());
}
void
Xapian::Document::Internal::set_data(const string &data_)
{
data = data_;
data_here = true;
}
TermList *
Xapian::Document::Internal::open_term_list() const
{
LOGCALL(DB, TermList *, "Document::Internal::open_term_list", NO_ARGS);
if (terms_here) {
RETURN(new MapTermList(terms.begin(), terms.end()));
}
if (!database.get()) RETURN(NULL);
RETURN(database->open_term_list(did));
}
void
Xapian::Document::Internal::add_value(Xapian::valueno slot, const string &value)
{
need_values();
if (!value.empty()) {
values[slot] = value;
} else {
// Empty values aren't stored, but replace any existing value by
// removing it.
values.erase(slot);
}
}
void
Xapian::Document::Internal::remove_value(Xapian::valueno slot)
{
need_values();
map<Xapian::valueno, string>::iterator i = values.find(slot);
if (i == values.end()) {
throw Xapian::InvalidArgumentError("Value #" + str(slot) +
" is not present in document, in "
"Xapian::Document::Internal::remove_value()");
}
values.erase(i);
}
void
Xapian::Document::Internal::clear_values()
{
values.clear();
values_here = true;
}
void
Xapian::Document::Internal::add_posting(const string & tname, Xapian::termpos tpos,
Xapian::termcount wdfinc)
{
need_terms();
positions_modified = true;
map<string, OmDocumentTerm>::iterator i;
i = terms.find(tname);
if (i == terms.end()) {
OmDocumentTerm newterm(wdfinc);
newterm.add_position(tpos);
terms.insert(make_pair(tname, newterm));
} else {
i->second.add_position(tpos);
if (wdfinc) i->second.inc_wdf(wdfinc);
}
}
void
Xapian::Document::Internal::add_term(const string & tname, Xapian::termcount wdfinc)
{
need_terms();
map<string, OmDocumentTerm>::iterator i;
i = terms.find(tname);
if (i == terms.end()) {
OmDocumentTerm newterm(wdfinc);
terms.insert(make_pair(tname, newterm));
} else {
if (wdfinc) i->second.inc_wdf(wdfinc);
}
}
void
Xapian::Document::Internal::remove_posting(const string & tname,
Xapian::termpos tpos,
Xapian::termcount wdfdec)
{
need_terms();
map<string, OmDocumentTerm>::iterator i;
i = terms.find(tname);
if (i == terms.end()) {
throw Xapian::InvalidArgumentError("Term '" + tname +
"' is not present in document, in "
"Xapian::Document::Internal::remove_posting()");
}
i->second.remove_position(tpos);
if (wdfdec) i->second.dec_wdf(wdfdec);
positions_modified = true;
}
void
Xapian::Document::Internal::remove_term(const string & tname)
{
need_terms();
map<string, OmDocumentTerm>::iterator i;
i = terms.find(tname);
if (i == terms.end()) {
throw Xapian::InvalidArgumentError("Term '" + tname +
"' is not present in document, in "
"Xapian::Document::Internal::remove_term()");
}
positions_modified = !i->second.positions.empty();
terms.erase(i);
}
void
Xapian::Document::Internal::clear_terms()
{
terms.clear();
terms_here = true;
// Assume there was a term with positions for now.
// FIXME: may be worth checking...
positions_modified = true;
}
Xapian::termcount
Xapian::Document::Internal::termlist_count() const
{
if (!terms_here) {
// How equivalent is this line to the rest?
// return database.get() ? database->open_term_list(did)->get_approx_size() : 0;
need_terms();
}
Assert(terms_here);
return terms.size();
}
void
Xapian::Document::Internal::need_terms() const
{
if (terms_here) return;
if (database.get()) {
Xapian::TermIterator t(database->open_term_list(did));
Xapian::TermIterator tend(NULL);
for ( ; t != tend; ++t) {
Xapian::PositionIterator p = t.positionlist_begin();
OmDocumentTerm term(t.get_wdf());
for ( ; p != t.positionlist_end(); ++p) {
term.add_position(*p);
}
terms.insert(make_pair(*t, term));
}
}
terms_here = true;
}
Xapian::valueno
Xapian::Document::Internal::values_count() const
{
LOGCALL(DB, Xapian::valueno, "Document::Internal::values_count", NO_ARGS);
need_values();
Assert(values_here);
RETURN(values.size());
}
string
Xapian::Document::Internal::get_description() const
{
string desc = "Document(";
// description_append ?
if (data_here) {
desc += "data='";
description_append(desc, data);
desc += "'";
}
if (values_here) {
if (data_here) desc += ", ";
desc += "values[";
desc += str(values.size());
desc += ']';
}
if (terms_here) {
if (data_here || values_here) desc += ", ";
desc += "terms[";
desc += str(terms.size());
desc += ']';
}
if (database.get()) {
if (data_here || values_here || terms_here) desc += ", ";
// database->get_description() if/when that returns a non-generic
// result.
desc += "db:yes";
}
desc += ')';
return desc;
}
void
Xapian::Document::Internal::need_values() const
{
if (!values_here) {
if (database.get()) {
Assert(values.empty());
do_get_all_values(values);
}
values_here = true;
}
}
Xapian::Document::Internal::~Internal()
{
if (database.get())
database->invalidate_doc_object(this);
}
|