Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

/include/xapian/query.h

Go to the documentation of this file.
00001 
00004 /* ----START-LICENCE----
00005  * Copyright 1999,2000,2001 BrightStation PLC
00006  * Copyright 2002 Ananova Ltd
00007  * Copyright 2003,2004 Olly Betts
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022  * USA
00023  * -----END-LICENCE-----
00024  */
00025 
00026 #ifndef XAPIAN_INCLUDED_QUERY_H
00027 #define XAPIAN_INCLUDED_QUERY_H
00028 
00029 #include <string>
00030 #include <vector>
00031 
00032 #include <xapian/base.h>
00033 #include <xapian/types.h>
00034 
00035 // FIXME: sort this out so we avoid exposing Xapian::Query::Internal
00036 // - we need to at present so that the Xapian::Query's template ctors
00037 // compile.
00038 class MultiMatch;
00039 class LocalSubMatch;
00040 class SortPosName;
00041 
00042 namespace Xapian {
00043 
00044 class TermIterator;
00045 
00050 class Query {
00051     public:
00053         class Internal;
00055         Xapian::Internal::RefCntPtr<Internal> internal;
00056 
00058         typedef enum {
00060             OP_AND,
00061 
00063             OP_OR,
00064 
00066             OP_AND_NOT,
00067 
00069             OP_XOR,
00070 
00072             OP_AND_MAYBE,
00073 
00075             OP_FILTER,
00076 
00085             OP_NEAR,
00086 
00095             OP_PHRASE,
00096 
00103             OP_WEIGHT_CUTOFF,
00104 
00108             OP_ELITE_SET
00109         } op;
00110 
00112         Query(const Query & copyme);
00113 
00115         Query & operator=(const Query & copyme);
00116 
00125         Query();
00126 
00128         ~Query();
00129 
00131         Query(const std::string & tname_, Xapian::termcount wqf_ = 1,
00132               Xapian::termpos pos_ = 0);
00133 
00135         Query(Query::op op_, const Query & left, const Query & right);
00136 
00137         /* A query consisting of two subquery pointers, opp-ed together. */
00138         // Don't have this because vector iterators are often implemented as
00139         // simple pointers, so this would override the template class and
00140         // produce unexpected results.  Only plausible solution we can think
00141         // of so far is to change to using construction methods (eg,
00142         // static Query::create_vector(op, begin, end) and
00143         // static Query::create_pair(op, begin, end)
00144         //Query(Query::op op_, const Query * left, const Query * right);
00145 
00147         Query(Query::op op_,
00148               const std::string & left, const std::string & right);
00149 
00163         template <class Iterator>
00164         Query(Query::op op_, Iterator qbegin, Iterator qend);
00165 
00171         template <class SubQ> Query(Query::op op_, SubQ q);
00172 
00175         void set_window(Xapian::termpos window);
00176 
00179         void set_cutoff(Xapian::weight cutoff);
00180 
00182         void set_elite_set_size(Xapian::termcount size);
00183 
00188         Xapian::termcount get_length() const;
00189 
00195         Xapian::termcount set_length(Xapian::termcount qlen);
00196 
00202         TermIterator get_terms_begin() const;
00203 
00207         TermIterator get_terms_end() const;
00208 
00212         bool is_empty() const;
00213                 
00217         std::string get_description() const;
00218 
00219     private:
00220         void add_subquery(const Query & subq);
00221         void add_subquery(const Query * subq);
00222         void add_subquery(const std::string & tname);
00223         void start_construction(Query::op op_);
00224         void end_construction();
00225         void abort_construction();
00226 };
00227 
00228 template <class Iterator>
00229 Query::Query(Query::op op_, Iterator qbegin, Iterator qend) : internal(0)
00230 {
00231     try {
00232         start_construction(op_);
00233 
00234         /* Add all the elements */
00235         while (qbegin != qend) {
00236             add_subquery(*qbegin);
00237             ++qbegin;
00238         }
00239 
00240         end_construction();
00241     } catch (...) {
00242         abort_construction();
00243         throw;
00244     }
00245 }
00246 
00247 template <class SubQ>
00248 Query::Query(Query::op op_, SubQ q) : internal(0)
00249 {
00250     try {
00251         start_construction(op_);
00252         add_subquery(q);
00253         end_construction();
00254     } catch (...) {
00255         abort_construction();
00256         throw;
00257     }
00258 }
00259 
00261 class Query::Internal : public Xapian::Internal::RefCntBase {
00262     friend class ::MultiMatch;
00263     friend class ::LocalSubMatch;
00264     friend class ::SortPosName;
00265     public:
00266         static const int OP_LEAF = -1;
00267 
00269         typedef std::vector<Internal *> subquery_list;
00270 
00272         typedef int op_t;
00273 
00274     private:
00276         op_t op;
00277 
00279         subquery_list subqs;
00280         
00282         Xapian::termcount qlen;
00283 
00287         Xapian::termpos window;
00288 
00291         double cutoff;
00292 
00295         Xapian::termcount elite_set_size;
00296 
00298         std::string tname;
00299 
00301         Xapian::termpos term_pos;
00302 
00304         Xapian::termcount wqf;
00305 
00313         void swap(Query::Internal &other);
00314 
00316         void initialise_from_copy(const Query::Internal & copyme);
00317 
00318         void accumulate_terms(
00319             std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
00320 
00325         Internal * simplify_query();
00326 
00331         void prevalidate_query() const;
00332 
00340         void validate_query() const;
00341 
00344         static std::string get_op_name(Xapian::Query::Internal::op_t op);
00345 
00348         void collapse_subqs();
00349 
00353         void flatten_subqs();
00354 
00355     public:
00357         Internal(const Query::Internal & copyme);
00358 
00360         void operator=(const Query::Internal & copyme);
00361 
00363         Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
00364                  Xapian::termpos term_pos_ = 0);
00365 
00367         Internal(op_t op_);
00368 
00370         ~Internal();
00371 
00372         static Xapian::Query::Internal * unserialise(const std::string &s);
00373 
00376         void add_subquery(const Query::Internal & subq);
00377 
00380         Query::Internal * end_construction();
00381 
00385         std::string serialise() const;
00386 
00390         std::string get_description() const;
00391 
00393         void set_window(Xapian::termpos window);
00394 
00396         void set_cutoff(double cutoff);
00397 
00399         void set_elite_set_size(Xapian::termcount size);
00400 
00405         Xapian::termcount get_length() const { return qlen; }
00406 
00412         Xapian::termcount set_length(Xapian::termcount qlen_);
00413 
00419         TermIterator get_terms() const;
00420 };
00421 
00422 }
00423 
00424 #endif /* XAPIAN_INCLUDED_QUERY_H */

Documentation for Xapian (version 0.8.0).
Generated on 20 Apr 2004 by Doxygen 1.2.15.