PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
trgm.h File Reference
#include "access/gist.h"
#include "access/itup.h"
#include "access/stratnum.h"
#include "storage/bufpage.h"
Include dependency graph for trgm.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TRGM
 

Macros

#define LPADDING   2
 
#define RPADDING   1
 
#define IGNORECASE
 
#define DIVUNION
 
#define SimilarityStrategyNumber   1
 
#define DistanceStrategyNumber   2
 
#define LikeStrategyNumber   3
 
#define ILikeStrategyNumber   4
 
#define RegExpStrategyNumber   5
 
#define RegExpICaseStrategyNumber   6
 
#define WordSimilarityStrategyNumber   7
 
#define WordDistanceStrategyNumber   8
 
#define StrictWordSimilarityStrategyNumber   9
 
#define StrictWordDistanceStrategyNumber   10
 
#define EqualStrategyNumber   11
 
#define CPTRGM(a, b)
 
#define ISWORDCHR(c)   (t_isalnum(c))
 
#define ISPRINTABLECHAR(a)   ( isascii( *(unsigned char*)(a) ) && (isalnum( *(unsigned char*)(a) ) || *(unsigned char*)(a)==' ') )
 
#define ISPRINTABLETRGM(t)   ( ISPRINTABLECHAR( ((char*)(t)) ) && ISPRINTABLECHAR( ((char*)(t))+1 ) && ISPRINTABLECHAR( ((char*)(t))+2 ) )
 
#define ISESCAPECHAR(x)   (*(x) == '\\') /* Wildcard escape character */
 
#define ISWILDCARDCHAR(x)
 
#define TRGMHDRSIZE   (VARHDRSZ + sizeof(uint8))
 
#define SIGLEN_DEFAULT   (sizeof(int) * 3)
 
#define SIGLEN_MAX   GISTMaxIndexKeySize
 
#define BITBYTE   8
 
#define SIGLENBIT(siglen)   ((siglen) * BITBYTE - 1) /* see makesign */
 
#define LOOPBYTE(siglen)    for (i = 0; i < (siglen); i++)
 
#define GETBYTE(x, i)   ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
 
#define GETBITBYTE(x, i)   ( (((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, siglen)   (((unsigned int)(val)) % SIGLENBIT(siglen))
 
#define HASH(sign, val, siglen)   SETBIT((sign), HASHVAL(val, siglen))
 
#define ARRKEY   0x01
 
#define SIGNKEY   0x02
 
#define ALLISTRUE   0x04
 
#define ISARRKEY(x)   ( ((TRGM*)x)->flag & ARRKEY )
 
#define ISSIGNKEY(x)   ( ((TRGM*)x)->flag & SIGNKEY )
 
#define ISALLTRUE(x)   ( ((TRGM*)x)->flag & ALLISTRUE )
 
#define CALCGTSIZE(flag, len)   ( TRGMHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(trgm)) : (((flag) & ALLISTRUE) ? 0 : (len)) ) )
 
#define GETSIGN(x)   ( (BITVECP)( (char*)x+TRGMHDRSIZE ) )
 
#define GETARR(x)   ( (trgm*)( (char*)x+TRGMHDRSIZE ) )
 
#define ARRNELEM(x)   ( ( VARSIZE(x) - TRGMHDRSIZE )/sizeof(trgm) )
 
#define CALCSML(count, len1, len2)   ((float4) (count)) / ((float4) ((len1) + (len2) - (count)))
 

Typedefs

typedef char trgm[3]
 
typedef char * BITVECP
 
typedef struct TrgmPackedGraph TrgmPackedGraph
 

Functions

double index_strategy_get_limit (StrategyNumber strategy)
 
uint32 trgm2int (trgm *ptr)
 
void compact_trigram (trgm *tptr, char *str, int bytelen)
 
TRGMgenerate_trgm (char *str, int slen)
 
TRGMgenerate_wildcard_trgm (const char *str, int slen)
 
float4 cnt_sml (TRGM *trg1, TRGM *trg2, bool inexact)
 
bool trgm_contained_by (TRGM *trg1, TRGM *trg2)
 
bool * trgm_presence_map (TRGM *query, TRGM *key)
 
TRGMcreateTrgmNFA (text *text_re, Oid collation, TrgmPackedGraph **graph, MemoryContext rcontext)
 
