PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
trgm.h
Go to the documentation of this file.
1/*
2 * contrib/pg_trgm/trgm.h
3 */
4#ifndef __TRGM_H__
5#define __TRGM_H__
6
7#include "access/gist.h"
8#include "access/itup.h"
9#include "access/stratnum.h"
10#include "storage/bufpage.h"
11
12/*
13 * Options ... but note that trgm_regexp.c effectively assumes these values
14 * of LPADDING and RPADDING.
15 */
16#define LPADDING 2
17#define RPADDING 1
18/*
19 * Caution: IGNORECASE macro means that trigrams are case-insensitive.
20 * If this macro is disabled, the ~* and ~~* operators must be removed from
21 * the operator classes, because we can't handle case-insensitive wildcard
22 * search with case-sensitive trigrams. Failure to do this will result in
23 * "cannot handle ~*(~~*) with case-sensitive trigrams" errors.
24 */
25#define IGNORECASE
26#define DIVUNION
27
28/* operator strategy numbers */
29#define SimilarityStrategyNumber 1
30#define DistanceStrategyNumber 2
31#define LikeStrategyNumber 3
32#define ILikeStrategyNumber 4
33#define RegExpStrategyNumber 5
34#define RegExpICaseStrategyNumber 6
35#define WordSimilarityStrategyNumber 7
36#define WordDistanceStrategyNumber 8
37#define StrictWordSimilarityStrategyNumber 9
38#define StrictWordDistanceStrategyNumber 10
39#define EqualStrategyNumber 11
40
41typedef char trgm[3];
42
43#define CMPCHAR(a,b) ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )
44#define CMPPCHAR(a,b,i) CMPCHAR( *(((const char*)(a))+i), *(((const char*)(b))+i) )
45#define CMPTRGM(a,b) ( CMPPCHAR(a,b,0) ? CMPPCHAR(a,b,0) : ( CMPPCHAR(a,b,1) ? CMPPCHAR(a,b,1) : CMPPCHAR(a,b,2) ) )
46
47#define CPTRGM(a,b) do { \
48 *(((char*)(a))+0) = *(((char*)(b))+0); \
49 *(((char*)(a))+1) = *(((char*)(b))+1); \
50 *(((char*)(a))+2) = *(((char*)(b))+2); \
51} while(0)
52
53#define ISWORDCHR(c) (t_isalnum(c))
54#define ISPRINTABLECHAR(a) ( isascii( *(unsigned char*)(a) ) && (isalnum( *(unsigned char*)(a) ) || *(unsigned char*)(a)==' ') )
55#define ISPRINTABLETRGM(t) ( ISPRINTABLECHAR( ((char*)(t)) ) && ISPRINTABLECHAR( ((char*)(t))+1 ) && ISPRINTABLECHAR( ((char*)(t))+2 ) )
56
57#define ISESCAPECHAR(x) (*(x) == '\\') /* Wildcard escape character */
58#define ISWILDCARDCHAR(x) (*(x) == '_' || *(x) == '%') /* Wildcard
59 * meta-character */
61typedef struct
63 int32 vl_len_; /* varlena header (do not touch directly!) */
66} TRGM;
68#define TRGMHDRSIZE (VARHDRSZ + sizeof(uint8))
69
70/* gist */
71#define SIGLEN_DEFAULT (sizeof(int) * 3)
72#define SIGLEN_MAX GISTMaxIndexKeySize
73#define BITBYTE 8
75#define SIGLENBIT(siglen) ((siglen) * BITBYTE - 1) /* see makesign */
77typedef char *BITVECP;
79#define LOOPBYTE(siglen) \
80 for (i = 0; i < (siglen); i++)
82#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
83#define GETBITBYTE(x,i) ( (((char)(x)) >> (i)) & 0x01 )
84#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
85#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
86#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
88#define HASHVAL(val, siglen) (((unsigned int)(val)) % SIGLENBIT(siglen))
89#define HASH(sign, val, siglen) SETBIT((sign), HASHVAL(val, siglen))
91#define ARRKEY 0x01
92#define SIGNKEY 0x02
93#define ALLISTRUE 0x04
95#define ISARRKEY(x) ( ((TRGM*)x)->flag & ARRKEY )
96#define ISSIGNKEY(x) ( ((TRGM*)x)->flag & SIGNKEY )
97#define ISALLTRUE(x) ( ((TRGM*)x)->flag & ALLISTRUE )
99#define CALCGTSIZE(flag, len) ( TRGMHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(trgm)) : (((flag) & ALLISTRUE) ? 0 : (len)) ) )
100#define GETSIGN(x) ( (BITVECP)( (char*)x+TRGMHDRSIZE ) )
101#define GETARR(x) ( (trgm*)( (char*)x+TRGMHDRSIZE ) )
102#define ARRNELEM(x) ( ( VARSIZE(x) - TRGMHDRSIZE )/sizeof(trgm) )
103
104/*
105 * If DIVUNION is defined then similarity formula is:
106 * count / (len1 + len2 - count)
107 * else if DIVUNION is not defined then similarity formula is:
108 * count / max(len1, len2)
109 */
110#ifdef DIVUNION
111#define CALCSML(count, len1, len2) ((float4) (count)) / ((float4) ((len1) + (len2) - (count)))
112#else
113#define CALCSML(count, len1, len2) ((float4) (count)) / ((float4) (((len1) > (len2)) ? (len1) : (len2)))
114#endif
116typedef struct TrgmPackedGraph TrgmPackedGraph;
117
118extern double similarity_threshold;
119extern double word_similarity_threshold;
121
122extern double index_strategy_get_limit(StrategyNumber strategy);
123extern uint32 trgm2int(trgm *ptr);
124extern void compact_trigram(trgm *tptr, char *str, int bytelen);
125extern TRGM *generate_trgm(char *str, int slen);
126extern TRGM *generate_wildcard_trgm(const char *str, int slen);
127extern float4 cnt_sml(TRGM *trg1, TRGM *trg2, bool inexact);
128extern bool trgm_contained_by(TRGM *trg1, TRGM *trg2);
129extern bool *trgm_presence_map(TRGM *query, TRGM *key);
130extern TRGM *createTrgmNFA(text *text_re, Oid collation,
131 TrgmPackedGraph **graph, MemoryContext rcontext);
132extern bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check);
133
134#endif /* __TRGM_H__ */
uint8_t uint8
Definition: c.h:483
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:417
int32_t int32
Definition: c.h:481
uint32_t uint32
Definition: c.h:485
float float4
Definition: c.h:583
const char * str
const void * data
unsigned int Oid
Definition: postgres_ext.h:31
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: trgm.h:61
Definition: c.h:641
char * flag(int b)
Definition: test-ctype.c:33
double strict_word_similarity_threshold
Definition: trgm_op.c:26
TRGM * generate_trgm(char *str, int slen)
Definition: trgm_op.c:359
uint32 trgm2int(trgm *ptr)
Definition: trgm_op.c:938
void compact_trigram(trgm *tptr, char *str, int bytelen)
Definition: trgm_op.c:201
bool * trgm_presence_map(TRGM *query, TRGM *key)
Definition: trgm_op.c:1081
double word_similarity_threshold
Definition: trgm_op.c:25
double index_strategy_get_limit(StrategyNumber strategy)
Definition: trgm_op.c:135
TRGM * createTrgmNFA(text *text_re, Oid collation, TrgmPackedGraph **graph, MemoryContext rcontext)
Definition: trgm_regexp.c:524
char * BITVECP
Definition: trgm.h:76
double similarity_threshold
Definition: trgm_op.c:24
bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check)
Definition: trgm_regexp.c:628
bool trgm_contained_by(TRGM *trg1, TRGM *trg2)
Definition: trgm_op.c:1042
char trgm[3]
Definition: trgm.h:41
TRGM * generate_wildcard_trgm(const char *str, int slen)
Definition: trgm_op.c:869
float4 cnt_sml(TRGM *trg1, TRGM *trg2, bool inexact)
Definition: trgm_op.c:994