2008-05-17 09:28:26 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_trgm/trgm.h
|
2008-05-17 09:28:26 +08:00
|
|
|
*/
|
2004-06-01 01:18:12 +08:00
|
|
|
#ifndef __TRGM_H__
|
|
|
|
#define __TRGM_H__
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
|
|
|
#include "storage/bufpage.h"
|
|
|
|
|
2013-04-09 13:05:55 +08:00
|
|
|
/*
|
|
|
|
* Options ... but note that trgm_regexp.c effectively assumes these values
|
|
|
|
* of LPADDING and RPADDING.
|
|
|
|
*/
|
2004-08-29 13:07:03 +08:00
|
|
|
#define LPADDING 2
|
|
|
|
#define RPADDING 1
|
2004-06-01 01:18:12 +08:00
|
|
|
#define KEEPONLYALNUM
|
2011-02-01 10:33:55 +08:00
|
|
|
/*
|
|
|
|
* Caution: IGNORECASE macro means that trigrams are case-insensitive.
|
2013-04-09 13:05:55 +08:00
|
|
|
* If this macro is disabled, the ~* and ~~* operators must be removed from
|
|
|
|
* the operator classes, because we can't handle case-insensitive wildcard
|
|
|
|
* search with case-sensitive trigrams. Failure to do this will result in
|
|
|
|
* "cannot handle ~*(~~*) with case-sensitive trigrams" errors.
|
2011-02-01 10:33:55 +08:00
|
|
|
*/
|
2004-08-29 13:07:03 +08:00
|
|
|
#define IGNORECASE
|
2004-06-01 01:18:12 +08:00
|
|
|
#define DIVUNION
|
|
|
|
|
2010-12-04 13:16:21 +08:00
|
|
|
/* operator strategy numbers */
|
2011-02-01 10:33:55 +08:00
|
|
|
#define SimilarityStrategyNumber 1
|
|
|
|
#define DistanceStrategyNumber 2
|
|
|
|
#define LikeStrategyNumber 3
|
|
|
|
#define ILikeStrategyNumber 4
|
2013-04-09 13:05:55 +08:00
|
|
|
#define RegExpStrategyNumber 5
|
|
|
|
#define RegExpICaseStrategyNumber 6
|
2010-12-04 13:16:21 +08:00
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
typedef char trgm[3];
|
|
|
|
|
|
|
|
#define CMPCHAR(a,b) ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )
|
2011-09-12 02:54:32 +08:00
|
|
|
#define CMPPCHAR(a,b,i) CMPCHAR( *(((const char*)(a))+i), *(((const char*)(b))+i) )
|
2004-06-01 01:18:12 +08:00
|
|
|
#define CMPTRGM(a,b) ( CMPPCHAR(a,b,0) ? CMPPCHAR(a,b,0) : ( CMPPCHAR(a,b,1) ? CMPPCHAR(a,b,1) : CMPPCHAR(a,b,2) ) )
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
#define CPTRGM(a,b) do { \
|
2004-06-01 01:18:12 +08:00
|
|
|
*(((char*)(a))+0) = *(((char*)(b))+0); \
|
|
|
|
*(((char*)(a))+1) = *(((char*)(b))+1); \
|
|
|
|
*(((char*)(a))+2) = *(((char*)(b))+2); \
|
|
|
|
} while(0);
|
|
|
|
|
2008-11-12 21:43:54 +08:00
|
|
|
#ifdef KEEPONLYALNUM
|
2013-04-09 13:05:55 +08:00
|
|
|
#define ISWORDCHR(c) (t_isalpha(c) || t_isdigit(c))
|
2008-11-12 21:43:54 +08:00
|
|
|
#define ISPRINTABLECHAR(a) ( isascii( *(unsigned char*)(a) ) && (isalnum( *(unsigned char*)(a) ) || *(unsigned char*)(a)==' ') )
|
|
|
|
#else
|
2013-04-09 13:05:55 +08:00
|
|
|
#define ISWORDCHR(c) (!t_isspace(c))
|
2008-11-12 21:43:54 +08:00
|
|
|
#define ISPRINTABLECHAR(a) ( isascii( *(unsigned char*)(a) ) && isprint( *(unsigned char*)(a) ) )
|
|
|
|
#endif
|
2011-02-01 10:33:55 +08:00
|
|
|
#define ISPRINTABLETRGM(t) ( ISPRINTABLECHAR( ((char*)(t)) ) && ISPRINTABLECHAR( ((char*)(t))+1 ) && ISPRINTABLECHAR( ((char*)(t))+2 ) )
|
|
|
|
|
2011-04-10 23:42:00 +08:00
|
|
|
#define ISESCAPECHAR(x) (*(x) == '\\') /* Wildcard escape character */
|
|
|
|
#define ISWILDCARDCHAR(x) (*(x) == '_' || *(x) == '%') /* Wildcard
|
|
|
|
* meta-character */
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2007-03-01 06:44:38 +08:00
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2004-08-29 13:07:03 +08:00
|
|
|
uint8 flag;
|
|
|
|
char data[1];
|
2009-06-11 22:49:15 +08:00
|
|
|
} TRGM;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
#define TRGMHDRSIZE (VARHDRSZ + sizeof(uint8))
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
/* gist */
|
|
|
|
#define BITBYTE 8
|
2004-08-29 13:07:03 +08:00
|
|
|
#define SIGLENINT 3 /* >122 => key will toast, so very slow!!! */
|
|
|
|
#define SIGLEN ( sizeof(int)*SIGLENINT )
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
#define SIGLENBIT (SIGLEN*BITBYTE - 1) /* see makesign */
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
typedef char BITVEC[SIGLEN];
|
|
|
|
typedef char *BITVECP;
|
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
#define LOOPBYTE \
|
|
|
|
for(i=0;i<SIGLEN;i++)
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
|
2011-02-01 10:33:55 +08:00
|
|
|
#define GETBITBYTE(x,i) ( (((char)(x)) >> (i)) & 0x01 )
|
2004-06-01 01:18:12 +08:00
|
|
|
#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))
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
#define ARRKEY 0x01
|
|
|
|
#define SIGNKEY 0x02
|
|
|
|
#define ALLISTRUE 0x04
|
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
#define ISARRKEY(x) ( ((TRGM*)x)->flag & ARRKEY )
|
2004-08-29 13:07:03 +08:00
|
|
|
#define ISSIGNKEY(x) ( ((TRGM*)x)->flag & SIGNKEY )
|
|
|
|
#define ISALLTRUE(x) ( ((TRGM*)x)->flag & ALLISTRUE )
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
#define CALCGTSIZE(flag, len) ( TRGMHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(trgm)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )
|
|
|
|
#define GETSIGN(x) ( (BITVECP)( (char*)x+TRGMHDRSIZE ) )
|
|
|
|
#define GETARR(x) ( (trgm*)( (char*)x+TRGMHDRSIZE ) )
|
|
|
|
#define ARRNELEM(x) ( ( VARSIZE(x) - TRGMHDRSIZE )/sizeof(trgm) )
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2013-04-09 13:05:55 +08:00
|
|
|
typedef struct TrgmPackedGraph TrgmPackedGraph;
|
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
extern float4 trgm_limit;
|
2007-03-01 06:44:38 +08:00
|
|
|
|
2013-04-09 13:05:55 +08:00
|
|
|
extern uint32 trgm2int(trgm *ptr);
|
|
|
|
extern void compact_trigram(trgm *tptr, char *str, int bytelen);
|
|
|
|
extern TRGM *generate_trgm(char *str, int slen);
|
|
|
|
extern TRGM *generate_wildcard_trgm(const char *str, int slen);
|
|
|
|
extern float4 cnt_sml(TRGM *trg1, TRGM *trg2);
|
|
|
|
extern bool trgm_contained_by(TRGM *trg1, TRGM *trg2);
|
2013-04-11 01:30:14 +08:00
|
|
|
extern bool *trgm_presence_map(TRGM *query, TRGM *key);
|
|
|
|
extern TRGM *createTrgmNFA(text *text_re, Oid collation,
|
|
|
|
TrgmPackedGraph **graph, MemoryContext rcontext);
|
2013-04-09 13:05:55 +08:00
|
|
|
extern bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check);
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2011-04-10 23:42:00 +08:00
|
|
|
#endif /* __TRGM_H__ */
|