| 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 |
|
| 41 |
typedef 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) |
| 48 |
extern int (*CMPTRGM) (const void *a, const void *b); |
| 49 |
|
| 50 |
#define ISWORDCHR(c, len) (t_isalnum_with_len(c, len)) |
| 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 */ |
| 57 |
|
| 58 |
typedef struct |
| 59 |
{ |
| 60 |
int32 vl_len_; /* varlena header (do not touch directly!) */ |
| 61 |
uint8 flag; |
| 62 |
char data[FLEXIBLE_ARRAY_MEMBER]; |
| 63 |
} TRGM; |
| 64 |
|
| 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 |
| 71 |
|
| 72 |
#define SIGLENBIT(siglen) ((siglen) * BITBYTE - 1) /* see makesign */ |
| 73 |
|
| 74 |
typedef char *BITVECP; |
| 75 |
|
| 76 |
#define LOOPBYTE(siglen) \ |
| 77 |
for (i = 0; i < (siglen); i++) |
| 78 |
|
| 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 ) |
| 84 |
|
| 85 |
#define HASHVAL(val, siglen) (((unsigned int)(val)) % SIGLENBIT(siglen)) |
| 86 |
#define HASH(sign, val, siglen) SETBIT((sign), HASHVAL(val, siglen)) |
| 87 |
|
| 88 |
#define ARRKEY 0x01 |
| 89 |
#define SIGNKEY 0x02 |
| 90 |
#define ALLISTRUE 0x04 |
| 91 |
|
| 92 |
#define ISARRKEY(x) ( ((TRGM*)x)->flag & ARRKEY ) |
| 93 |
#define ISSIGNKEY(x) ( ((TRGM*)x)->flag & SIGNKEY ) |
| 94 |
#define ISALLTRUE(x) ( ((TRGM*)x)->flag & ALLISTRUE ) |
| 95 |
|
| 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 |
| 112 |
|
| 113 |
typedef struct TrgmPackedGraph TrgmPackedGraph; |
| 114 |
|
| 115 |
extern double similarity_threshold; |
| 116 |
extern double word_similarity_threshold; |
| 117 |
extern double strict_word_similarity_threshold; |
| 118 |
|
| 119 |
extern double index_strategy_get_limit(StrategyNumber strategy); |
| 120 |
extern uint32 trgm2int(trgm *ptr); |
| 121 |
extern void compact_trigram(trgm *tptr, char *str, int bytelen); |
| 122 |
extern TRGM *generate_trgm(char *str, int slen); |
| 123 |
extern TRGM *generate_wildcard_trgm(const char *str, int slen); |
| 124 |
extern float4 cnt_sml(TRGM *trg1, TRGM *trg2, bool inexact); |
| 125 |
extern bool trgm_contained_by(TRGM *trg1, TRGM *trg2); |
| 126 |
extern bool *trgm_presence_map(TRGM *query, TRGM *key); |
| 127 |
extern TRGM *createTrgmNFA(text *text_re, Oid collation, |
| 128 |
TrgmPackedGraph **graph, MemoryContext rcontext); |
| 129 |
extern bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check); |
| 130 |
|
| 131 |
#endif /* __TRGM_H__ */ |