# 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 Olly Betts
    00007  *
    00008  * This program is free software; you can redistribute it and/or
    00009  * modify it under the terms of the GNU General Public License as
    00010  * published by the Free Software Foundation; either version 2 of the
    00011  * License, or (at your option) any later version.
    00012  *
    00013  * This program is distributed in the hope that it will be useful,
    00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00016  * GNU General Public License for more details.
    00017  *
    00018  * You should have received a copy of the GNU General Public License
    00019  * along with this program; if not, write to the Free Software
    00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
    00021  * USA
    00022  */
    00023
    00024 #ifndef XAPIAN_INCLUDED_QUERY_H
    00025 #define XAPIAN_INCLUDED_QUERY_H
    00026
    00027 #include <string>
    00028 #include <vector>
    00029
    00030 #include <xapian/base.h>
    00031 #include <xapian/types.h>
    00032 #include <xapian/termiterator.h>
    00033
    00034 // FIXME: sort this out so we avoid exposing Xapian::Query::Internal
    00035 // - we need to at present so that the Xapian::Query's template ctors
    00036 // compile.
    00037 class MultiMatch;
    00038 class LocalSubMatch;
    00039 struct SortPosName;
    00040
    00041 namespace Xapian {
    00042
    00047 class Query {
    00048     public:
    00050         class Internal;
    00052         Xapian::Internal::RefCntPtr<Internal> internal;
    00053
    00055         typedef enum {
    00057             OP_AND,
    00058
    00060             OP_OR,
    00061
    00063             OP_AND_NOT,
    00064
    00066             OP_XOR,
    00067
    00069             OP_AND_MAYBE,
    00070
    00072             OP_FILTER,
    00073
    00082             OP_NEAR,
    00083
    00092             OP_PHRASE,
    00093
    00097             OP_ELITE_SET = 10
    00098         } op;
    00099
    00101         Query(const Query & copyme);
    00102
    00104         Query & operator=(const Query & copyme);
    00105
    00114         Query();
    00115
    00117         ~Query();
    00118
    00120         Query(const std::string & tname_, Xapian::termcount wqf_ = 1,
    00121               Xapian::termpos pos_ = 0);
    00122
    00124         Query(Query::op op_, const Query & left, const Query & right);
    00125
    00127         Query(Query::op op_,
    00128               const std::string & left, const std::string & right);
    00129
    00145         template <class Iterator>
    00146         Query(Query::op op_, Iterator qbegin, Iterator qend,
    00147               Xapian::termcount parameter = 0);
    00148
    00150         Query(Query::op op_, Xapian::Query q);
    00151
    00156         Xapian::termcount get_length() const;
    00157
    00163         TermIterator get_terms_begin() const;
    00164
    00168         TermIterator get_terms_end() const {
    00169             return TermIterator(NULL);
    00170         }
    00171
    00175         bool empty() const;
    00176
    00178         XAPIAN_DEPRECATED(bool is_empty() const);
    00179
    00183         std::string get_description() const;
    00184
    00185     private:
    00186         void add_subquery(const Query & subq);
    00187         void add_subquery(const Query * subq);
    00188         void add_subquery(const std::string & tname);
    00189         void start_construction(Query::op op_, Xapian::termcount parameter);
    00190         void end_construction();
    00191         void abort_construction();
    00192 };
    00193
    00194 template <class Iterator>
    00195 Query::Query(Query::op op_, Iterator qbegin, Iterator qend, termcount parameter)
    00196     : internal(0)
    00197 {
    00198     try {
    00199         start_construction(op_, parameter);
    00200
    00201         /* Add all the elements */
    00202         while (qbegin != qend) {
    00203             add_subquery(*qbegin);
    00204             ++qbegin;
    00205         }
    00206
    00207         end_construction();
    00208     } catch (...) {
    00209         abort_construction();
    00210         throw;
    00211     }
    00212 }
    00213
    00215 class Query::Internal : public Xapian::Internal::RefCntBase {
    00216     friend class ::MultiMatch;
    00217     friend class ::LocalSubMatch;
    00218     friend struct ::SortPosName;
    00219     public:
    00220         static const int OP_LEAF = -1;
    00221
    00223         typedef std::vector<Internal *> subquery_list;
    00224
    00226         typedef int op_t;
    00227
    00228     private:
    00230         op_t op;
    00231
    00233         subquery_list subqs;
    00234
    00240         Xapian::termcount parameter;
    00241
    00243         std::string tname;
    00244
    00246         Xapian::termpos term_pos;
    00247
    00249         Xapian::termcount wqf;
    00250
    00258         void swap(Query::Internal &other);
    00259
    00261         void initialise_from_copy(const Query::Internal & copyme);
    00262
    00263         void accumulate_terms(
    00264             std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
    00265
    00270         Internal * simplify_query();
    00271
    00276         void prevalidate_query() const;
    00277
    00285         void validate_query() const;
    00286
    00289         static std::string get_op_name(Xapian::Query::Internal::op_t op);
    00290
    00293         void collapse_subqs();
    00294
    00298         void flatten_subqs();
    00299
    00300     public:
    00302         Internal(const Query::Internal & copyme);
    00303
    00305         void operator=(const Query::Internal & copyme);
    00306
    00308         Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
    00309                  Xapian::termpos term_pos_ = 0);
    00310
    00312         Internal(op_t op_, Xapian::termcount parameter);
    00313
    00315         ~Internal();
    00316
    00317         static Xapian::Query::Internal * unserialise(const std::string &s);
    00318
    00321         void add_subquery(const Query::Internal & subq);
    00322
    00325         Query::Internal * end_construction();
    00326
    00330         std::string serialise() const;
    00331
    00335         std::string get_description() const;
    00336
    00341         Xapian::termcount get_length() const;
    00342
    00348         TermIterator get_terms() const;
    00349 };
    00350
    00351 }
    00352
    00353 #endif /* XAPIAN_INCLUDED_QUERY_H */