# include/xapian/query.h

Go to the documentation of this file.

    00001
    00004 /* Copyright 1999,2000,2001 BrightStation PLC
    00005  * Copyright 2002 Ananova Ltd
    00006  * Copyright 2003,2004,2005,2006,2007 Olly Betts
    00007  * Copyright 2006 Lemur Consulting Ltd
    00008  *
    00009  * This program is free software; you can redistribute it and/or
    00010  * modify it under the terms of the GNU General Public License as
    00011  * published by the Free Software Foundation; either version 2 of the
    00012  * License, or (at your option) any later version.
    00013  *
    00014  * This program is distributed in the hope that it will be useful,
    00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00017  * GNU General Public License for more details.
    00018  *
    00019  * You should have received a copy of the GNU General Public License
    00020  * along with this program; if not, write to the Free Software
    00021  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
    00022  * USA
    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/types.h>
    00033 #include <xapian/termiterator.h>
    00034 #include <xapian/visibility.h>
    00035
    00036 // FIXME: sort this out so we avoid exposing Xapian::Query::Internal
    00037 // - we need to at present so that the Xapian::Query's template ctors
    00038 // compile.
    00039 class MultiMatch;
    00040 class LocalSubMatch;
    00041 struct SortPosName;
    00042
    00043 namespace Xapian {
    00044
    00049 class XAPIAN_VISIBILITY_DEFAULT Query {
    00050     public:
    00052         class Internal;
    00054         Xapian::Internal::RefCntPtr<Internal> internal;
    00055
    00057         typedef enum {
    00059             OP_AND,
    00060
    00062             OP_OR,
    00063
    00065             OP_AND_NOT,
    00066
    00068             OP_XOR,
    00069
    00071             OP_AND_MAYBE,
    00072
    00074             OP_FILTER,
    00075
    00084             OP_NEAR,
    00085
    00094             OP_PHRASE,
    00095
    00097             OP_VALUE_RANGE,
    00098
    00102             OP_ELITE_SET = 10
    00103         } op;
    00104
    00106         Query(const Query & copyme);
    00107
    00109         Query & operator=(const Query & copyme);
    00110
    00119         Query();
    00120
    00122         ~Query();
    00123
    00125         Query(const std::string & tname_, Xapian::termcount wqf_ = 1,
    00126               Xapian::termpos pos_ = 0);
    00127
    00129         Query(Query::op op_, const Query & left, const Query & right);
    00130
    00132         Query(Query::op op_,
    00133               const std::string & left, const std::string & right);
    00134
    00150         template <class Iterator>
    00151         Query(Query::op op_, Iterator qbegin, Iterator qend,
    00152               Xapian::termcount parameter = 0);
    00153
    00155         Query(Query::op op_, Xapian::Query q);
    00156
    00170         Query(Query::op op_, Xapian::valueno valno,
    00171               const std::string &begin, const std::string &end);
    00172
    00174         static Xapian::Query MatchAll;
    00175
    00177         static Xapian::Query MatchNothing;
    00178
    00183         Xapian::termcount get_length() const;
    00184
    00190         TermIterator get_terms_begin() const;
    00191
    00195         TermIterator get_terms_end() const {
    00196             return TermIterator(NULL);
    00197         }
    00198
    00202         bool empty() const;
    00203
    00207         std::string get_description() const;
    00208
    00209     private:
    00210         void add_subquery(const Query & subq);
    00211         void add_subquery(const Query * subq);
    00212         void add_subquery(const std::string & tname);
    00213         void start_construction(Query::op op_, Xapian::termcount parameter);
    00214         void end_construction();
    00215         void abort_construction();
    00216 };
    00217
    00218 template <class Iterator>
    00219 Query::Query(Query::op op_, Iterator qbegin, Iterator qend, termcount parameter)
    00220     : internal(0)
    00221 {
    00222     try {
    00223         start_construction(op_, parameter);
    00224
    00225         /* Add all the elements */
    00226         while (qbegin != qend) {
    00227             add_subquery(*qbegin);
    00228             ++qbegin;
    00229         }
    00230
    00231         end_construction();
    00232     } catch (...) {
    00233         abort_construction();
    00234         throw;
    00235     }
    00236 }
    00237
    00239 class XAPIAN_VISIBILITY_DEFAULT Query::Internal : public Xapian::Internal::RefCntBase {
    00240     friend class ::MultiMatch;
    00241     friend class ::LocalSubMatch;
    00242     friend struct ::SortPosName;
    00243     public:
    00244         static const int OP_LEAF = -1;
    00245
    00247         typedef std::vector<Internal *> subquery_list;
    00248
    00250         typedef int op_t;
    00251
    00252     private:
    00254         op_t op;
    00255
    00257         subquery_list subqs;
    00258
    00266         Xapian::termcount parameter;
    00267
    00273         std::string tname;
    00274
    00276         std::string str_parameter;
    00277
    00279         Xapian::termpos term_pos;
    00280
    00282         Xapian::termcount wqf;
    00283
    00291         void swap(Query::Internal &other);
    00292
    00294         void initialise_from_copy(const Query::Internal & copyme);
    00295
    00296         void accumulate_terms(
    00297             std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
    00298
    00303         Internal * simplify_query();
    00304
    00310         void validate_query() const;
    00311
    00317         bool simplify_matchnothing();
    00318
    00321         static std::string get_op_name(Xapian::Query::Internal::op_t op);
    00322
    00325         void collapse_subqs();
    00326
    00330         void flatten_subqs();
    00331
    00334         std::string serialise(Xapian::termpos & curpos) const;
    00335
    00336     public:
    00338         Internal(const Query::Internal & copyme);
    00339
    00341         void operator=(const Query::Internal & copyme);
    00342
    00344         explicit Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
    00345                           Xapian::termpos term_pos_ = 0);
    00346
    00348         Internal(op_t op_, Xapian::termcount parameter);
    00349
    00351         Internal(op_t op_, Xapian::valueno valno,
    00352                  const std::string &begin, const std::string &end);
    00353
    00355         ~Internal();
    00356
    00357         static Xapian::Query::Internal * unserialise(const std::string &s);
    00358
    00361         void add_subquery(const Query::Internal * subq);
    00362
    00365         Query::Internal * end_construction();
    00366
    00370         std::string serialise() const {
    00371             Xapian::termpos curpos = 1;
    00372             return serialise(curpos);
    00373         }
    00374
    00378         std::string get_description() const;
    00379
    00387         Xapian::termcount get_parameter() const { return parameter; }
    00388
    00393         Xapian::termcount get_length() const;
    00394
    00400         TermIterator get_terms() const;
    00401 };
    00402
    00403 }
    00404
    00405 #endif /* XAPIAN_INCLUDED_QUERY_H */