PostgreSQL Source Code  git master
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 KEEPONLYALNUM
 
#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 CMPCHAR(a, b)   ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )
 
#define CMPPCHAR(a, b, i)   CMPCHAR( *(((const char*)(a))+i), *(((const char*)(b))+i) )
 
#define CMPTRGM(a, b)   ( CMPPCHAR(a,b,0) ? CMPPCHAR(a,b,0) : ( CMPPCHAR(a,b,1) ? CMPPCHAR(a,b,1) : CMPPCHAR(a,b,2) ) )
 
#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

double similarity_threshold
 
double word_similarity_threshold
 
double strict_word_similarity_threshold
 

Macro Definition Documentation

◆ ALLISTRUE

#define ALLISTRUE   0x04

Definition at line 98 of file trgm.h.

◆ ARRKEY

#define ARRKEY   0x01

Definition at line 96 of file trgm.h.

◆ ARRNELEM

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

Definition at line 107 of file trgm.h.

◆ BITBYTE

#define BITBYTE   8

Definition at line 78 of file trgm.h.

◆ CALCGTSIZE

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

Definition at line 104 of file trgm.h.

◆ CALCSML

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

Definition at line 116 of file trgm.h.

◆ CLRBIT

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

Definition at line 89 of file trgm.h.

◆ CMPCHAR

#define CMPCHAR (   a,
  b 
)    ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )

Definition at line 44 of file trgm.h.

◆ CMPPCHAR

#define CMPPCHAR (   a,
  b,
  i 
)    CMPCHAR( *(((const char*)(a))+i), *(((const char*)(b))+i) )

Definition at line 45 of file trgm.h.

◆ CMPTRGM

#define CMPTRGM (   a,
  b 
)    ( CMPPCHAR(a,b,0) ? CMPPCHAR(a,b,0) : ( CMPPCHAR(a,b,1) ? CMPPCHAR(a,b,1) : CMPPCHAR(a,b,2) ) )

Definition at line 46 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:69
int a
Definition: isn.c:68

Definition at line 48 of file trgm.h.

◆ DistanceStrategyNumber

#define DistanceStrategyNumber   2

Definition at line 31 of file trgm.h.

◆ DIVUNION

#define DIVUNION

Definition at line 27 of file trgm.h.

◆ EqualStrategyNumber

#define EqualStrategyNumber   11

Definition at line 40 of file trgm.h.

◆ GETARR

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

Definition at line 106 of file trgm.h.

◆ GETBIT

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

Definition at line 91 of file trgm.h.

◆ GETBITBYTE

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

Definition at line 88 of file trgm.h.

◆ GETBYTE

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

Definition at line 87 of file trgm.h.

◆ GETSIGN

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

Definition at line 105 of file trgm.h.

◆ HASH

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

Definition at line 94 of file trgm.h.

◆ HASHVAL

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

Definition at line 93 of file trgm.h.

◆ IGNORECASE

#define IGNORECASE

Definition at line 26 of file trgm.h.

◆ ILikeStrategyNumber

#define ILikeStrategyNumber   4

Definition at line 33 of file trgm.h.

◆ ISALLTRUE

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

Definition at line 102 of file trgm.h.

◆ ISARRKEY

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

Definition at line 100 of file trgm.h.

◆ ISESCAPECHAR

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

Definition at line 63 of file trgm.h.

◆ ISPRINTABLECHAR

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

Definition at line 56 of file trgm.h.

◆ ISPRINTABLETRGM

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

Definition at line 61 of file trgm.h.

◆ ISSIGNKEY

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

Definition at line 101 of file trgm.h.

◆ ISWILDCARDCHAR

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

Definition at line 64 of file trgm.h.

◆ ISWORDCHR

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

Definition at line 55 of file trgm.h.

◆ KEEPONLYALNUM

#define KEEPONLYALNUM

Definition at line 18 of file trgm.h.

◆ LikeStrategyNumber

#define LikeStrategyNumber   3

Definition at line 32 of file trgm.h.

◆ LOOPBYTE

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

Definition at line 84 of file trgm.h.

◆ LPADDING

#define LPADDING   2

Definition at line 16 of file trgm.h.

◆ RegExpICaseStrategyNumber

#define RegExpICaseStrategyNumber   6

Definition at line 35 of file trgm.h.

◆ RegExpStrategyNumber

#define RegExpStrategyNumber   5

Definition at line 34 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 90 of file trgm.h.

◆ SIGLEN_DEFAULT

#define SIGLEN_DEFAULT   (sizeof(int) * 3)

Definition at line 76 of file trgm.h.

◆ SIGLEN_MAX

#define SIGLEN_MAX   GISTMaxIndexKeySize

Definition at line 77 of file trgm.h.

◆ SIGLENBIT

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

Definition at line 80 of file trgm.h.

◆ SIGNKEY

#define SIGNKEY   0x02

Definition at line 97 of file trgm.h.

◆ SimilarityStrategyNumber

#define SimilarityStrategyNumber   1

Definition at line 30 of file trgm.h.

◆ StrictWordDistanceStrategyNumber

