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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
|
/* 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,2018 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 "overflow.h"
#include "str.h"
#include "unicode/description_append.h"
#include <xapian/error.h>
#include <xapian/types.h>
#include <xapian/valueiterator.h>
#include <algorithm>
#include <limits>
#include <string>
using namespace std;
namespace Xapian {
// implementation of Document
Document::Document(Document::Internal *internal_) : internal(internal_)
{
}
Document::Document(Document&&) = default;
Document&
Document::operator=(Document&&) = default;
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);
}
Xapian::termpos
Document::remove_postings(const string& term,
Xapian::termpos termpos_first,
Xapian::termpos termpos_last,
Xapian::termcount wdf_dec)
{
if (term.empty()) {
throw InvalidArgumentError("Empty termnames aren't allowed.");
}
if (rare(termpos_first > termpos_last)) {
return 0;
}
return internal->remove_postings(term, termpos_first, termpos_last,
wdf_dec);
}
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::merge() const
{
Assert(!is_deleted());
inplace_merge(positions.begin(),
positions.begin() + split,
positions.end());
split = 0;
}
bool
OmDocumentTerm::add_position(Xapian::termcount wdf_inc, Xapian::termpos tpos)
{
LOGCALL(DB, bool, "OmDocumentTerm::add_position", wdf_inc | tpos);
if (rare(is_deleted())) {
wdf = wdf_inc;
split = 0;
positions.push_back(tpos);
return true;
}
wdf += wdf_inc;
// Optimise the common case of adding positions in ascending order.
if (positions.empty()) {
positions.push_back(tpos);
return false;
}
if (tpos > positions.back()) {
if (split) {
// Check for duplicate before split.
auto i = lower_bound(positions.cbegin(),
positions.cbegin() + split,
tpos);
if (i != positions.cbegin() + split && *i == tpos)
return false;
}
positions.push_back(tpos);
return false;
}
if (tpos == positions.back()) {
// Duplicate of last entry.
return false;
}
if (split > 0) {
// We could merge in the new entry at the same time, but that seems to
// make things much more complex for minor gains.
merge();
}
// 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) {
auto new_split = positions.size();
if (sizeof(split) < sizeof(Xapian::termpos)) {
if (rare(new_split > numeric_limits<decltype(split)>::max())) {
// The split point would be beyond the size of the type used to
// hold it, which is really unlikely if that type is 32-bit.
// Just insert the old way in this case.
positions.insert(i, tpos);
return false;
}
} else {
// This assertion should always be true because we shouldn't have
// duplicate entries and the split point can't be after the final
// entry.
AssertRel(new_split, <=, numeric_limits<decltype(split)>::max());
}
split = new_split;
positions.push_back(tpos);
}
return false;
}
void
OmDocumentTerm::remove_position(Xapian::termpos tpos)
{
LOGCALL_VOID(DB, "OmDocumentTerm::remove_position", tpos);
Assert(!is_deleted());
if (rare(positions.empty())) {
not_there:
throw Xapian::InvalidArgumentError("Position " + str(tpos) +
" not in list, can't remove");
}
// Special case removing the final position, which we can handle in O(1).
if (positions.back() == tpos) {
positions.pop_back();
if (split == positions.size()) {
split = 0;
// We removed the only entry from after the split.
}
return;
}
if (split > 0) {
// We could remove the requested entry at the same time, but that seems
// fiddly to do.
merge();
}
// We keep positions sorted, so use lower_bound() which can binary chop to
// find the entry.
auto i = lower_bound(positions.begin(), positions.end(), tpos);
if (i == positions.end() || *i != tpos) {
goto not_there;
}
positions.erase(i);
}
Xapian::termpos
OmDocumentTerm::remove_positions(Xapian::termpos termpos_first,
Xapian::termpos termpos_last)
{
LOGCALL(DB, Xapian::termpos, "OmDocumentTerm::remove_position", termpos_first | termpos_last);
Assert(!is_deleted());
if (split > 0) {
// We could remove the requested entries at the same time, but that
// seems fiddly to do.
merge();
}
// Find the range [i, j) that the specified termpos range maps to. Use
// binary chop to search, since this is a sorted list.
auto i = lower_bound(positions.begin(), positions.end(), termpos_first);
if (i == positions.end() || *i > termpos_last) {
return 0;
}
auto j = upper_bound(i, positions.end(), termpos_last);
size_t size_before = positions.size();
positions.erase(i, j);
return Xapian::termpos(size_before - positions.size());
}
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()) {
++termlist_size;
OmDocumentTerm newterm(wdfinc);
newterm.append_position(tpos);
terms.insert(make_pair(tname, std::move(newterm)));
} else {
if (i->second.add_position(wdfinc, tpos))
++termlist_size;
}
}
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()) {
++termlist_size;
OmDocumentTerm newterm(wdfinc);
terms.insert(make_pair(tname, std::move(newterm)));
} else {
if (i->second.increase_wdf(wdfinc))
++termlist_size;
}
}
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() || i->second.is_deleted()) {
if (tname.empty())
throw Xapian::InvalidArgumentError("Empty termnames are invalid");
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.decrease_wdf(wdfdec);
positions_modified = true;
}
Xapian::termpos
Xapian::Document::Internal::remove_postings(const string& term,
Xapian::termpos termpos_first,
Xapian::termpos termpos_last,
Xapian::termcount wdf_dec)
{
need_terms();
auto i = terms.find(term);
if (i == terms.end() || i->second.is_deleted()) {
if (term.empty())
throw Xapian::InvalidArgumentError("Empty termnames are invalid");
throw Xapian::InvalidArgumentError("Term '" + term +
"' is not present in document, in "
"Xapian::Document::Internal::remove_postings()");
}
auto n_removed = i->second.remove_positions(termpos_first, termpos_last);
if (n_removed) {
positions_modified = true;
if (wdf_dec) {
Xapian::termcount wdf_delta;
if (mul_overflows(n_removed, wdf_dec, wdf_delta)) {
// Decreasing by the maximum value will zero the wdf.
wdf_delta = numeric_limits<Xapian::termcount>::max();
}
i->second.decrease_wdf(wdf_delta);
}
}
return n_removed;
}
void
Xapian::Document::Internal::remove_term(const string & tname)
{
need_terms();
map<string, OmDocumentTerm>::iterator i;
i = terms.find(tname);
if (i == terms.end() || i->second.is_deleted()) {
if (tname.empty())
throw Xapian::InvalidArgumentError("Empty termnames are invalid");
throw Xapian::InvalidArgumentError("Term '" + tname +
"' is not present in document, in "
"Xapian::Document::Internal::remove_term()");
}
--termlist_size;
if (!positions_modified) {
positions_modified = (i->second.positionlist_count() > 0);
}
i->second.remove();
}
void
Xapian::Document::Internal::clear_terms()
{
terms.clear();
termlist_size = 0;
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 termlist_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.append_position(*p);
}
terms.insert(terms.end(), make_pair(*t, std::move(term)));
}
}
termlist_size = terms.size();
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(termlist_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);
}
|