]> granicus.if.org Git - postgresql/blob - src/include/tsearch/ts_utils.h
93da9cb4a9d2bcd2cac74596390f89ea98097d0f
[postgresql] / src / include / tsearch / ts_utils.h
1 /*-------------------------------------------------------------------------
2  *
3  * ts_utils.h
4  *        helper utilities for tsearch
5  *
6  * Copyright (c) 1998-2007, PostgreSQL Global Development Group
7  *
8  * $PostgreSQL: pgsql/src/include/tsearch/ts_utils.h,v 1.4 2007/09/10 12:36:41 teodor Exp $
9  *
10  *-------------------------------------------------------------------------
11  */
12 #ifndef _PG_TS_UTILS_H_
13 #define _PG_TS_UTILS_H_
14
15 #include "tsearch/ts_type.h"
16 #include "tsearch/ts_public.h"
17 #include "nodes/pg_list.h"
18
19 /*
20  * Common parse definitions for tsvector and tsquery
21  */
22
23 /* tsvector parser support. */
24
25 struct TSVectorParseStateData;
26 typedef struct TSVectorParseStateData *TSVectorParseState;
27
28 extern TSVectorParseState init_tsvector_parser(char *input, bool oprisdelim);
29 extern void reset_tsvector_parser(TSVectorParseState state, char *input);
30 extern bool gettoken_tsvector(TSVectorParseState state, 
31                                                           char **token, int *len,
32                                                           WordEntryPos **pos, int *poslen,
33                                                           char **endptr);
34 extern void close_tsvector_parser(TSVectorParseState state);
35
36 /* parse_tsquery */
37
38 struct TSQueryParserStateData;  /* private in backend/utils/adt/tsquery.c */
39 typedef struct TSQueryParserStateData *TSQueryParserState;
40
41 typedef void (*PushFunction)(Datum opaque, TSQueryParserState state, 
42                                 char *token, int tokenlen, 
43                                 int2 tokenweights /* bitmap as described in QueryOperand struct */ );
44
45 extern TSQuery parse_tsquery(char *buf,
46                           PushFunction pushval,
47                           Datum opaque, bool isplain);
48
49 /* Functions for use by PushFunction implementations */
50 extern void pushValue(TSQueryParserState state,
51                          char *strval, int lenval, int2 weight);
52 extern void pushStop(TSQueryParserState state);
53 extern void pushOperator(TSQueryParserState state, int8 operator);
54
55 /*
56  * parse plain text and lexize words
57  */
58 typedef struct
59 {
60         uint16          len;
61         uint16          nvariant;
62         union
63         {
64                 uint16          pos;
65                 /*
66                  * When apos array is used, apos[0] is the number of elements
67                  * in the array (excluding apos[0]), and alen is the allocated
68                  * size of the array.
69                  */
70                 uint16     *apos;
71         }                       pos;
72         char       *word;
73         uint32          alen;
74 } ParsedWord;
75
76 typedef struct
77 {
78         ParsedWord *words;
79         int4            lenwords;
80         int4            curwords;
81         int4            pos;
82 } ParsedText;
83
84 extern void parsetext(Oid cfgId, ParsedText * prs, char *buf, int4 buflen);
85
86 /*
87  * headline framework, flow in common to generate:
88  *      1 parse text with hlparsetext
89  *      2 parser-specific function to find part
90  *      3 generateHeadline to generate result text
91  */
92
93 extern void hlparsetext(Oid cfgId, HeadlineParsedText * prs, TSQuery query,
94                         char *buf, int4 buflen);
95 extern text *generateHeadline(HeadlineParsedText * prs);
96
97 /*
98  * Common check function for tsvector @@ tsquery
99  */
100
101 extern bool TS_execute(QueryItem * curitem, void *checkval, bool calcnot,
102                    bool (*chkcond) (void *checkval, QueryOperand * val));
103
104 /*
105  * Useful conversion macros
106  */
107 #define TextPGetCString(t) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(t)))
108 #define CStringGetTextP(c) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(c)))
109
110 /*
111  * to_ts* - text transformation to tsvector, tsquery
112  */
113 extern TSVector make_tsvector(ParsedText *prs);
114
115 extern Datum to_tsvector_byid(PG_FUNCTION_ARGS);
116 extern Datum to_tsvector(PG_FUNCTION_ARGS);
117 extern Datum to_tsquery_byid(PG_FUNCTION_ARGS);
118 extern Datum to_tsquery(PG_FUNCTION_ARGS);
119 extern Datum plainto_tsquery_byid(PG_FUNCTION_ARGS);
120 extern Datum plainto_tsquery(PG_FUNCTION_ARGS);
121
122 /*
123  * GiST support function
124  */
125
126 extern Datum gtsvector_compress(PG_FUNCTION_ARGS);
127 extern Datum gtsvector_decompress(PG_FUNCTION_ARGS);
128 extern Datum gtsvector_consistent(PG_FUNCTION_ARGS);
129 extern Datum gtsvector_union(PG_FUNCTION_ARGS);
130 extern Datum gtsvector_same(PG_FUNCTION_ARGS);
131 extern Datum gtsvector_penalty(PG_FUNCTION_ARGS);
132 extern Datum gtsvector_picksplit(PG_FUNCTION_ARGS);
133
134 /*
135  * IO functions for pseudotype gtsvector
136  * used internally in tsvector GiST opclass
137  */
138 extern Datum gtsvectorin(PG_FUNCTION_ARGS);
139 extern Datum gtsvectorout(PG_FUNCTION_ARGS);
140
141 /*
142  * GIN support function
143  */
144
145 extern Datum gin_extract_tsvector(PG_FUNCTION_ARGS);
146 extern Datum gin_extract_query(PG_FUNCTION_ARGS);
147 extern Datum gin_ts_consistent(PG_FUNCTION_ARGS);
148
149 /*
150  * Possible strategy numbers for indexes
151  *        TSearchStrategyNumber  - (tsvector|text) @@ tsquery
152  *        TSearchWithClassStrategyNumber  - tsvector @@@ tsquery
153  */
154 #define TSearchStrategyNumber                   1
155 #define TSearchWithClassStrategyNumber  2
156
157 /*
158  * TSQuery Utilities
159  */
160 extern QueryItem *clean_NOT(QueryItem * ptr, int4 *len);
161 extern QueryItem *clean_fakeval(QueryItem * ptr, int4 *len);
162
163 typedef struct QTNode
164 {
165         QueryItem  *valnode;
166         uint32          flags;
167         int4            nchild;
168         char       *word;
169         uint32          sign;
170         struct QTNode **child;
171 } QTNode;
172
173 #define QTN_NEEDFREE    0x01
174 #define QTN_NOCHANGE    0x02
175 #define QTN_WORDFREE    0x04
176
177 typedef uint64 TSQuerySign;
178
179 #define TSQS_SIGLEN  (sizeof(TSQuerySign)*BITS_PER_BYTE)
180
181
182 extern QTNode *QT2QTN(QueryItem * in, char *operand);
183 extern TSQuery QTN2QT(QTNode *in);
184 extern void QTNFree(QTNode * in);
185 extern void QTNSort(QTNode * in);
186 extern void QTNTernary(QTNode * in);
187 extern void QTNBinary(QTNode * in);
188 extern int      QTNodeCompare(QTNode * an, QTNode * bn);
189 extern QTNode *QTNCopy(QTNode *in);
190 extern bool QTNEq(QTNode * a, QTNode * b);
191 extern TSQuerySign makeTSQuerySign(TSQuery a);
192
193 /*
194  * TSQuery GiST support
195  */
196 extern Datum gtsquery_compress(PG_FUNCTION_ARGS);
197 extern Datum gtsquery_decompress(PG_FUNCTION_ARGS);
198 extern Datum gtsquery_consistent(PG_FUNCTION_ARGS);
199 extern Datum gtsquery_union(PG_FUNCTION_ARGS);
200 extern Datum gtsquery_same(PG_FUNCTION_ARGS);
201 extern Datum gtsquery_penalty(PG_FUNCTION_ARGS);
202 extern Datum gtsquery_picksplit(PG_FUNCTION_ARGS);
203
204 /*
205  * Parser interface to SQL
206  */
207 extern Datum ts_token_type_byid(PG_FUNCTION_ARGS);
208 extern Datum ts_token_type_byname(PG_FUNCTION_ARGS);
209 extern Datum ts_parse_byid(PG_FUNCTION_ARGS);
210 extern Datum ts_parse_byname(PG_FUNCTION_ARGS);
211
212 /*
213  * Default word parser
214  */
215
216 extern Datum prsd_start(PG_FUNCTION_ARGS);
217 extern Datum prsd_nexttoken(PG_FUNCTION_ARGS);
218 extern Datum prsd_end(PG_FUNCTION_ARGS);
219 extern Datum prsd_headline(PG_FUNCTION_ARGS);
220 extern Datum prsd_lextype(PG_FUNCTION_ARGS);
221
222 /*
223  * Dictionary interface to SQL
224  */
225 extern Datum ts_lexize_byid(PG_FUNCTION_ARGS);
226 extern Datum ts_lexize_byname(PG_FUNCTION_ARGS);
227
228 /*
229  * Simple built-in dictionary
230  */
231 extern Datum dsimple_init(PG_FUNCTION_ARGS);
232 extern Datum dsimple_lexize(PG_FUNCTION_ARGS);
233
234 /*
235  * Synonym built-in dictionary
236  */
237 extern Datum dsynonym_init(PG_FUNCTION_ARGS);
238 extern Datum dsynonym_lexize(PG_FUNCTION_ARGS);
239
240 /*
241  * ISpell dictionary
242  */
243 extern Datum dispell_init(PG_FUNCTION_ARGS);
244 extern Datum dispell_lexize(PG_FUNCTION_ARGS);
245
246 /*
247  * Thesaurus
248  */
249 extern Datum thesaurus_init(PG_FUNCTION_ARGS);
250 extern Datum thesaurus_lexize(PG_FUNCTION_ARGS);
251
252 /*
253  * headline
254  */
255 extern Datum ts_headline_byid_opt(PG_FUNCTION_ARGS);
256 extern Datum ts_headline_byid(PG_FUNCTION_ARGS);
257 extern Datum ts_headline(PG_FUNCTION_ARGS);
258 extern Datum ts_headline_opt(PG_FUNCTION_ARGS);
259
260 /*
261  * current cfg
262  */
263 extern Datum get_current_ts_config(PG_FUNCTION_ARGS);
264
265 #endif   /* _PG_TS_UTILS_H_ */