#define StrictWordDistanceStrategyNumber   10

Definition at line 39 of file trgm.h.

◆ StrictWordSimilarityStrategyNumber

#define StrictWordSimilarityStrategyNumber   9

Definition at line 38 of file trgm.h.

◆ TRGMHDRSIZE

#define TRGMHDRSIZE   (VARHDRSZ + sizeof(uint8))

Definition at line 73 of file trgm.h.

◆ WordDistanceStrategyNumber

#define WordDistanceStrategyNumber   8

Definition at line 37 of file trgm.h.

◆ WordSimilarityStrategyNumber

#define WordSimilarityStrategyNumber   7

Definition at line 36 of file trgm.h.

Typedef Documentation

◆ BITVECP

typedef char* BITVECP

Definition at line 82 of file trgm.h.

◆ trgm

typedef char trgm[3]

Definition at line 42 of file trgm.h.

◆ TrgmPackedGraph

Definition at line 82 of file trgm.h.

Function Documentation

◆ cnt_sml()

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

Definition at line 992 of file trgm_op.c.

995 {
996  trgm *ptr1,
997  *ptr2;
998  int count = 0;
999  int len1,
1000  len2;
1001 
1002  ptr1 = GETARR(trg1);
1003  ptr2 = GETARR(trg2);
1004 
1005  len1 = ARRNELEM(trg1);
1006  len2 = ARRNELEM(trg2);
1007 
1008  /* explicit test is needed to avoid 0/0 division when both lengths are 0 */
1009  if (len1 <= 0 || len2 <= 0)
1010  return (float4) 0.0;
1011 
1012  while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1013  {
1014  int res = CMPTRGM(ptr1, ptr2);
1015 
1016  if (res < 0)
1017  ptr1++;
1018  else if (res > 0)
1019  ptr2++;
1020  else
1021  {
1022  ptr1++;
1023  ptr2++;
1024  count++;
1025  }
1026  }
1027 
1028  /*
1029  * If inexact then len2 is equal to count, because we don't know actual
1030  * length of second string in inexact search and we can assume that count
1031  * is a lower bound of len2.
1032  */
float float4
Definition: c.h:583
#define ARRNELEM(x)
Definition: trgm.h:107
#define CMPTRGM(a, b)
Definition: trgm.h:46
char trgm[3]
Definition: trgm.h:42
#define GETARR(x)
Definition: trgm.h:106

References ARRNELEM, CALCSML, CMPTRGM, GETARR, and res.

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

◆ compact_trigram()

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

Definition at line 199 of file trgm_op.c.

202 {
203  if (bytelen == 3)
204  {
205  CPTRGM(tptr, str);
206  }
207  else
208  {
209  pg_crc32 crc;
210 
212  COMP_LEGACY_CRC32(crc, str, bytelen);
214 
215  /*
216  * use only 3 upper bytes from crc, hope, it's good enough hashing
217  */
218  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:48

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 522 of file trgm_regexp.c.

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

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 357 of file trgm_op.c.

360 {
361  TRGM *trg;
362  int len;
363 
364  protect_out_of_mem(slen);
365 
366  trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
367  trg->flag = ARRKEY;
368 
369  len = generate_trgm_only(GETARR(trg), str, slen, NULL);
371 
372  if (len == 0)
373  return trg;
374 
375  /*
376  * Make trigrams unique.
377  */
378  if (len > 1)
379  {
380  qsort(GETARR(trg), len, sizeof(trgm), comp_trgm);
381  len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
382  }
383 
385 
#define CALCGTSIZE(flag, siglen)
Definition: hstore_gist.c:60
void * palloc(Size size)
Definition: mcxt.c:1317
const void size_t len
#define qsort(a, b, c, d)
Definition: port.h:447
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:69
#define ARRKEY
Definition: trgm.h:96
#define TRGMHDRSIZE
Definition: trgm.h:73
static int comp_trgm(const void *a, const void *b)
Definition: trgm_op.c:162
static void protect_out_of_mem(int slen)
Definition: trgm_op.c:340
static int generate_trgm_only(trgm *trg, char *str, int slen, TrgmBound *bounds)
Definition: trgm_op.c:276
#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 867 of file trgm_op.c.

870 {
871  TRGM *trg;
872  char *buf,
873  *buf2;
874  trgm *tptr;
875  int len,
876  charlen,
877  bytelen;
878  const char *eword;
879 
880  protect_out_of_mem(slen);
881 
882  trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
883  trg->flag = ARRKEY;
884  SET_VARSIZE(trg, TRGMHDRSIZE);
885 
886  if (slen + LPADDING + RPADDING < 3 || slen == 0)
887  return trg;
888 
889  tptr = GETARR(trg);
890 
891  /* Allocate a buffer for blank-padded, but not yet case-folded, words */
892  buf = palloc(sizeof(char) * (slen + 4));
893 
894  /*
895  * Extract trigrams from each substring extracted by get_wildcard_part.
896  */
897  eword = str;
898  while ((eword = get_wildcard_part(eword, slen - (eword - str),
899  buf, &bytelen, &charlen)) != NULL)
900  {
901 #ifdef IGNORECASE
902  buf2 = lowerstr_with_len(buf, bytelen);
903  bytelen = strlen(buf2);
904 #else
905  buf2 = buf;
906 #endif
907 
908  /*
909  * count trigrams
910  */
911  tptr = make_trigrams(tptr, buf2, bytelen, charlen);
912 
913 #ifdef IGNORECASE
914  pfree(buf2);
915 #endif
916  }
917 
918  pfree(buf);
919 
920  if ((len = tptr - GETARR(trg)) == 0)
921  return trg;
922 
923  /*
924  * Make trigrams unique.
925  */
926  if (len > 1)
927  {
928  qsort(GETARR(trg), len, sizeof(trgm), comp_trgm);
929  len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
930  }
931 
933 
void pfree(void *pointer)
Definition: mcxt.c:1521
static char * buf
Definition: pg_test_fsync.c:72
#define RPADDING
Definition: trgm.h:17
#define LPADDING
Definition: trgm.h:16
static const char * get_wildcard_part(const char *str, int lenstr, char *buf, int *bytelen, int *charlen)
Definition: trgm_op.c:722
static trgm * make_trigrams(trgm *tptr, char *str, int bytelen, int charlen)
Definition: trgm_op.c:224
char * lowerstr_with_len(const char *str, int len)
Definition: ts_locale.c:266

References ARRKEY, buf, CALCGTSIZE, comp_trgm(), TRGM::flag, get_wildcard_part(), GETARR, len, lowerstr_with_len(), LPADDING, make_trigrams(), palloc(), pfree(), protect_out_of_mem(), qsort, qunique(), RPADDING, SET_VARSIZE, str, 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 133 of file trgm_op.c.

136 {
137  switch (strategy)
138  {
140  return similarity_threshold;
145  default:
146  elog(ERROR, "unrecognized strategy number: %d", strategy);
147  break;
148  }
149 
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define WordSimilarityStrategyNumber
Definition: trgm.h:36
#define StrictWordSimilarityStrategyNumber
Definition: trgm.h:38
#define SimilarityStrategyNumber
Definition: trgm.h:30
double strict_word_similarity_threshold
Definition: trgm_op.c:24
double word_similarity_threshold
Definition: trgm_op.c:23
double similarity_threshold
Definition: trgm_op.c:22

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 936 of file trgm_op.c.

939 {
940  uint32 val = 0;
941 
942  val |= *(((unsigned char *) ptr));
943  val <<= 8;
944  val |= *(((unsigned char *) ptr) + 1);
945  val <<= 8;
946  val |= *(((unsigned char *) ptr) + 2);
947 
uint32_t uint32
Definition: c.h:485
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 1040 of file trgm_op.c.

1043 {
1044  trgm *ptr1,
1045  *ptr2;
1046  int len1,
1047  len2;
1048 
1049  ptr1 = GETARR(trg1);
1050  ptr2 = GETARR(trg2);
1051 
1052  len1 = ARRNELEM(trg1);
1053  len2 = ARRNELEM(trg2);
1054 
1055  while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1056  {
1057  int res = CMPTRGM(ptr1, ptr2);
1058 
1059  if (res < 0)
1060  return false;
1061  else if (res > 0)
1062  ptr2++;
1063  else
1064  {
1065  ptr1++;
1066  ptr2++;
1067  }
1068  }
1069  if (ptr1 - GETARR(trg1) < len1)
1070  return false;
1071  else

References ARRNELEM, CMPTRGM, GETARR, and res.

Referenced by gtrgm_consistent().

◆ trgm_presence_map()

bool* trgm_presence_map ( TRGM query,
TRGM key 
)

Definition at line 1079 of file trgm_op.c.

1082 {
1083  bool *result;
1084  trgm *ptrq = GETARR(query),
1085  *ptrk = GETARR(key);
1086  int lenq = ARRNELEM(query),
1087  lenk = ARRNELEM(key),
1088  i;
1089 
1090  result = (bool *) palloc0(lenq * sizeof(bool));
1091 
1092  /* for each query trigram, do a binary search in the key array */
1093  for (i = 0; i < lenq; i++)
1094  {
1095  int lo = 0;
1096  int hi = lenk;
1097 
1098  while (lo < hi)
1099  {
1100  int mid = (lo + hi) / 2;
1101  int res = CMPTRGM(ptrq, ptrk + mid);
1102 
1103  if (res < 0)
1104  hi = mid;
1105  else if (res > 0)
1106  lo = mid + 1;
1107  else
1108  {
1109  result[i] = true;
1110  break;
1111  }
1112  }
1113  ptrq++;
1114  }
1115 
int i
Definition: isn.c:72
void * palloc0(Size size)
Definition: mcxt.c:1347

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

Referenced by gtrgm_consistent().

◆ trigramsMatchGraph()

bool trigramsMatchGraph ( TrgmPackedGraph graph,
bool *  check 
)

Definition at line 626 of file trgm_regexp.c.

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

◆ similarity_threshold

double similarity_threshold
extern

Definition at line 22 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