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 TSTernaryValue checkcondition_arr (void *checkval, QueryOperand *val, ExecPhraseData *data)
 
static TSTernaryValue 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

Definition at line 71 of file tsgistidx.c.

◆ ARRKEY

#define ARRKEY   0x01

Definition at line 69 of file tsgistidx.c.

◆ ARRNELEM

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

Definition at line 83 of file tsgistidx.c.

◆ ARROUTSTR

#define ARROUTSTR   "%d unique words"

Definition at line 99 of file tsgistidx.c.

◆ CALCGTSIZE

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

Definition at line 78 of file tsgistidx.c.

◆ 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 100 of file tsgistidx.c.

◆ GET_SIGLEN

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

Definition at line 36 of file tsgistidx.c.

◆ GETARR

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

Definition at line 82 of file tsgistidx.c.

◆ GETBIT

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

Definition at line 51 of file tsgistidx.c.

◆ 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.

◆ GETSIGLEN

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

Definition at line 81 of file tsgistidx.c.

◆ GETSIGN

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

Definition at line 80 of file tsgistidx.c.

◆ 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.

◆ HASHVAL

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

Definition at line 53 of file tsgistidx.c.

◆ ISALLTRUE

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

Definition at line 75 of file tsgistidx.c.

◆ ISARRKEY

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

Definition at line 73 of file tsgistidx.c.

◆ ISSIGNKEY

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

Definition at line 74 of file tsgistidx.c.

◆ LOOPBYTE

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

Definition at line 44 of file tsgistidx.c.

◆ 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.

◆ SIGLEN_MAX

#define SIGLEN_MAX   GISTMaxIndexKeySize

Definition at line 35 of file tsgistidx.c.

◆ SIGLENBIT

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

Definition at line 40 of file tsgistidx.c.

◆ SIGNKEY

#define SIGNKEY   0x02

Definition at line 70 of file tsgistidx.c.

◆ SINGOUTSTR

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

Definition at line 98 of file tsgistidx.c.

◆ WISH_F

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

Definition at line 588 of file tsgistidx.c.

Typedef Documentation

◆ BITVECP

typedef char* BITVECP

Definition at line 42 of file tsgistidx.c.

Function Documentation

◆ checkcondition_arr()

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

Definition at line 286 of file tsgistidx.c.

287 {
288  int32 *StopLow = ((CHKVAL *) checkval)->arrb;
289  int32 *StopHigh = ((CHKVAL *) checkval)->arre;
290  int32 *StopMiddle;
291 
292  /* Loop invariant: StopLow <= val < StopHigh */
293 
294  /*
295  * we are not able to find a prefix by hash value
296  */
297  if (val->prefix)
298  return TS_MAYBE;
299 
300  while (StopLow < StopHigh)
301  {
302  StopMiddle = StopLow + (StopHigh - StopLow) / 2;
303  if (*StopMiddle == val->valcrc)
304  return TS_MAYBE;
305  else if (*StopMiddle < val->valcrc)
306  StopLow = StopMiddle + 1;
307  else
308  StopHigh = StopMiddle;
309  }
310 
311  return TS_NO;
312 }
signed int int32
Definition: c.h:483
long val
Definition: informix.c:664
@ TS_MAYBE
Definition: ts_utils.h:136
@ TS_NO
Definition: ts_utils.h:134

References TS_MAYBE, TS_NO, and val.

Referenced by gtsvector_consistent().

◆ checkcondition_bit()

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

Definition at line 318 of file tsgistidx.c.

319 {
320  void *key = (SignTSVector *) checkval;
321 
322  /*
323  * we are not able to find a prefix in signature tree
324  */
325  if (val->prefix)
326  return TS_MAYBE;
327 
328  if (GETBIT(GETSIGN(key), HASHVAL(val->valcrc, GETSIGLEN(key))))
329  return TS_MAYBE;
330  else
331  return TS_NO;
332 }
#define HASHVAL(val, siglen)
Definition: tsgistidx.c:53
#define GETBIT(x, i)
Definition: tsgistidx.c:51
#define GETSIGLEN(x)
Definition: tsgistidx.c:81
#define GETSIGN(x)
Definition: tsgistidx.c:80

References GETBIT, GETSIGLEN, GETSIGN, HASHVAL, sort-test::key, TS_MAYBE, TS_NO, and val.

Referenced by gtsvector_consistent().

◆ comparecost()

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