bool trigramsMatchGraph (TrgmPackedGraph *graph, bool *check)
 

Variables

int(* CMPTRGM )(const void *a, const void *b)
 
double similarity_threshold
 
double word_similarity_threshold
 
double strict_word_similarity_threshold
 

Macro Definition Documentation

◆ ALLISTRUE

#define ALLISTRUE   0x04

Definition at line 89 of file trgm.h.

◆ ARRKEY

#define ARRKEY   0x01

Definition at line 87 of file trgm.h.

◆ ARRNELEM

#define ARRNELEM (   x)    ( ( VARSIZE(x) - TRGMHDRSIZE )/sizeof(trgm) )

Definition at line 98 of file trgm.h.

◆ BITBYTE

#define BITBYTE   8

Definition at line 69 of file trgm.h.

◆ CALCGTSIZE

#define CALCGTSIZE (   flag,
  len 
)    ( TRGMHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(trgm)) : (((flag) & ALLISTRUE) ? 0 : (len)) ) )

Definition at line 95 of file trgm.h.

◆ CALCSML

#define CALCSML (   count,
  len1,
  len2 
)    ((float4) (count)) / ((float4) ((len1) + (len2) - (count)))

Definition at line 107 of file trgm.h.

◆ CLRBIT

#define CLRBIT (   x,
  i 
)    GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )

Definition at line 80 of file trgm.h.

◆ CPTRGM

#define CPTRGM (   a,
  b 
)
Value:
do { \
*(((char*)(a))+0) = *(((char*)(b))+0); \
*(((char*)(a))+1) = *(((char*)(b))+1); \
*(((char*)(a))+2) = *(((char*)(b))+2); \
} while(0)
int b
Definition: isn.c:74
int a
Definition: isn.c:73

Definition at line 43 of file trgm.h.

◆ DistanceStrategyNumber

#define DistanceStrategyNumber   2

Definition at line 30 of file trgm.h.

◆ DIVUNION

#define DIVUNION

Definition at line 26 of file trgm.h.

◆ EqualStrategyNumber

#define EqualStrategyNumber   11

Definition at line 39 of file trgm.h.

◆ GETARR

#define GETARR (   x)    ( (trgm*)( (char*)x+TRGMHDRSIZE ) )

Definition at line 97 of file trgm.h.

◆ GETBIT

#define GETBIT (   x,
  i 
)    ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )

Definition at line 82 of file trgm.h.

◆ GETBITBYTE

#define GETBITBYTE (   x,
  i 
)    ( (((char)(x)) >> (i)) & 0x01 )

Definition at line 79 of file trgm.h.

◆ GETBYTE

#define GETBYTE (   x,
  i 
)    ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )

Definition at line 78 of file trgm.h.

◆ GETSIGN

#define GETSIGN (   x)    ( (BITVECP)( (char*)x+TRGMHDRSIZE ) )

Definition at line 96 of file trgm.h.

◆ HASH

#define HASH (   sign,
  val,
  siglen 
)    SETBIT((sign), HASHVAL(val, siglen))

Definition at line 85 of file trgm.h.

◆ HASHVAL

#define HASHVAL (   val,
  siglen 
)    (((unsigned int)(val)) % SIGLENBIT(siglen))

Definition at line 84 of file trgm.h.

◆ IGNORECASE

#define IGNORECASE

Definition at line 25 of file trgm.h.

◆ ILikeStrategyNumber

#define ILikeStrategyNumber   4

Definition at line 32 of file trgm.h.

◆ ISALLTRUE

#define ISALLTRUE (   x)    ( ((TRGM*)x)->flag & ALLISTRUE )

Definition at line 93 of file trgm.h.

◆ ISARRKEY

#define ISARRKEY (   x)    ( ((TRGM*)x)->flag & ARRKEY )

Definition at line 91 of file trgm.h.

◆ ISESCAPECHAR

#define ISESCAPECHAR (   x)    (*(x) == '\\') /* Wildcard escape character */

Definition at line 54 of file trgm.h.

◆ ISPRINTABLECHAR

#define ISPRINTABLECHAR (   a)    ( isascii( *(unsigned char*)(a) ) && (isalnum( *(unsigned char*)(a) ) || *(unsigned char*)(a)==' ') )

