00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef XAPIAN_INCLUDED_QUERY_H
00026 #define XAPIAN_INCLUDED_QUERY_H
00027
00028 #include <string>
00029 #include <vector>
00030
00031 #include <xapian/base.h>
00032 #include <xapian/deprecated.h>
00033 #include <xapian/types.h>
00034 #include <xapian/termiterator.h>
00035 #include <xapian/visibility.h>
00036
00037
00038
00039
00040 class LocalSubMatch;
00041 class MultiMatch;
00042 class QueryOptimiser;
00043 struct SortPosName;
00044
00045 namespace Xapian {
00046
00051 class XAPIAN_VISIBILITY_DEFAULT Query {
00052 public:
00054 class Internal;
00056 Xapian::Internal::RefCntPtr<Internal> internal;
00057
00059 typedef enum {
00061 OP_AND,
00062
00064 OP_OR,
00065
00067 OP_AND_NOT,
00068
00070 OP_XOR,
00071
00073 OP_AND_MAYBE,
00074
00076 OP_FILTER,
00077
00086 OP_NEAR,
00087
00096 OP_PHRASE,
00097
00099 OP_VALUE_RANGE,
00100
00109 OP_SCALE_WEIGHT,
00110
00114 OP_ELITE_SET,
00115
00117 OP_VALUE_GE,
00118
00120 OP_VALUE_LE
00121 } op;
00122
00124 Query(const Query & copyme);
00125
00127 Query & operator=(const Query & copyme);
00128
00137 Query();
00138
00140 ~Query();
00141
00143 Query(const std::string & tname_, Xapian::termcount wqf_ = 1,
00144 Xapian::termpos pos_ = 0);
00145
00147 Query(Query::op op_, const Query & left, const Query & right);
00148
00150 Query(Query::op op_,
00151 const std::string & left, const std::string & right);
00152
00168 template <class Iterator>
00169 Query(Query::op op_, Iterator qbegin, Iterator qend,
00170 Xapian::termcount parameter = 0);
00171
00178 XAPIAN_DEPRECATED(Query(Query::op op_, Xapian::Query q));
00179
00183 Query(Query::op op_, Xapian::Query q, double parameter);
00184
00198 Query(Query::op op_, Xapian::valueno valno,
00199 const std::string &begin, const std::string &end);
00200
00212 Query(Query::op op_, Xapian::valueno valno, const std::string &value);
00213
00215 static Xapian::Query MatchAll;
00216
00218 static Xapian::Query MatchNothing;
00219
00224 Xapian::termcount get_length() const;
00225
00231 TermIterator get_terms_begin() const;
00232
00236 TermIterator get_terms_end() const {
00237 return TermIterator(NULL);
00238 }
00239
00243 bool empty() const;
00244
00246 std::string get_description() const;
00247
00248 private:
00249 void add_subquery(const Query & subq);
00250 void add_subquery(const Query * subq);
00251 void add_subquery(const std::string & tname);
00252 void start_construction(Query::op op_, Xapian::termcount parameter);
00253 void end_construction();
00254 void abort_construction();
00255 };
00256
00257 template <class Iterator>
00258 Query::Query(Query::op op_, Iterator qbegin, Iterator qend, termcount parameter)
00259 : internal(0)
00260 {
00261 try {
00262 start_construction(op_, parameter);
00263
00264
00265 while (qbegin != qend) {
00266 add_subquery(*qbegin);
00267 ++qbegin;
00268 }
00269
00270 end_construction();
00271 } catch (...) {
00272 abort_construction();
00273 throw;
00274 }
00275 }
00276
00278 class XAPIAN_VISIBILITY_DEFAULT Query::Internal : public Xapian::Internal::RefCntBase {
00279 friend class ::LocalSubMatch;
00280 friend class ::MultiMatch;
00281 friend class ::QueryOptimiser;
00282 friend struct ::SortPosName;
00283 friend class Query;
00284 public:
00285 static const int OP_LEAF = -1;
00286
00288 typedef std::vector<Internal *> subquery_list;
00289
00291 typedef int op_t;
00292
00293 private:
00295 Xapian::Query::Internal::op_t op;
00296
00298 subquery_list subqs;
00299
00307 Xapian::termcount parameter;
00308
00315 std::string tname;
00316
00318 std::string str_parameter;
00319
00321 Xapian::termpos term_pos;
00322
00324 Xapian::termcount wqf;
00325
00327 void initialise_from_copy(const Query::Internal & copyme);
00328
00329 void accumulate_terms(
00330 std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
00331
00336 Internal * simplify_query();
00337
00343 void validate_query() const;
00344
00350 bool simplify_matchnothing();
00351
00354 static std::string get_op_name(Xapian::Query::Internal::op_t op);
00355
00358 void collapse_subqs();
00359
00363 Xapian::Query::Internal * flatten_subqs();
00364
00367 std::string serialise(Xapian::termpos & curpos) const;
00368
00369 public:
00371 Internal(const Query::Internal & copyme);
00372
00374 void operator=(const Query::Internal & copyme);
00375
00377 explicit Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
00378 Xapian::termpos term_pos_ = 0);
00379
00381 Internal(op_t op_, Xapian::termcount parameter);
00382
00384 Internal(op_t op_, Xapian::valueno valno,
00385 const std::string &begin, const std::string &end);
00386
00389 Internal(op_t op_, Xapian::valueno valno, const std::string &value);
00390
00392 ~Internal();
00393
00394 static Xapian::Query::Internal * unserialise(const std::string &s);
00395
00397 void add_subquery(const Query::Internal * subq);
00398
00403 void add_subquery_nocopy(Query::Internal * subq);
00404
00405 void set_dbl_parameter(double dbl_parameter_);
00406
00407 double get_dbl_parameter() const;
00408
00411 Query::Internal * end_construction();
00412
00416 std::string serialise() const {
00417 Xapian::termpos curpos = 1;
00418 return serialise(curpos);
00419 }
00420
00422 std::string get_description() const;
00423
00431 Xapian::termcount get_parameter() const { return parameter; }
00432
00437 Xapian::termcount get_length() const;
00438
00444 TermIterator get_terms() const;
00445 };
00446
00447 }
00448
00449 #endif