Definition at line 596 of file tsgistidx.c.

597 {
598  const SPLITCOST *a = (const SPLITCOST *) va;
599  const SPLITCOST *b = (const SPLITCOST *) vb;
600 
601  if (a->cost == b->cost)
602  return 0;
603  else
604  return (a->cost > b->cost) ? 1 : -1;
605 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69

References a, and b.

Referenced by gtsvector_picksplit().

◆ compareint()

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

Definition at line 134 of file tsgistidx.c.

135 {
136  int32 a = *((const int32 *) va);
137  int32 b = *((const int32 *) vb);
138 
139  if (a == b)
140  return 0;
141  return (a > b) ? 1 : -1;
142 }

References a, and b.

Referenced by gtsvector_compress().

◆ fillcache()

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

Definition at line 577 of file tsgistidx.c.

578 {
579  item->allistrue = false;
580  if (ISARRKEY(key))
581  makesign(item->sign, key, siglen);
582  else if (ISALLTRUE(key))
583  item->allistrue = true;
584  else
585  memcpy(item->sign, GETSIGN(key), siglen);
586 }
BITVECP sign
Definition: trgm_gist.c:745
bool allistrue
Definition: trgm_gist.c:744
#define ISARRKEY(x)
Definition: tsgistidx.c:73
#define ISALLTRUE(x)
Definition: tsgistidx.c:75
static void makesign(BITVECP sign, SignTSVector *a, int siglen)
Definition: tsgistidx.c:145

References CACHESIGN::allistrue, GETSIGN, ISALLTRUE, ISARRKEY, sort-test::key, makesign(), and CACHESIGN::sign.

Referenced by gtsvector_picksplit().

◆ gtsvector_alloc()

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

Definition at line 157 of file tsgistidx.c.

158 {
159  int size = CALCGTSIZE(flag, len);
160  SignTSVector *res = palloc(size);
161 
162  SET_VARSIZE(res, size);
163  res->flag = flag;
164 
165  if ((flag & (SIGNKEY | ALLISTRUE)) == SIGNKEY && sign)
166  memcpy(GETSIGN(res), sign, len);
167 
168  return res;
169 }
char sign
Definition: informix.c:668
void * palloc(Size size)
Definition: mcxt.c:1226
const void size_t len
char * flag(int b)
Definition: test-ctype.c:33
#define ALLISTRUE
Definition: tsgistidx.c:71
#define CALCGTSIZE(flag, len)
Definition: tsgistidx.c:78
#define SIGNKEY
Definition: tsgistidx.c:70
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References ALLISTRUE, CALCGTSIZE, flag(), GETSIGN, len, palloc(), res, SET_VARSIZE, sign, and SIGNKEY.

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

◆ gtsvector_compress()

Datum gtsvector_compress ( PG_FUNCTION_ARGS  )

Definition at line 173 of file tsgistidx.c.

174 {
175  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
176  int siglen = GET_SIGLEN();
177  GISTENTRY *retval = entry;
178 
179  if (entry->leafkey)
180  { /* tsvector */
181  TSVector val = DatumGetTSVector(entry->key);
182  SignTSVector *res = gtsvector_alloc(ARRKEY, val->size, NULL);
183  int32 len;
184  int32 *arr;
185  WordEntry *ptr = ARRPTR(val);
186  char *words = STRPTR(val);
187 
188  arr = GETARR(res);
189  len = val->size;
190  while (len--)
191  {
192  pg_crc32 c;
193 
195  COMP_LEGACY_CRC32(c, words + ptr->pos, ptr->len);
197 
198  *arr = *(int32 *) &c;
199  arr++;
200  ptr++;
201  }
202 
203  qsort(GETARR(res), val->size, sizeof(int), compareint);
204  len = qunique(GETARR(res), val->size, sizeof(int), compareint);
205  if (len != val->size)
206  {
207  /*
208  * there is a collision of hash-function; len is always less than
209  * val->size
210  */
211  len = CALCGTSIZE(ARRKEY, len);
212  res = (SignTSVector *) repalloc(res, len);
213  SET_VARSIZE(res, len);
214  }
215 
216  /* make signature, if array is too long */
218  {
219  SignTSVector *ressign = gtsvector_alloc(SIGNKEY, siglen, NULL);
220 
221  makesign(GETSIGN(ressign), res, siglen);
222  res = ressign;
223  }
224 
225  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
227  entry->rel, entry->page,
228  entry->offset, false);
229  }
230  else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
231  !ISALLTRUE(DatumGetPointer(entry->key)))
232  {
233  int32 i;
234  SignTSVector *res;
236 
237  LOOPBYTE(siglen)
238  {
239  if ((sign[i] & 0xff) != 0xff)
240  PG_RETURN_POINTER(retval);
241  }
242 
243  res = gtsvector_alloc(SIGNKEY | ALLISTRUE, siglen, sign);
244  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
246  entry->rel, entry->page,
247  entry->offset, false);
248  }
249  PG_RETURN_POINTER(retval);
250 }
#define ARRPTR(x)
Definition: cube.c:25
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:242
#define TOAST_INDEX_TARGET
Definition: heaptoast.h:68
#define STRPTR(x)
Definition: hstore.h:76
char * BITVECP
Definition: hstore_gist.c:30
int i
Definition: isn.c:73
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1476
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 qsort(a, b, c, d)
Definition: port.h:445
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
char * c
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
OffsetNumber offset
Definition: gist.h:161
Datum key
Definition: gist.h:158
Page page
Definition: gist.h:160
Relation rel
Definition: gist.h:159
bool leafkey
Definition: gist.h:162
uint32 pos
Definition: ts_type.h:46
uint32 len
Definition: ts_type.h:45
static TSVector DatumGetTSVector(Datum X)
Definition: ts_type.h:118
#define LOOPBYTE(siglen)
Definition: tsgistidx.c:44
static SignTSVector * gtsvector_alloc(int flag, int len, BITVECP sign)
Definition: tsgistidx.c:157
#define GET_SIGLEN()
Definition: tsgistidx.c:36
#define ISSIGNKEY(x)
Definition: tsgistidx.c:74
#define GETARR(x)
Definition: tsgistidx.c:82
static int compareint(const void *va, const void *vb)
Definition: tsgistidx.c:134
#define ARRKEY
Definition: tsgistidx.c:69
#define VARSIZE(PTR)
Definition: varatt.h:279

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, len, LOOPBYTE, makesign(), GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum(), WordEntry::pos, qsort, qunique(), GISTENTRY::rel, repalloc(), res, SET_VARSIZE, sign, SIGNKEY, STRPTR, TOAST_INDEX_TARGET, val, and VARSIZE.

