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 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
|
/* omqueryinternal.cc: Internals of query interface
*
* Copyright 1999,2000,2001 BrightStation PLC
* Copyright 2002 Ananova Ltd
* Copyright 2002,2003,2004,2005,2006,2007,2008,2009 Olly Betts
* Copyright 2006,2007,2008,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 "omqueryinternal.h"
#include "debuglog.h"
#include "registryinternal.h"
#include "serialise.h"
#include "serialise-double.h"
#include "str.h"
#include <xapian/error.h>
#include <xapian/postingsource.h>
#include <xapian/termiterator.h>
#include <xapian/version.h>
#include "vectortermlist.h"
#include <algorithm>
#include "autoptr.h"
#include <cfloat>
#include <climits>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
// Properties for query operations.
static unsigned int
get_min_subqs(Xapian::Query::Internal::op_t op)
{
switch (op) {
case Xapian::Query::Internal::OP_EXTERNAL_SOURCE:
case Xapian::Query::Internal::OP_LEAF:
case Xapian::Query::OP_AND:
case Xapian::Query::OP_OR:
case Xapian::Query::OP_XOR:
case Xapian::Query::OP_NEAR:
case Xapian::Query::OP_PHRASE:
case Xapian::Query::OP_ELITE_SET:
case Xapian::Query::OP_VALUE_RANGE:
case Xapian::Query::OP_VALUE_GE:
case Xapian::Query::OP_VALUE_LE:
case Xapian::Query::OP_SYNONYM:
return 0;
case Xapian::Query::OP_SCALE_WEIGHT:
return 1;
case Xapian::Query::OP_FILTER:
case Xapian::Query::OP_AND_MAYBE:
case Xapian::Query::OP_AND_NOT:
return 2;
default:
Assert(false);
throw Xapian::InvalidOperationError("get_min_subqs called with invalid operator type");
}
}
static unsigned int
get_max_subqs(Xapian::Query::Internal::op_t op)
{
switch (op) {
case Xapian::Query::Internal::OP_EXTERNAL_SOURCE:
case Xapian::Query::Internal::OP_LEAF:
case Xapian::Query::OP_VALUE_RANGE:
case Xapian::Query::OP_VALUE_GE:
case Xapian::Query::OP_VALUE_LE:
return 0;
case Xapian::Query::OP_SCALE_WEIGHT:
return 1;
case Xapian::Query::OP_FILTER:
case Xapian::Query::OP_AND_MAYBE:
case Xapian::Query::OP_AND_NOT:
return 2;
case Xapian::Query::OP_AND:
case Xapian::Query::OP_OR:
case Xapian::Query::OP_XOR:
case Xapian::Query::OP_NEAR:
case Xapian::Query::OP_PHRASE:
case Xapian::Query::OP_ELITE_SET:
case Xapian::Query::OP_SYNONYM:
return UINT_MAX;
default:
Assert(false);
throw Xapian::InvalidOperationError("get_max_subqs called with invalid operator type");
}
}
static inline bool
is_leaf(Xapian::Query::Internal::op_t op)
{
return (op == Xapian::Query::Internal::OP_LEAF);
}
inline bool
is_distributable(Xapian::Query::Internal::op_t op)
{
switch (op) {
case Xapian::Query::OP_AND:
case Xapian::Query::OP_OR:
case Xapian::Query::OP_XOR:
case Xapian::Query::OP_SYNONYM:
return true;
default:
return false;
}
}
// Methods for Xapian::Query::Internal
/** serialising method, for network matches.
*
* The format is designed to be relatively easy to parse.
*
* A single-term query becomes `[<encodedtname>@<termpos>#<wqf>'
* where:
* <wqf> is the decimal within query frequency (1 if omitted),
* <termpos> is the decimal term position (index of term if omitted).
*
* A compound query becomes `(<subqueries><op>', where:
* <subqueries> is the list of subqueries
* <op> is one of: &|%+-^
* also ~N "N >F *N (N unsigned int; F floating point)
*
* If querylen != sum(wqf) we append `=len' (at present we always do this
* for compound queries as it's simpler than working out what sum(wqf) would
* be - FIXME).
*/
string
Xapian::Query::Internal::serialise(Xapian::termpos & curpos) const
{
#ifdef XAPIAN_HAS_REMOTE_BACKEND
string result;
if (op == Xapian::Query::Internal::OP_LEAF) {
result += '[';
result += encode_length(tname.length());
result += tname;
if (term_pos != curpos) result += '@' + encode_length(term_pos);
// parameter is wqf.
if (parameter != 1) result += '#' + encode_length(parameter);
++curpos;
} else if (op == Xapian::Query::Internal::OP_EXTERNAL_SOURCE) {
string sourcename = external_source->name();
if (sourcename.empty())
throw Xapian::UnimplementedError("This PostingSource doesn't support remote use.");
result += '!';
result += encode_length(sourcename.length());
result += sourcename;
string sourcedata = external_source->serialise();
result += encode_length(sourcedata.length());
result += sourcedata;
} else {
result += "(";
for (subquery_list::const_iterator i = subqs.begin();
i != subqs.end();
++i) {
result += (*i)->serialise(curpos);
}
switch (op) {
case Xapian::Query::Internal::OP_LEAF:
Assert(false);
break;
case Xapian::Query::Internal::OP_EXTERNAL_SOURCE:
Assert(false);
break;
case Xapian::Query::OP_AND:
result += "&";
break;
case Xapian::Query::OP_OR:
result += "|";
break;
case Xapian::Query::OP_FILTER:
result += "%";
break;
case Xapian::Query::OP_AND_MAYBE:
result += "+";
break;
case Xapian::Query::OP_AND_NOT:
result += "-";
break;
case Xapian::Query::OP_XOR:
result += "^";
break;
case Xapian::Query::OP_NEAR:
result += "~" + encode_length(parameter);
break;
case Xapian::Query::OP_PHRASE:
result += "\"" + encode_length(parameter);
break;
case Xapian::Query::OP_ELITE_SET:
result += "*" + encode_length(parameter);
break;
case Xapian::Query::OP_VALUE_RANGE:
result += "]";
result += encode_length(tname.length());
result += tname;
result += encode_length(str_parameter.length());
result += str_parameter;
result += encode_length(parameter);
break;
case Xapian::Query::OP_VALUE_GE:
result += "}";
result += encode_length(tname.length());
result += tname;
result += encode_length(parameter);
break;
case Xapian::Query::OP_VALUE_LE:
result += "{";
result += encode_length(tname.length());
result += tname;
result += encode_length(parameter);
break;
case Xapian::Query::OP_SCALE_WEIGHT:
result += ".";
result += str_parameter; // serialise_double(get_dbl_parameter());
break;
case Xapian::Query::OP_SYNONYM:
result += "=";
break;
}
}
return result;
#else
(void)curpos;
throw Xapian::InternalError("query serialisation not compiled in");
#endif
}
string
Xapian::Query::Internal::get_op_name(Xapian::Query::Internal::op_t op)
{
string name;
switch (op) {
case Xapian::Query::Internal::OP_EXTERNAL_SOURCE:
name = "EXTERNAL_SOURCE"; break;
case Xapian::Query::Internal::OP_LEAF: name = "LEAF"; break;
case Xapian::Query::OP_AND: name = "AND"; break;
case Xapian::Query::OP_OR: name = "OR"; break;
case Xapian::Query::OP_FILTER: name = "FILTER"; break;
case Xapian::Query::OP_AND_MAYBE: name = "AND_MAYBE"; break;
case Xapian::Query::OP_AND_NOT: name = "AND_NOT"; break;
case Xapian::Query::OP_XOR: name = "XOR"; break;
case Xapian::Query::OP_NEAR: name = "NEAR"; break;
case Xapian::Query::OP_PHRASE: name = "PHRASE"; break;
case Xapian::Query::OP_ELITE_SET: name = "ELITE_SET"; break;
case Xapian::Query::OP_VALUE_RANGE: name = "VALUE_RANGE"; break;
case Xapian::Query::OP_VALUE_GE: name = "VALUE_GE"; break;
case Xapian::Query::OP_VALUE_LE: name = "VALUE_LE"; break;
case Xapian::Query::OP_SCALE_WEIGHT: name = "SCALE_WEIGHT"; break;
case Xapian::Query::OP_SYNONYM: name = "SYNONYM"; break;
}
return name;
}
string
Xapian::Query::Internal::get_description() const
{
string opstr;
if (is_leaf(op)) {
if (term_pos != 0) {
opstr += "pos=" + str(term_pos);
}
// parameter is wqf.
if (parameter != 1) {
if (!opstr.empty()) opstr += ",";
opstr += "wqf=" + str(parameter);
}
if (!opstr.empty()) opstr = ":(" + opstr + ")";
if (tname.empty()) return "<alldocuments>" + opstr;
return tname + opstr;
}
switch (op) {
case Xapian::Query::OP_VALUE_RANGE:
opstr = get_op_name(op);
opstr += ' ';
opstr += str(parameter);
opstr += ' ';
opstr += tname;
opstr += ' ';
opstr += str_parameter;
return opstr;
case Xapian::Query::OP_VALUE_GE:
case Xapian::Query::OP_VALUE_LE:
opstr = get_op_name(op);
opstr += ' ';
opstr += str(parameter);
opstr += ' ';
opstr += tname;
return opstr;
case Xapian::Query::OP_SCALE_WEIGHT:
opstr += str(get_dbl_parameter());
opstr += " * ";
opstr += subqs[0]->get_description();
return opstr;
case Xapian::Query::Internal::OP_EXTERNAL_SOURCE:
opstr = "PostingSource(";
opstr += external_source->get_description();
opstr += ')';
return opstr;
}
opstr = " " + get_op_name(op) + " ";
if (op == Xapian::Query::OP_NEAR ||
op == Xapian::Query::OP_PHRASE ||
op == Xapian::Query::OP_ELITE_SET)
opstr += str(parameter) + " ";
string description;
subquery_list::const_iterator i;
for (i = subqs.begin(); i != subqs.end(); i++) {
if (!description.empty()) description += opstr;
description += (**i).get_description();
}
return "(" + description + ")";
}
Xapian::termcount
Xapian::Query::Internal::get_length() const
{
if (is_leaf(op)) {
// parameter is wqf.
return parameter;
}
Xapian::termcount len = 0;
subquery_list::const_iterator i;
for (i = subqs.begin(); i != subqs.end(); ++i) {
len += (**i).get_length();
}
return len;
}
/** Private function used to implement get_terms() */
void
Xapian::Query::Internal::accumulate_terms(
vector<pair<string, Xapian::termpos> > &terms) const
{
if (is_leaf(op)) {
// We're a leaf, so just return our term, but skip Query::MatchAllTerms
// (which is Query("")).
if (!tname.empty())
terms.push_back(make_pair(tname, term_pos));
} else {
subquery_list::const_iterator end = subqs.end();
// Not a leaf, concatenate results from all subqueries.
for (subquery_list::const_iterator i = subqs.begin(); i != end; ++i) {
(*i)->accumulate_terms(terms);
}
}
}
struct LessByTermpos {
typedef const pair<string, Xapian::termpos> argtype;
bool operator()(argtype &left, argtype &right) {
if (left.second != right.second) {
return left.second < right.second;
} else {
return left.first < right.first;
}
}
};
Xapian::TermIterator
Xapian::Query::Internal::get_terms() const
{
vector<pair<string, Xapian::termpos> > terms;
accumulate_terms(terms);
sort(terms.begin(), terms.end(), LessByTermpos());
// remove adjacent duplicates, and return an iterator pointing
// to just after the last unique element
vector<pair<string, Xapian::termpos> >::iterator newlast =
unique(terms.begin(), terms.end());
// and remove the rest... (See Stroustrup 18.6.3)
terms.erase(newlast, terms.end());
vector<string> result;
vector<pair<string, Xapian::termpos> >::const_iterator i;
for (i = terms.begin(); i != terms.end(); ++i) {
result.push_back(i->first);
}
return Xapian::TermIterator(new VectorTermList(result.begin(),
result.end()));
}
#ifdef XAPIAN_HAS_REMOTE_BACKEND
// Methods.
class QUnserial {
private:
const char *p;
const char *end;
Xapian::termpos curpos;
const Xapian::Registry & reg;
Xapian::Query::Internal * readquery();
Xapian::Query::Internal * readexternal();
Xapian::Query::Internal * readcompound();
public:
QUnserial(const string & s, const Xapian::Registry & reg_)
: p(s.c_str()), end(p + s.size()), curpos(1), reg(reg_) { }
Xapian::Query::Internal * decode();
};
Xapian::Query::Internal *
QUnserial::decode() {
LOGLINE(UNKNOWN, "QUnserial::decode(" << p << ")");
AutoPtr<Xapian::Query::Internal> qint(readquery());
if (p != end)
throw Xapian::InvalidArgumentError("Bad serialised query");
return qint.release();
}
Xapian::Query::Internal *
QUnserial::readquery() {
if (p == end)
throw Xapian::InvalidArgumentError("Bad serialised query");
switch (*p++) {
case '[': {
size_t length = decode_length(&p, end, true);
string tname(p, length);
p += length;
Xapian::termpos term_pos = curpos;
Xapian::termcount wqf = 1;
if (p != end) {
if (*p == '@') {
++p;
term_pos = decode_length(&p, end, false);
}
if (*p == '#') {
++p;
wqf = decode_length(&p, end, false);
}
}
++curpos;
return new Xapian::Query::Internal(tname, wqf, term_pos);
}
case '!':
return readexternal();
case '(':
return readcompound();
default:
LOGLINE(UNKNOWN, "Can't parse remainder `" << p - 1 << "'");
throw Xapian::InvalidArgumentError("Invalid query string");
}
}
Xapian::Query::Internal *
QUnserial::readexternal()
{
if (p == end)
throw Xapian::InvalidArgumentError("Bad serialised query");
size_t length = decode_length(&p, end, true);
string sourcename(p, length);
const Xapian::PostingSource * source = reg.get_posting_source(sourcename);
if (source == NULL) {
throw Xapian::InvalidArgumentError("PostingSource " + sourcename +
" not registered");
}
p += length;
length = decode_length(&p, end, true);
string sourcedata(p, length);
p += length;
return new Xapian::Query::Internal(source->unserialise(sourcedata), true);
}
static Xapian::Query::Internal *
qint_from_vector(Xapian::Query::op op,
const vector<Xapian::Query::Internal *> & vec,
Xapian::termcount parameter = 0)
{
Xapian::Query::Internal * qint = new Xapian::Query::Internal(op, parameter);
vector<Xapian::Query::Internal *>::const_iterator i;
for (i = vec.begin(); i != vec.end(); i++) {
qint->add_subquery_nocopy(*i);
}
Xapian::Query::Internal * r = qint->end_construction();
// We're only called during unserialisation, so no simplification should
// happen.
AssertEq(r, qint);
return r;
}
static Xapian::Query::Internal *
qint_from_vector(Xapian::Query::op op,
const vector<Xapian::Query::Internal *> & vec,
Xapian::termcount parameter,
double dbl_parameter)
{
Xapian::Query::Internal * qint = new Xapian::Query::Internal(op, parameter);
qint->set_dbl_parameter(dbl_parameter);
vector<Xapian::Query::Internal *>::const_iterator i;
for (i = vec.begin(); i != vec.end(); i++) {
qint->add_subquery_nocopy(*i);
}
Xapian::Query::Internal * r = qint->end_construction();
// We're only called during unserialisation, so no simplification should
// happen.
AssertEq(r, qint);
return r;
}
Xapian::Query::Internal *
QUnserial::readcompound() {
vector<Xapian::Query::Internal *> subqs;
try {
while (true) {
if (p == end)
throw Xapian::InvalidArgumentError("Bad serialised query");
switch (*p++) {
case '[':
--p;
subqs.push_back(readquery());
break;
case '!':
subqs.push_back(readexternal());
break;
case '(': {
subqs.push_back(readcompound());
break;
}
case '&':
return qint_from_vector(Xapian::Query::OP_AND, subqs);
case '|':
return qint_from_vector(Xapian::Query::OP_OR, subqs);
case '%':
return qint_from_vector(Xapian::Query::OP_FILTER, subqs);
case '^':
return qint_from_vector(Xapian::Query::OP_XOR, subqs);
case '+':
return qint_from_vector(Xapian::Query::OP_AND_MAYBE, subqs);
case '-':
return qint_from_vector(Xapian::Query::OP_AND_NOT, subqs);
case '~': {
Xapian::termcount window(decode_length(&p, end, false));
return qint_from_vector(Xapian::Query::OP_NEAR, subqs, window);
}
case '"': {
Xapian::termcount window(decode_length(&p, end, false));
return qint_from_vector(Xapian::Query::OP_PHRASE, subqs, window);
}
case '*': {
Xapian::termcount elite_set_size(decode_length(&p, end, false));
return qint_from_vector(Xapian::Query::OP_ELITE_SET, subqs,
elite_set_size);
}
case ']': {
size_t len = decode_length(&p, end, true);
string start(p, len);
p += len;
len = decode_length(&p, end, true);
string stop(p, len);
p += len;
Xapian::valueno slot(decode_length(&p, end, false));
return new Xapian::Query::Internal(Xapian::Query::OP_VALUE_RANGE, slot,
start, stop);
}
case '}': {
size_t len = decode_length(&p, end, true);
string start(p, len);
p += len;
Xapian::valueno slot(decode_length(&p, end, false));
return new Xapian::Query::Internal(Xapian::Query::OP_VALUE_GE, slot,
start);
}
case '{': {
size_t len = decode_length(&p, end, true);
string start(p, len);
p += len;
Xapian::valueno slot(decode_length(&p, end, false));
return new Xapian::Query::Internal(Xapian::Query::OP_VALUE_LE, slot,
start);
}
case '.': {
double param = unserialise_double(&p, end);
return qint_from_vector(Xapian::Query::OP_SCALE_WEIGHT,
subqs, 0, param);
}
case '=': {
return qint_from_vector(Xapian::Query::OP_SYNONYM, subqs);
}
default:
LOGLINE(UNKNOWN, "Can't parse remainder `" << p - 1 << "'");
throw Xapian::InvalidArgumentError("Invalid query string");
}
}
} catch (...) {
vector<Xapian::Query::Internal *>::iterator i;
for (i = subqs.begin(); i != subqs.end(); i++)
delete *i;
throw;
}
}
Xapian::Query::Internal *
Xapian::Query::Internal::unserialise(const string &s,
const Xapian::Registry & reg)
{
Assert(s.length() > 1);
QUnserial u(s, reg);
Xapian::Query::Internal * qint = u.decode();
AssertEq(s, qint->serialise());
return qint;
}
#else
Xapian::Query::Internal *
Xapian::Query::Internal::unserialise(const string &, const Xapian::Registry &)
{
throw Xapian::InternalError("query serialisation not compiled in");
}
#endif
Xapian::Query::Internal::Internal(const Xapian::Query::Internal ©me)
: Xapian::Internal::RefCntBase(),
op(copyme.op),
subqs(),
parameter(copyme.parameter),
tname(copyme.tname),
str_parameter(copyme.str_parameter),
term_pos(copyme.term_pos),
external_source(NULL),
external_source_owned(false)
{
for (subquery_list::const_iterator i = copyme.subqs.begin();
i != copyme.subqs.end();
++i) {
subqs.push_back(new Xapian::Query::Internal(**i));
}
if (copyme.external_source) {
external_source = copyme.external_source->clone();
if (external_source == NULL) {
external_source = copyme.external_source;
external_source_owned = false;
} else {
external_source_owned = true;
}
}
}
//////////////////////////////////////////
// Methods for making new query objects
Xapian::Query::Internal::Internal(const string & tname_, Xapian::termcount wqf_,
Xapian::termpos term_pos_)
: op(Xapian::Query::Internal::OP_LEAF),
subqs(),
parameter(wqf_),
tname(tname_),
term_pos(term_pos_),
external_source(NULL),
external_source_owned(false)
{
validate_query();
}
Xapian::Query::Internal::Internal(op_t op_, Xapian::termcount parameter_)
: op(op_),
subqs(),
parameter(parameter_),
tname(),
term_pos(0),
external_source(NULL),
external_source_owned(false)
{
if (parameter != 0 && op != OP_PHRASE && op != OP_NEAR && op != OP_ELITE_SET)
throw Xapian::InvalidArgumentError("parameter is only meaningful for OP_NEAR, OP_PHRASE, or OP_ELITE_SET");
}
Xapian::Query::Internal::Internal(op_t op_, Xapian::valueno slot,
const string &begin, const string &end)
: op(op_),
parameter(Xapian::termcount(slot)),
tname(begin),
str_parameter(end),
external_source(NULL),
external_source_owned(false)
{
if (op != OP_VALUE_RANGE)
throw Xapian::InvalidArgumentError("This constructor is only meaningful for OP_VALUE_RANGE");
validate_query();
}
Xapian::Query::Internal::Internal(op_t op_, Xapian::valueno slot,
const std::string &value)
: op(op_),
parameter(Xapian::termcount(slot)),
tname(value),
external_source(NULL),
external_source_owned(false)
{
if (op != OP_VALUE_GE && op != OP_VALUE_LE)
throw Xapian::InvalidArgumentError("This constructor is only meaningful for OP_VALUE_GE or OP_VALUE_LE");
if (op == OP_VALUE_GE && value.empty()) {
// Map '<value> >= ""' to MatchAll.
op = OP_LEAF;
parameter = 1; // wqf
term_pos = 0;
}
validate_query();
}
Xapian::Query::Internal::Internal(PostingSource * external_source_, bool owned)
: op(OP_EXTERNAL_SOURCE), external_source(external_source_),
external_source_owned(owned)
{
Assert(external_source);
}
Xapian::Query::Internal::~Internal()
{
subquery_list::iterator i;
for (i = subqs.begin(); i != subqs.end(); i++) {
delete *i;
}
if (external_source_owned) {
delete external_source;
}
}
Xapian::Query::Internal *
Xapian::Query::Internal::end_construction()
{
LOGCALL_VOID(MATCH, "Xapian::Query::Internal::end_construction", NO_ARGS);
validate_query();
Xapian::Query::Internal * qint = simplify_query();
if (qint) qint->validate_query();
return qint;
}
void
Xapian::Query::Internal::validate_query() const
{
LOGCALL_VOID(MATCH, "Xapian::Query::Internal::validate_query", NO_ARGS);
// Check that the number of subqueries is in acceptable limits for this op
if (subqs.size() < get_min_subqs(op) ||
subqs.size() > get_max_subqs(op)) {
throw Xapian::InvalidArgumentError("Xapian::Query: " + get_op_name(op) +
" requires a minimum of " + str(get_min_subqs(op)) +
" and a maximum of " + str(get_max_subqs(op)) +
" sub queries, had " +
str(subqs.size()) + ".");
}
if (op == OP_SCALE_WEIGHT && get_dbl_parameter() < 0) {
throw Xapian::InvalidArgumentError("Xapian::Query: " + get_op_name(op) + " requires a non-negative parameter.");
}
// Check that the termname is null in a branch query, unless the op
// is OP_VALUE_RANGE or OP_VALUE_GE or OP_VALUE_LE.
Assert(is_leaf(op) ||
op == OP_VALUE_RANGE ||
op == OP_VALUE_GE ||
op == OP_VALUE_LE ||
tname.empty());
}
bool
Xapian::Query::Internal::simplify_matchnothing()
{
subquery_list::iterator sq;
switch (op) {
case OP_PHRASE:
case OP_NEAR:
case OP_AND:
case OP_FILTER:
// Doing an "AND" type operation - if we've got any MatchNothing
// nodes, we match nothing.
for (sq = subqs.begin(); sq != subqs.end(); sq++) {
if (*sq == 0) {
for (sq = subqs.begin(); sq != subqs.end(); sq++)
delete *sq;
subqs.clear();
return true;
}
}
break;
case OP_ELITE_SET:
case OP_OR:
case OP_XOR:
case OP_SYNONYM:
// Doing an "OR" type operation - if we've got any MatchNothing
// subnodes, drop them; except that we mustn't become an empty
// node due to this, so we never drop a MatchNothing subnode
// if it's the only subnode.
sq = subqs.begin();
while (sq != subqs.end() && subqs.size() > 1) {
if (*sq == 0) {
sq = subqs.erase(sq);
} else {
++sq;
}
}
break;
case OP_AND_MAYBE:
case OP_AND_NOT:
// If left hand side is MatchNothing, we match nothing.
// If right hand side is MatchNothing, replace node with LHS.
// So, if either node is MatchNothing, replace node with LHS.
// Easiest way to do this is to remove the right hand node,
// and let simplify_query() perform the replacement of
// the unary operator with its one remaining child.
Assert(subqs.size() == 2);
if (subqs[0] == 0 || subqs[1] == 0) {
sq = subqs.begin();
++sq;
delete *sq;
subqs.erase(sq);
}
break;
case OP_SCALE_WEIGHT:
Assert(subqs.size() == 1);
// We should have already handled OP_SCALE_WEIGHT applied to
// MatchNothing in the relevant constructor.
Assert(subqs[0]);
break;
case OP_LEAF:
// Do nothing.
break;
}
return false;
}
Xapian::Query::Internal *
Xapian::Query::Internal::simplify_query()
{
LOGCALL(MATCH, Xapian::Query::Internal *, "Xapian::Query::Internal::simplify_query", NO_ARGS);
// Simplify any MatchNothing nodes.
if (simplify_matchnothing()) {
return 0;
}
// General simplifications, dependent on operator.
switch (op) {
case OP_LEAF:
return this;
case OP_VALUE_RANGE:
// If the start of the range is greater than the end then we won't
// match anything.
if (tname > str_parameter) return 0;
return this;
case OP_VALUE_GE:
case OP_VALUE_LE:
return this;
case OP_SCALE_WEIGHT:
if (fabs(get_dbl_parameter() - 1.0) > DBL_EPSILON) return this;
// If the multiplier is 1, this node doesn't actually do anything,
// so we leave it to be removed.
break;
case OP_PHRASE: case OP_NEAR:
// Empty and single subquery OP_PHRASE and OP_NEAR get handled
// below.
if (subqs.size() <= 1) break;
// Default to the number of subqueries.
if (!parameter) parameter = subqs.size();
// Flatten out sub queries of a phrase or near operation.
return flatten_subqs();
case OP_ELITE_SET:
if (!parameter) {
// Default to sqrt(number of subqueries), or a minimum of 10.
// Gives a reasonable default.
if (subqs.size() <= 100) {
parameter = 10;
} else {
parameter = Xapian::termcount(ceil(sqrt(double(subqs.size()))));
Assert(parameter > 10);
}
}
break;
case OP_OR: case OP_AND: case OP_XOR: case OP_SYNONYM:
// Remove duplicates if we can.
if (subqs.size() > 1) collapse_subqs();
break;
default:
break;
}
// If we have no subqueries, then we're an empty query.
if (subqs.empty())
return 0;
// Any nodes which are valid with only one subquery can be replaced by
// that solitary subquery.
if (subqs.size() == 1) {
Xapian::Query::Internal * qint = subqs[0];
subqs[0] = 0;
return qint;
}
return this;
}
struct SortPosName {
bool operator()(const Xapian::Query::Internal * left,
const Xapian::Query::Internal * right) const {
Assert(left != 0);
Assert(right != 0);
Assert(is_leaf(left->op));
Assert(is_leaf(right->op));
if (left->term_pos != right->term_pos) {
return left->term_pos < right->term_pos;
} else {
return left->tname < right->tname;
}
}
};
/** Collapse occurrences of a term at the same position into a
* single occurrence with higher wqf.
*/
void
Xapian::Query::Internal::collapse_subqs()
{
Assert(op == OP_OR || op == OP_AND || op == OP_XOR || op == OP_SYNONYM);
typedef set<Xapian::Query::Internal *, SortPosName> subqtable;
subqtable sqtab;
subquery_list::iterator sq = subqs.begin();
while (sq != subqs.end()) {
Assert(*sq != 0);
if (is_leaf((*sq)->op)) {
Assert((*sq)->subqs.empty());
subqtable::iterator s = sqtab.find(*sq);
if (s == sqtab.end()) {
sqtab.insert(*sq);
++sq;
} else {
AssertEq((*s)->tname, (*sq)->tname);
AssertEq((*s)->term_pos, (*sq)->term_pos);
// parameter is wqf.
(*s)->parameter += (*sq)->parameter;
// Rather than incrementing sq, delete the current
// element, as it has been merged into the other
// equivalent term.
delete *sq;
sq = subqs.erase(sq);
}
} else {
++sq;
}
}
}
/// Change, eg, A NEAR (B AND C) to (A NEAR B) AND (A NEAR C)
Xapian::Query::Internal *
Xapian::Query::Internal::flatten_subqs()
{
Assert(op == Xapian::Query::OP_NEAR || op == Xapian::Query::OP_PHRASE);
subquery_list::iterator sq;
for (sq = subqs.begin(); sq != subqs.end(); ++sq) {
if (!is_leaf((*sq)->op)) break;
}
if (sq == subqs.end()) return this;
if ((*sq)->op == Xapian::Query::OP_NEAR ||
(*sq)->op == Xapian::Query::OP_PHRASE) {
// FIXME: A PHRASE (B PHRASE C) -> (A PHRASE B) AND (B PHRASE C)?
throw Xapian::UnimplementedError("Can't use NEAR/PHRASE with a subexpression containing NEAR or PHRASE");
}
AutoPtr<Xapian::Query::Internal> flattenme(*sq);
*sq = 0;
subquery_list::iterator j;
for (j = flattenme->subqs.begin(); j != flattenme->subqs.end(); ++j) {
*sq = *j;
*j = 0;
AutoPtr<Xapian::Query::Internal> newq(new Xapian::Query::Internal(*this));
delete *sq;
*sq = 0;
newq.reset(newq->flatten_subqs());
*j = newq.release();
}
if (flattenme->op == OP_AND ||
flattenme->op == OP_OR ||
flattenme->op == OP_XOR) {
size_t i = flattenme->subqs.size();
do {
--i;
Xapian::Query::Internal * q = flattenme->subqs[i];
if (flattenme->op == q->op) {
subquery_list::iterator k;
for (k = q->subqs.begin(), ++k;
k != q->subqs.end();
++k) {
flattenme->subqs.push_back(0);
flattenme->subqs.back() = *k;
*k = 0;
}
flattenme->subqs[i] = q->subqs[0];
q->subqs.clear();
delete q;
}
} while (i != 0);
}
return flattenme.release();
}
void
Xapian::Query::Internal::add_subquery(const Xapian::Query::Internal * subq)
{
Assert(!is_leaf(op));
if (subq == 0) {
subqs.push_back(0);
} else if (op == subq->op && is_distributable(op)) {
// Distribute the subquery.
for (subquery_list::const_iterator i = subq->subqs.begin();
i != subq->subqs.end(); i++) {
add_subquery(*i);
}
} else {
subqs.push_back(new Xapian::Query::Internal(*subq));
}
}
void
Xapian::Query::Internal::add_subquery_nocopy(Xapian::Query::Internal * subq)
{
Assert(!is_leaf(op));
if (subq == 0) {
subqs.push_back(0);
} else if (op == subq->op && is_distributable(op)) {
// Distribute the subquery.
for (subquery_list::const_iterator i = subq->subqs.begin();
i != subq->subqs.end(); i++) {
add_subquery(*i);
}
delete subq;
} else {
subqs.push_back(subq);
}
}
void
Xapian::Query::Internal::set_dbl_parameter(double dbl_parameter_)
{
// We store the double parameter encoded as a string because
// Xapian::Query::Internal is defined in an external API header and we want
// to avoid any risk of ABI breakage (we suspect it would be OK, but it's
// not risking). FIXME:1.3: rework in 1.3.x series - see ticket #280
str_parameter = serialise_double(dbl_parameter_);
}
double
Xapian::Query::Internal::get_dbl_parameter() const
{
// We store the double parameter encoded as a string because
// Xapian::Query::Internal is defined in an external API header and we want
// to avoid any risk of ABI breakage (we suspect it would be OK, but it's
// not risking). FIXME:1.3: rework in 1.3.x series - see ticket #280
const char * p = str_parameter.data();
const char * end = p + str_parameter.size();
return unserialise_double(&p, end);
}
|