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 "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
#include "utils/fmgrprotos.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 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)
 

Macro Definition Documentation

◆ ALLISTRUE

#define ALLISTRUE   0x04

Definition at line 72 of file tsgistidx.c.

◆ ARRKEY

#define ARRKEY   0x01

Definition at line 70 of file tsgistidx.c.

◆ ARRNELEM

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

Definition at line 84 of file tsgistidx.c.

◆ CALCGTSIZE

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

Definition at line 79 of file tsgistidx.c.

◆ CLRBIT

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

Definition at line 50 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:35

Definition at line 37 of file tsgistidx.c.

◆ GETARR

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

Definition at line 83 of file tsgistidx.c.

◆ GETBIT

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

Definition at line 52 of file tsgistidx.c.

◆ GETBITBYTE

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

Definition at line 49 of file tsgistidx.c.

◆ GETBYTE

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

Definition at line 48 of file tsgistidx.c.

◆ GETENTRY

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

Definition at line 57 of file tsgistidx.c.

◆ GETSIGLEN

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

Definition at line 82 of file tsgistidx.c.

◆ GETSIGN

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

Definition at line 81 of file tsgistidx.c.

◆ GTHDRSIZE

#define GTHDRSIZE   ( VARHDRSZ + sizeof(int32) )

Definition at line 78 of file tsgistidx.c.

◆ HASH

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

Definition at line 55 of file tsgistidx.c.

◆ HASHVAL

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

Definition at line 54 of file tsgistidx.c.

◆ ISALLTRUE

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

Definition at line 76 of file tsgistidx.c.

◆ ISARRKEY

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

Definition at line 74 of file tsgistidx.c.

◆ ISSIGNKEY

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

Definition at line 75 of file tsgistidx.c.

◆ LOOPBYTE

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

Definition at line 45 of file tsgistidx.c.

◆ SETBIT

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

Definition at line 51 of file tsgistidx.c.

◆ SIGLEN_DEFAULT

#define SIGLEN_DEFAULT   (31 * 4)

Definition at line 35 of file tsgistidx.c.

◆ SIGLEN_MAX

#define SIGLEN_MAX   GISTMaxIndexKeySize

Definition at line 36 of file tsgistidx.c.

◆ SIGLENBIT

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

Definition at line 41 of file tsgistidx.c.

◆ SIGNKEY

#define SIGNKEY   0x02

Definition at line 71 of file tsgistidx.c.

◆ WISH_F

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

Definition at line 578 of file tsgistidx.c.

Typedef Documentation

◆ BITVECP

typedef char* BITVECP

Definition at line 43 of file tsgistidx.c.

Function Documentation

◆ checkcondition_arr()

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

Definition at line 276 of file tsgistidx.c.

277 {
278  int32 *StopLow = ((CHKVAL *) checkval)->arrb;
279  int32 *StopHigh = ((CHKVAL *) checkval)->arre;
280  int32 *StopMiddle;
281 
282  /* Loop invariant: StopLow <= val < StopHigh */
283 
284  /*
285  * we are not able to find a prefix by hash value
286  */
287  if (val->prefix)
288  return TS_MAYBE;
289 
290  while (StopLow < StopHigh)
291  {
292  StopMiddle = StopLow + (StopHigh - StopLow) / 2;
293  if (*StopMiddle == val->valcrc)
294  return TS_MAYBE;
295  else if (*StopMiddle < val->valcrc)
296  StopLow = StopMiddle + 1;
297  else
298  StopHigh = StopMiddle;
299  }
300 
301  return TS_NO;
302 }
signed int int32
Definition: c.h:497
long val
Definition: informix.c:689
@ 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 308 of file tsgistidx.c.

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 TS_MAYBE;
317 
318  if (GETBIT(GETSIGN(key), HASHVAL(val->valcrc, GETSIGLEN(key))))
319  return TS_MAYBE;
320  else
321  return TS_NO;
322 }
#define HASHVAL(val, siglen)
Definition: tsgistidx.c:54
#define GETBIT(x, i)
Definition: tsgistidx.c:52
#define GETSIGLEN(x)
Definition: tsgistidx.c:82
#define GETSIGN(x)
Definition: tsgistidx.c:81

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

587 {
588  const SPLITCOST *a = (const SPLITCOST *) va;
589  const SPLITCOST *b = (const SPLITCOST *) vb;
590 
591  return pg_cmp_s32(a->cost, b->cost);
592 }
static int pg_cmp_s32(int32 a, int32 b)
Definition: int.h:598
int b
Definition: isn.c:70
int a
Definition: isn.c:69

References a, b, and pg_cmp_s32().

Referenced by gtsvector_picksplit().

◆ compareint()

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

