PostgreSQL Source Code  git master
tsgistidx.c File Reference
#include "postgres.h"
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
#include "utils/pg_crc.h"
Include dependency graph for tsgistidx.c:

Go to the source code of this file.

Data Structures

struct  GistTsVectorOptions
 
struct  SignTSVector
 
struct  CHKVAL
 
struct  CACHESIGN
 
struct  SPLITCOST
 

Macros

#define SIGLEN_DEFAULT   (31 * 4)
 
#define SIGLEN_MAX   GISTMaxIndexKeySize
 
#define GET_SIGLEN()
 
#define SIGLENBIT(siglen)   ((siglen) * BITS_PER_BYTE)
 
#define LOOPBYTE(siglen)   for (i = 0; i < siglen; i++)
 
#define GETBYTE(x, i)   ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
 
#define GETBITBYTE(x, i)   ( ((char)(x)) >> (i) & 0x01 )
 
#define CLRBIT(x, i)   GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
 
#define SETBIT(x, i)   GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) )
 
#define GETBIT(x, i)   ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
 
#define HASHVAL(val, siglen)   (((unsigned int)(val)) % SIGLENBIT(siglen))
 
#define HASH(sign, val, siglen)   SETBIT((sign), HASHVAL(val, siglen))
 
#define GETENTRY(vec, pos)   ((SignTSVector *) DatumGetPointer((vec)->vector[(pos)].key))
 
#define ARRKEY   0x01
 
#define SIGNKEY   0x02
 
#define ALLISTRUE   0x04
 
#define ISARRKEY(x)   ( ((SignTSVector*)(x))->flag & ARRKEY )
 
#define ISSIGNKEY(x)   ( ((SignTSVector*)(x))->flag & SIGNKEY )
 
#define ISALLTRUE(x)   ( ((SignTSVector*)(x))->flag & ALLISTRUE )
 
#define GTHDRSIZE   ( VARHDRSZ + sizeof(int32) )
 
#define CALCGTSIZE(flag, len)   ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int32)) : (((flag) & ALLISTRUE) ? 0 : (len)) ) )
 
#define GETSIGN(x)   ( (BITVECP)( (char*)(x)+GTHDRSIZE ) )
 
#define GETSIGLEN(x)   ( VARSIZE(x) - GTHDRSIZE )
 
#define GETARR(x)   ( (int32*)( (char*)(x)+GTHDRSIZE ) )
 
#define ARRNELEM(x)   ( ( VARSIZE(x) - GTHDRSIZE )/sizeof(int32) )
 
#define SINGOUTSTR   "%d true bits, %d false bits"
 
#define ARROUTSTR   "%d unique words"
 
#define EXTRALEN   ( 2*13 )
 
#define WISH_F(a, b, c)   (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
 

Typedefs

typedef char * BITVECP
 

Functions

static int32 sizebitvec (BITVECP sign, int siglen)
 
Datum gtsvectorin (PG_FUNCTION_ARGS)
 
Datum gtsvectorout (PG_FUNCTION_ARGS)
 
static int compareint (const void *va, const void *vb)
 
static void makesign (BITVECP sign, SignTSVector *a, int siglen)
 
static SignTSVectorgtsvector_alloc (int flag, int len, BITVECP sign)
 
Datum gtsvector_compress (PG_FUNCTION_ARGS)
 
Datum gtsvector_decompress (PG_FUNCTION_ARGS)
 
static bool checkcondition_arr (void *checkval, QueryOperand *val, ExecPhraseData *data)
 
static bool checkcondition_bit (void *checkval, QueryOperand *val, ExecPhraseData *data)
 
Datum gtsvector_consistent (PG_FUNCTION_ARGS)
 
static int32 unionkey (BITVECP sbase, SignTSVector *add, int siglen)
 
Datum gtsvector_union (PG_FUNCTION_ARGS)
 
Datum gtsvector_same (PG_FUNCTION_ARGS)
 
static int hemdistsign (BITVECP a, BITVECP b, int siglen)
 
static int hemdist (SignTSVector *a, SignTSVector *b)
 
Datum gtsvector_penalty (PG_FUNCTION_ARGS)
 
static void fillcache (CACHESIGN *item, SignTSVector *key, int siglen)
 
static int comparecost (const void *va, const void *vb)
 
static int hemdistcache (CACHESIGN *a, CACHESIGN *b, int siglen)
 
Datum gtsvector_picksplit (PG_FUNCTION_ARGS)
 
Datum gtsvector_consistent_oldsig (PG_FUNCTION_ARGS)
 
Datum gtsvector_options (PG_FUNCTION_ARGS)
 

Variables

static int outbuf_maxlen = 0
 

Macro Definition Documentation

◆ ALLISTRUE

#define ALLISTRUE   0x04

◆ ARRKEY

#define ARRKEY   0x01

Definition at line 69 of file tsgistidx.c.

Referenced by gtsvector_compress().

◆ ARRNELEM

#define ARRNELEM (   x)    ( ( VARSIZE(x) - GTHDRSIZE )/sizeof(int32) )

Definition at line 83 of file tsgistidx.c.

Referenced by gtsvector_consistent(), gtsvector_same(), gtsvectorout(), makesign(), and unionkey().

◆ ARROUTSTR

#define ARROUTSTR   "%d unique words"

Definition at line 97 of file tsgistidx.c.

Referenced by gtsvectorout().

◆ CALCGTSIZE

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

Definition at line 78 of file tsgistidx.c.

Referenced by gtsvector_alloc(), gtsvector_compress(), and gtsvector_union().

◆ CLRBIT

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

Definition at line 49 of file tsgistidx.c.

◆ EXTRALEN

#define EXTRALEN   ( 2*13 )

Definition at line 98 of file tsgistidx.c.

Referenced by gtsvectorout().

◆ GET_SIGLEN

#define GET_SIGLEN ( )
Value:
#define SIGLEN_DEFAULT
Definition: tsgistidx.c:34
#define PG_GET_OPCLASS_OPTIONS()
Definition: fmgr.h:341
#define PG_HAS_OPCLASS_OPTIONS()
Definition: fmgr.h:340

Definition at line 36 of file tsgistidx.c.

Referenced by gtsvector_compress(), gtsvector_penalty(), gtsvector_picksplit(), gtsvector_same(), and gtsvector_union().

◆ GETARR

