PostgreSQL Source Code  git master
ltree.h
Go to the documentation of this file.
1 /* contrib/ltree/ltree.h */
2 
3 #ifndef __LTREE_H__
4 #define __LTREE_H__
5 
6 #include "fmgr.h"
7 #include "tsearch/ts_locale.h"
8 #include "utils/memutils.h"
9 
10 typedef struct
11 {
14 } ltree_level;
15 
16 #define LEVEL_HDRSIZE (offsetof(ltree_level,name))
17 #define LEVEL_NEXT(x) ( (ltree_level*)( ((char*)(x)) + MAXALIGN(((ltree_level*)(x))->len + LEVEL_HDRSIZE) ) )
18 
19 typedef struct
20 {
21  int32 vl_len_; /* varlena header (do not touch directly!) */
24 } ltree;
25 
26 #define LTREE_HDRSIZE MAXALIGN( offsetof(ltree, data) )
27 #define LTREE_FIRST(x) ( (ltree_level*)( ((char*)(x))+LTREE_HDRSIZE ) )
28 #define LTREE_MAX_LEVELS PG_UINT16_MAX /* ltree.numlevel is uint16 */
29 
30 
31 /* lquery */
32 
33 typedef struct
34 {
37  uint8 flag; /* see LVAR_xxx flags below */
40 
41 #define LVAR_HDRSIZE MAXALIGN(offsetof(lquery_variant, name))
42 #define LVAR_NEXT(x) ( (lquery_variant*)( ((char*)(x)) + MAXALIGN(((lquery_variant*)(x))->len) + LVAR_HDRSIZE ) )
43 
44 #define LVAR_ANYEND 0x01
45 #define LVAR_INCASE 0x02
46 #define LVAR_SUBLEXEME 0x04
47 
48 typedef struct
49 {
50  uint16 totallen; /* total length of this level, in bytes */
51  uint16 flag; /* see LQL_xxx flags below */
52  uint16 numvar; /* number of variants; 0 means '*' */
53  uint16 low; /* minimum repeat count for '*' */
54  uint16 high; /* maximum repeat count for '*' */
55  /* Array of maxalign'd lquery_variant structs follows: */
56  char variants[FLEXIBLE_ARRAY_MEMBER];
57 } lquery_level;
58 
59 #define LQL_HDRSIZE MAXALIGN( offsetof(lquery_level,variants) )
60 #define LQL_NEXT(x) ( (lquery_level*)( ((char*)(x)) + MAXALIGN(((lquery_level*)(x))->totallen) ) )
61 #define LQL_FIRST(x) ( (lquery_variant*)( ((char*)(x))+LQL_HDRSIZE ) )
62 
63 #define LQL_NOT 0x10
64 
65 #ifdef LOWER_NODE
66 #define FLG_CANLOOKSIGN(x) ( ( (x) & ( LQL_NOT | LVAR_ANYEND | LVAR_SUBLEXEME ) ) == 0 )
67 #else
68 #define FLG_CANLOOKSIGN(x) ( ( (x) & ( LQL_NOT | LVAR_ANYEND | LVAR_SUBLEXEME | LVAR_INCASE ) ) == 0 )
69 #endif
70 #define LQL_CANLOOKSIGN(x) FLG_CANLOOKSIGN( ((lquery_level*)(x))->flag )
71 
72 typedef struct
73 {
74  int32 vl_len_; /* varlena header (do not touch directly!) */
75  uint16 numlevel; /* number of lquery_levels */
77  uint16 flag; /* see LQUERY_xxx flags below */
78  /* Array of maxalign'd lquery_level structs follows: */
80 } lquery;
81 
82 #define LQUERY_HDRSIZE MAXALIGN( offsetof(lquery, data) )
83 #define LQUERY_FIRST(x) ( (lquery_level*)( ((char*)(x))+LQUERY_HDRSIZE ) )
84 #define LQUERY_MAX_LEVELS PG_UINT16_MAX /* lquery.numlevel is uint16 */
85 
86 #define LQUERY_HASNOT 0x01
87 
88 #define ISALNUM(x) ( t_isalpha(x) || t_isdigit(x) || ( pg_mblen(x) == 1 && t_iseq((x), '_') ) )
89 
90 /* full text query */
91 
92 /*
93  * item in polish notation with back link
94  * to left operand
95  */
96 typedef struct ITEM
97 {
98  int16 type;
99  int16 left;
100  int32 val;
102  /* user-friendly value */
105 } ITEM;
106 
107 /*
108  *Storage:
109  * (len)(size)(array of ITEM)(array of operand in user-friendly form)
110  */
111 typedef struct
112 {
113  int32 vl_len_; /* varlena header (do not touch directly!) */
116 } ltxtquery;
117 
118 #define HDRSIZEQT MAXALIGN(VARHDRSZ + sizeof(int32))
119 #define COMPUTESIZE(size,lenofoperand) ( HDRSIZEQT + (size) * sizeof(ITEM) + (lenofoperand) )
120 #define LTXTQUERY_TOO_BIG(size,lenofoperand) \
121  ((size) > (MaxAllocSize - HDRSIZEQT - (lenofoperand)) / sizeof(ITEM))
122 #define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT )
123 #define GETOPERAND(x) ( (char*)GETQUERY(x) + ((ltxtquery*)x)->size * sizeof(ITEM) )
124 
125 #define ISOPERATOR(x) ( (x)=='!' || (x)=='&' || (x)=='|' || (x)=='(' || (x)==')' )
126 
127 #define END 0
128 #define ERR 1
129 #define VAL 2
130 #define OPR 3
131 #define OPEN 4
132 #define CLOSE 5
133 #define VALTRUE 6 /* for stop words */
134 #define VALFALSE 7
135 
136 
137 /* use in array iterator */
154 
155 /* Concatenation functions */
159 
160 /* Util function */
162 
163 bool ltree_execute(ITEM *curitem, void *checkval,
164  bool calcnot, bool (*chkcond) (void *checkval, ITEM *val));
165 
166 int ltree_compare(const ltree *a, const ltree *b);
167 bool inner_isparent(const ltree *c, const ltree *p);
168 bool compare_subnode(ltree_level *t, char *q, int len,
169  int (*cmpptr) (const char *, const char *, size_t), bool anyend);
170 ltree *lca_inner(ltree **a, int len);
171 int ltree_strncasecmp(const char *a, const char *b, size_t s);
172 
173 /* fmgr macros for ltree objects */
174 #define DatumGetLtreeP(X) ((ltree *) PG_DETOAST_DATUM(X))
175 #define DatumGetLtreePCopy(X) ((ltree *) PG_DETOAST_DATUM_COPY(X))
176 #define PG_GETARG_LTREE_P(n) DatumGetLtreeP(PG_GETARG_DATUM(n))
177 #define PG_GETARG_LTREE_P_COPY(n) DatumGetLtreePCopy(PG_GETARG_DATUM(n))
178 
179 #define DatumGetLqueryP(X) ((lquery *) PG_DETOAST_DATUM(X))
180 #define DatumGetLqueryPCopy(X) ((lquery *) PG_DETOAST_DATUM_COPY(X))
181 #define PG_GETARG_LQUERY_P(n) DatumGetLqueryP(PG_GETARG_DATUM(n))
182 #define PG_GETARG_LQUERY_P_COPY(n) DatumGetLqueryPCopy(PG_GETARG_DATUM(n))
183 
184 #define DatumGetLtxtqueryP(X) ((ltxtquery *) PG_DETOAST_DATUM(X))
185 #define DatumGetLtxtqueryPCopy(X) ((ltxtquery *) PG_DETOAST_DATUM_COPY(X))
186 #define PG_GETARG_LTXTQUERY_P(n) DatumGetLtxtqueryP(PG_GETARG_DATUM(n))
187 #define PG_GETARG_LTXTQUERY_P_COPY(n) DatumGetLtxtqueryPCopy(PG_GETARG_DATUM(n))
188 
189 /* GiST support for ltree */
190 
191 #define BITBYTE 8
192 #define SIGLENINT 2
193 #define SIGLEN ( sizeof(int32)*SIGLENINT )
194 #define SIGLENBIT (SIGLEN*BITBYTE)
195 typedef unsigned char BITVEC[SIGLEN];
196 typedef unsigned char *BITVECP;
197 
198 #define LOOPBYTE \
199  for(i=0;i<SIGLEN;i++)
200 
201 #define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
202 #define GETBITBYTE(x,i) ( ((unsigned char)(x)) >> i & 0x01 )
203 #define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
204 #define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
205 #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
206 
207 #define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
208 #define HASH(sign, val) SETBIT((sign), HASHVAL(val))
209 
210 /*
211  * type of index key for ltree. Tree are combined B-Tree and R-Tree
212  * Storage:
213  * Leaf pages
214  * (len)(flag)(ltree)
215  * Non-Leaf
216  * (len)(flag)(sign)(left_ltree)(right_ltree)
217  * ALLTRUE: (len)(flag)(left_ltree)(right_ltree)
218  *
219  */
220 
221 typedef struct
222 {
223  int32 vl_len_; /* varlena header (do not touch directly!) */
226 } ltree_gist;
227 
228 #define LTG_ONENODE 0x01
229 #define LTG_ALLTRUE 0x02
230 #define LTG_NORIGHT 0x04
231 
232 #define LTG_HDRSIZE MAXALIGN(VARHDRSZ + sizeof(uint32))
233 #define LTG_SIGN(x) ( (BITVECP)( ((char*)(x))+LTG_HDRSIZE ) )
234 #define LTG_NODE(x) ( (ltree*)( ((char*)(x))+LTG_HDRSIZE ) )
235 #define LTG_ISONENODE(x) ( ((ltree_gist*)(x))->flag & LTG_ONENODE )
236 #define LTG_ISALLTRUE(x) ( ((ltree_gist*)(x))->flag & LTG_ALLTRUE )
237 #define LTG_ISNORIGHT(x) ( ((ltree_gist*)(x))->flag & LTG_NORIGHT )
238 #define LTG_LNODE(x) ( (ltree*)( ( ((char*)(x))+LTG_HDRSIZE ) + ( LTG_ISALLTRUE(x) ? 0 : SIGLEN ) ) )
239 #define LTG_RENODE(x) ( (ltree*)( ((char*)LTG_LNODE(x)) + VARSIZE(LTG_LNODE(x))) )
240 #define LTG_RNODE(x) ( LTG_ISNORIGHT(x) ? LTG_LNODE(x) : LTG_RENODE(x) )
241 
242 #define LTG_GETLNODE(x) ( LTG_ISONENODE(x) ? LTG_NODE(x) : LTG_LNODE(x) )
243 #define LTG_GETRNODE(x) ( LTG_ISONENODE(x) ? LTG_NODE(x) : LTG_RNODE(x) )
244 
245 
246 /* GiST support for ltree[] */
247 
248 #define ASIGLENINT (7)
249 #define ASIGLEN (sizeof(int32)*ASIGLENINT)
250 #define ASIGLENBIT (ASIGLEN*BITBYTE)
251 typedef unsigned char ABITVEC[ASIGLEN];
252 
253 #define ALOOPBYTE \
254  for(i=0;i<ASIGLEN;i++)
255 
256 #define AHASHVAL(val) (((unsigned int)(val)) % ASIGLENBIT)
257 #define AHASH(sign, val) SETBIT((sign), AHASHVAL(val))
258 
259 /* type of key is the same to ltree_gist */
260 
261 #endif
uint16 firstgood
Definition: ltree.h:76
signed short int16
Definition: c.h:354
Definition: _int.h:119
int32 vl_len_
Definition: ltree.h:74
Datum _ltxtq_exec(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:178
unsigned char BITVEC[SIGLEN]
Definition: ltree.h:195
uint8 flag
Definition: ltree.h:37
Datum _ltxtq_rexec(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:190
Datum _ltree_isparent(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:71
Datum _lt_q_rregex(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:168
int32 vl_len_
Definition: ltree.h:223
uint16 low
Definition: ltree.h:53
int32 size
Definition: ltree.h:114
uint16 len
Definition: ltree.h:36
uint16 len
Definition: ltree.h:12
Datum lt_q_regex(PG_FUNCTION_ARGS)
Definition: lquery_op.c:379
bool inner_isparent(const ltree *c, const ltree *p)
Definition: ltree_op.c:143
Definition: ltree.h:72
uint8 length
Definition: ltree.h:103
#define SIGLEN
Definition: ltree.h:193
Datum ltq_rregex(PG_FUNCTION_ARGS)
Definition: lquery_op.c:370
uint32 flag
Definition: ltree.h:224
Datum ltree_risparent(PG_FUNCTION_ARGS)
Definition: ltree_op.c:179
unsigned char uint8
Definition: c.h:365
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:276
bool compare_subnode(ltree_level *t, char *q, int len, int(*cmpptr)(const char *, const char *, size_t), bool anyend)
Definition: lquery_op.c:53
uint16 flag
Definition: ltree.h:77
uint16 totallen
Definition: ltree.h:50
uint8 flag
Definition: ltree.h:101
int32 vl_len_
Definition: ltree.h:113
int ltree_strncasecmp(const char *a, const char *b, size_t s)
Definition: lquery_op.c:87
Datum ltree_in(PG_FUNCTION_ARGS)
Definition: ltree_io.c:38
Datum _ltq_regex(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:113
signed int int32
Definition: c.h:355
bool ltree_execute(ITEM *curitem, void *checkval, bool calcnot, bool(*chkcond)(void *checkval, ITEM *val))
Definition: ltxtquery_op.c:20
Datum ltree_textadd(PG_FUNCTION_ARGS)
Definition: ltree_op.c:395
int16 type
Definition: _int.h:121
unsigned short uint16
Definition: c.h:366
uint16 flag
Definition: ltree.h:51
ltree * lca_inner(ltree **a, int len)
Definition: ltree_op.c:426
Datum ltq_regex(PG_FUNCTION_ARGS)
Definition: lquery_op.c:337
unsigned char * BITVECP
Definition: ltree.h:196
Datum _lt_q_regex(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:134
char * c
int32 val
Definition: _int.h:123
unsigned int uint32
Definition: c.h:367
Datum _ltree_risparent(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:92
Definition: ltree.h:19
Datum ltree_isparent(PG_FUNCTION_ARGS)
Definition: ltree_op.c:167
struct ITEM ITEM
uint16 numlevel
Definition: ltree.h:22
uintptr_t Datum
Definition: postgres.h:367
Datum ltree_addltree(PG_FUNCTION_ARGS)
Definition: ltree_op.c:297
int16 left
Definition: _int.h:122
int32 val
Definition: ltree.h:35
unsigned char ABITVEC[ASIGLEN]
Definition: ltree.h:251
uint16 numlevel
Definition: ltree.h:75
Datum ltxtq_exec(PG_FUNCTION_ARGS)
Definition: ltxtquery_op.c:84
uint16 numvar
Definition: ltree.h:52
#define ASIGLEN
Definition: ltree.h:249
const char * name
Definition: encode.c:521
Datum _ltq_rregex(PG_FUNCTION_ARGS)
Definition: _ltree_op.c:125
Datum lt_q_rregex(PG_FUNCTION_ARGS)
Definition: lquery_op.c:415
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42
int32 vl_len_
Definition: ltree.h:21
uint16 distance
Definition: ltree.h:104
uint16 high
Definition: ltree.h:54
Datum ltxtq_rexec(PG_FUNCTION_ARGS)
Definition: ltxtquery_op.c:105
Datum ltree_addtext(PG_FUNCTION_ARGS)
Definition: ltree_op.c:310