~ubuntu-branches/ubuntu/trusty/sphinxsearch/trusty-proposed

« back to all changes in this revision

Viewing changes to src/sphinxquery.h

  • Committer: Bazaar Package Importer
  • Author(s): Radu Spineanu
  • Date: 2009-11-17 22:19:42 UTC
  • Revision ID: james.westby@ubuntu.com-20091117221942-nm751ur701m9vrzt
Tags: upstream-0.9.8.1
ImportĀ upstreamĀ versionĀ 0.9.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// $Id: sphinxquery.h 1164 2008-02-20 00:34:50Z shodan $
 
3
//
 
4
 
 
5
//
 
6
// Copyright (c) 2001-2008, Andrew Aksyonoff. All rights reserved.
 
7
//
 
8
// This program is free software; you can redistribute it and/or modify
 
9
// it under the terms of the GNU General Public License. You should have
 
10
// received a copy of the GPL license along with this program; if you
 
11
// did not, you can find it at http://www.gnu.org/
 
12
//
 
13
 
 
14
#ifndef _sphinxquery_
 
15
#define _sphinxquery_
 
16
 
 
17
#include "sphinx.h"
 
18
 
 
19
//////////////////////////////////////////////////////////////////////////////
 
20
 
 
21
/// Sphinx boolean query expression type
 
22
enum ESphBooleanQueryExpr
 
23
{
 
24
        NODE_UNDEF,
 
25
        NODE_AND,
 
26
        NODE_OR
 
27
};
 
28
 
 
29
 
 
30
/// Sphinx boolean query expression
 
31
struct CSphBooleanQueryExpr
 
32
{
 
33
        ESphBooleanQueryExpr    m_eType;                ///< this node's type
 
34
        CSphString                              m_sWord;                ///< used for simple, ie. 1-word, subexpressions
 
35
        CSphBooleanQueryExpr *  m_pExpr;                ///< used for composite, ie. non-word, subexperssions
 
36
        CSphBooleanQueryExpr *  m_pPrev;                ///< next node in the list
 
37
        CSphBooleanQueryExpr *  m_pNext;                ///< prev node in the list
 
38
        CSphBooleanQueryExpr *  m_pParent;              ///< parent node
 
39
        bool                                    m_bInvert;              ///< whether to invert word/subexpr matching result
 
40
        bool                                    m_bEvaluable;   ///< whether this node evaluates to a document list or can only be used as a filter
 
41
 
 
42
        /// ctor. zeroes out everything
 
43
                                                        CSphBooleanQueryExpr ();
 
44
 
 
45
        /// dtor. automatically kills the child and all siblings to the right
 
46
                                                        ~CSphBooleanQueryExpr ();
 
47
 
 
48
        /// detaches this node from its siblings, parent, and subexpressions
 
49
        void                                    Detach ();
 
50
 
 
51
        /// create a new tail, if i'm the tail
 
52
        CSphBooleanQueryExpr *  NewTail ();
 
53
 
 
54
        /// check if this node is totally empty
 
55
        bool                                    IsNull ();
 
56
 
 
57
        /// check if this node has no siblings
 
58
        bool                                    IsAlone ();
 
59
};
 
60
 
 
61
 
 
62
struct CSphBooleanQuery : ISphNoncopyable
 
63
{
 
64
        CSphString                              m_sParseError;
 
65
        CSphBooleanQueryExpr *  m_pTree;
 
66
 
 
67
        CSphBooleanQuery () : m_pTree ( NULL )  {}
 
68
        ~CSphBooleanQuery ()                                    { SafeDelete ( m_pTree ); }
 
69
};
 
70
 
 
71
//////////////////////////////////////////////////////////////////////////////
 
72
 
 
73
/// extended query word with attached position within atom
 
74
struct CSphExtendedQueryAtomWord
 
75
{
 
76
        CSphString      m_sWord;
 
77
        int                     m_iAtomPos;
 
78
 
 
79
        CSphExtendedQueryAtomWord () : m_iAtomPos ( -1 ) {}
 
80
        CSphExtendedQueryAtomWord ( const char * sWord, int iPos ) : m_sWord ( sWord ), m_iAtomPos ( iPos ) {}
 
81
};
 
82
 
 
83
 
 
84
/// extended query atom
 
85
/// atom is a list of required query words with field and proximity constraints
 
86
struct CSphExtendedQueryAtom
 