#define GETARR (   x)    ( (int32*)( (char*)(x)+GTHDRSIZE ) )

◆ GETBIT

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

Definition at line 51 of file tsgistidx.c.

Referenced by checkcondition_bit().

◆ GETBITBYTE

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

Definition at line 48 of file tsgistidx.c.

◆ GETBYTE

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

Definition at line 47 of file tsgistidx.c.

◆ GETENTRY

#define GETENTRY (   vec,
  pos 
)    ((SignTSVector *) DatumGetPointer((vec)->vector[(pos)].key))

Definition at line 56 of file tsgistidx.c.

Referenced by gtsvector_picksplit(), and gtsvector_union().

◆ GETSIGLEN

#define GETSIGLEN (   x)    ( VARSIZE(x) - GTHDRSIZE )

Definition at line 81 of file tsgistidx.c.

Referenced by checkcondition_bit(), gtsvector_same(), gtsvectorout(), hemdist(), and unionkey().

◆ GETSIGN

◆ GTHDRSIZE

#define GTHDRSIZE   ( VARHDRSZ + sizeof(int32) )

Definition at line 77 of file tsgistidx.c.

◆ HASH

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

Definition at line 54 of file tsgistidx.c.

Referenced by makesign(), and unionkey().

◆ HASHVAL

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

Definition at line 53 of file tsgistidx.c.

Referenced by checkcondition_bit().

◆ ISALLTRUE

◆ ISARRKEY

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

Definition at line 73 of file tsgistidx.c.

Referenced by fillcache(), gtsvector_penalty(), and gtsvectorout().

◆ ISSIGNKEY

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

Definition at line 74 of file tsgistidx.c.

Referenced by gtsvector_compress(), gtsvector_consistent(), gtsvector_same(), and unionkey().

◆ LOOPBYTE

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

◆ SETBIT

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

Definition at line 50 of file tsgistidx.c.

◆ SIGLEN_DEFAULT

#define SIGLEN_DEFAULT   (31 * 4)

Definition at line 34 of file tsgistidx.c.

Referenced by gtsvector_options().

◆ SIGLEN_MAX

#define SIGLEN_MAX   GISTMaxIndexKeySize

Definition at line 35 of file tsgistidx.c.

Referenced by gtsvector_options().

◆ SIGLENBIT

#define SIGLENBIT (   siglen)    ((siglen) * BITS_PER_BYTE)

◆ SIGNKEY

#define SIGNKEY   0x02

◆ SINGOUTSTR

#define SINGOUTSTR   "%d true bits, %d false bits"

Definition at line 96 of file tsgistidx.c.

Referenced by gtsvectorout().

◆ WISH_F

#define WISH_F (   a,
  b,
  c 
)    (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )

Definition at line 575 of file tsgistidx.c.

Referenced by gtsvector_picksplit().

Typedef Documentation

◆ BITVECP

typedef char* BITVECP

Definition at line 42 of file tsgistidx.c.

Function Documentation

◆ checkcondition_arr()

static bool checkcondition_arr ( void *  checkval,
QueryOperand val,
ExecPhraseData data 
)
static

Definition at line 279 of file tsgistidx.c.

References QueryOperand::prefix, and QueryOperand::valcrc.

Referenced by gtsvector_consistent().

280 {
281  int32 *StopLow = ((CHKVAL *) checkval)->arrb;
282  int32 *StopHigh = ((CHKVAL *) checkval)->arre;
283  int32 *StopMiddle;
284 
285  /* Loop invariant: StopLow <= val < StopHigh */
286 
287  /*
288  * we are not able to find a prefix by hash value
289  */
290  if (val->prefix)
291  return true;
292 
293  while (StopLow < StopHigh)
294  {
295  StopMiddle = StopLow + (StopHigh - StopLow) / 2;
296  if (*StopMiddle == val->valcrc)
297  return true;
298  else if (*StopMiddle < val->valcrc)
299  StopLow = StopMiddle + 1;
300  else
301  StopHigh = StopMiddle;
302  }
303 
304  return false;
305 }
signed int int32
Definition: c.h:355
int32 valcrc
Definition: ts_type.h:151
bool prefix
Definition: ts_type.h:150

◆ checkcondition_bit()

static bool checkcondition_bit ( void *  checkval,
QueryOperand val,
ExecPhraseData data 
)
static

Definition at line 308 of file tsgistidx.c.

References GETBIT, GETSIGLEN, GETSIGN, HASHVAL, sort-test::key, QueryOperand::prefix, and QueryOperand::valcrc.

Referenced by gtsvector_consistent().

309 {
310  void *key = (SignTSVector *) checkval;
311 
312  /*
313  * we are not able to find a prefix in signature tree
314  */
315  if (val->prefix)
316  return true;
317  return GETBIT(GETSIGN(key), HASHVAL(val->valcrc, GETSIGLEN(key)));
318 }
#define GETSIGLEN(x)
Definition: tsgistidx.c:81
int32 valcrc
Definition: ts_type.h:151
#define GETBIT(x, i)
Definition: tsgistidx.c:51
#define HASHVAL(val, siglen)
Definition: tsgistidx.c:53
#define GETSIGN(x)
Definition: tsgistidx.c:80
bool prefix
Definition: ts_type.h:150

◆ comparecost()

static int comparecost ( const void *  va,
const void *  vb 
)
static

Definition at line 583 of file tsgistidx.c.

References SPLITCOST::cost.

Referenced by gtsvector_picksplit().

584 {
585  const SPLITCOST *a = (const SPLITCOST *) va;
586  const SPLITCOST *b = (const SPLITCOST *) vb;
587 
588  if (a->cost == b->cost)
589  return 0;
590  else
591  return (a->cost > b->cost) ? 1 : -1;
592 }
int32 cost
Definition: hstore_gist.c:347

◆ compareint()

static int compareint ( const void *  va,
const void *  vb 
)
static

Definition at line 127 of file tsgistidx.c.

Referenced by gtsvector_compress().

128 {
129  int32 a = *((const int32 *) va);
130  int32 b = *((const int32 *) vb);
131 
132  if (a == b)
133  return 0;
134  return (a > b) ? 1 : -1;
135 }
signed int int32
Definition: c.h:355

◆ fillcache()

static void fillcache ( CACHESIGN item,
SignTSVector key,
int  siglen 
)
static

Definition at line 564 of file tsgistidx.c.

