gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
8.0KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * ts_utils.h
  4. * helper utilities for tsearch
  5. *
  6. * Copyright (c) 1998-2019, PostgreSQL Global Development Group
  7. *
  8. * src/include/tsearch/ts_utils.h
  9. *
  10. *-------------------------------------------------------------------------
  11. */
  12. #ifndef _PG_TS_UTILS_H_
  13. #define _PG_TS_UTILS_H_
  14. #include "nodes/pg_list.h"
  15. #include "tsearch/ts_public.h"
  16. #include "tsearch/ts_type.h"
  17. /*
  18. * Common parse definitions for tsvector and tsquery
  19. */
  20. /* tsvector parser support. */
  21. struct TSVectorParseStateData; /* opaque struct in tsvector_parser.c */
  22. typedef struct TSVectorParseStateData *TSVectorParseState;
  23. #define P_TSV_OPR_IS_DELIM (1 << 0)
  24. #define P_TSV_IS_TSQUERY (1 << 1)
  25. #define P_TSV_IS_WEB (1 << 2)
  26. extern TSVectorParseState init_tsvector_parser(char *input, int flags);
  27. extern void reset_tsvector_parser(TSVectorParseState state, char *input);
  28. extern bool gettoken_tsvector(TSVectorParseState state,
  29. char **token, int *len,
  30. WordEntryPos **pos, int *poslen,
  31. char **endptr);
  32. extern void close_tsvector_parser(TSVectorParseState state);
  33. /* phrase operator begins with '<' */
  34. #define ISOPERATOR(x) \
  35. ( pg_mblen(x) == 1 && ( *(x) == '!' || \
  36. *(x) == '&' || \
  37. *(x) == '|' || \
  38. *(x) == '(' || \
  39. *(x) == ')' || \
  40. *(x) == '<' \
  41. ) )
  42. /* parse_tsquery */
  43. struct TSQueryParserStateData; /* private in backend/utils/adt/tsquery.c */
  44. typedef struct TSQueryParserStateData *TSQueryParserState;
  45. typedef void (*PushFunction) (Datum opaque, TSQueryParserState state,
  46. char *token, int tokenlen,
  47. int16 tokenweights, /* bitmap as described in
  48. * QueryOperand struct */
  49. bool prefix);
  50. #define P_TSQ_PLAIN (1 << 0)
  51. #define P_TSQ_WEB (1 << 1)
  52. extern TSQuery parse_tsquery(char *buf,
  53. PushFunction pushval,
  54. Datum opaque,
  55. int flags);
  56. /* Functions for use by PushFunction implementations */
  57. extern void pushValue(TSQueryParserState state,
  58. char *strval, int lenval, int16 weight, bool prefix);
  59. extern void pushStop(TSQueryParserState state);
  60. extern void pushOperator(TSQueryParserState state, int8 oper, int16 distance);
  61. /*
  62. * parse plain text and lexize words
  63. */
  64. typedef struct
  65. {
  66. uint16 len;
  67. uint16 nvariant;
  68. union
  69. {
  70. uint16 pos;
  71. /*
  72. * When apos array is used, apos[0] is the number of elements in the
  73. * array (excluding apos[0]), and alen is the allocated size of the
  74. * array.
  75. */
  76. uint16 *apos;
  77. } pos;
  78. uint16 flags; /* currently, only TSL_PREFIX */
  79. char *word;
  80. uint32 alen;
  81. } ParsedWord;
  82. typedef struct
  83. {
  84. ParsedWord *words;
  85. int32 lenwords;
  86. int32 curwords;
  87. int32 pos;
  88. } ParsedText;
  89. extern void parsetext(Oid cfgId, ParsedText *prs, char *buf, int32 buflen);
  90. /*
  91. * headline framework, flow in common to generate:
  92. * 1 parse text with hlparsetext
  93. * 2 parser-specific function to find part
  94. * 3 generateHeadline to generate result text
  95. */
  96. extern void hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query,
  97. char *buf, int32 buflen);
  98. extern text *generateHeadline(HeadlineParsedText *prs);
  99. /*
  100. * TSQuery execution support
  101. *
  102. * TS_execute() executes a tsquery against data that can be represented in
  103. * various forms. The TSExecuteCallback callback function is called to check
  104. * whether a given primitive tsquery value is matched in the data.
  105. */
  106. /*
  107. * struct ExecPhraseData is passed to a TSExecuteCallback function if we need
  108. * lexeme position data (because of a phrase-match operator in the tsquery).
  109. * The callback should fill in position data when it returns true (success).
  110. * If it cannot return position data, it may leave "data" unchanged, but
  111. * then the caller of TS_execute() must pass the TS_EXEC_PHRASE_NO_POS flag
  112. * and must arrange for a later recheck with position data available.
  113. *
  114. * The reported lexeme positions must be sorted and unique. Callers must only
  115. * consult the position bits of the pos array, ie, WEP_GETPOS(data->pos[i]).
  116. * This allows the returned "pos" to point directly to the WordEntryPos
  117. * portion of a tsvector value. If "allocated" is true then the pos array
  118. * is palloc'd workspace and caller may free it when done.
  119. *
  120. * "negate" means that the pos array contains positions where the query does
  121. * not match, rather than positions where it does. "width" is positive when
  122. * the match is wider than one lexeme. Neither of these fields normally need
  123. * to be touched by TSExecuteCallback functions; they are used for
  124. * phrase-search processing within TS_execute.
  125. *
  126. * All fields of the ExecPhraseData struct are initially zeroed by caller.
  127. */
  128. typedef struct ExecPhraseData
  129. {
  130. int npos; /* number of positions reported */
  131. bool allocated; /* pos points to palloc'd data? */
  132. bool negate; /* positions are where query is NOT matched */
  133. WordEntryPos *pos; /* ordered, non-duplicate lexeme positions */
  134. int width; /* width of match in lexemes, less 1 */
  135. } ExecPhraseData;
  136. /*
  137. * Signature for TSQuery lexeme check functions
  138. *
  139. * arg: opaque value passed through from caller of TS_execute
  140. * val: lexeme to test for presence of
  141. * data: to be filled with lexeme positions; NULL if position data not needed
  142. *
  143. * Return true if lexeme is present in data, else false. If data is not
  144. * NULL, it should be filled with lexeme positions, but function can leave
  145. * it as zeroes if position data is not available.
  146. */
  147. typedef bool (*TSExecuteCallback) (void *arg, QueryOperand *val,
  148. ExecPhraseData *data);
  149. /*
  150. * Flag bits for TS_execute
  151. */
  152. #define TS_EXEC_EMPTY (0x00)
  153. /*
  154. * If TS_EXEC_CALC_NOT is not set, then NOT expressions are automatically
  155. * evaluated to be true. Useful in cases where NOT cannot be accurately
  156. * computed (GiST) or it isn't important (ranking). From TS_execute's
  157. * perspective, !CALC_NOT means that the TSExecuteCallback function might
  158. * return false-positive indications of a lexeme's presence.
  159. */
  160. #define TS_EXEC_CALC_NOT (0x01)
  161. /*
  162. * If TS_EXEC_PHRASE_NO_POS is set, allow OP_PHRASE to be executed lossily
  163. * in the absence of position information: a true result indicates that the
  164. * phrase might be present. Without this flag, OP_PHRASE always returns
  165. * false if lexeme position information is not available.
  166. */
  167. #define TS_EXEC_PHRASE_NO_POS (0x02)
  168. extern bool TS_execute(QueryItem *curitem, void *arg, uint32 flags,
  169. TSExecuteCallback chkcond);
  170. extern bool tsquery_requires_match(QueryItem *curitem);
  171. /*
  172. * to_ts* - text transformation to tsvector, tsquery
  173. */
  174. extern TSVector make_tsvector(ParsedText *prs);
  175. extern int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix);
  176. /*
  177. * Possible strategy numbers for indexes
  178. * TSearchStrategyNumber - (tsvector|text) @@ tsquery
  179. * TSearchWithClassStrategyNumber - tsvector @@@ tsquery
  180. */
  181. #define TSearchStrategyNumber 1
  182. #define TSearchWithClassStrategyNumber 2
  183. /*
  184. * TSQuery Utilities
  185. */
  186. extern QueryItem *clean_NOT(QueryItem *ptr, int32 *len);
  187. extern TSQuery cleanup_tsquery_stopwords(TSQuery in);
  188. typedef struct QTNode
  189. {
  190. QueryItem *valnode;
  191. uint32 flags;
  192. int32 nchild;
  193. char *word;
  194. uint32 sign;
  195. struct QTNode **child;
  196. } QTNode;
  197. /* bits in QTNode.flags */
  198. #define QTN_NEEDFREE 0x01
  199. #define QTN_NOCHANGE 0x02
  200. #define QTN_WORDFREE 0x04
  201. typedef uint64 TSQuerySign;
  202. #define TSQS_SIGLEN (sizeof(TSQuerySign)*BITS_PER_BYTE)
  203. #define TSQuerySignGetDatum(X) Int64GetDatum((int64) (X))
  204. #define DatumGetTSQuerySign(X) ((TSQuerySign) DatumGetInt64(X))
  205. #define PG_RETURN_TSQUERYSIGN(X) return TSQuerySignGetDatum(X)
  206. #define PG_GETARG_TSQUERYSIGN(n) DatumGetTSQuerySign(PG_GETARG_DATUM(n))
  207. extern QTNode *QT2QTN(QueryItem *in, char *operand);
  208. extern TSQuery QTN2QT(QTNode *in);
  209. extern void QTNFree(QTNode *in);
  210. extern void QTNSort(QTNode *in);
  211. extern void QTNTernary(QTNode *in);
  212. extern void QTNBinary(QTNode *in);
  213. extern int QTNodeCompare(QTNode *an, QTNode *bn);
  214. extern QTNode *QTNCopy(QTNode *in);
  215. extern void QTNClearFlags(QTNode *in, uint32 flags);
  216. extern bool QTNEq(QTNode *a, QTNode *b);
  217. extern TSQuerySign makeTSQuerySign(TSQuery a);
  218. extern QTNode *findsubquery(QTNode *root, QTNode *ex, QTNode *subs,
  219. bool *isfind);
  220. #endif /* _PG_TS_UTILS_H_ */
上海开阖软件有限公司 沪ICP备12045867号-1