87
{
 
88
        CSphVector<CSphExtendedQueryAtomWord>   m_dWords;
 
89
        DWORD           m_uFields;
 
90
        int                     m_iMaxDistance;
 
91
        bool            m_bQuorum;
 
92
 
 
93
        /// default ctor
 
94
        CSphExtendedQueryAtom ()
 
95
                : m_uFields             ( 0xFFFFFFFF )
 
96
                , m_iMaxDistance( -1 )
 
97
                , m_bQuorum             ( false )
 
98
        {}
 
99
 
 
100
        /// default dtor
 
101
        virtual ~CSphExtendedQueryAtom () {}
 
102
 
 
103
        /// clears but does NOT free everything
 
104
        /// NOTE: preserves field ID!
 
105
        virtual void Reset ()
 
106
        {
 
107
                m_dWords.Reset ();
 
108
                m_iMaxDistance = -1;
 
109
        }
 
110
 
 
111
        /// check if we're empty
 
112
        bool IsEmpty () const
 
113
        {
 
114
                return m_dWords.GetLength()==0;
 
115
        }
 
116
};
 
117
 
 
118
 
 
119
/// extended query node
 
120
/// plain nodes are just an atom
 
121
/// non-plain nodes are a logical function over children nodes
 
122
struct CSphExtendedQueryNode : public ISphNoncopyable
 
123
{
 
124
        CSphExtendedQueryNode *                                 m_pParent;              ///< my parent node (NULL for root ones)
 
125
 
 
126
        CSphExtendedQueryAtom                                   m_tAtom;                ///< plain node atom
 
127
        bool                                                                    m_bAny;                 ///< whether to match any or all children (ie. OR or AND)
 
128
        CSphVector<CSphExtendedQueryNode*>              m_dChildren;    ///< non-plain node children
 
129
 
 
130
public:
 
131
        /// ctor
 
132
        CSphExtendedQueryNode ()
 
133
                : m_pParent ( NULL )
 
134
                , m_bAny ( false )
 
135
        {}
 
136
 
 
137
        /// dtor
 
138
        ~CSphExtendedQueryNode ()
 
139
        {
 
140
                ARRAY_FOREACH ( i, m_dChildren )
 
141
                        SafeDelete ( m_dChildren[i] );
 
142
        }
 
143
 
 
144
        /// check if i'm empty
 
145
        bool IsEmpty () const
 
146
        {
 
147
                assert ( m_tAtom.IsEmpty() || m_dChildren.GetLength()==0 );
 
148
                return m_tAtom.IsEmpty() && ( m_dChildren.GetLength()==0 );
 
149
        }
 
150
 
 
151
        /// check if i'm plain
 
152
        bool IsPlain () const
 
153
        {
 
154
                return m_dChildren.GetLength()==0;
 
155
        }
 
156
 
 
157
        /// merge in expr
 
158
        /// WARNING! pNew contents may no longer be used, and pointer gets set to NULL
 
159
        void Submit ( CSphExtendedQueryNode * & pNew, bool bAny );
 
160
 
 
161
        /// make a sublevel from this node contents and given node
 
162
        /// WARNING! pNew contents may no longer be used, and pointer gets set to NULL
 
163
        void Sublevelize ( CSphExtendedQueryNode * & pNew, bool bAny );
 
164
};
 
165
 
 
166
 
 
167
/// extended query
 
168
struct CSphExtendedQuery : public ISphNoncopyable
 
169
{
 
170
        CSphString                                      m_sParseError;
 
171
        CSphString                                      m_sParseWarning;
 
172
        CSphExtendedQueryNode *         m_pAccept;
 
173
        CSphExtendedQueryNode *         m_pReject;
 
174
 
 
175
        /// ctor
 
176
        CSphExtendedQuery ()
 
177
        {
 
178
                m_pAccept = new CSphExtendedQueryNode ();
 
179
                m_pReject = new CSphExtendedQueryNode ();
 
180
        }
 
181
 
 
182
        /// dtor
 
183
        ~CSphExtendedQuery ()
 
184
        {
 
185
                SafeDelete ( m_pAccept );
 
186
                SafeDelete ( m_pReject );
 
187
        }
 
188
};
 
189
 
 
190
//////////////////////////////////////////////////////////////////////////////
 
191
 
 
192
/// parses the query and returns the resulting tree
 
193
/// return false and fills tQuery.m_sParseError on error
 
194
/// WARNING, parsed tree might be NULL (eg. if query was empty)
 
195
bool    sphParseBooleanQuery ( CSphBooleanQuery & tQuery, const char * sQuery, const ISphTokenizer * pTokenizer );
 
196
 
 
197
/// parses the query and returns the resulting tree
 
198
/// return false and fills tQuery.m_sParseError on error
 
199
/// WARNING, parsed tree might be NULL (eg. if query was empty)
 
200
bool    sphParseExtendedQuery ( CSphExtendedQuery & tQuery, const char * sQuery, const ISphTokenizer * pTokenizer, const CSphSchema * pSchema, CSphDict * pDict );
 
201
 
 
202
#endif // _sphinxquery_
 
203
 
 
204
//
 
205
// $Id: sphinxquery.h 1164 2008-02-20 00:34:50Z shodan $
 
206
//