References CACHESIGN::allistrue, GETSIGN, ISALLTRUE, ISARRKEY, makesign(), and CACHESIGN::sign.

Referenced by gtsvector_picksplit().

565 {
566  item->allistrue = false;
567  if (ISARRKEY(key))
568  makesign(item->sign, key, siglen);
569  else if (ISALLTRUE(key))
570  item->allistrue = true;
571  else
572  memcpy((void *) item->sign, (void *) GETSIGN(key), siglen);
573 }
#define ISARRKEY(x)
Definition: tsgistidx.c:73
static void makesign(BITVECP sign, SignTSVector *a, int siglen)
Definition: tsgistidx.c:138
#define GETSIGN(x)
Definition: tsgistidx.c:80
#define ISALLTRUE(x)
Definition: tsgistidx.c:75
bool allistrue
Definition: trgm_gist.c:735
BITVECP sign
Definition: trgm_gist.c:736

◆ gtsvector_alloc()

static SignTSVector* gtsvector_alloc ( int  flag,
int  len,
BITVECP  sign 
)
static

Definition at line 150 of file tsgistidx.c.

References ALLISTRUE, CALCGTSIZE, flag(), SignTSVector::flag, GETSIGN, palloc(), SET_VARSIZE, and SIGNKEY.

Referenced by gtsvector_compress(), gtsvector_picksplit(), and gtsvector_union().

151 {
152  int size = CALCGTSIZE(flag, len);
153  SignTSVector *res = palloc(size);
154 
155  SET_VARSIZE(res, size);
156  res->flag = flag;
157 
158  if ((flag & (SIGNKEY | ALLISTRUE)) == SIGNKEY && sign)
159  memcpy(GETSIGN(res), sign, len);
160 
161  return res;
162 }
#define CALCGTSIZE(flag, len)
Definition: tsgistidx.c:78
char sign
Definition: informix.c:668
char * flag(int b)
Definition: test-ctype.c:33
int32 flag
Definition: tsgistidx.c:65
#define GETSIGN(x)
Definition: tsgistidx.c:80
#define SIGNKEY
Definition: tsgistidx.c:70
void * palloc(Size size)
Definition: mcxt.c:949
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define ALLISTRUE
Definition: tsgistidx.c:71

◆ gtsvector_compress()

Datum gtsvector_compress ( PG_FUNCTION_ARGS  )

Definition at line 166 of file tsgistidx.c.

References ALLISTRUE, ARRKEY, ARRPTR, CALCGTSIZE, COMP_LEGACY_CRC32, compareint(), DatumGetPointer, DatumGetTSVector, FIN_LEGACY_CRC32, GET_SIGLEN, GETARR, GETSIGN, gistentryinit, gtsvector_alloc(), i, INIT_LEGACY_CRC32, ISALLTRUE, ISSIGNKEY, GISTENTRY::key, GISTENTRY::leafkey, WordEntry::len, LOOPBYTE, makesign(), GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, WordEntry::pos, qsort, qunique(), GISTENTRY::rel, repalloc(), SET_VARSIZE, sign, SIGNKEY, TSVectorData::size, STRPTR, TOAST_INDEX_TARGET, val, and VARSIZE.

167 {
168  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
169  int siglen = GET_SIGLEN();
170  GISTENTRY *retval = entry;
171 
172  if (entry->leafkey)
173  { /* tsvector */
174  TSVector val = DatumGetTSVector(entry->key);
175  SignTSVector *res = gtsvector_alloc(ARRKEY, val->size, NULL);
176  int32 len;
177  int32 *arr;
178  WordEntry *ptr = ARRPTR(val);
179  char *words = STRPTR(val);
180 
181  arr = GETARR(res);
182  len = val->size;
183  while (len--)
184  {
185  pg_crc32 c;
186 
188  COMP_LEGACY_CRC32(c, words + ptr->pos, ptr->len);
189  FIN_LEGACY_CRC32(c);
190 
191  *arr = *(int32 *) &c;
192  arr++;
193  ptr++;
194  }
195 
196  qsort(GETARR(res), val->size, sizeof(int), compareint);
197  len = qunique(GETARR(res), val->size, sizeof(int), compareint);
198  if (len != val->size)
199  {
200  /*
201  * there is a collision of hash-function; len is always less than
202  * val->size
203  */
204  len = CALCGTSIZE(ARRKEY, len);
205  res = (SignTSVector *) repalloc((void *) res, len);
206  SET_VARSIZE(res, len);
207  }
208 
209  /* make signature, if array is too long */
210  if (VARSIZE(res) > TOAST_INDEX_TARGET)
211  {
212  SignTSVector *ressign = gtsvector_alloc(SIGNKEY, siglen, NULL);
213 
214  makesign(GETSIGN(ressign), res, siglen);
215  res = ressign;
216  }
217 
218  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
219  gistentryinit(*retval, PointerGetDatum(res),
220  entry->rel, entry->page,
221  entry->offset, false);
222  }
223  else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
224  !ISALLTRUE(DatumGetPointer(entry->key)))
225  {
226  int32 i;
227  SignTSVector *res;
229 
230  LOOPBYTE(siglen)
231  {
232  if ((sign[i] & 0xff) != 0xff)
233  PG_RETURN_POINTER(retval);
234  }
235 
236  res = gtsvector_alloc(SIGNKEY | ALLISTRUE, siglen, sign);
237  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
238  gistentryinit(*retval, PointerGetDatum(res),
239  entry->rel, entry->page,
240  entry->offset, false);
241  }
242  PG_RETURN_POINTER(retval);
243 }
Relation rel
Definition: gist.h:152
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define INIT_LEGACY_CRC32(crc)
Definition: pg_crc.h:79
static int compareint(const void *va, const void *vb)
Definition: tsgistidx.c:127
#define GETARR(x)
Definition: tsgistidx.c:82
#define VARSIZE(PTR)
Definition: postgres.h:303
#define PointerGetDatum(X)
Definition: postgres.h:556
static void makesign(BITVECP sign, SignTSVector *a, int siglen)
Definition: tsgistidx.c:138
uint32 len
Definition: ts_type.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define LOOPBYTE(siglen)
Definition: tsgistidx.c:44
#define FIN_LEGACY_CRC32(crc)
Definition: pg_crc.h:80
signed int int32
Definition: c.h:355
Page page
Definition: gist.h:153
#define TOAST_INDEX_TARGET
Definition: heaptoast.h:68
#define CALCGTSIZE(flag, len)
Definition: tsgistidx.c:78
char sign
Definition: informix.c:668
Datum key
Definition: gist.h:151
char * c
int32 size
Definition: ts_type.h:93
#define ARRKEY
Definition: tsgistidx.c:69
#define DatumGetTSVector(X)
Definition: ts_type.h:117
bool leafkey
Definition: gist.h:155
#define GETSIGN(x)
Definition: tsgistidx.c:80
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:235
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
uint32 pos
Definition: ts_type.h:44
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
static SignTSVector * gtsvector_alloc(int flag, int len, BITVECP sign)
Definition: tsgistidx.c:150
#define SIGNKEY
Definition: tsgistidx.c:70
#define DatumGetPointer(X)
Definition: postgres.h:549
void * palloc(Size size)
Definition: mcxt.c:949
#define STRPTR(x)
Definition: hstore.h:76
#define COMP_LEGACY_CRC32(crc, data, len)
Definition: pg_crc.h:81
int i
#define ISALLTRUE(x)
Definition: tsgistidx.c:75
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
char * BITVECP
Definition: hstore_gist.c:30
uint32 pg_crc32
Definition: pg_crc.h:37
#define ARRPTR(x)
Definition: cube.c:24
#define qsort(a, b, c, d)
Definition: port.h:479
OffsetNumber offset
Definition: gist.h:154
#define ISSIGNKEY(x)
Definition: tsgistidx.c:74
#define ALLISTRUE
Definition: tsgistidx.c:71
long val
Definition: informix.c:664
#define GET_SIGLEN()
Definition: tsgistidx.c:36