◆ gtsvector_consistent()

Datum gtsvector_consistent ( PG_FUNCTION_ARGS  )

Definition at line 335 of file tsgistidx.c.

336 {
337  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
338  TSQuery query = PG_GETARG_TSQUERY(1);
339 
340  /* StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); */
341  /* Oid subtype = PG_GETARG_OID(3); */
342  bool *recheck = (bool *) PG_GETARG_POINTER(4);
344 
345  /* All cases served by this function are inexact */
346  *recheck = true;
347 
348  if (!query->size)
349  PG_RETURN_BOOL(false);
350 
351  if (ISSIGNKEY(key))
352  {
353  if (ISALLTRUE(key))
354  PG_RETURN_BOOL(true);
355 
357  key,
360  }
361  else
362  { /* only leaf pages */
363  CHKVAL chkval;
364 
365  chkval.arrb = GETARR(key);
366  chkval.arre = chkval.arrb + ARRNELEM(key);
368  (void *) &chkval,
371  }
372 }
#define GETQUERY(x)
Definition: _int.h:157
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 * arrb
Definition: _int_bool.c:226
int32 * arre
Definition: _int_bool.c:227
int32 size
Definition: ts_type.h:221
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:266
#define TS_EXEC_PHRASE_NO_POS
Definition: ts_utils.h:202
static TSTernaryValue checkcondition_arr(void *checkval, QueryOperand *val, ExecPhraseData *data)
Definition: tsgistidx.c:286
#define ARRNELEM(x)
Definition: tsgistidx.c:83
static TSTernaryValue checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
Definition: tsgistidx.c:318
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1856

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

Referenced by gtsvector_consistent_oldsig().

◆ gtsvector_consistent_oldsig()

Datum gtsvector_consistent_oldsig ( PG_FUNCTION_ARGS  )

Definition at line 807 of file tsgistidx.c.

