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)
 
booltrgm_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:70
int a
Definition: isn.c:69

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:71

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

999 {
1000  trgm *ptr1,
1001  *ptr2;
1002  int count = 0;
1003  int len1,
1004  len2;
1005 
1006  ptr1 = GETARR(trg1);
1007  ptr2 = GETARR(trg2);
1008 
1009  len1 = ARRNELEM(trg1);
1010  len2 = ARRNELEM(trg2);
1011 
1012  /* explicit test is needed to avoid 0/0 division when both lengths are 0 */
1013  if (len1 <= 0 || len2 <= 0)
1014  return (float4) 0.0;
1015 
1016  while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1017  {
1018  int res = CMPTRGM(ptr1, ptr2);
1019 
1020  if (res < 0)
1021  ptr1++;
1022  else if (res > 0)
1023  ptr2++;
1024  else
1025  {
1026  ptr1++;
1027  ptr2++;
1028  count++;
1029  }
1030  }
1031 
1032  /*
1033  * If inexact then len2 is equal to count, because we don't know actual
1034  * length of second string in inexact search and we can assume that count
1035  * is a lower bound of len2.
1036  */
float float4
Definition: c.h:613
#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 198 of file trgm_op.c.

201 {
202  if (bytelen == 3)
203  {
204  CPTRGM(tptr, str);
205  }
206  else
207  {
208  pg_crc32 crc;
209 
211  COMP_LEGACY_CRC32(crc, str, bytelen);
213 
214  /*
215  * use only 3 upper bytes from crc, hope, it's good enough hashing
216  */
217  CPTRGM(tptr, &crc);
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 generate_unaccent_rules::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:135
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:403
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
#define REG_ICASE
Definition: regex.h:106
#define REG_ADVANCED
Definition: regex.h:103
#define REG_NOSUB
Definition: regex.h:107
Definition: trgm.h:67
Definition: regex.h:56
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, and REG_NOSUB.

Referenced by gin_extract_query_trgm(), and gtrgm_consistent().

◆ generate_trgm()

TRGM* generate_trgm ( char *  str,
int  slen 
)

Definition at line 356 of file trgm_op.c.

359 {
360  TRGM *trg;
361  int len;
362 
363  protect_out_of_mem(slen);
364 
365  trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
366  trg->flag = ARRKEY;
367 
368  len = generate_trgm_only(GETARR(trg), str, slen, NULL);
370 
371  if (len == 0)
372  return trg;
373 
374  /*
375  * Make trigrams unique.
376  */
377  if (len > 1)
378  {
379  qsort(GETARR(trg), len, sizeof(trgm), comp_trgm);
380  len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
381  }
382 
384 
#define CALCGTSIZE(flag, siglen)
Definition: hstore_gist.c:59
void * palloc(Size size)
Definition: mcxt.c:1226
const void size_t len
#define qsort(a, b, c, d)
Definition: port.h:445
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:161
static void protect_out_of_mem(int slen)
Definition: trgm_op.c:339
static int generate_trgm_only(trgm *trg, char *str, int slen, TrgmBound *bounds)
Definition: trgm_op.c:275
#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, generate_unaccent_rules::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 871 of file trgm_op.c.

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

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, generate_unaccent_rules::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 132 of file trgm_op.c.

135 {
136  switch (strategy)
137  {
139  return similarity_threshold;
144  default:
145  elog(ERROR, "unrecognized strategy number: %d", strategy);
146  break;
147  }
148 
#define ERROR
Definition: elog.h:39
#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:23
double word_similarity_threshold
Definition: trgm_op.c:22
double similarity_threshold
Definition: trgm_op.c:21

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

943 {
944  uint32 val = 0;
945 
946  val |= *(((unsigned char *) ptr));
947  val <<= 8;
948  val |= *(((unsigned char *) ptr) + 1);
949  val <<= 8;
950  val |= *(((unsigned char *) ptr) + 2);
951 
unsigned int uint32
Definition: c.h:490
long val
Definition: informix.c:664

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

1047 {
1048  trgm *ptr1,
1049  *ptr2;
1050  int len1,
1051  len2;
1052 
1053  ptr1 = GETARR(trg1);
1054  ptr2 = GETARR(trg2);
1055 
1056  len1 = ARRNELEM(trg1);
1057  len2 = ARRNELEM(trg2);
1058 
1059  while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1060  {
1061  int res = CMPTRGM(ptr1, ptr2);
1062 
1063  if (res < 0)
1064  return false;
1065  else if (res > 0)
1066  ptr2++;
1067  else
1068  {
1069  ptr1++;
1070  ptr2++;
1071  }
1072  }
1073  if (ptr1 - GETARR(trg1) < len1)
1074  return false;
1075  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 1083 of file trgm_op.c.

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

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:74
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 21 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