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 CPTRGM(a,b) do { \
44 *(((char*)(a))+0) = *(((char*)(b))+0); \
45 *(((char*)(a))+1) = *(((char*)(b))+1); \
46 *(((char*)(a))+2) = *(((char*)(b))+2); \
47} while(0)
48extern int (*CMPTRGM) (const void *a, const void *b);
49
50#define ISWORDCHR(c) (t_isalnum(c))
51#define ISPRINTABLECHAR(a) ( isascii( *(unsigned char*)(a) ) && (isalnum( *(unsigned char*)(a) ) || *(unsigned char*)(a)==' ') )
52#define ISPRINTABLETRGM(t) ( ISPRINTABLECHAR( ((char*)(t)) ) && ISPRINTABLECHAR( ((char*)(t))+1 ) && ISPRINTABLECHAR( ((char*)(t))+2 ) )
53
54#define ISESCAPECHAR(x) (*(x) == '\\') /* Wildcard escape character */
55#define ISWILDCARDCHAR(x) (*(x) == '_' || *(x) == '%') /* Wildcard
56 * meta-character */
58typedef struct
60 int32 vl_len_; /* varlena header (do not touch directly!) */
63} TRGM;
65#define TRGMHDRSIZE (VARHDRSZ + sizeof(uint8))
66
67/* gist */
68#define SIGLEN_DEFAULT (sizeof(int) * 3)
69#define SIGLEN_MAX GISTMaxIndexKeySize
70#define BITBYTE 8
72#define SIGLENBIT(siglen) ((siglen) * BITBYTE - 1) /* see makesign */
74typedef char *BITVECP;
76#define LOOPBYTE(siglen) \
77 for (i = 0; i < (siglen); i++)
79#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
80#define GETBITBYTE(x,i) ( (((char)(x)) >> (i)) & 0x01 )
81#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
82#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
83#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
85#define HASHVAL(val, siglen) (((unsigned int)(val)) % SIGLENBIT(siglen))
86#define HASH(sign, val, siglen) SETBIT((sign), HASHVAL(val, siglen))
88#define ARRKEY 0x01
89#define SIGNKEY 0x02
90#define ALLISTRUE 0x04
92#define ISARRKEY(x) ( ((TRGM*)x)->flag & ARRKEY )
93#define ISSIGNKEY(x) ( ((TRGM*)x)->flag & SIGNKEY )
94#define ISALLTRUE(x) ( ((TRGM*)x)->flag & ALLISTRUE )
96#define CALCGTSIZE(flag, len) ( TRGMHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(trgm)) : (((flag) & ALLISTRUE) ? 0 : (len)) ) )
97#define GETSIGN(x) ( (BITVECP)( (char*)x+TRGMHDRSIZE ) )
98#define GETARR(x) ( (trgm*)( (char*)x+TRGMHDRSIZE ) )
99#define ARRNELEM(x) ( ( VARSIZE(x) - TRGMHDRSIZE )/sizeof(trgm) )
100
101/*
102 * If DIVUNION is defined then similarity formula is:
103 * count / (len1 + len2 - count)
104 * else if DIVUNION is not defined then similarity formula is:
105 * count / max(len1, len2)
106 */
107#ifdef DIVUNION
108#define CALCSML(count, len1, len2) ((float4) (count)) / ((float4) ((len1) + (len2) - (count)))
109#else
110#define CALCSML(count, len1, len2) ((float4) (count)) / ((float4) (((len1) > (len2)) ? (len1) : (len2)))
111#endif
113typedef struct TrgmPackedGraph TrgmPackedGraph;
114
115extern double similarity_threshold;
116extern double word_similarity_threshold;
118
119extern double index_strategy_get_limit(StrategyNumber strategy);
120extern uint32 trgm2int(trgm *ptr);
121extern void compact_trigram(trgm *tptr, char *str, int bytelen);
122extern TRGM *generate_trgm(char *str, int slen);
123extern TRGM *generate_wildcard_trgm(const char *str, int slen);
124extern float4 cnt_sml(TRGM *trg1, TRGM *trg2, bool inexact);
125extern bool trgm_contained_by(TRGM *trg1, TRGM *trg2);
126extern bool *trgm_presence_map(TRGM *query, TRGM *key);
127extern TRGM *createTrgmNFA(text *text_re, Oid collation,
128 TrgmPackedGraph **graph, MemoryContext rcontext);
129extern bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check);
130
131#endif /* __TRGM_H__ */
uint8_t uint8
Definition: c.h:500
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:434
int32_t int32
Definition: c.h:498
uint32_t uint32
Definition: c.h:502
float float4
Definition: c.h:600
const char * str
int b
Definition: isn.c:74
int a
Definition: isn.c:73
const void * data
unsigned int Oid
Definition: postgres_ext.h:30
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: trgm.h:58
Definition: c.h:658
char * flag(int b)
Definition: test-ctype.c:33
double strict_word_similarity_threshold
Definition: trgm_op.c:29
TRGM * generate_trgm(char *str, int slen)
Definition: trgm_op.c:406
uint32 trgm2int(trgm *ptr)
Definition: trgm_op.c:985
int(* CMPTRGM)(const void *a, const void *b)
Definition: trgm_op.c:49
void compact_trigram(trgm *tptr, char *str, int bytelen)
Definition: trgm_op.c:248
bool * trgm_presence_map(TRGM *query, TRGM *key)
Definition: trgm_op.c:1128
double word_similarity_threshold
Definition: trgm_op.c:28
double index_strategy_get_limit(StrategyNumber strategy)
Definition: trgm_op.c:182
TRGM * createTrgmNFA(text *text_re, Oid collation, TrgmPackedGraph **graph, MemoryContext rcontext)
Definition: trgm_regexp.c:524
char * BITVECP
Definition: trgm.h:73
double similarity_threshold
Definition: trgm_op.c:27
bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check)
Definition: trgm_regexp.c:628
bool trgm_contained_by(TRGM *trg1, TRGM *trg2)
Definition: trgm_op.c:1089
char trgm[3]
Definition: trgm.h:41
TRGM * generate_wildcard_trgm(const char *str, int slen)
Definition: trgm_op.c:916
float4 cnt_sml(TRGM *trg1, TRGM *trg2, bool inexact)
Definition: trgm_op.c:1041