Definition at line 51 of file trgm.h.

◆ ISPRINTABLETRGM

#define ISPRINTABLETRGM (   t)    ( ISPRINTABLECHAR( ((char*)(t)) ) && ISPRINTABLECHAR( ((char*)(t))+1 ) && ISPRINTABLECHAR( ((char*)(t))+2 ) )

Definition at line 52 of file trgm.h.

◆ ISSIGNKEY

#define ISSIGNKEY (   x)    ( ((TRGM*)x)->flag & SIGNKEY )

Definition at line 92 of file trgm.h.

◆ ISWILDCARDCHAR

#define ISWILDCARDCHAR (   x)
Value:
(*(x) == '_' || *(x) == '%') /* Wildcard
* meta-character */
int x
Definition: isn.c:75

Definition at line 55 of file trgm.h.

◆ ISWORDCHR

#define ISWORDCHR (   c)    (t_isalnum(c))

Definition at line 50 of file trgm.h.

◆ LikeStrategyNumber

#define LikeStrategyNumber   3

Definition at line 31 of file trgm.h.

◆ LOOPBYTE

#define LOOPBYTE (   siglen)     for (i = 0; i < (siglen); i++)

Definition at line 75 of file trgm.h.

◆ LPADDING

#define LPADDING   2

Definition at line 16 of file trgm.h.

◆ RegExpICaseStrategyNumber

#define RegExpICaseStrategyNumber   6

Definition at line 34 of file trgm.h.

◆ RegExpStrategyNumber

#define RegExpStrategyNumber   5

Definition at line 33 of file trgm.h.

◆ RPADDING

#define RPADDING   1

Definition at line 17 of file trgm.h.

◆ SETBIT

#define SETBIT (   x,
  i 
)    GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )

Definition at line 81 of file trgm.h.

◆ SIGLEN_DEFAULT

#define SIGLEN_DEFAULT   (sizeof(int) * 3)

Definition at line 67 of file trgm.h.

◆ SIGLEN_MAX

#define SIGLEN_MAX   GISTMaxIndexKeySize

Definition at line 68 of file trgm.h.

◆ SIGLENBIT

#define SIGLENBIT (   siglen)    ((siglen) * BITBYTE - 1) /* see makesign */

Definition at line 71 of file trgm.h.

◆ SIGNKEY

#define SIGNKEY   0x02

Definition at line 88 of file trgm.h.

◆ SimilarityStrategyNumber

#define SimilarityStrategyNumber   1

Definition at line 29 of file trgm.h.

◆ StrictWordDistanceStrategyNumber

#define StrictWordDistanceStrategyNumber   10

Definition at line 38 of file trgm.h.

◆ StrictWordSimilarityStrategyNumber

#define StrictWordSimilarityStrategyNumber   9

Definition at line 37 of file trgm.h.

◆ TRGMHDRSIZE

#define TRGMHDRSIZE   (VARHDRSZ + sizeof(uint8))

Definition at line 64 of file trgm.h.

◆ WordDistanceStrategyNumber

#define WordDistanceStrategyNumber   8

Definition at line 36 of file trgm.h.

◆ WordSimilarityStrategyNumber

#define WordSimilarityStrategyNumber   7

Definition at line 35 of file trgm.h.

Typedef Documentation

◆ BITVECP

typedef char* BITVECP

Definition at line 73 of file trgm.h.

◆ trgm

typedef char trgm[3]

Definition at line 41 of file trgm.h.

◆ TrgmPackedGraph

Definition at line 112 of file trgm.h.

Function Documentation

◆ cnt_sml()

float4 cnt_sml ( TRGM trg1,
TRGM trg2,
bool  inexact 
)

Definition at line 1041 of file trgm_op.c.