808 {
809  return gtsvector_consistent(fcinfo);
810 }
Datum gtsvector_consistent(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:335

References gtsvector_consistent().

◆ gtsvector_decompress()

Datum gtsvector_decompress ( PG_FUNCTION_ARGS  )

Definition at line 253 of file tsgistidx.c.

254 {
255  /*
256  * We need to detoast the stored value, because the other gtsvector
257  * support functions don't cope with toasted values.
258  */
259  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
261 
262  if (key != (SignTSVector *) DatumGetPointer(entry->key))
263  {
264  GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
265 
267  entry->rel, entry->page,
268  entry->offset, false);
269 
270  PG_RETURN_POINTER(retval);
271  }
272 
273  PG_RETURN_POINTER(entry);
274 }
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240

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

◆ gtsvector_options()

Datum gtsvector_options ( PG_FUNCTION_ARGS  )

Definition at line 813 of file tsgistidx.c.

814 {
816 
817  init_local_reloptions(relopts, sizeof(GistTsVectorOptions));
818  add_local_int_reloption(relopts, "siglen", "signature length",
820  offsetof(GistTsVectorOptions, siglen));
821 
822  PG_RETURN_VOID();
823 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void init_local_reloptions(local_relopts *relopts, Size relopt_struct_size)
Definition: reloptions.c:736
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:920
#define SIGLEN_MAX
Definition: tsgistidx.c:35

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

◆ gtsvector_penalty()

Datum gtsvector_penalty ( PG_FUNCTION_ARGS  )

Definition at line 534 of file tsgistidx.c.

535 {
536  GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
537  GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
538  float *penalty = (float *) PG_GETARG_POINTER(2);
539  int siglen = GET_SIGLEN();
540  SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
542  BITVECP orig = GETSIGN(origval);
543 
544  *penalty = 0.0;
545 
546  if (ISARRKEY(newval))
547  {
548  BITVECP sign = palloc(siglen);
549 
550  makesign(sign, newval, siglen);
551 
552  if (ISALLTRUE(origval))
553  {
554  int siglenbit = SIGLENBIT(siglen);
555 
556  *penalty =
557  (float) (siglenbit - sizebitvec(sign, siglen)) /
558  (float) (siglenbit + 1);
559  }
560  else
561  *penalty = hemdistsign(sign, orig, siglen);
562 
563  pfree(sign);
564  }
565  else
566  *penalty = hemdist(origval, newval);
567  PG_RETURN_POINTER(penalty);
568 }
#define newval
void pfree(void *pointer)
Definition: mcxt.c:1456
static int hemdist(SignTSVector *a, SignTSVector *b)
Definition: tsgistidx.c:513
static int32 sizebitvec(BITVECP sign, int siglen)
Definition: tsgistidx.c:491
static int hemdistsign(BITVECP a, BITVECP b, int siglen)
Definition: tsgistidx.c:497
#define SIGLENBIT(siglen)
Definition: tsgistidx.c:40

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().

◆ gtsvector_picksplit()

Datum gtsvector_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 625 of file tsgistidx.c.

626 {
629  int siglen = GET_SIGLEN();
630  OffsetNumber k,
631  j;
632  SignTSVector *datum_l,
633  *datum_r;
634  BITVECP union_l,
635  union_r;
636  int32 size_alpha,
637  size_beta;
638  int32 size_waste,
639  waste = -1;
640  int32 nbytes;
641  OffsetNumber seed_1 = 0,
642  seed_2 = 0;
643  OffsetNumber *left,
644  *right;
645  OffsetNumber maxoff;
646  BITVECP ptr;
647  int i;
648  CACHESIGN *cache;
649  char *cache_sign;
650  SPLITCOST *costvector;
651 
652  maxoff = entryvec->n - 2;
653  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
654  v->spl_left = (OffsetNumber *) palloc(nbytes);
655  v->spl_right = (OffsetNumber *) palloc(nbytes);
656 
657  cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
658  cache_sign = palloc(siglen * (maxoff + 2));
659 
660  for (j = 0; j < maxoff + 2; j++)
661  cache[j].sign = &cache_sign[siglen * j];
662 
664  siglen);
665 
666  for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
667  {
668  for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
669  {
670  if (k == FirstOffsetNumber)
671  fillcache(&cache[j], GETENTRY(entryvec, j), siglen);
672 
673  size_waste = hemdistcache(&(cache[j]), &(cache[k]), siglen);
674  if (size_waste > waste)
675  {
676  waste = size_waste;
677  seed_1 = k;
678  seed_2 = j;
679  }
680  }
681  }
682 
683  left = v->spl_left;
684  v->spl_nleft = 0;
685  right = v->spl_right;
686  v->spl_nright = 0;
687 
688  if (seed_1 == 0 || seed_2 == 0)
689  {
690  seed_1 = 1;
691  seed_2 = 2;
692  }
693 
694  /* form initial .. */
695  datum_l = gtsvector_alloc(SIGNKEY | (cache[seed_1].allistrue ? ALLISTRUE : 0),
696  siglen, cache[seed_1].sign);
697  datum_r = gtsvector_alloc(SIGNKEY | (cache[seed_2].allistrue ? ALLISTRUE : 0),
698  siglen, cache[seed_2].sign);
699  union_l = GETSIGN(datum_l);
700  union_r = GETSIGN(datum_r);
701  maxoff = OffsetNumberNext(maxoff);
702  fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen);
703  /* sort before ... */
704  costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
705  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
706  {
707  costvector[j - 1].pos = j;
708  size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]), siglen);
709  size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]), siglen);
710  costvector[j - 1].cost = abs(size_alpha - size_beta);
711  }
712  qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
713 
714  for (k = 0; k < maxoff; k++)
715  {
716  j = costvector[k].pos;
717  if (j == seed_1)
718  {
719  *left++ = j;
720  v->spl_nleft++;
721  continue;
722  }
723  else if (j == seed_2)
724  {
725  *right++ = j;
726  v->spl_nright++;
727  continue;
728  }
729 
730  if (ISALLTRUE(datum_l) || cache[j].allistrue)
731  {
732  if (ISALLTRUE(datum_l) && cache[j].allistrue)
733  size_alpha = 0;
734  else
735  size_alpha = SIGLENBIT(siglen) -
736  sizebitvec((cache[j].allistrue) ?
737  GETSIGN(datum_l) :
738  cache[j].sign,
739  siglen);
740  }
741  else
742  size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l), siglen);
743 
744  if (ISALLTRUE(datum_r) || cache[j].allistrue)
745  {
746  if (ISALLTRUE(datum_r) && cache[j].allistrue)
747  size_beta = 0;
748  else
749  size_beta = SIGLENBIT(siglen) -
750  sizebitvec((cache[j].allistrue) ?
751  GETSIGN(datum_r) :
752  cache[j].sign,
753  siglen);
754  }
755  else
756  size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r), siglen);
757 
758  if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
759  {
760  if (ISALLTRUE(datum_l) || cache[j].allistrue)
761  {
762  if (!ISALLTRUE(datum_l))
763  memset(GETSIGN(datum_l), 0xff, siglen);
764  }
765  else
766  {
767  ptr = cache[j].sign;
768  LOOPBYTE(siglen)
769  union_l[i] |= ptr[i];
770  }
771  *left++ = j;
772  v->spl_nleft++;
773  }
774  else
775  {
776  if (ISALLTRUE(datum_r) || cache[j].allistrue)
777  {
778  if (!ISALLTRUE(datum_r))
779  memset(GETSIGN(datum_r), 0xff, siglen);
780  }
781  else
782  {
783  ptr = cache[j].sign;
784  LOOPBYTE(siglen)
785  union_r[i] |= ptr[i];
786  }
787  *right++ = j;
788  v->spl_nright++;
789  }
790  }
791 
792  *right = *left = FirstOffsetNumber;
793  v->spl_ldatum = PointerGetDatum(datum_l);
794  v->spl_rdatum = PointerGetDatum(datum_r);
795 
797 }
int j
Definition: isn.c:74
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
int spl_nleft
Definition: gist.h:141
OffsetNumber * spl_right
Definition: gist.h:145
Datum spl_ldatum
Definition: gist.h:142
Datum spl_rdatum
Definition: gist.h:147
int spl_nright
Definition: gist.h:146
OffsetNumber * spl_left
Definition: gist.h:140
int32 n
Definition: gist.h:233
int32 cost
Definition: hstore_gist.c:353
OffsetNumber pos
Definition: hstore_gist.c:352
static int hemdistcache(CACHESIGN *a, CACHESIGN *b, int siglen)
Definition: tsgistidx.c:609
#define WISH_F(a, b, c)
Definition: tsgistidx.c:588
#define GETENTRY(vec, pos)
Definition: tsgistidx.c:56
static void fillcache(CACHESIGN *item, SignTSVector *key, int siglen)
Definition: tsgistidx.c:577
static int comparecost(const void *va, const void *vb)
Definition: tsgistidx.c:596