◆ gtsvector_consistent()

Datum gtsvector_consistent ( PG_FUNCTION_ARGS  )

Definition at line 321 of file tsgistidx.c.

References CHKVAL::arrb, CHKVAL::arre, ARRNELEM, checkcondition_arr(), checkcondition_bit(), DatumGetPointer, GETARR, GETQUERY, ISALLTRUE, ISSIGNKEY, sort-test::key, GISTENTRY::key, PG_GETARG_POINTER, PG_GETARG_TSQUERY, PG_RETURN_BOOL, TSQueryData::size, TS_EXEC_CALC_NOT, TS_EXEC_PHRASE_NO_POS, and TS_execute().

Referenced by gtsvector_consistent_oldsig().

322 {
323  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
324  TSQuery query = PG_GETARG_TSQUERY(1);
325 
326  /* StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); */
327  /* Oid subtype = PG_GETARG_OID(3); */
328  bool *recheck = (bool *) PG_GETARG_POINTER(4);
330 
331  /* All cases served by this function are inexact */
332  *recheck = true;
333 
334  if (!query->size)
335  PG_RETURN_BOOL(false);
336 
337  if (ISSIGNKEY(key))
338  {
339  if (ISALLTRUE(key))
340  PG_RETURN_BOOL(true);
341 
342  /* since signature is lossy, cannot specify CALC_NOT here */
344  key,
347  }
348  else
349  { /* only leaf pages */
350  CHKVAL chkval;
351 
352  chkval.arrb = GETARR(key);
353  chkval.arre = chkval.arrb + ARRNELEM(key);
355  (void *) &chkval,
358  }
359 }
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1797
#define GETARR(x)
Definition: tsgistidx.c:82
static bool checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
Definition: tsgistidx.c:308
#define TS_EXEC_CALC_NOT
Definition: ts_utils.h:183
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define GETQUERY(x)
Definition: _int.h:157
int32 * arrb
Definition: _int_bool.c:227
Datum key
Definition: gist.h:151
static bool checkcondition_arr(void *checkval, QueryOperand *val, ExecPhraseData *data)
Definition: tsgistidx.c:279
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define TS_EXEC_PHRASE_NO_POS
Definition: ts_utils.h:190
#define ARRNELEM(x)
Definition: tsgistidx.c:83
#define DatumGetPointer(X)
Definition: postgres.h:549
int32 * arre
Definition: _int_bool.c:228
int32 size
Definition: ts_type.h:208
#define ISALLTRUE(x)
Definition: tsgistidx.c:75
#define ISSIGNKEY(x)
Definition: tsgistidx.c:74

◆ gtsvector_consistent_oldsig()

Datum gtsvector_consistent_oldsig ( PG_FUNCTION_ARGS  )

Definition at line 794 of file tsgistidx.c.

References gtsvector_consistent().