1044{
1045 trgm *ptr1,
1046 *ptr2;
1047 int count = 0;
1048 int len1,
1049 len2;
1050
1051 ptr1 = GETARR(trg1);
1052 ptr2 = GETARR(trg2);
1053
1054 len1 = ARRNELEM(trg1);
1055 len2 = ARRNELEM(trg2);
1056
1057 /* explicit test is needed to avoid 0/0 division when both lengths are 0 */
1058 if (len1 <= 0 || len2 <= 0)
1059 return (float4) 0.0;
1060
1061 while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1062 {
1063 int res = CMPTRGM(ptr1, ptr2);
1064
1065 if (res < 0)
1066 ptr1++;
1067 else if (res > 0)
1068 ptr2++;
1069 else
1070 {
1071 ptr1++;
1072 ptr2++;
1073 count++;
1074 }
1075 }
1076
1077 /*
1078 * If inexact then len2 is equal to count, because we don't know actual
1079 * length of second string in inexact search and we can assume that count
1080 * is a lower bound of len2.
1081 */
float float4
Definition: c.h:600
#define ARRNELEM(x)
Definition: trgm.h:98
char trgm[3]
Definition: trgm.h:41
#define GETARR(x)
Definition: trgm.h:97
int(* CMPTRGM)(const void *a, const void *b)
Definition: trgm_op.c:49

References ARRNELEM, CALCSML, CMPTRGM, and GETARR.

Referenced by gtrgm_consistent(), gtrgm_distance(), and similarity().

◆ compact_trigram()

void compact_trigram ( trgm tptr,
char *  str,
int  bytelen 
)

Definition at line 248 of file trgm_op.c.

251{
252 if (bytelen == 3)
253 {
254 CPTRGM(tptr, str);
255 }
256 else
257 {
259
261 COMP_LEGACY_CRC32(crc, str, bytelen);
263
264 /*
265 * use only 3 upper bytes from crc, hope, it's good enough hashing
266 */
267 CPTRGM(tptr, &crc);
const char * str
return crc
uint32 pg_crc32
Definition: pg_crc.h:37
#define INIT_LEGACY_CRC32(crc)
Definition: pg_crc.h:79
#define COMP_LEGACY_CRC32(crc, data, len)
Definition: pg_crc.h:81
#define FIN_LEGACY_CRC32(crc)
Definition: pg_crc.h:80
#define CPTRGM(a, b)
Definition: trgm.h:43

References COMP_LEGACY_CRC32, CPTRGM, crc, FIN_LEGACY_CRC32, INIT_LEGACY_CRC32, and str.

Referenced by fillTrgm(), and make_trigrams().

◆ createTrgmNFA()

TRGM * createTrgmNFA ( text text_re,
Oid  collation,
TrgmPackedGraph **  graph,
MemoryContext  rcontext 
)

Definition at line 524 of file trgm_regexp.c.

526{
527 TRGM *trg;
528 regex_t regex;
529 MemoryContext tmpcontext;
530 MemoryContext oldcontext;
531
532 /*
533 * This processing generates a great deal of cruft, which we'd like to
534 * clean up before returning (since this function may be called in a
535 * query-lifespan memory context). Make a temp context we can work in so
536 * that cleanup is easy.
537 */
539 "createTrgmNFA temporary context",
541 oldcontext = MemoryContextSwitchTo(tmpcontext);
542
543 /*
544 * Stage 1: Compile the regexp into a NFA, using the regexp library.
545 */
546#ifdef IGNORECASE
547 RE_compile(&regex, text_re,
548 REG_ADVANCED | REG_NOSUB | REG_ICASE, collation);
549#else
550 RE_compile(&regex, text_re,
551 REG_ADVANCED | REG_NOSUB, collation);
552#endif
553
554 trg = createTrgmNFAInternal(&regex, graph, rcontext);
555
556 /* Clean up all the cruft we created (including regex) */
557 MemoryContextSwitchTo(oldcontext);
558 MemoryContextDelete(tmpcontext);
559
560 return trg;
561}
MemoryContext CurrentMemoryContext
Definition: mcxt.c:159
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:485
#define AllocSetContextCreate
Definition: memutils.h:149
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:180
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define REG_ICASE
Definition: regex.h:184
#define REG_ADVANCED
Definition: regex.h:181
#define REG_NOSUB
Definition: regex.h:185
#define regex_t
Definition: regex.h:245
Definition: trgm.h:58
static TRGM * createTrgmNFAInternal(regex_t *regex, TrgmPackedGraph **graph, MemoryContext rcontext)
Definition: trgm_regexp.c:567
static void RE_compile(regex_t *regex, text *text_re, int cflags, Oid collation)
Definition: trgm_regexp.c:721

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, createTrgmNFAInternal(), CurrentMemoryContext, MemoryContextDelete(), MemoryContextSwitchTo(), RE_compile(), REG_ADVANCED, REG_ICASE, REG_NOSUB, and regex_t.

Referenced by gin_extract_query_trgm(), and gtrgm_consistent().

◆ generate_trgm()

TRGM * generate_trgm ( char *  str,
int  slen 
)

Definition at line 406 of file trgm_op.c.

409{
410 TRGM *trg;
411 int len;
412
413 protect_out_of_mem(slen);
414
415 trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
416 trg->flag = ARRKEY;
417
418 len = generate_trgm_only(GETARR(trg), str, slen, NULL);
420
421 if (len == 0)
422 return trg;
423
424 /*
425 * Make trigrams unique.
426 */
427 if (len > 1)
428 {
429 qsort(GETARR(trg), len, sizeof(trgm), comp_trgm);
430 len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
431 }
432
434
#define CALCGTSIZE(flag, siglen)
Definition: hstore_gist.c:60
void * palloc(Size size)
Definition: mcxt.c:1943
const void size_t len
#define qsort(a, b, c, d)
Definition: port.h:479
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
uint8 flag
Definition: trgm.h:60
#define ARRKEY
Definition: trgm.h:87
#define TRGMHDRSIZE
Definition: trgm.h:64
static int comp_trgm(const void *a, const void *b)
Definition: trgm_op.c:211
static void protect_out_of_mem(int slen)
Definition: trgm_op.c:389
static int generate_trgm_only(trgm *trg, char *str, int slen, TrgmBound *bounds)
Definition: trgm_op.c:325
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References ARRKEY, CALCGTSIZE, comp_trgm(), TRGM::flag, generate_trgm_only(), GETARR, len, palloc(), protect_out_of_mem(), qsort, qunique(), SET_VARSIZE, str, and TRGMHDRSIZE.

Referenced by gin_extract_query_trgm(), gin_extract_value_trgm(), gtrgm_compress(), gtrgm_consistent(), gtrgm_distance(), show_trgm(), and similarity().

◆ generate_wildcard_trgm()

TRGM * generate_wildcard_trgm ( const char *  str,
int  slen 
)

Definition at line 916 of file trgm_op.c.

919{
920 TRGM *trg;
921 char *buf,
922 *buf2;
923 trgm *tptr;
924 int len,
925 charlen,
926 bytelen;
927 const char *eword;
928
929 protect_out_of_mem(slen);
930
931 trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
932 trg->flag = ARRKEY;
934
935 if (slen + LPADDING + RPADDING < 3 || slen == 0)
936 return trg;
937
938 tptr = GETARR(trg);
939
940 /* Allocate a buffer for blank-padded, but not yet case-folded, words */
941 buf = palloc(sizeof(char) * (slen + 4));
942
943 /*
944 * Extract trigrams from each substring extracted by get_wildcard_part.
945 */
946 eword = str;
947 while ((eword = get_wildcard_part(eword, slen - (eword - str),
948 buf, &bytelen, &charlen)) != NULL)
949 {
950#ifdef IGNORECASE
951 buf2 = str_tolower(buf, bytelen, DEFAULT_COLLATION_OID);
952 bytelen = strlen(buf2);
953#else
954 buf2 = buf;
955#endif
956
957 /*
958 * count trigrams
959 */
960 tptr = make_trigrams(tptr, buf2, bytelen, charlen);
961
962#ifdef IGNORECASE
963 pfree(buf2);
964#endif
965 }
966
967 pfree(buf);
968
969 if ((len = tptr - GETARR(trg)) == 0)
970 return trg;
971
972 /*
973 * Make trigrams unique.
974 */
975 if (len > 1)
976 {
977 qsort(GETARR(trg), len, sizeof(trgm), comp_trgm);
978 len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
979 }
980
982
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1637
void pfree(void *pointer)
Definition: mcxt.c:2150
static char * buf
Definition: pg_test_fsync.c:72
#define RPADDING
Definition: trgm.h:17
#define LPADDING
Definition: trgm.h:16
static trgm * make_trigrams(trgm *tptr, char *str, int bytelen, int charlen)
Definition: trgm_op.c:273
static const char * get_wildcard_part(const char *str, int lenstr, char *buf, int *bytelen, int *charlen)
Definition: trgm_op.c:771

References ARRKEY, buf, CALCGTSIZE, comp_trgm(), TRGM::flag, get_wildcard_part(), GETARR, len, LPADDING, make_trigrams(), palloc(), pfree(), protect_out_of_mem(), qsort, qunique(), RPADDING, SET_VARSIZE, str, str_tolower(), and TRGMHDRSIZE.

Referenced by gin_extract_query_trgm(), and gtrgm_consistent().

◆ index_strategy_get_limit()

double index_strategy_get_limit ( StrategyNumber  strategy)

Definition at line 182 of file trgm_op.c.

185{
186 switch (strategy)
187 {
194 default:
195 elog(ERROR, "unrecognized strategy number: %d", strategy);
196 break;
197 }
198
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define WordSimilarityStrategyNumber
Definition: trgm.h:35
#define StrictWordSimilarityStrategyNumber
Definition: trgm.h:37
#define SimilarityStrategyNumber
Definition: trgm.h:29
double strict_word_similarity_threshold
Definition: trgm_op.c:29
double word_similarity_threshold
Definition: trgm_op.c:28
double similarity_threshold
Definition: trgm_op.c:27

References elog, ERROR, similarity_threshold, SimilarityStrategyNumber, strict_word_similarity_threshold, StrictWordSimilarityStrategyNumber, word_similarity_threshold, and WordSimilarityStrategyNumber.

Referenced by gin_trgm_consistent(), gin_trgm_triconsistent(), and gtrgm_consistent().

◆ trgm2int()

uint32 trgm2int ( trgm ptr)

Definition at line 985 of file trgm_op.c.

988{
989 uint32 val = 0;
990
991 val |= *(((unsigned char *) ptr));
992 val <<= 8;
993 val |= *(((unsigned char *) ptr) + 1);
994 val <<= 8;
995 val |= *(((unsigned char *) ptr) + 2);
996
uint32_t uint32
Definition: c.h:502
long val
Definition: informix.c:689

References val.

Referenced by gin_extract_query_trgm(), gin_extract_value_trgm(), and show_trgm().

◆ trgm_contained_by()

bool trgm_contained_by ( TRGM trg1,
TRGM trg2 
)

Definition at line 1089 of file trgm_op.c.

1092{
1093 trgm *ptr1,
1094 *ptr2;
1095 int len1,
1096 len2;
1097
1098 ptr1 = GETARR(trg1);
1099 ptr2 = GETARR(trg2);
1100
1101 len1 = ARRNELEM(trg1);
1102 len2 = ARRNELEM(trg2);
1103
1104 while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1105 {
1106 int res = CMPTRGM(ptr1, ptr2);
1107
1108 if (res < 0)
1109 return false;
1110 else if (res > 0)
1111 ptr2++;
1112 else
1113 {
1114 ptr1++;
1115 ptr2++;
1116 }
1117 }
1118 if (ptr1 - GETARR(trg1) < len1)
1119 return false;
1120 else

References ARRNELEM, CMPTRGM, and GETARR.

Referenced by gtrgm_consistent().

◆ trgm_presence_map()

bool * trgm_presence_map ( TRGM query,
TRGM key 
)

Definition at line 1128 of file trgm_op.c.

1131{
1132 bool *result;
1133 trgm *ptrq = GETARR(query),
1134 *ptrk = GETARR(key);
1135 int lenq = ARRNELEM(query),
1136 lenk = ARRNELEM(key),
1137 i;
1138
1139 result = (bool *) palloc0(lenq * sizeof(bool));
1140
1141 /* for each query trigram, do a binary search in the key array */
1142 for (i = 0; i < lenq; i++)
1143 {
1144 int lo = 0;
1145 int hi = lenk;
1146
1147 while (lo < hi)
1148 {
1149 int mid = (lo + hi) / 2;
1150 int res = CMPTRGM(ptrq, ptrk + mid);
1151
1152 if (res < 0)
1153 hi = mid;
1154 else if (res > 0)
1155 lo = mid + 1;
1156 else
1157 {
1158 result[i] = true;
1159 break;
1160 }
1161 }
1162 ptrq++;
1163 }
1164
int i
Definition: isn.c:77
void * palloc0(Size size)
Definition: mcxt.c:1973

References ARRNELEM, CMPTRGM, GETARR, i, sort-test::key, and palloc0().

Referenced by gtrgm_consistent().

◆ trigramsMatchGraph()

bool trigramsMatchGraph ( TrgmPackedGraph graph,
bool *  check 
)

Definition at line 628 of file trgm_regexp.c.

629{
630 int i,
631 j,
632 k,
633 queueIn,
634 queueOut;
635
636 /*
637 * Reset temporary working areas.
638 */
639 memset(graph->colorTrigramsActive, 0,
640 sizeof(bool) * graph->colorTrigramsCount);
641 memset(graph->statesActive, 0, sizeof(bool) * graph->statesCount);
642
643 /*
644 * Check which color trigrams were matched. A match for any simple
645 * trigram associated with a color trigram counts as a match of the color
646 * trigram.
647 */
648 j = 0;
649 for (i = 0; i < graph->colorTrigramsCount; i++)
650 {
651 int cnt = graph->colorTrigramGroups[i];
652
653 for (k = j; k < j + cnt; k++)
654 {
655 if (check[k])
656 {
657 /*
658 * Found one matched trigram in the group. Can skip the rest
659 * of them and go to the next group.
660 */
661 graph->colorTrigramsActive[i] = true;
662 break;
663 }
664 }
665 j = j + cnt;
666 }
667
668 /*
669 * Initialize the statesQueue to hold just the initial state. Note:
670 * statesQueue has room for statesCount entries, which is certainly enough
671 * since no state will be put in the queue more than once. The
672 * statesActive array marks which states have been queued.
673 */
674 graph->statesActive[0] = true;
675 graph->statesQueue[0] = 0;
676 queueIn = 0;
677 queueOut = 1;
678
679 /* Process queued states as long as there are any. */
680 while (queueIn < queueOut)
681 {
682 int stateno = graph->statesQueue[queueIn++];
683 TrgmPackedState *state = &graph->states[stateno];
684 int cnt = state->arcsCount;
685
686 /* Loop over state's out-arcs */
687 for (i = 0; i < cnt; i++)
688 {
689 TrgmPackedArc *arc = &state->arcs[i];
690
691 /*
692 * If corresponding color trigram is present then activate the
693 * corresponding state. We're done if that's the final state,
694 * otherwise queue the state if it's not been queued already.
695 */
696 if (graph->colorTrigramsActive[arc->colorTrgm])
697 {
698 int nextstate = arc->targetState;
699
700 if (nextstate == 1)
701 return true; /* success: final state is reachable */
702
703 if (!graph->statesActive[nextstate])
704 {
705 graph->statesActive[nextstate] = true;
706 graph->statesQueue[queueOut++] = nextstate;
707 }
708 }
709 }
710 }
711
712 /* Queue is empty, so match fails. */
713 return false;
714}
int j
Definition: isn.c:78
TrgmPackedState * states
Definition: trgm_regexp.c:461
bool * statesActive
Definition: trgm_regexp.c:465
bool * colorTrigramsActive
Definition: trgm_regexp.c:464
int * colorTrigramGroups
Definition: trgm_regexp.c:454
Definition: regguts.h:296
Definition: regguts.h:323

References TrgmPackedGraph::colorTrigramGroups, TrgmPackedGraph::colorTrigramsActive, TrgmPackedGraph::colorTrigramsCount, i, j, TrgmPackedGraph::states, TrgmPackedGraph::statesActive, TrgmPackedGraph::statesCount, and TrgmPackedGraph::statesQueue.

Referenced by gin_trgm_consistent(), gin_trgm_triconsistent(), and gtrgm_consistent().

Variable Documentation

◆ CMPTRGM

int(* CMPTRGM) (const void *a, const void *b) ( const void *  a,
const void *  b 
)
extern

◆ similarity_threshold

double similarity_threshold
extern

Definition at line 27 of file trgm_op.c.

Referenced by _PG_init(), index_strategy_get_limit(), set_limit(), show_limit(), and similarity_op().

◆ strict_word_similarity_threshold

double strict_word_similarity_threshold
extern

◆ word_similarity_threshold

double word_similarity_threshold
extern