Definition at line 126 of file tsgistidx.c.

127 {
128  int32 a = *((const int32 *) va);
129  int32 b = *((const int32 *) vb);
130 
131  return pg_cmp_s32(a, b);
132 }

References a, b, and pg_cmp_s32().

Referenced by gtsvector_compress().

◆ fillcache()

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

Definition at line 567 of file tsgistidx.c.

568 {
569  item->allistrue = false;
570  if (ISARRKEY(key))
571  makesign(item->sign, key, siglen);
572  else if (ISALLTRUE(key))
573  item->allistrue = true;
574  else
575  memcpy(item->sign, GETSIGN(key), siglen);
576 }
BITVECP sign
Definition: trgm_gist.c:745
bool allistrue
Definition: trgm_gist.c:744
#define ISARRKEY(x)
Definition: tsgistidx.c:74
#define ISALLTRUE(x)
Definition: tsgistidx.c:76
static void makesign(BITVECP sign, SignTSVector *a, int siglen)
Definition: tsgistidx.c:135

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

148 {
149  int size = CALCGTSIZE(flag, len);
151 
152  SET_VARSIZE(res, size);
153  res->flag = flag;
154 
155  if ((flag & (SIGNKEY | ALLISTRUE)) == SIGNKEY && sign)
156  memcpy(GETSIGN(res), sign, len);
157 
158  return res;
159 }
char sign
Definition: informix.c:693
void * palloc(Size size)
Definition: mcxt.c:1317
const void size_t len
static pg_noinline void Size size
Definition: slab.c:607
char * flag(int b)
Definition: test-ctype.c:33
#define ALLISTRUE
Definition: tsgistidx.c:72
#define CALCGTSIZE(flag, len)
Definition: tsgistidx.c:79
#define SIGNKEY
Definition: tsgistidx.c:71
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

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

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

◆ gtsvector_compress()

Datum gtsvector_compress ( PG_FUNCTION_ARGS  )

Definition at line 163 of file tsgistidx.c.

164 {
165  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
166  int siglen = GET_SIGLEN();
167  GISTENTRY *retval = entry;
168 
169  if (entry->leafkey)
170  { /* tsvector */
171  TSVector val = DatumGetTSVector(entry->key);
172  SignTSVector *res = gtsvector_alloc(ARRKEY, val->size, NULL);
173  int32 len;
174  int32 *arr;
175  WordEntry *ptr = ARRPTR(val);
176  char *words = STRPTR(val);
177 
178  arr = GETARR(res);
179  len = val->size;
180  while (len--)
181  {
182  pg_crc32 c;
183 
185  COMP_LEGACY_CRC32(c, words + ptr->pos, ptr->len);
187 
188  *arr = *(int32 *) &c;
189  arr++;
190  ptr++;
191  }
192 
193  qsort(GETARR(res), val->size, sizeof(int), compareint);
194  len = qunique(GETARR(res), val->size, sizeof(int), compareint);
195  if (len != val->size)
196  {
197  /*
198  * there is a collision of hash-function; len is always less than
199  * val->size
200  */
201  len = CALCGTSIZE(ARRKEY, len);
202  res = (SignTSVector *) repalloc(res, len);
203  SET_VARSIZE(res, len);
204  }
205 
206  /* make signature, if array is too long */
208  {
209  SignTSVector *ressign = gtsvector_alloc(SIGNKEY, siglen, NULL);
210 
211  makesign(GETSIGN(ressign), res, siglen);
212  res = ressign;
213  }
214 
215  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
217  entry->rel, entry->page,
218  entry->offset, false);
219  }
220  else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
221  !ISALLTRUE(DatumGetPointer(entry->key)))
222  {
223  int32 i;
224  SignTSVector *res;
226 
227  LOOPBYTE(siglen)
228  {
229  if ((sign[i] & 0xff) != 0xff)
230  PG_RETURN_POINTER(retval);
231  }
232 
233  res = gtsvector_alloc(SIGNKEY | ALLISTRUE, siglen, sign);
234  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
236  entry->rel, entry->page,
237  entry->offset, false);
238  }
239  PG_RETURN_POINTER(retval);
240 }
#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:244
#define TOAST_INDEX_TARGET
Definition: heaptoast.h:68
#define STRPTR(x)
Definition: hstore.h:76
char * BITVECP
Definition: hstore_gist.c:31
int i
Definition: isn.c:73
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1541
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:447
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:163
Datum key
Definition: gist.h:160
Page page
Definition: gist.h:162
Relation rel
Definition: gist.h:161
bool leafkey
Definition: gist.h:164
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:45
static SignTSVector * gtsvector_alloc(int flag, int len, BITVECP sign)
Definition: tsgistidx.c:147
#define GET_SIGLEN()
Definition: tsgistidx.c:37
#define ISSIGNKEY(x)
Definition: tsgistidx.c:75
#define GETARR(x)
Definition: tsgistidx.c:83
static int compareint(const void *va, const void *vb)
Definition: tsgistidx.c:126
#define ARRKEY
Definition: tsgistidx.c:70
#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 325 of file tsgistidx.c.