795 {
796  return gtsvector_consistent(fcinfo);
797 }
Datum gtsvector_consistent(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:321

◆ gtsvector_decompress()

Datum gtsvector_decompress ( PG_FUNCTION_ARGS  )

Definition at line 246 of file tsgistidx.c.

References DatumGetPointer, gistentryinit, sort-test::key, GISTENTRY::key, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_DETOAST_DATUM, PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, and GISTENTRY::rel.

247 {
248  /*
249  * We need to detoast the stored value, because the other gtsvector
250  * support functions don't cope with toasted values.
251  */
252  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
254 
255  if (key != (SignTSVector *) DatumGetPointer(entry->key))
256  {
257  GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
258 
259  gistentryinit(*retval, PointerGetDatum(key),
260  entry->rel, entry->page,
261  entry->offset, false);
262 
263  PG_RETURN_POINTER(retval);
264  }
265 
266  PG_RETURN_POINTER(entry);
267 }
Relation rel
Definition: gist.h:152
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
Page page
Definition: gist.h:153
Datum key
Definition: gist.h:151
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:235
#define DatumGetPointer(X)
Definition: postgres.h:549
void * palloc(Size size)
Definition: mcxt.c:949
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
OffsetNumber offset
Definition: gist.h:154

◆ gtsvector_options()

Datum gtsvector_options ( PG_FUNCTION_ARGS  )

Definition at line 800 of file tsgistidx.c.

References add_local_int_reloption(), init_local_reloptions(), offsetof, PG_GETARG_POINTER, PG_RETURN_VOID, SIGLEN_DEFAULT, and SIGLEN_MAX.

801 {
803 
804  init_local_reloptions(relopts, sizeof(GistTsVectorOptions));
805  add_local_int_reloption(relopts, "siglen", "signature length",
807  offsetof(GistTsVectorOptions, siglen));
808 
809  PG_RETURN_VOID();
810 }
void init_local_reloptions(local_relopts *opts, Size relopt_struct_size)
Definition: reloptions.c:710
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define SIGLEN_DEFAULT
Definition: tsgistidx.c:34
void add_local_int_reloption(local_relopts *relopts, const char *name, const char *desc, int default_val, int min_val, int max_val, int offset)
Definition: reloptions.c:894
#define SIGLEN_MAX
Definition: tsgistidx.c:35
#define PG_RETURN_VOID()
Definition: fmgr.h:348
#define offsetof(type, field)
Definition: c.h:661

◆ gtsvector_penalty()

Datum gtsvector_penalty ( PG_FUNCTION_ARGS  )

Definition at line 521 of file tsgistidx.c.

References DatumGetPointer, GET_SIGLEN, GETSIGN, hemdist(), hemdistsign(), ISALLTRUE, ISARRKEY, GISTENTRY::key, makesign(), newval, palloc(), pfree(), PG_GETARG_POINTER, PG_RETURN_POINTER, SIGLENBIT, sign, and sizebitvec().

522 {
523  GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
524  GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
525  float *penalty = (float *) PG_GETARG_POINTER(2);
526  int siglen = GET_SIGLEN();
527  SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
529  BITVECP orig = GETSIGN(origval);
530 
531  *penalty = 0.0;
532 
533  if (ISARRKEY(newval))
534  {
535  BITVECP sign = palloc(siglen);
536 
537  makesign(sign, newval, siglen);
538 
539  if (ISALLTRUE(origval))
540  {
541  int siglenbit = SIGLENBIT(siglen);
542 
543  *penalty =
544  (float) (siglenbit - sizebitvec(sign, siglen)) /
545  (float) (siglenbit + 1);
546  }
547  else
548  *penalty = hemdistsign(sign, orig, siglen);
549 
550  pfree(sign);
551  }
552  else
553  *penalty = hemdist(origval, newval);
554  PG_RETURN_POINTER(penalty);
555 }
static int hemdist(SignTSVector *a, SignTSVector *b)
Definition: tsgistidx.c:500
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define SIGLENBIT(siglen)
Definition: tsgistidx.c:40
static int32 sizebitvec(BITVECP sign, int siglen)
Definition: tsgistidx.c:478
#define ISARRKEY(x)
Definition: tsgistidx.c:73
static void makesign(BITVECP sign, SignTSVector *a, int siglen)
Definition: tsgistidx.c:138
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
void pfree(void *pointer)
Definition: mcxt.c:1056
char sign
Definition: informix.c:668
Datum key
Definition: gist.h:151
static int hemdistsign(BITVECP a, BITVECP b, int siglen)
Definition: tsgistidx.c:484
#define GETSIGN(x)
Definition: tsgistidx.c:80
#define newval
#define DatumGetPointer(X)
Definition: postgres.h:549
void * palloc(Size size)
Definition: mcxt.c:949
#define ISALLTRUE(x)
Definition: tsgistidx.c:75
char * BITVECP
Definition: hstore_gist.c:30
#define GET_SIGLEN()
Definition: tsgistidx.c:36

◆ gtsvector_picksplit()

Datum gtsvector_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 612 of file tsgistidx.c.

References Abs, ALLISTRUE, comparecost(), SPLITCOST::cost, fillcache(), FirstOffsetNumber, GET_SIGLEN, GETENTRY, GETSIGN, gtsvector_alloc(), hemdistcache(), hemdistsign(), i, ISALLTRUE, LOOPBYTE, MemSet, GistEntryVector::n, OffsetNumberNext, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, SPLITCOST::pos, qsort, SIGLENBIT, sign, CACHESIGN::sign, SIGNKEY, sizebitvec(), GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, and WISH_F.

613 {
616  int siglen = GET_SIGLEN();
617  OffsetNumber k,
618  j;
619  SignTSVector *datum_l,
620  *datum_r;
621  BITVECP union_l,
622  union_r;
623  int32 size_alpha,
624  size_beta;
625  int32 size_waste,
626  waste = -1;
627  int32 nbytes;
628  OffsetNumber seed_1 = 0,
629  seed_2 = 0;
630  OffsetNumber *left,
631  *right;
632  OffsetNumber maxoff;
633  BITVECP ptr;
634  int i;
635  CACHESIGN *cache;
636  char *cache_sign;
637  SPLITCOST *costvector;
638 
639  maxoff = entryvec->n - 2;
640  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
641  v->spl_left = (OffsetNumber *) palloc(nbytes);
642  v->spl_right = (OffsetNumber *) palloc(nbytes);
643 
644  cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
645  cache_sign = palloc(siglen * (maxoff + 2));
646 
647  for (j = 0; j < maxoff + 2; j++)
648  cache[j].sign = &cache_sign[siglen * j];
649 
650  fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber),
651  siglen);
652 
653  for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
654  {
655  for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
656  {
657  if (k == FirstOffsetNumber)
658  fillcache(&cache[j], GETENTRY(entryvec, j), siglen);
659 
660  size_waste = hemdistcache(&(cache[j]), &(cache[k]), siglen);
661  if (size_waste > waste)
662  {
663  waste = size_waste;
664  seed_1 = k;
665  seed_2 = j;
666  }
667  }
668  }
669 
670  left = v->spl_left;
671  v->spl_nleft = 0;
672  right = v->spl_right;
673  v->spl_nright = 0;
674 
675  if (seed_1 == 0 || seed_2 == 0)
676  {
677  seed_1 = 1;
678  seed_2 = 2;
679  }
680 
681  /* form initial .. */
682  datum_l = gtsvector_alloc(SIGNKEY | (cache[seed_1].allistrue ? ALLISTRUE : 0),
683  siglen, cache[seed_1].sign);
684  datum_r = gtsvector_alloc(SIGNKEY | (cache[seed_2].allistrue ? ALLISTRUE : 0),
685  siglen, cache[seed_2].sign);
686  union_l = GETSIGN(datum_l);
687  union_r = GETSIGN(datum_r);
688  maxoff = OffsetNumberNext(maxoff);
689  fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen);
690  /* sort before ... */
691  costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
692  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
693  {
694  costvector[j - 1].pos = j;
695  size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]), siglen);
696  size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]), siglen);
697  costvector[j - 1].cost = Abs(size_alpha - size_beta);
698  }
699  qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
700 
701  for (k = 0; k < maxoff; k++)
702  {
703  j = costvector[k].pos;
704  if (j == seed_1)
705  {
706  *left++ = j;
707  v->spl_nleft++;
708  continue;
709  }
710  else if (j == seed_2)
711  {
712  *right++ = j;
713  v->spl_nright++;
714  continue;
715  }
716 
717  if (ISALLTRUE(datum_l) || cache[j].allistrue)
718  {
719  if (ISALLTRUE(datum_l) && cache[j].allistrue)
720  size_alpha = 0;
721  else
722  size_alpha = SIGLENBIT(siglen) -
723  sizebitvec((cache[j].allistrue) ?
724  GETSIGN(datum_l) :
725  GETSIGN(cache[j].sign),
726  siglen);
727  }
728  else
729  size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l), siglen);
730 
731  if (ISALLTRUE(datum_r) || cache[j].allistrue)
732  {
733  if (ISALLTRUE(datum_r) && cache[j].allistrue)
734  size_beta = 0;
735  else
736  size_beta = SIGLENBIT(siglen) -
737  sizebitvec((cache[j].allistrue) ?
738  GETSIGN(datum_r) :
739  GETSIGN(cache[j].sign),
740  siglen);
741  }
742  else
743  size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r), siglen);
744 
745  if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
746  {
747  if (ISALLTRUE(datum_l) || cache[j].allistrue)
748  {
749  if (!ISALLTRUE(datum_l))
750  MemSet((void *) GETSIGN(datum_l), 0xff, siglen);
751  }
752  else
753  {
754  ptr = cache[j].sign;
755  LOOPBYTE(siglen)
756  union_l[i] |= ptr[i];
757  }
758  *left++ = j;
759  v->spl_nleft++;
760  }
761  else
762  {
763  if (ISALLTRUE(datum_r) || cache[j].allistrue)
764  {
765  if (!ISALLTRUE(datum_r))
766  MemSet((void *) GETSIGN(datum_r), 0xff, siglen);
767  }
768  else
769  {
770  ptr = cache[j].sign;
771  LOOPBYTE(siglen)
772  union_r[i] |= ptr[i];
773  }
774  *right++ = j;
775  v->spl_nright++;
776  }
777  }
778 
779  *right = *left = FirstOffsetNumber;
780  v->spl_ldatum = PointerGetDatum(datum_l);
781  v->spl_rdatum = PointerGetDatum(datum_r);
782 
784 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
OffsetNumber pos
Definition: hstore_gist.c:346
#define SIGLENBIT(siglen)
Definition: tsgistidx.c:40
static int32 sizebitvec(BITVECP sign, int siglen)
Definition: tsgistidx.c:478
#define PointerGetDatum(X)
Definition: postgres.h:556
OffsetNumber * spl_left
Definition: gist.h:133
Datum spl_rdatum
Definition: gist.h:140
int32 n
Definition: gist.h:226
#define MemSet(start, val, len)
Definition: c.h:971
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
int spl_nleft
Definition: gist.h:134
static int comparecost(const void *va, const void *vb)
Definition: tsgistidx.c:583
#define LOOPBYTE(siglen)
Definition: tsgistidx.c:44
signed int int32
Definition: c.h:355
int32 cost
Definition: hstore_gist.c:347
uint16 OffsetNumber
Definition: off.h:24
#define Abs(x)
Definition: c.h:926
static int hemdistcache(CACHESIGN *a, CACHESIGN *b, int siglen)
Definition: tsgistidx.c:596
#define WISH_F(a, b, c)
Definition: tsgistidx.c:575
int spl_nright
Definition: gist.h:139
char sign
Definition: informix.c:668
#define FirstOffsetNumber
Definition: off.h:27
#define GETENTRY(vec, pos)
Definition: tsgistidx.c:56
static void fillcache(CACHESIGN *item, SignTSVector *key, int siglen)
Definition: tsgistidx.c:564
static int hemdistsign(BITVECP a, BITVECP b, int siglen)
Definition: tsgistidx.c:484
Datum spl_ldatum
Definition: gist.h:135
#define GETSIGN(x)
Definition: tsgistidx.c:80
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
OffsetNumber * spl_right
Definition: gist.h:138
static SignTSVector * gtsvector_alloc(int flag, int len, BITVECP sign)
Definition: tsgistidx.c:150
#define SIGNKEY
Definition: tsgistidx.c:70
void * palloc(Size size)
Definition: mcxt.c:949
int i
#define ISALLTRUE(x)
Definition: tsgistidx.c:75
char * BITVECP
Definition: hstore_gist.c:30
BITVECP sign
Definition: trgm_gist.c:736
#define qsort(a, b, c, d)
Definition: port.h:479
#define ALLISTRUE
Definition: tsgistidx.c:71
#define GET_SIGLEN()
Definition: tsgistidx.c:36

