2010-09-21 04:08:53 +08:00
|
|
|
/* contrib/ltree/ltree.h */
|
2006-03-11 12:38:42 +08:00
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
#ifndef __LTREE_H__
|
|
|
|
#define __LTREE_H__
|
|
|
|
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "fmgr.h"
|
2008-07-01 02:30:48 +08:00
|
|
|
#include "tsearch/ts_locale.h"
|
2014-02-17 22:33:31 +08:00
|
|
|
#include "utils/memutils.h"
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2008-07-01 02:30:48 +08:00
|
|
|
uint16 len;
|
2015-02-20 13:11:42 +08:00
|
|
|
char name[FLEXIBLE_ARRAY_MEMBER];
|
2009-06-11 22:49:15 +08:00
|
|
|
} ltree_level;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2008-07-01 02:30:48 +08:00
|
|
|
#define LEVEL_HDRSIZE (offsetof(ltree_level,name))
|
2002-08-04 13:02:50 +08:00
|
|
|
#define LEVEL_NEXT(x) ( (ltree_level*)( ((char*)(x)) + MAXALIGN(((ltree_level*)(x))->len + LEVEL_HDRSIZE) ) )
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2007-03-01 06:44:38 +08:00
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2002-09-05 04:31:48 +08:00
|
|
|
uint16 numlevel;
|
2015-02-20 13:11:42 +08:00
|
|
|
char data[FLEXIBLE_ARRAY_MEMBER];
|
2009-06-11 22:49:15 +08:00
|
|
|
} ltree;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2008-07-01 02:30:48 +08:00
|
|
|
#define LTREE_HDRSIZE MAXALIGN( offsetof(ltree, data) )
|
2002-08-04 13:02:50 +08:00
|
|
|
#define LTREE_FIRST(x) ( (ltree_level*)( ((char*)(x))+LTREE_HDRSIZE ) )
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* lquery */
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2012-06-25 06:51:46 +08:00
|
|
|
int32 val;
|
2008-07-01 02:30:48 +08:00
|
|
|
uint16 len;
|
2002-09-05 04:31:48 +08:00
|
|
|
uint8 flag;
|
2015-02-20 13:11:42 +08:00
|
|
|
char name[FLEXIBLE_ARRAY_MEMBER];
|
2009-06-11 22:49:15 +08:00
|
|
|
} lquery_variant;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2008-07-01 02:30:48 +08:00
|
|
|
#define LVAR_HDRSIZE MAXALIGN(offsetof(lquery_variant, name))
|
2002-08-04 13:02:50 +08:00
|
|
|
#define LVAR_NEXT(x) ( (lquery_variant*)( ((char*)(x)) + MAXALIGN(((lquery_variant*)(x))->len) + LVAR_HDRSIZE ) )
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
#define LVAR_ANYEND 0x01
|
|
|
|
#define LVAR_INCASE 0x02
|
2006-03-01 14:30:32 +08:00
|
|
|
#define LVAR_SUBLEXEME 0x04
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint16 totallen;
|
|
|
|
uint16 flag;
|
|
|
|
uint16 numvar;
|
|
|
|
uint16 low;
|
|
|
|
uint16 high;
|
2015-02-20 13:11:42 +08:00
|
|
|
char variants[FLEXIBLE_ARRAY_MEMBER];
|
2009-06-11 22:49:15 +08:00
|
|
|
} lquery_level;
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2008-07-01 02:30:48 +08:00
|
|
|
#define LQL_HDRSIZE MAXALIGN( offsetof(lquery_level,variants) )
|
2002-09-05 04:31:48 +08:00
|
|
|
#define LQL_NEXT(x) ( (lquery_level*)( ((char*)(x)) + MAXALIGN(((lquery_level*)(x))->totallen) ) )
|
2002-08-04 13:02:50 +08:00
|
|
|
#define LQL_FIRST(x) ( (lquery_variant*)( ((char*)(x))+LQL_HDRSIZE ) )
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
#define LQL_NOT 0x10
|
|
|
|
#ifdef LOWER_NODE
|
2006-03-01 14:30:32 +08:00
|
|
|
#define FLG_CANLOOKSIGN(x) ( ( (x) & ( LQL_NOT | LVAR_ANYEND | LVAR_SUBLEXEME ) ) == 0 )
|
2002-07-31 00:40:34 +08:00
|
|
|
#else
|
2006-03-01 14:30:32 +08:00
|
|
|
#define FLG_CANLOOKSIGN(x) ( ( (x) & ( LQL_NOT | LVAR_ANYEND | LVAR_SUBLEXEME | LVAR_INCASE ) ) == 0 )
|
2002-07-31 00:40:34 +08:00
|
|
|
#endif
|
2002-09-05 04:31:48 +08:00
|
|
|
#define LQL_CANLOOKSIGN(x) FLG_CANLOOKSIGN( ((lquery_level*)(x))->flag )
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2007-03-01 06:44:38 +08:00
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2002-09-05 04:31:48 +08:00
|
|
|
uint16 numlevel;
|
|
|
|
uint16 firstgood;
|
|
|
|
uint16 flag;
|
2015-02-20 13:11:42 +08:00
|
|
|
char data[FLEXIBLE_ARRAY_MEMBER];
|
2009-06-11 22:49:15 +08:00
|
|
|
} lquery;
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2008-07-01 02:30:48 +08:00
|
|
|
#define LQUERY_HDRSIZE MAXALIGN( offsetof(lquery, data) )
|
2002-08-04 13:02:50 +08:00
|
|
|
#define LQUERY_FIRST(x) ( (lquery_level*)( ((char*)(x))+LQUERY_HDRSIZE ) )
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
#define LQUERY_HASNOT 0x01
|
|
|
|
|
2009-06-11 22:49:15 +08:00
|
|
|
#define ISALNUM(x) ( t_isalpha(x) || t_isdigit(x) || ( pg_mblen(x) == 1 && t_iseq((x), '_') ) )
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
/* full text query */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* item in polish notation with back link
|
|
|
|
* to left operand
|
|
|
|
*/
|
|
|
|
typedef struct ITEM
|
|
|
|
{
|
2012-06-25 06:51:46 +08:00
|
|
|
int16 type;
|
|
|
|
int16 left;
|
|
|
|
int32 val;
|
2002-07-31 00:40:34 +08:00
|
|
|
uint8 flag;
|
2002-09-05 04:31:48 +08:00
|
|
|
/* user-friendly value */
|
|
|
|
uint8 length;
|
|
|
|
uint16 distance;
|
2009-06-11 22:49:15 +08:00
|
|
|
} ITEM;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
*Storage:
|
2002-09-05 04:31:48 +08:00
|
|
|
* (len)(size)(array of ITEM)(array of operand in user-friendly form)
|
2002-07-31 00:40:34 +08:00
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2007-03-01 06:44:38 +08:00
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2012-06-25 06:51:46 +08:00
|
|
|
int32 size;
|
2015-02-20 13:11:42 +08:00
|
|
|
char data[FLEXIBLE_ARRAY_MEMBER];
|
2009-06-11 22:49:15 +08:00
|
|
|
} ltxtquery;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2012-06-25 06:51:46 +08:00
|
|
|
#define HDRSIZEQT MAXALIGN(VARHDRSZ + sizeof(int32))
|
2005-05-26 05:40:43 +08:00
|
|
|
#define COMPUTESIZE(size,lenofoperand) ( HDRSIZEQT + (size) * sizeof(ITEM) + (lenofoperand) )
|
2014-02-17 22:33:31 +08:00
|
|
|
#define LTXTQUERY_TOO_BIG(size,lenofoperand) \
|
|
|
|
((size) > (MaxAllocSize - HDRSIZEQT - (lenofoperand)) / sizeof(ITEM))
|
2002-07-31 00:40:34 +08:00
|
|
|
#define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT )
|
2002-09-05 04:31:48 +08:00
|
|
|
#define GETOPERAND(x) ( (char*)GETQUERY(x) + ((ltxtquery*)x)->size * sizeof(ITEM) )
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
#define ISOPERATOR(x) ( (x)=='!' || (x)=='&' || (x)=='|' || (x)=='(' || (x)==')' )
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
#define END 0
|
|
|
|
#define ERR 1
|
|
|
|
#define VAL 2
|
|
|
|
#define OPR 3
|
|
|
|
#define OPEN 4
|
|
|
|
#define CLOSE 5
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-22 03:18:54 +08:00
|
|
|
#define VALTRUE 6 /* for stop words */
|
2002-09-05 04:31:48 +08:00
|
|
|
#define VALFALSE 7
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* use in array iterator */
|
2016-11-07 23:19:22 +08:00
|
|
|
Datum ltree_isparent(PG_FUNCTION_ARGS);
|
|
|
|
Datum ltree_risparent(PG_FUNCTION_ARGS);
|
|
|
|
Datum ltq_regex(PG_FUNCTION_ARGS);
|
|
|
|
Datum ltq_rregex(PG_FUNCTION_ARGS);
|
|
|
|
Datum lt_q_regex(PG_FUNCTION_ARGS);
|
|
|
|
Datum lt_q_rregex(PG_FUNCTION_ARGS);
|
|
|
|
Datum ltxtq_exec(PG_FUNCTION_ARGS);
|
|
|
|
Datum ltxtq_rexec(PG_FUNCTION_ARGS);
|
|
|
|
Datum _ltq_regex(PG_FUNCTION_ARGS);
|
|
|
|
Datum _ltq_rregex(PG_FUNCTION_ARGS);
|
|
|
|
Datum _lt_q_regex(PG_FUNCTION_ARGS);
|
|
|
|
Datum _lt_q_rregex(PG_FUNCTION_ARGS);
|
|
|
|
Datum _ltxtq_exec(PG_FUNCTION_ARGS);
|
|
|
|
Datum _ltxtq_rexec(PG_FUNCTION_ARGS);
|
|
|
|
Datum _ltree_isparent(PG_FUNCTION_ARGS);
|
|
|
|
Datum _ltree_risparent(PG_FUNCTION_ARGS);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
/* Concatenation functions */
|
2016-11-07 23:19:22 +08:00
|
|
|
Datum ltree_addltree(PG_FUNCTION_ARGS);
|
|
|
|
Datum ltree_addtext(PG_FUNCTION_ARGS);
|
|
|
|
Datum ltree_textadd(PG_FUNCTION_ARGS);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
/* Util function */
|
2016-11-07 23:19:22 +08:00
|
|
|
Datum ltree_in(PG_FUNCTION_ARGS);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2009-06-11 22:49:15 +08:00
|
|
|
bool ltree_execute(ITEM *curitem, void *checkval,
|
|
|
|
bool calcnot, bool (*chkcond) (void *checkval, ITEM *val));
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2009-06-11 22:49:15 +08:00
|
|
|
int ltree_compare(const ltree *a, const ltree *b);
|
|
|
|
bool inner_isparent(const ltree *c, const ltree *p);
|
|
|
|
bool compare_subnode(ltree_level *t, char *q, int len,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-22 03:35:54 +08:00
|
|
|
int (*cmpptr) (const char *, const char *, size_t), bool anyend);
|
2009-06-11 22:49:15 +08:00
|
|
|
ltree *lca_inner(ltree **a, int len);
|
|
|
|
int ltree_strncasecmp(const char *a, const char *b, size_t s);
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2017-09-19 03:21:23 +08:00
|
|
|
/* fmgr macros for ltree objects */
|
|
|
|
#define DatumGetLtreeP(X) ((ltree *) PG_DETOAST_DATUM(X))
|
|
|
|
#define DatumGetLtreePCopy(X) ((ltree *) PG_DETOAST_DATUM_COPY(X))
|
|
|
|
#define PG_GETARG_LTREE_P(n) DatumGetLtreeP(PG_GETARG_DATUM(n))
|
|
|
|
#define PG_GETARG_LTREE_P_COPY(n) DatumGetLtreePCopy(PG_GETARG_DATUM(n))
|
|
|
|
|
|
|
|
#define DatumGetLqueryP(X) ((lquery *) PG_DETOAST_DATUM(X))
|
|
|
|
#define DatumGetLqueryPCopy(X) ((lquery *) PG_DETOAST_DATUM_COPY(X))
|
|
|
|
#define PG_GETARG_LQUERY_P(n) DatumGetLqueryP(PG_GETARG_DATUM(n))
|
|
|
|
#define PG_GETARG_LQUERY_P_COPY(n) DatumGetLqueryPCopy(PG_GETARG_DATUM(n))
|
|
|
|
|
|
|
|
#define DatumGetLtxtqueryP(X) ((ltxtquery *) PG_DETOAST_DATUM(X))
|
|
|
|
#define DatumGetLtxtqueryPCopy(X) ((ltxtquery *) PG_DETOAST_DATUM_COPY(X))
|
|
|
|
#define PG_GETARG_LTXTQUERY_P(n) DatumGetLtxtqueryP(PG_GETARG_DATUM(n))
|
|
|
|
#define PG_GETARG_LTXTQUERY_P_COPY(n) DatumGetLtxtqueryPCopy(PG_GETARG_DATUM(n))
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
/* GiST support for ltree */
|
|
|
|
|
|
|
|
#define BITBYTE 8
|
2003-02-19 11:50:09 +08:00
|
|
|
#define SIGLENINT 2
|
2012-06-25 06:51:46 +08:00
|
|
|
#define SIGLEN ( sizeof(int32)*SIGLENINT )
|
2002-07-31 00:40:34 +08:00
|
|
|
#define SIGLENBIT (SIGLEN*BITBYTE)
|
|
|
|
typedef unsigned char BITVEC[SIGLEN];
|
|
|
|
typedef unsigned char *BITVECP;
|
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
#define LOOPBYTE \
|
|
|
|
for(i=0;i<SIGLEN;i++)
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
|
|
|
|
#define GETBITBYTE(x,i) ( ((unsigned char)(x)) >> i & 0x01 )
|
|
|
|
#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
|
|
|
|
#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
|
|
|
|
#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
|
|
|
|
|
|
|
|
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
|
|
|
|
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* type of index key for ltree. Tree are combined B-Tree and R-Tree
|
|
|
|
* Storage:
|
2002-09-05 04:31:48 +08:00
|
|
|
* Leaf pages
|
2002-07-31 00:40:34 +08:00
|
|
|
* (len)(flag)(ltree)
|
|
|
|
* Non-Leaf
|
2002-09-05 04:31:48 +08:00
|
|
|
* (len)(flag)(sign)(left_ltree)(right_ltree)
|
2002-07-31 00:40:34 +08:00
|
|
|
* ALLTRUE: (len)(flag)(left_ltree)(right_ltree)
|
2002-09-05 04:31:48 +08:00
|
|
|
*
|
2002-07-31 00:40:34 +08:00
|
|
|
*/
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2007-03-01 06:44:38 +08:00
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2002-09-05 04:31:48 +08:00
|
|
|
uint32 flag;
|
2015-02-20 13:11:42 +08:00
|
|
|
char data[FLEXIBLE_ARRAY_MEMBER];
|
2009-06-11 22:49:15 +08:00
|
|
|
} ltree_gist;
|
2002-09-05 04:31:48 +08:00
|
|
|
|
|
|
|
#define LTG_ONENODE 0x01
|
|
|
|
#define LTG_ALLTRUE 0x02
|
|
|
|
#define LTG_NORIGHT 0x04
|
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
#define LTG_HDRSIZE MAXALIGN(VARHDRSZ + sizeof(uint32))
|
2002-09-05 04:31:48 +08:00
|
|
|
#define LTG_SIGN(x) ( (BITVECP)( ((char*)(x))+LTG_HDRSIZE ) )
|
|
|
|
#define LTG_NODE(x) ( (ltree*)( ((char*)(x))+LTG_HDRSIZE ) )
|
|
|
|
#define LTG_ISONENODE(x) ( ((ltree_gist*)(x))->flag & LTG_ONENODE )
|
|
|
|
#define LTG_ISALLTRUE(x) ( ((ltree_gist*)(x))->flag & LTG_ALLTRUE )
|
|
|
|
#define LTG_ISNORIGHT(x) ( ((ltree_gist*)(x))->flag & LTG_NORIGHT )
|
2002-08-04 13:02:50 +08:00
|
|
|
#define LTG_LNODE(x) ( (ltree*)( ( ((char*)(x))+LTG_HDRSIZE ) + ( LTG_ISALLTRUE(x) ? 0 : SIGLEN ) ) )
|
2007-03-01 06:44:38 +08:00
|
|
|
#define LTG_RENODE(x) ( (ltree*)( ((char*)LTG_LNODE(x)) + VARSIZE(LTG_LNODE(x))) )
|
2002-07-31 00:40:34 +08:00
|
|
|
#define LTG_RNODE(x) ( LTG_ISNORIGHT(x) ? LTG_LNODE(x) : LTG_RENODE(x) )
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
#define LTG_GETLNODE(x) ( LTG_ISONENODE(x) ? LTG_NODE(x) : LTG_LNODE(x) )
|
|
|
|
#define LTG_GETRNODE(x) ( LTG_ISONENODE(x) ? LTG_NODE(x) : LTG_RNODE(x) )
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* GiST support for ltree[] */
|
|
|
|
|
2003-06-12 02:44:15 +08:00
|
|
|
#define ASIGLENINT (7)
|
2012-06-25 06:51:46 +08:00
|
|
|
#define ASIGLEN (sizeof(int32)*ASIGLENINT)
|
2002-09-05 04:31:48 +08:00
|
|
|
#define ASIGLENBIT (ASIGLEN*BITBYTE)
|
2002-07-31 00:40:34 +08:00
|
|
|
typedef unsigned char ABITVEC[ASIGLEN];
|
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
#define ALOOPBYTE \
|
|
|
|
for(i=0;i<ASIGLEN;i++)
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
#define AHASHVAL(val) (((unsigned int)(val)) % ASIGLENBIT)
|
|
|
|
#define AHASH(sign, val) SETBIT((sign), AHASHVAL(val))
|
|
|
|
|
|
|
|
/* type of key is the same to ltree_gist */
|
|
|
|
|
|
|
|
#endif
|