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