326 {
327  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
328  TSQuery query = PG_GETARG_TSQUERY(1);
329 
330  /* StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); */
331  /* Oid subtype = PG_GETARG_OID(3); */
332  bool *recheck = (bool *) PG_GETARG_POINTER(4);
334 
335  /* All cases served by this function are inexact */
336  *recheck = true;
337 
338  if (!query->size)
339  PG_RETURN_BOOL(false);
340 
341  if (ISSIGNKEY(key))
342  {
343  if (ISALLTRUE(key))
344  PG_RETURN_BOOL(true);
345 
347  key,
350  }
351  else
352  { /* only leaf pages */
353  CHKVAL chkval;
354 
355  chkval.arrb = GETARR(key);
356  chkval.arre = chkval.arrb + ARRNELEM(key);
358  (void *) &chkval,
361  }
362 }
#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:276
#define ARRNELEM(x)
Definition: tsgistidx.c:84
static TSTernaryValue checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
Definition: tsgistidx.c:308
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1854

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

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

References gtsvector_consistent().

◆ gtsvector_decompress()

Datum gtsvector_decompress ( PG_FUNCTION_ARGS  )

Definition at line 243 of file tsgistidx.c.

244 {
245  /*
246  * We need to detoast the stored value, because the other gtsvector
247  * support functions don't cope with toasted values.
248  */
249  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
251 
252  if (key != (SignTSVector *) DatumGetPointer(entry->key))
253  {
254  GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
255 
257  entry->rel, entry->page,
258  entry->offset, false);
259 
260  PG_RETURN_POINTER(retval);
261  }
262 
263  PG_RETURN_POINTER(entry);
264 }
#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 800 of file tsgistidx.c.

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 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void init_local_reloptions(local_relopts *relopts, Size relopt_struct_size)
Definition: reloptions.c:734
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:918
#define SIGLEN_MAX
Definition: tsgistidx.c:36

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

