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.

259 line
7.9KB

  1. /* contrib/ltree/ltree.h */
  2. #ifndef __LTREE_H__
  3. #define __LTREE_H__
  4. #include "fmgr.h"
  5. #include "tsearch/ts_locale.h"
  6. #include "utils/memutils.h"
  7. typedef struct
  8. {
  9. uint16 len;
  10. char name[FLEXIBLE_ARRAY_MEMBER];
  11. } ltree_level;
  12. #define LEVEL_HDRSIZE (offsetof(ltree_level,name))
  13. #define LEVEL_NEXT(x) ( (ltree_level*)( ((char*)(x)) + MAXALIGN(((ltree_level*)(x))->len + LEVEL_HDRSIZE) ) )
  14. typedef struct
  15. {
  16. int32 vl_len_; /* varlena header (do not touch directly!) */
  17. uint16 numlevel;
  18. char data[FLEXIBLE_ARRAY_MEMBER];
  19. } ltree;
  20. #define LTREE_HDRSIZE MAXALIGN( offsetof(ltree, data) )
  21. #define LTREE_FIRST(x) ( (ltree_level*)( ((char*)(x))+LTREE_HDRSIZE ) )
  22. #define LTREE_MAX_LEVELS PG_UINT16_MAX /* ltree.numlevel is uint16 */
  23. /* lquery */
  24. typedef struct
  25. {
  26. int32 val;
  27. uint16 len;
  28. uint8 flag;
  29. char name[FLEXIBLE_ARRAY_MEMBER];
  30. } lquery_variant;
  31. #define LVAR_HDRSIZE MAXALIGN(offsetof(lquery_variant, name))
  32. #define LVAR_NEXT(x) ( (lquery_variant*)( ((char*)(x)) + MAXALIGN(((lquery_variant*)(x))->len) + LVAR_HDRSIZE ) )
  33. #define LVAR_ANYEND 0x01
  34. #define LVAR_INCASE 0x02
  35. #define LVAR_SUBLEXEME 0x04
  36. typedef struct
  37. {
  38. uint16 totallen;
  39. uint16 flag;
  40. uint16 numvar;
  41. uint16 low;
  42. uint16 high;
  43. char variants[FLEXIBLE_ARRAY_MEMBER];
  44. } lquery_level;
  45. #define LQL_HDRSIZE MAXALIGN( offsetof(lquery_level,variants) )
  46. #define LQL_NEXT(x) ( (lquery_level*)( ((char*)(x)) + MAXALIGN(((lquery_level*)(x))->totallen) ) )
  47. #define LQL_FIRST(x) ( (lquery_variant*)( ((char*)(x))+LQL_HDRSIZE ) )
  48. #define LQL_NOT 0x10
  49. #ifdef LOWER_NODE
  50. #define FLG_CANLOOKSIGN(x) ( ( (x) & ( LQL_NOT | LVAR_ANYEND | LVAR_SUBLEXEME ) ) == 0 )
  51. #else
  52. #define FLG_CANLOOKSIGN(x) ( ( (x) & ( LQL_NOT | LVAR_ANYEND | LVAR_SUBLEXEME | LVAR_INCASE ) ) == 0 )
  53. #endif
  54. #define LQL_CANLOOKSIGN(x) FLG_CANLOOKSIGN( ((lquery_level*)(x))->flag )
  55. typedef struct
  56. {
  57. int32 vl_len_; /* varlena header (do not touch directly!) */
  58. uint16 numlevel;
  59. uint16 firstgood;
  60. uint16 flag;
  61. char data[FLEXIBLE_ARRAY_MEMBER];
  62. } lquery;
  63. #define LQUERY_HDRSIZE MAXALIGN( offsetof(lquery, data) )
  64. #define LQUERY_FIRST(x) ( (lquery_level*)( ((char*)(x))+LQUERY_HDRSIZE ) )
  65. #define LQUERY_MAX_LEVELS PG_UINT16_MAX /* lquery.numlevel is uint16 */
  66. #define LQUERY_HASNOT 0x01
  67. #define ISALNUM(x) ( t_isalpha(x) || t_isdigit(x) || ( pg_mblen(x) == 1 && t_iseq((x), '_') ) )
  68. /* full text query */
  69. /*
  70. * item in polish notation with back link
  71. * to left operand
  72. */
  73. typedef struct ITEM
  74. {
  75. int16 type;
  76. int16 left;
  77. int32 val;
  78. uint8 flag;
  79. /* user-friendly value */
  80. uint8 length;
  81. uint16 distance;
  82. } ITEM;
  83. /*
  84. *Storage:
  85. * (len)(size)(array of ITEM)(array of operand in user-friendly form)
  86. */
  87. typedef struct
  88. {
  89. int32 vl_len_; /* varlena header (do not touch directly!) */
  90. int32 size;
  91. char data[FLEXIBLE_ARRAY_MEMBER];
  92. } ltxtquery;
  93. #define HDRSIZEQT MAXALIGN(VARHDRSZ + sizeof(int32))
  94. #define COMPUTESIZE(size,lenofoperand) ( HDRSIZEQT + (size) * sizeof(ITEM) + (lenofoperand) )
  95. #define LTXTQUERY_TOO_BIG(size,lenofoperand) \
  96. ((size) > (MaxAllocSize - HDRSIZEQT - (lenofoperand)) / sizeof(ITEM))
  97. #define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT )
  98. #define GETOPERAND(x) ( (char*)GETQUERY(x) + ((ltxtquery*)x)->size * sizeof(ITEM) )
  99. #define ISOPERATOR(x) ( (x)=='!' || (x)=='&' || (x)=='|' || (x)=='(' || (x)==')' )
  100. #define END 0
  101. #define ERR 1
  102. #define VAL 2
  103. #define OPR 3
  104. #define OPEN 4
  105. #define CLOSE 5
  106. #define VALTRUE 6 /* for stop words */
  107. #define VALFALSE 7
  108. /* use in array iterator */
  109. Datum ltree_isparent(PG_FUNCTION_ARGS);
  110. Datum ltree_risparent(PG_FUNCTION_ARGS);
  111. Datum ltq_regex(PG_FUNCTION_ARGS);
  112. Datum ltq_rregex(PG_FUNCTION_ARGS);
  113. Datum lt_q_regex(PG_FUNCTION_ARGS);
  114. Datum lt_q_rregex(PG_FUNCTION_ARGS);
  115. Datum ltxtq_exec(PG_FUNCTION_ARGS);
  116. Datum ltxtq_rexec(PG_FUNCTION_ARGS);
  117. Datum _ltq_regex(PG_FUNCTION_ARGS);
  118. Datum _ltq_rregex(PG_FUNCTION_ARGS);
  119. Datum _lt_q_regex(PG_FUNCTION_ARGS);
  120. Datum _lt_q_rregex(PG_FUNCTION_ARGS);
  121. Datum _ltxtq_exec(PG_FUNCTION_ARGS);
  122. Datum _ltxtq_rexec(PG_FUNCTION_ARGS);
  123. Datum _ltree_isparent(PG_FUNCTION_ARGS);
  124. Datum _ltree_risparent(PG_FUNCTION_ARGS);
  125. /* Concatenation functions */
  126. Datum ltree_addltree(PG_FUNCTION_ARGS);
  127. Datum ltree_addtext(PG_FUNCTION_ARGS);
  128. Datum ltree_textadd(PG_FUNCTION_ARGS);
  129. /* Util function */
  130. Datum ltree_in(PG_FUNCTION_ARGS);
  131. bool ltree_execute(ITEM *curitem, void *checkval,
  132. bool calcnot, bool (*chkcond) (void *checkval, ITEM *val));
  133. int ltree_compare(const ltree *a, const ltree *b);
  134. bool inner_isparent(const ltree *c, const ltree *p);
  135. bool compare_subnode(ltree_level *t, char *q, int len,
  136. int (*cmpptr) (const char *, const char *, size_t), bool anyend);
  137. ltree *lca_inner(ltree **a, int len);
  138. int ltree_strncasecmp(const char *a, const char *b, size_t s);
  139. /* fmgr macros for ltree objects */
  140. #define DatumGetLtreeP(X) ((ltree *) PG_DETOAST_DATUM(X))
  141. #define DatumGetLtreePCopy(X) ((ltree *) PG_DETOAST_DATUM_COPY(X))
  142. #define PG_GETARG_LTREE_P(n) DatumGetLtreeP(PG_GETARG_DATUM(n))
  143. #define PG_GETARG_LTREE_P_COPY(n) DatumGetLtreePCopy(PG_GETARG_DATUM(n))
  144. #define DatumGetLqueryP(X) ((lquery *) PG_DETOAST_DATUM(X))
  145. #define DatumGetLqueryPCopy(X) ((lquery *) PG_DETOAST_DATUM_COPY(X))
  146. #define PG_GETARG_LQUERY_P(n) DatumGetLqueryP(PG_GETARG_DATUM(n))
  147. #define PG_GETARG_LQUERY_P_COPY(n) DatumGetLqueryPCopy(PG_GETARG_DATUM(n))
  148. #define DatumGetLtxtqueryP(X) ((ltxtquery *) PG_DETOAST_DATUM(X))
  149. #define DatumGetLtxtqueryPCopy(X) ((ltxtquery *) PG_DETOAST_DATUM_COPY(X))
  150. #define PG_GETARG_LTXTQUERY_P(n) DatumGetLtxtqueryP(PG_GETARG_DATUM(n))
  151. #define PG_GETARG_LTXTQUERY_P_COPY(n) DatumGetLtxtqueryPCopy(PG_GETARG_DATUM(n))
  152. /* GiST support for ltree */
  153. #define BITBYTE 8
  154. #define SIGLENINT 2
  155. #define SIGLEN ( sizeof(int32)*SIGLENINT )
  156. #define SIGLENBIT (SIGLEN*BITBYTE)
  157. typedef unsigned char BITVEC[SIGLEN];
  158. typedef unsigned char *BITVECP;
  159. #define LOOPBYTE \
  160. for(i=0;i<SIGLEN;i++)
  161. #define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
  162. #define GETBITBYTE(x,i) ( ((unsigned char)(x)) >> i & 0x01 )
  163. #define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
  164. #define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
  165. #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
  166. #define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
  167. #define HASH(sign, val) SETBIT((sign), HASHVAL(val))
  168. /*
  169. * type of index key for ltree. Tree are combined B-Tree and R-Tree
  170. * Storage:
  171. * Leaf pages
  172. * (len)(flag)(ltree)
  173. * Non-Leaf
  174. * (len)(flag)(sign)(left_ltree)(right_ltree)
  175. * ALLTRUE: (len)(flag)(left_ltree)(right_ltree)
  176. *
  177. */
  178. typedef struct
  179. {
  180. int32 vl_len_; /* varlena header (do not touch directly!) */
  181. uint32 flag;
  182. char data[FLEXIBLE_ARRAY_MEMBER];
  183. } ltree_gist;
  184. #define LTG_ONENODE 0x01
  185. #define LTG_ALLTRUE 0x02
  186. #define LTG_NORIGHT 0x04
  187. #define LTG_HDRSIZE MAXALIGN(VARHDRSZ + sizeof(uint32))
  188. #define LTG_SIGN(x) ( (BITVECP)( ((char*)(x))+LTG_HDRSIZE ) )
  189. #define LTG_NODE(x) ( (ltree*)( ((char*)(x))+LTG_HDRSIZE ) )
  190. #define LTG_ISONENODE(x) ( ((ltree_gist*)(x))->flag & LTG_ONENODE )
  191. #define LTG_ISALLTRUE(x) ( ((ltree_gist*)(x))->flag & LTG_ALLTRUE )
  192. #define LTG_ISNORIGHT(x) ( ((ltree_gist*)(x))->flag & LTG_NORIGHT )
  193. #define LTG_LNODE(x) ( (ltree*)( ( ((char*)(x))+LTG_HDRSIZE ) + ( LTG_ISALLTRUE(x) ? 0 : SIGLEN ) ) )
  194. #define LTG_RENODE(x) ( (ltree*)( ((char*)LTG_LNODE(x)) + VARSIZE(LTG_LNODE(x))) )
  195. #define LTG_RNODE(x) ( LTG_ISNORIGHT(x) ? LTG_LNODE(x) : LTG_RENODE(x) )
  196. #define LTG_GETLNODE(x) ( LTG_ISONENODE(x) ? LTG_NODE(x) : LTG_LNODE(x) )
  197. #define LTG_GETRNODE(x) ( LTG_ISONENODE(x) ? LTG_NODE(x) : LTG_RNODE(x) )
  198. /* GiST support for ltree[] */
  199. #define ASIGLENINT (7)
  200. #define ASIGLEN (sizeof(int32)*ASIGLENINT)
  201. #define ASIGLENBIT (ASIGLEN*BITBYTE)
  202. typedef unsigned char ABITVEC[ASIGLEN];
  203. #define ALOOPBYTE \
  204. for(i=0;i<ASIGLEN;i++)
  205. #define AHASHVAL(val) (((unsigned int)(val)) % ASIGLENBIT)
  206. #define AHASH(sign, val) SETBIT((sign), AHASHVAL(val))
  207. /* type of key is the same to ltree_gist */
  208. #endif
上海开阖软件有限公司 沪ICP备12045867号-1