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 
00158         Query(Query::op op_, Xapian::valueno valno,
00159               const std::string &begin, const std::string &end);
00160 
00162         static Xapian::Query MatchAll;
00163 
00165         static Xapian::Query MatchNothing;
00166 
00171         Xapian::termcount get_length() const;
00172 
00178         TermIterator get_terms_begin() const;
00179 
00183         TermIterator get_terms_end() const {
00184             return TermIterator(NULL);
00185         }
00186 
00190         bool empty() const;
00191 
00195         std::string get_description() const;
00196 
00197     private:
00198         void add_subquery(const Query & subq);
00199         void add_subquery(const Query * subq);
00200         void add_subquery(const std::string & tname);
00201         void start_construction(Query::op op_, Xapian::termcount parameter);
00202         void end_construction();
00203         void abort_construction();
00204 };
00205 
00206 template <class Iterator>
00207 Query::Query(Query::op op_, Iterator qbegin, Iterator qend, termcount parameter)
00208     : internal(0)
00209 {
00210     try {
00211         start_construction(op_, parameter);
00212 
00213         /* Add all the elements */
00214         while (qbegin != qend) {
00215             add_subquery(*qbegin);
00216             ++qbegin;
00217         }
00218 
00219         end_construction();
00220     } catch (...) {
00221         abort_construction();
00222         throw;
00223     }
00224 }
00225 
00227 class XAPIAN_VISIBILITY_DEFAULT Query::Internal : public Xapian::Internal::RefCntBase {
00228     friend class ::MultiMatch;
00229     friend class ::LocalSubMatch;
00230     friend struct ::SortPosName;
00231     public:
00232         static const int OP_LEAF = -1;
00233 
00235         typedef std::vector<Internal *> subquery_list;
00236 
00238         typedef int op_t;
00239 
00240     private:
00242         op_t op;
00243 
00245         subquery_list subqs;
00246 
00254         Xapian::termcount parameter;
00255 
00261         std::string tname;
00262 
00264         std::string str_parameter;
00265 
00267         Xapian::termpos term_pos;
00268 
00270         Xapian::termcount wqf;
00271 
00279         void swap(Query::Internal &other);
00280 
00282         void initialise_from_copy(const Query::Internal & copyme);
00283 
00284         void accumulate_terms(
00285             std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
00286 
00291         Internal * simplify_query();
00292 
00298         void validate_query() const;
00299 
00305         bool simplify_matchnothing();
00306 
00309         static std::string get_op_name(Xapian::Query::Internal::op_t op);
00310 
00313         void collapse_subqs();
00314 
00318         void flatten_subqs();
00319 
00322         std::string serialise(Xapian::termpos & curpos) const;
00323 
00324     public:
00326         Internal(const Query::Internal & copyme);
00327 
00329         void operator=(const Query::Internal & copyme);
00330 
00332         explicit Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
00333                           Xapian::termpos term_pos_ = 0);
00334 
00336         Internal(op_t op_, Xapian::termcount parameter);
00337 
00339         Internal(op_t op_, Xapian::valueno valno,
00340                  const std::string &begin, const std::string &end);
00341 
00343         ~Internal();
00344 
00345         static Xapian::Query::Internal * unserialise(const std::string &s);
00346 
00349         void add_subquery(const Query::Internal * subq);
00350 
00353         Query::Internal * end_construction();
00354 
00358         std::string serialise() const {
00359             Xapian::termpos curpos = 1;
00360             return serialise(curpos);
00361         }
00362 
00366         std::string get_description() const;
00367 
00372         Xapian::termcount get_length() const;
00373 
00379         TermIterator get_terms() const;
00380 };
00381 
00382 }
00383 
00384 #endif /* XAPIAN_INCLUDED_QUERY_H */

Documentation for Xapian (version 1.0.0).
Generated on 18 May 2007 by Doxygen 1.4.6.