525 {
526  GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
527  GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
528  float *penalty = (float *) PG_GETARG_POINTER(2);
529  int siglen = GET_SIGLEN();
530  SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
532  BITVECP orig = GETSIGN(origval);
533 
534  *penalty = 0.0;
535 
536  if (ISARRKEY(newval))
537  {
538  BITVECP sign = palloc(siglen);
539 
540  makesign(sign, newval, siglen);
541 
542  if (ISALLTRUE(origval))
543  {
544  int siglenbit = SIGLENBIT(siglen);
545 
546  *penalty =
547  (float) (siglenbit - sizebitvec(sign, siglen)) /
548  (float) (siglenbit + 1);
549  }
550  else
551  *penalty = hemdistsign(sign, orig, siglen);
552 
553  pfree(sign);
554  }
555  else
556  *penalty = hemdist(origval, newval);
557  PG_RETURN_POINTER(penalty);
558 }
#define newval
void pfree(void *pointer)
Definition: mcxt.c:1521
static int hemdist(SignTSVector *a, SignTSVector *b)
Definition: tsgistidx.c:503
static int32 sizebitvec(BITVECP sign, int siglen)
Definition: tsgistidx.c:481
static int hemdistsign(BITVECP a, BITVECP b, int siglen)
Definition: tsgistidx.c:487
#define SIGLENBIT(siglen)
Definition: tsgistidx.c:41

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

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 
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(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  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  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(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(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 }
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:143
OffsetNumber * spl_right
Definition: gist.h:147
Datum spl_ldatum
Definition: gist.h:144
Datum spl_rdatum
Definition: gist.h:149
int spl_nright
Definition: gist.h:148
OffsetNumber * spl_left
Definition: gist.h:142
int32 n
Definition: gist.h:235
int32 cost
Definition: hstore_gist.c:354
OffsetNumber pos
Definition: hstore_gist.c:353
static int hemdistcache(CACHESIGN *a, CACHESIGN *b, int siglen)
Definition: tsgistidx.c:596
#define WISH_F(a, b, c)
Definition: tsgistidx.c:578
#define GETENTRY(vec, pos)
Definition: tsgistidx.c:57
static void fillcache(CACHESIGN *item, SignTSVector *key, int siglen)
Definition: tsgistidx.c:567
static int comparecost(const void *va, const void *vb)
Definition: tsgistidx.c:586

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

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

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

394 {
396  int *size = (int *) PG_GETARG_POINTER(1);
397  int siglen = GET_SIGLEN();
398  SignTSVector *result = gtsvector_alloc(SIGNKEY, siglen, NULL);
399  BITVECP base = GETSIGN(result);
400  int32 i;
401 
402  memset(base, 0, siglen);
403 
404  for (i = 0; i < entryvec->n; i++)
405  {
406  if (unionkey(base, GETENTRY(entryvec, i), siglen))
407  {
408  result->flag |= ALLISTRUE;
409  SET_VARSIZE(result, CALCGTSIZE(result->flag, siglen));
410  break;
411  }
412  }
413 
414  *size = VARSIZE(result);
415 
416  PG_RETURN_POINTER(result);
417 }
int32 flag
Definition: tsgistidx.c:66
static int32 unionkey(BITVECP sbase, SignTSVector *add, int siglen)
Definition: tsgistidx.c:365

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

◆ gtsvectorin()

Datum gtsvectorin ( PG_FUNCTION_ARGS  )

Definition at line 89 of file tsgistidx.c.

90 {
91  /* There's no need to support input of gtsvectors */
92  ereport(ERROR,
93  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
94  errmsg("cannot accept a value of type %s", "gtsvector")));
95 
96  PG_RETURN_VOID(); /* keep compiler quiet */
97 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#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 100 of file tsgistidx.c.

101 {
103  char *outbuf;
104 
105  if (ISARRKEY(key))
106  outbuf = psprintf("%d unique words", (int) ARRNELEM(key));
107  else
108  {
109  if (ISALLTRUE(key))
110  outbuf = pstrdup("all true bits");
111  else
112  {
113  int siglen = GETSIGLEN(key);
114  int cnttrue = sizebitvec(GETSIGN(key), siglen);
115 
116  outbuf = psprintf("%d true bits, %d false bits",
117  cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
118  }
119  }
120 
121  PG_FREE_IF_COPY(key, 0);
122  PG_RETURN_POINTER(outbuf);
123 }
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
char * pstrdup(const char *in)
Definition: mcxt.c:1696
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

References ARRNELEM, GETSIGLEN, GETSIGN, ISALLTRUE, ISARRKEY, sort-test::key, PG_DETOAST_DATUM, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_RETURN_POINTER, psprintf(), pstrdup(), SIGLENBIT, and sizebitvec().

◆ hemdist()

static int hemdist ( SignTSVector a,
SignTSVector b 
)
static

Definition at line 503 of file tsgistidx.c.

504 {
505  int siglena = GETSIGLEN(a);
506  int siglenb = GETSIGLEN(b);
507 
508  if (ISALLTRUE(a))
509  {
510  if (ISALLTRUE(b))
511  return 0;
512  else
513  return SIGLENBIT(siglenb) - sizebitvec(GETSIGN(b), siglenb);
514  }
515  else if (ISALLTRUE(b))
516  return SIGLENBIT(siglena) - sizebitvec(GETSIGN(a), siglena);
517 
518  Assert(siglena == siglenb);
519 
520  return hemdistsign(GETSIGN(a), GETSIGN(b), siglena);
521 }

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

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 }

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

488 {
489  int i,
490  diff,
491  dist = 0;
492 
493  LOOPBYTE(siglen)
494  {
495  diff = (unsigned char) (a[i] ^ b[i]);
496  /* Using the popcount functions here isn't likely to win */
497  dist += pg_number_of_ones[diff];
498  }
499  return dist;
500 }
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 135 of file tsgistidx.c.

136 {
137  int32 k,
138  len = ARRNELEM(a);
139  int32 *ptr = GETARR(a);
140 
141  MemSet(sign, 0, siglen);
142  for (k = 0; k < len; k++)
143  HASH(sign, ptr[k], siglen);
144 }
#define MemSet(start, val, len)
Definition: c.h:1023
#define HASH(sign, val, siglen)
Definition: tsgistidx.c:55

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

482 {
483  return pg_popcount(sign, siglen);
484 }
static uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.h:339

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

366 {
367  int32 i;
368 
369  if (ISSIGNKEY(add))
370  {
371  BITVECP sadd = GETSIGN(add);
372 
373  if (ISALLTRUE(add))
374  return 1;
375 
376  Assert(GETSIGLEN(add) == siglen);
377 
378  LOOPBYTE(siglen)
379  sbase[i] |= sadd[i];
380  }
381  else
382  {
383  int32 *ptr = GETARR(add);
384 
385  for (i = 0; i < ARRNELEM(add); i++)
386  HASH(sbase, ptr[i], siglen);
387  }
388  return 0;
389 }

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

Referenced by gtsvector_union().