References ALLISTRUE, comparecost(), SPLITCOST::cost, fillcache(), FirstOffsetNumber, GET_SIGLEN, GETENTRY, GETSIGN, gtsvector_alloc(), hemdistcache(), hemdistsign(), i, ISALLTRUE, j, LOOPBYTE, GistEntryVector::n, OffsetNumberNext, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum(), SPLITCOST::pos, qsort, SIGLENBIT, CACHESIGN::sign, 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.

◆ gtsvector_same()

Datum gtsvector_same ( PG_FUNCTION_ARGS  )

Definition at line 430 of file tsgistidx.c.

431 {
434  bool *result = (bool *) PG_GETARG_POINTER(2);
435  int siglen = GET_SIGLEN();
436 
437  if (ISSIGNKEY(a))
438  { /* then b also ISSIGNKEY */
439  if (ISALLTRUE(a) && ISALLTRUE(b))
440  *result = true;
441  else if (ISALLTRUE(a))
442  *result = false;
443  else if (ISALLTRUE(b))
444  *result = false;
445  else
446  {
447  int32 i;
448  BITVECP sa = GETSIGN(a),
449  sb = GETSIGN(b);
450 
451  Assert(GETSIGLEN(a) == siglen && GETSIGLEN(b) == siglen);
452 
453  *result = true;
454  LOOPBYTE(siglen)
455  {
456  if (sa[i] != sb[i])
457  {
458  *result = false;
459  break;
460  }
461  }
462  }
463  }
464  else
465  { /* a and b ISARRKEY */
466  int32 lena = ARRNELEM(a),
467  lenb = ARRNELEM(b);
468 
469  if (lena != lenb)
470  *result = false;
471  else
472  {
473  int32 *ptra = GETARR(a),
474  *ptrb = GETARR(b);
475  int32 i;
476 
477  *result = true;
478  for (i = 0; i < lena; i++)
479  if (ptra[i] != ptrb[i])
480  {
481  *result = false;
482  break;
483  }
484  }
485  }
486 
487  PG_RETURN_POINTER(result);
488 }
Assert(fmt[strlen(fmt) - 1] !='\n')

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