◆ gtsvector_same()

Datum gtsvector_same ( PG_FUNCTION_ARGS  )

Definition at line 417 of file tsgistidx.c.

References ARRNELEM, Assert, GET_SIGLEN, GETARR, GETSIGLEN, GETSIGN, i, ISALLTRUE, ISSIGNKEY, LOOPBYTE, PG_GETARG_POINTER, and PG_RETURN_POINTER.

418 {
421  bool *result = (bool *) PG_GETARG_POINTER(2);
422  int siglen = GET_SIGLEN();
423 
424  if (ISSIGNKEY(a))
425  { /* then b also ISSIGNKEY */
426  if (ISALLTRUE(a) && ISALLTRUE(b))
427  *result = true;
428  else if (ISALLTRUE(a))
429  *result = false;
430  else if (ISALLTRUE(b))
431  *result = false;
432  else
433  {
434  int32 i;
435  BITVECP sa = GETSIGN(a),
436  sb = GETSIGN(b);
437 
438  Assert(GETSIGLEN(a) == siglen && GETSIGLEN(b) == siglen);
439 
440  *result = true;
441  LOOPBYTE(siglen)
442  {
443  if (sa[i] != sb[i])
444  {
445  *result = false;
446  break;
447  }
448  }
449  }
450  }
451  else
452  { /* a and b ISARRKEY */
453  int32 lena = ARRNELEM(a),
454  lenb = ARRNELEM(b);
455 
456  if (lena != lenb)
457  *result = false;
458  else
459  {
460  int32 *ptra = GETARR(a),
461  *ptrb = GETARR(b);
462  int32 i;
463 
464  *result = true;
465  for (i = 0; i < lena; i++)
466  if (ptra[i] != ptrb[i])
467  {
468  *result = false;
469  break;
470  }
471  }
472  }
473 
474  PG_RETURN_POINTER(result);
475 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define GETARR(x)
Definition: tsgistidx.c:82
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define LOOPBYTE(siglen)
Definition: tsgistidx.c:44
#define GETSIGLEN(x)
Definition: tsgistidx.c:81
signed int int32
Definition: c.h:355
#define GETSIGN(x)
Definition: tsgistidx.c:80
#define Assert(condition)
Definition: c.h:738
#define ARRNELEM(x)
Definition: tsgistidx.c:83
int i
#define ISALLTRUE(x)
Definition: tsgistidx.c:75
char * BITVECP
Definition: hstore_gist.c:30
#define ISSIGNKEY(x)
Definition: tsgistidx.c:74
#define GET_SIGLEN()
Definition: tsgistidx.c:36

◆ gtsvector_union()

Datum gtsvector_union ( PG_FUNCTION_ARGS  )

Definition at line 390 of file tsgistidx.c.

References ALLISTRUE, CALCGTSIZE, SignTSVector::flag, GET_SIGLEN, GETENTRY, GETSIGN, gtsvector_alloc(), i, GistEntryVector::n, PG_GETARG_POINTER, PG_RETURN_POINTER, SET_VARSIZE, SIGNKEY, unionkey(), and VARSIZE.

391 {
393  int *size = (int *) PG_GETARG_POINTER(1);
394  int siglen = GET_SIGLEN();
395  SignTSVector *result = gtsvector_alloc(SIGNKEY, siglen, NULL);
396  BITVECP base = GETSIGN(result);
397  int32 i;
398 
399  memset(base, 0, siglen);
400 
401  for (i = 0; i < entryvec->n; i++)
402  {
403  if (unionkey(base, GETENTRY(entryvec, i), siglen))
404  {
405  result->flag |= ALLISTRUE;
406  SET_VARSIZE(result, CALCGTSIZE(result->flag, siglen));
407  break;
408  }
409  }
410 
411  *size = VARSIZE(result);
412 
413  PG_RETURN_POINTER(result);
414 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define VARSIZE(PTR)
Definition: postgres.h:303
static int32 unionkey(BITVECP sbase, SignTSVector *add, int siglen)
Definition: tsgistidx.c:362
int32 n
Definition: gist.h:226
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
signed int int32
Definition: c.h:355
#define CALCGTSIZE(flag, len)
Definition: tsgistidx.c:78
int32 flag
Definition: tsgistidx.c:65
#define GETENTRY(vec, pos)
Definition: tsgistidx.c:56
#define GETSIGN(x)
Definition: tsgistidx.c:80
static SignTSVector * gtsvector_alloc(int flag, int len, BITVECP sign)
Definition: tsgistidx.c:150
#define SIGNKEY
Definition: tsgistidx.c:70
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
char * BITVECP
Definition: hstore_gist.c:30
#define ALLISTRUE
Definition: tsgistidx.c:71
#define GET_SIGLEN()
Definition: tsgistidx.c:36

◆ gtsvectorin()

Datum gtsvectorin ( PG_FUNCTION_ARGS  )

Definition at line 88 of file tsgistidx.c.

References ereport, errcode(), errmsg(), ERROR, and PG_RETURN_DATUM.

89 {
90  ereport(ERROR,
91  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
92  errmsg("gtsvector_in not implemented")));
93  PG_RETURN_DATUM(0);
94 }
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ gtsvectorout()

Datum gtsvectorout ( PG_FUNCTION_ARGS  )

Definition at line 103 of file tsgistidx.c.

References ARRNELEM, ARROUTSTR, EXTRALEN, GETSIGLEN, GETSIGN, ISALLTRUE, ISARRKEY, sort-test::key, Max, outbuf_maxlen, palloc(), PG_DETOAST_DATUM, PG_FREE_IF_COPY, PG_GETARG_POINTER, PG_RETURN_POINTER, SIGLENBIT, SINGOUTSTR, sizebitvec(), and sprintf.

104 {
106  char *outbuf;
107 
108  if (outbuf_maxlen == 0)
109  outbuf_maxlen = 2 * EXTRALEN + Max(strlen(SINGOUTSTR), strlen(ARROUTSTR)) + 1;
110  outbuf = palloc(outbuf_maxlen);
111 
112  if (ISARRKEY(key))
113  sprintf(outbuf, ARROUTSTR, (int) ARRNELEM(key));
114  else
115  {
116  int siglen = GETSIGLEN(key);
117  int cnttrue = (ISALLTRUE(key)) ? SIGLENBIT(siglen) : sizebitvec(GETSIGN(key), siglen);
118 
119  sprintf(outbuf, SINGOUTSTR, cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
120  }
121 
122  PG_FREE_IF_COPY(key, 0);
123  PG_RETURN_POINTER(outbuf);
124 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define SIGLENBIT(siglen)
Definition: tsgistidx.c:40
static int32 sizebitvec(BITVECP sign, int siglen)
Definition: tsgistidx.c:478
#define ISARRKEY(x)
Definition: tsgistidx.c:73
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define GETSIGLEN(x)
Definition: tsgistidx.c:81
#define sprintf
Definition: port.h:195
#define SINGOUTSTR
Definition: tsgistidx.c:96
#define ARROUTSTR
Definition: tsgistidx.c:97
#define EXTRALEN
Definition: tsgistidx.c:98
#define Max(x, y)
Definition: c.h:914
#define GETSIGN(x)
Definition: tsgistidx.c:80
#define ARRNELEM(x)
Definition: tsgistidx.c:83
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
void * palloc(Size size)
Definition: mcxt.c:949
#define ISALLTRUE(x)
Definition: tsgistidx.c:75
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
static int outbuf_maxlen
Definition: tsgistidx.c:100

◆ hemdist()

static int hemdist ( SignTSVector a,
SignTSVector b 
)
static

Definition at line 500 of file tsgistidx.c.

References Assert, GETSIGLEN, GETSIGN, hemdistsign(), ISALLTRUE, SIGLENBIT, and sizebitvec().

Referenced by gtsvector_penalty().

501 {
502  int siglena = GETSIGLEN(a);
503  int siglenb = GETSIGLEN(b);
504 
505  if (ISALLTRUE(a))
506  {
507  if (ISALLTRUE(b))
508  return 0;
509  else
510  return SIGLENBIT(siglenb) - sizebitvec(GETSIGN(b), siglenb);
511  }
512  else if (ISALLTRUE(b))
513  return SIGLENBIT(siglena) - sizebitvec(GETSIGN(a), siglena);
514 
515  Assert(siglena == siglenb);
516 
517  return hemdistsign(GETSIGN(a), GETSIGN(b), siglena);
518 }
#define SIGLENBIT(siglen)
Definition: tsgistidx.c:40
static int32 sizebitvec(BITVECP sign, int siglen)
Definition: tsgistidx.c:478
#define GETSIGLEN(x)
Definition: tsgistidx.c:81
static int hemdistsign(BITVECP a, BITVECP b, int siglen)
Definition: tsgistidx.c:484
#define GETSIGN(x)
Definition: tsgistidx.c:80
#define Assert(condition)
Definition: c.h:738
#define ISALLTRUE(x)
Definition: tsgistidx.c:75

◆ hemdistcache()

static int hemdistcache ( CACHESIGN a,
CACHESIGN b,
int  siglen 
)
static

Definition at line 596 of file tsgistidx.c.

References CACHESIGN::allistrue, hemdistsign(), SIGLENBIT, CACHESIGN::sign, and sizebitvec().

Referenced by gtsvector_picksplit().

597 {
598  if (a->allistrue)
599  {
600  if (b->allistrue)
601  return 0;
602  else
603  return SIGLENBIT(siglen) - sizebitvec(b->sign, siglen);
604  }
605  else if (b->allistrue)
606  return SIGLENBIT(siglen) - sizebitvec(a->sign, siglen);
607 
608  return hemdistsign(a->sign, b->sign, siglen);
609 }
#define SIGLENBIT(siglen)
Definition: tsgistidx.c:40
static int32 sizebitvec(BITVECP sign, int siglen)
Definition: tsgistidx.c:478
static int hemdistsign(BITVECP a, BITVECP b, int siglen)
Definition: tsgistidx.c:484
bool allistrue
Definition: trgm_gist.c:735
BITVECP sign
Definition: trgm_gist.c:736

◆ hemdistsign()

static int hemdistsign ( BITVECP  a,
BITVECP  b,
int  siglen 
)
static

Definition at line 484 of file tsgistidx.c.

References i, LOOPBYTE, and pg_number_of_ones.

Referenced by gtsvector_penalty(), gtsvector_picksplit(), hemdist(), and hemdistcache().

485 {
486  int i,
487  diff,
488  dist = 0;
489 
490  LOOPBYTE(siglen)
491  {
492  diff = (unsigned char) (a[i] ^ b[i]);
493  /* Using the popcount functions here isn't likely to win */
494  dist += pg_number_of_ones[diff];
495  }
496  return dist;
497 }
#define LOOPBYTE(siglen)
Definition: tsgistidx.c:44
PGDLLIMPORT const uint8 pg_number_of_ones[256]
Definition: pg_bitutils.c:87
int i

◆ makesign()

static void makesign ( BITVECP  sign,
SignTSVector a,
int  siglen 
)
static

Definition at line 138 of file tsgistidx.c.

References ARRNELEM, GETARR, HASH, and MemSet.

Referenced by fillcache(), gtsvector_compress(), and gtsvector_penalty().

139 {
140  int32 k,
141  len = ARRNELEM(a);
142  int32 *ptr = GETARR(a);
143 
144  MemSet((void *) sign, 0, siglen);
145  for (k = 0; k < len; k++)
146  HASH(sign, ptr[k], siglen);
147 }
#define GETARR(x)
Definition: tsgistidx.c:82
#define MemSet(start, val, len)
Definition: c.h:971
signed int int32
Definition: c.h:355
char sign
Definition: informix.c:668
#define ARRNELEM(x)
Definition: tsgistidx.c:83
#define HASH(sign, val, siglen)
Definition: tsgistidx.c:54

◆ sizebitvec()

static int32 sizebitvec ( BITVECP  sign,
int  siglen 
)
static

Definition at line 478 of file tsgistidx.c.

References pg_popcount().

Referenced by gtsvector_penalty(), gtsvector_picksplit(), gtsvectorout(), hemdist(), and hemdistcache().

479 {
480  return pg_popcount(sign, siglen);
481 }
uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.c:282
char sign
Definition: informix.c:668

◆ unionkey()

static int32 unionkey ( BITVECP  sbase,
SignTSVector add,
int  siglen 
)
static

Definition at line 362 of file tsgistidx.c.

References ARRNELEM, Assert, GETARR, GETSIGLEN, GETSIGN, HASH, i, ISALLTRUE, ISSIGNKEY, and LOOPBYTE.

Referenced by gtsvector_union().

363 {
364  int32 i;
365 
366  if (ISSIGNKEY(add))
367  {
368  BITVECP sadd = GETSIGN(add);
369 
370  if (ISALLTRUE(add))
371  return 1;
372 
373  Assert(GETSIGLEN(add) == siglen);
374 
375  LOOPBYTE(siglen)
376  sbase[i] |= sadd[i];
377  }
378  else
379  {
380  int32 *ptr = GETARR(add);
381 
382  for (i = 0; i < ARRNELEM(add); i++)
383  HASH(sbase, ptr[i], siglen);
384  }
385  return 0;
386 }
#define GETARR(x)
Definition: tsgistidx.c:82
#define LOOPBYTE(siglen)
Definition: tsgistidx.c:44
#define GETSIGLEN(x)
Definition: tsgistidx.c:81
signed int int32
Definition: c.h:355
#define GETSIGN(x)
Definition: tsgistidx.c:80
#define Assert(condition)
Definition: c.h:738
#define ARRNELEM(x)
Definition: tsgistidx.c:83
int i
#define HASH(sign, val, siglen)
Definition: tsgistidx.c:54
#define ISALLTRUE(x)
Definition: tsgistidx.c:75
char * BITVECP
Definition: hstore_gist.c:30
#define ISSIGNKEY(x)
Definition: tsgistidx.c:74

Variable Documentation

◆ outbuf_maxlen

int outbuf_maxlen = 0
static

Definition at line 100 of file tsgistidx.c.

Referenced by gtsvectorout().