◆ gtsvector_union()

Datum gtsvector_union ( PG_FUNCTION_ARGS  )

Definition at line 403 of file tsgistidx.c.

404 {
406  int *size = (int *) PG_GETARG_POINTER(1);
407  int siglen = GET_SIGLEN();
408  SignTSVector *result = gtsvector_alloc(SIGNKEY, siglen, NULL);
409  BITVECP base = GETSIGN(result);
410  int32 i;
411 
412  memset(base, 0, siglen);
413 
414  for (i = 0; i < entryvec->n; i++)
415  {
416  if (unionkey(base, GETENTRY(entryvec, i), siglen))
417  {
418  result->flag |= ALLISTRUE;
419  SET_VARSIZE(result, CALCGTSIZE(result->flag, siglen));
420  break;
421  }
422  }
423 
424  *size = VARSIZE(result);
425 
426  PG_RETURN_POINTER(result);
427 }
int32 flag
Definition: tsgistidx.c:65
static int32 unionkey(BITVECP sbase, SignTSVector *add, int siglen)
Definition: tsgistidx.c:375

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.

◆ gtsvectorin()

Datum gtsvectorin ( PG_FUNCTION_ARGS  )

Definition at line 88 of file tsgistidx.c.

89 {
90  /* There's no need to support input of gtsvectors */
91  ereport(ERROR,
92  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
93  errmsg("cannot accept a value of type %s", "gtsvector")));
94 
95  PG_RETURN_VOID(); /* keep compiler quiet */
96 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149

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

◆ gtsvectorout()

Datum gtsvectorout ( PG_FUNCTION_ARGS  )

Definition at line 105 of file tsgistidx.c.

106 {
108  char *outbuf;
109 
110  if (outbuf_maxlen == 0)
111  outbuf_maxlen = 2 * EXTRALEN + Max(strlen(SINGOUTSTR), strlen(ARROUTSTR)) + 1;
112  outbuf = palloc(outbuf_maxlen);
113 
114  if (ISARRKEY(key))
115  sprintf(outbuf, ARROUTSTR, (int) ARRNELEM(key));
116  else
117  {
118  if (ISALLTRUE(key))
119  sprintf(outbuf, "all true bits");
120  else
121  {
122  int siglen = GETSIGLEN(key);
123  int cnttrue = sizebitvec(GETSIGN(key), siglen);
124 
125  sprintf(outbuf, SINGOUTSTR, cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
126  }
127  }
128 
129  PG_FREE_IF_COPY(key, 0);
130  PG_RETURN_POINTER(outbuf);
131 }
#define Max(x, y)
Definition: c.h:987
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define sprintf
Definition: port.h:240
#define ARROUTSTR
Definition: tsgistidx.c:99
#define EXTRALEN
Definition: tsgistidx.c:100
static int outbuf_maxlen
Definition: tsgistidx.c:102
#define SINGOUTSTR
Definition: tsgistidx.c:98

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

◆ hemdist()

static int hemdist ( SignTSVector a,
SignTSVector b 
)
static

Definition at line 513 of file tsgistidx.c.

514 {
515  int siglena = GETSIGLEN(a);
516  int siglenb = GETSIGLEN(b);
517 
518  if (ISALLTRUE(a))
519  {
520  if (ISALLTRUE(b))
521  return 0;
522  else
523  return SIGLENBIT(siglenb) - sizebitvec(GETSIGN(b), siglenb);
524  }
525  else if (ISALLTRUE(b))
526  return SIGLENBIT(siglena) - sizebitvec(GETSIGN(a), siglena);
527 
528  Assert(siglena == siglenb);
529 
530  return hemdistsign(GETSIGN(a), GETSIGN(b), siglena);
531 }

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

Referenced by gtsvector_penalty().

◆ hemdistcache()

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

Definition at line 609 of file tsgistidx.c.

610 {
611  if (a->allistrue)
612  {
613  if (b->allistrue)
614  return 0;
615  else
616  return SIGLENBIT(siglen) - sizebitvec(b->sign, siglen);
617  }
618  else if (b->allistrue)
619  return SIGLENBIT(siglen) - sizebitvec(a->sign, siglen);
620 
621  return hemdistsign(a->sign, b->sign, siglen);
622 }

References a, b, hemdistsign(), SIGLENBIT, and sizebitvec().

Referenced by gtsvector_picksplit().

◆ hemdistsign()

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

Definition at line 497 of file tsgistidx.c.

498 {
499  int i,
500  diff,
501  dist = 0;
502 
503  LOOPBYTE(siglen)
504  {
505  diff = (unsigned char) (a[i] ^ b[i]);
506  /* Using the popcount functions here isn't likely to win */
507  dist += pg_number_of_ones[diff];
508  }
509  return dist;
510 }
PGDLLIMPORT const uint8 pg_number_of_ones[256]
Definition: pg_bitutils.c:87

References a, b, i, LOOPBYTE, and pg_number_of_ones.

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

◆ makesign()

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

Definition at line 145 of file tsgistidx.c.

146 {
147  int32 k,
148  len = ARRNELEM(a);
149  int32 *ptr = GETARR(a);
150 
151  MemSet(sign, 0, siglen);
152  for (k = 0; k < len; k++)
153  HASH(sign, ptr[k], siglen);
154 }
#define MemSet(start, val, len)
Definition: c.h:1009
#define HASH(sign, val, siglen)
Definition: tsgistidx.c:54

References a, ARRNELEM, GETARR, HASH, len, MemSet, and sign.

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

◆ sizebitvec()

static int32 sizebitvec ( BITVECP  sign,
int  siglen 
)
static

Definition at line 491 of file tsgistidx.c.

492 {
493  return pg_popcount(sign, siglen);
494 }
uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.c:296

References pg_popcount(), and sign.

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

◆ unionkey()

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

Definition at line 375 of file tsgistidx.c.

376 {
377  int32 i;
378 
379  if (ISSIGNKEY(add))
380  {
381  BITVECP sadd = GETSIGN(add);
382 
383  if (ISALLTRUE(add))
384  return 1;
385 
386  Assert(GETSIGLEN(add) == siglen);
387 
388  LOOPBYTE(siglen)
389  sbase[i] |= sadd[i];
390  }
391  else
392  {
393  int32 *ptr = GETARR(add);
394 
395  for (i = 0; i < ARRNELEM(add); i++)
396  HASH(sbase, ptr[i], siglen);
397  }
398  return 0;
399 }

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

Referenced by gtsvector_union().

Variable Documentation

◆ outbuf_maxlen

int outbuf_maxlen = 0
static

Definition at line 102 of file tsgistidx.c.

Referenced by gtsvectorout().