PostgreSQL Source Code  git master
hstore_gist.c File Reference
#include "postgres.h"
#include "access/gist.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
#include "utils/pg_crc.h"
#include "hstore.h"
Include dependency graph for hstore_gist.c:

Go to the source code of this file.

Data Structures

struct  GISTTYPE
 
struct  SPLITCOST
 

Macros

#define BITBYTE   8
 
#define SIGLENINT   4 /* >122 => key will toast, so very slow!!! */
 
#define SIGLEN   ( sizeof(int)*SIGLENINT )
 
#define SIGLENBIT   (SIGLEN*BITBYTE)
 
#define LOOPBYTE   for(i=0;i<SIGLEN;i++)
 
#define LOOPBIT   for(i=0;i<SIGLENBIT;i++)
 
#define GETBYTE(x, i)   ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
 
#define GETBITBYTE(x, i)   ( (*((char*)(x)) >> (i)) & 0x01 )
 
#define CLRBIT(x, i)   GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
 
#define SETBIT(x, i)   GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
 
#define GETBIT(x, i)   ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
 
#define HASHVAL(val)   (((unsigned int)(val)) % SIGLENBIT)
 
#define HASH(sign, val)   SETBIT((sign), HASHVAL(val))
 
#define ALLISTRUE   0x04
 
#define ISALLTRUE(x)   ( ((GISTTYPE*)x)->flag & ALLISTRUE )
 
#define GTHDRSIZE   (VARHDRSZ + sizeof(int32))
 
#define CALCGTSIZE(flag)   ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
 
#define GETSIGN(x)   ( (BITVECP)( (char*)x+GTHDRSIZE ) )
 
#define SUMBIT(val)
 
#define GETENTRY(vec, pos)   ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
 
#define WISH_F(a, b, c)   (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
 

Typedefs

typedef char BITVEC[SIGLEN]
 
typedef char * BITVECP
 

Functions

static pg_crc32 crc32_sz (char *buf, int size)
 
 PG_FUNCTION_INFO_V1 (ghstore_in)
 
 PG_FUNCTION_INFO_V1 (ghstore_out)
 
Datum ghstore_in (PG_FUNCTION_ARGS)
 
Datum ghstore_out (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (ghstore_consistent)
 
 PG_FUNCTION_INFO_V1 (ghstore_compress)
 
 PG_FUNCTION_INFO_V1 (ghstore_decompress)
 
 PG_FUNCTION_INFO_V1 (ghstore_penalty)
 
 PG_FUNCTION_INFO_V1 (ghstore_picksplit)
 
 PG_FUNCTION_INFO_V1 (ghstore_union)
 
 PG_FUNCTION_INFO_V1 (ghstore_same)
 
Datum ghstore_compress (PG_FUNCTION_ARGS)
 
Datum ghstore_decompress (PG_FUNCTION_ARGS)
 
Datum ghstore_same (PG_FUNCTION_ARGS)
 
static int32 sizebitvec (BITVECP sign)
 
static int hemdistsign (BITVECP a, BITVECP b)
 
static int hemdist (GISTTYPE *a, GISTTYPE *b)
 
static int32 unionkey (BITVECP sbase, GISTTYPE *add)
 
Datum ghstore_union (PG_FUNCTION_ARGS)
 
Datum ghstore_penalty (PG_FUNCTION_ARGS)
 
static int comparecost (const void *a, const void *b)
 
Datum ghstore_picksplit (PG_FUNCTION_ARGS)
 
Datum ghstore_consistent (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ ALLISTRUE

◆ BITBYTE

#define BITBYTE   8

Definition at line 14 of file hstore_gist.c.

◆ CALCGTSIZE

◆ CLRBIT

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

Definition at line 31 of file hstore_gist.c.

◆ GETBIT

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

Definition at line 33 of file hstore_gist.c.

Referenced by ghstore_consistent(), and hemdistsign().

◆ GETBITBYTE

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

Definition at line 30 of file hstore_gist.c.

◆ GETBYTE

◆ GETENTRY

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

Definition at line 64 of file hstore_gist.c.

Referenced by ghstore_picksplit(), and ghstore_union().

◆ GETSIGN

◆ GTHDRSIZE

#define GTHDRSIZE   (VARHDRSZ + sizeof(int32))

Definition at line 48 of file hstore_gist.c.

Referenced by g_intbig_picksplit(), and ghstore_picksplit().

◆ HASH

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

◆ HASHVAL

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

◆ ISALLTRUE

◆ LOOPBIT

#define LOOPBIT   for(i=0;i<SIGLENBIT;i++)

Definition at line 25 of file hstore_gist.c.

Referenced by hemdistsign().

◆ LOOPBYTE

◆ SETBIT

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

Definition at line 32 of file hstore_gist.c.

◆ SIGLEN

#define SIGLEN   ( sizeof(int)*SIGLENINT )

◆ SIGLENBIT

#define SIGLENBIT   (SIGLEN*BITBYTE)

Definition at line 17 of file hstore_gist.c.

Referenced by gtrgm_penalty(), gtrgm_picksplit(), hemdist(), hemdistcache(), and makesign().

◆ SIGLENINT

#define SIGLENINT   4 /* >122 => key will toast, so very slow!!! */

Definition at line 15 of file hstore_gist.c.

◆ SUMBIT

#define SUMBIT (   val)
Value:
( \
GETBITBYTE((val),0) + \
GETBITBYTE((val),1) + \
GETBITBYTE((val),2) + \
GETBITBYTE((val),3) + \
GETBITBYTE((val),4) + \
GETBITBYTE((val),5) + \
GETBITBYTE((val),6) + \
GETBITBYTE((val),7) \
)
long val
Definition: informix.c:684

Definition at line 53 of file hstore_gist.c.

Referenced by sizebitvec().

◆ WISH_F

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

Definition at line 66 of file hstore_gist.c.

Referenced by g_int_picksplit(), g_intbig_picksplit(), and ghstore_picksplit().

Typedef Documentation

◆ BITVEC

typedef char BITVEC[SIGLEN]

Definition at line 19 of file hstore_gist.c.

◆ BITVECP

typedef char* BITVECP

Definition at line 20 of file hstore_gist.c.

Function Documentation

◆ comparecost()

static int comparecost ( const void *  a,
const void *  b 
)
static

Definition at line 325 of file hstore_gist.c.

Referenced by ghstore_picksplit().

326 {
327  return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
328 }

◆ crc32_sz()

static pg_crc32 crc32_sz ( char *  buf,
int  size 
)
static

Definition at line 70 of file hstore_gist.c.

References COMP_TRADITIONAL_CRC32, FIN_TRADITIONAL_CRC32, ghstore_in(), ghstore_out(), INIT_TRADITIONAL_CRC32, and PG_FUNCTION_INFO_V1().

Referenced by ghstore_compress(), and ghstore_consistent().

71 {
72  pg_crc32 crc;
73 
75  COMP_TRADITIONAL_CRC32(crc, buf, size);
77 
78  return crc;
79 }
#define COMP_TRADITIONAL_CRC32(crc, data, len)
Definition: pg_crc.h:48
#define INIT_TRADITIONAL_CRC32(crc)
Definition: pg_crc.h:46
static char * buf
Definition: pg_test_fsync.c:68
#define FIN_TRADITIONAL_CRC32(crc)
Definition: pg_crc.h:47
uint32 pg_crc32
Definition: pg_crc.h:37

◆ ghstore_compress()

Datum ghstore_compress ( PG_FUNCTION_ARGS  )

Definition at line 109 of file hstore_gist.c.

References ALLISTRUE, ARRPTR, CALCGTSIZE, crc32_sz(), DatumGetHStoreP, DatumGetPointer, GISTTYPE::flag, GETSIGN, gistentryinit, HASH, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, ISALLTRUE, GISTENTRY::key, GISTENTRY::leafkey, LOOPBYTE, GISTENTRY::offset, GISTENTRY::page, palloc(), palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, GISTENTRY::rel, SET_VARSIZE, sign, STRPTR, and val.

Referenced by ghstore_out().

110 {
111  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
112  GISTENTRY *retval = entry;
113 
114  if (entry->leafkey)
115  {
116  GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
117  HStore *val = DatumGetHStoreP(entry->key);
118  HEntry *hsent = ARRPTR(val);
119  char *ptr = STRPTR(val);
120  int count = HS_COUNT(val);
121  int i;
122 
123  SET_VARSIZE(res, CALCGTSIZE(0));
124 
125  for (i = 0; i < count; ++i)
126  {
127  int h;
128 
129  h = crc32_sz((char *) HSTORE_KEY(hsent, ptr, i),
130  HSTORE_KEYLEN(hsent, i));
131  HASH(GETSIGN(res), h);
132  if (!HSTORE_VALISNULL(hsent, i))
133  {
134  h = crc32_sz((char *) HSTORE_VAL(hsent, ptr, i),
135  HSTORE_VALLEN(hsent, i));
136  HASH(GETSIGN(res), h);
137  }
138  }
139 
140  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
141  gistentryinit(*retval, PointerGetDatum(res),
142  entry->rel, entry->page,
143  entry->offset,
144  false);
145  }
146  else if (!ISALLTRUE(DatumGetPointer(entry->key)))
147  {
148  int32 i;
149  GISTTYPE *res;
151 
152  LOOPBYTE
153  {
154  if ((sign[i] & 0xff) != 0xff)
155  PG_RETURN_POINTER(retval);
156  }
157 
158  res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
160  res->flag = ALLISTRUE;
161 
162  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
163  gistentryinit(*retval, PointerGetDatum(res),
164  entry->rel, entry->page,
165  entry->offset,
166  false);
167  }
168 
169  PG_RETURN_POINTER(retval);
170 }
Relation rel
Definition: gist.h:132
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
Definition: hstore.h:44
#define DatumGetHStoreP(d)
Definition: hstore.h:152
#define GETSIGN(x)
Definition: hstore_gist.c:51
#define PointerGetDatum(X)
Definition: postgres.h:556
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define ALLISTRUE
Definition: hstore_gist.c:44
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define HASH(sign, val)
Definition: hstore_gist.c:35
int32 flag
Definition: hstore_gist.c:40
signed int int32
Definition: c.h:346
Page page
Definition: gist.h:133
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
char sign
Definition: informix.c:688
Datum key
Definition: gist.h:131
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
bool leafkey
Definition: gist.h:135
void * palloc0(Size size)
Definition: mcxt.c:980
static pg_crc32 crc32_sz(char *buf, int size)
Definition: hstore_gist.c:70
#define ISALLTRUE(x)
Definition: hstore_gist.c:46
#define CALCGTSIZE(flag)
Definition: hstore_gist.c:49
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:215
Definition: hstore.h:18
#define DatumGetPointer(X)
Definition: postgres.h:549
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * palloc(Size size)
Definition: mcxt.c:949
#define STRPTR(x)
Definition: hstore.h:76
#define LOOPBYTE
Definition: hstore_gist.c:22
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
char * BITVECP
Definition: hstore_gist.c:20
#define ARRPTR(x)
Definition: cube.c:25
OffsetNumber offset
Definition: gist.h:134
long val
Definition: informix.c:684

◆ ghstore_consistent()

Datum ghstore_consistent ( PG_FUNCTION_ARGS  )

Definition at line 497 of file hstore_gist.c.

References ARRPTR, crc32_sz(), DatumGetPointer, deconstruct_array(), elog, ERROR, GETBIT, GETSIGN, HASHVAL, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, HStoreContainsStrategyNumber, HStoreExistsAllStrategyNumber, HStoreExistsAnyStrategyNumber, HStoreExistsStrategyNumber, HStoreOldContainsStrategyNumber, i, ISALLTRUE, PG_GETARG_ARRAYTYPE_P, PG_GETARG_HSTORE_P, PG_GETARG_POINTER, PG_GETARG_TEXT_PP, PG_GETARG_UINT16, PG_RETURN_BOOL, sign, STRPTR, VARDATA, VARDATA_ANY, VARHDRSZ, VARSIZE, and VARSIZE_ANY_EXHDR.

Referenced by ghstore_out().

498 {
499  GISTTYPE *entry = (GISTTYPE *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
501 
502  /* Oid subtype = PG_GETARG_OID(3); */
503  bool *recheck = (bool *) PG_GETARG_POINTER(4);
504  bool res = true;
505  BITVECP sign;
506 
507  /* All cases served by this function are inexact */
508  *recheck = true;
509 
510  if (ISALLTRUE(entry))
511  PG_RETURN_BOOL(true);
512 
513  sign = GETSIGN(entry);
514 
515  if (strategy == HStoreContainsStrategyNumber ||
517  {
518  HStore *query = PG_GETARG_HSTORE_P(1);
519  HEntry *qe = ARRPTR(query);
520  char *qv = STRPTR(query);
521  int count = HS_COUNT(query);
522  int i;
523 
524  for (i = 0; res && i < count; ++i)
525  {
526  int crc = crc32_sz((char *) HSTORE_KEY(qe, qv, i),
527  HSTORE_KEYLEN(qe, i));
528 
529  if (GETBIT(sign, HASHVAL(crc)))
530  {
531  if (!HSTORE_VALISNULL(qe, i))
532  {
533  crc = crc32_sz((char *) HSTORE_VAL(qe, qv, i),
534  HSTORE_VALLEN(qe, i));
535  if (!GETBIT(sign, HASHVAL(crc)))
536  res = false;
537  }
538  }
539  else
540  res = false;
541  }
542  }
543  else if (strategy == HStoreExistsStrategyNumber)
544  {
545  text *query = PG_GETARG_TEXT_PP(1);
546  int crc = crc32_sz(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query));
547 
548  res = (GETBIT(sign, HASHVAL(crc))) ? true : false;
549  }
550  else if (strategy == HStoreExistsAllStrategyNumber)
551  {
552  ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
553  Datum *key_datums;
554  bool *key_nulls;
555  int key_count;
556  int i;
557 
558  deconstruct_array(query,
559  TEXTOID, -1, false, 'i',
560  &key_datums, &key_nulls, &key_count);
561 
562  for (i = 0; res && i < key_count; ++i)
563  {
564  int crc;
565 
566  if (key_nulls[i])
567  continue;
568  crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
569  if (!(GETBIT(sign, HASHVAL(crc))))
570  res = false;
571  }
572  }
573  else if (strategy == HStoreExistsAnyStrategyNumber)
574  {
575  ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
576  Datum *key_datums;
577  bool *key_nulls;
578  int key_count;
579  int i;
580 
581  deconstruct_array(query,
582  TEXTOID, -1, false, 'i',
583  &key_datums, &key_nulls, &key_count);
584 
585  res = false;
586 
587  for (i = 0; !res && i < key_count; ++i)
588  {
589  int crc;
590 
591  if (key_nulls[i])
592  continue;
593  crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
594  if (GETBIT(sign, HASHVAL(crc)))
595  res = true;
596  }
597  }
598  else
599  elog(ERROR, "Unsupported strategy number: %d", strategy);
600 
601  PG_RETURN_BOOL(res);
602 }
Definition: hstore.h:44
#define HStoreContainsStrategyNumber
Definition: hstore.h:180
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
#define VARSIZE(PTR)
Definition: postgres.h:303
#define GETSIGN(x)
Definition: hstore_gist.c:51
#define VARHDRSZ
Definition: c.h:555
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
uint16 StrategyNumber
Definition: stratnum.h:22
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define ERROR
Definition: elog.h:43
#define HStoreExistsStrategyNumber
Definition: hstore.h:181
char sign
Definition: informix.c:688
#define HStoreExistsAllStrategyNumber
Definition: hstore.h:183
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HASHVAL(val)
Definition: hstore_gist.c:34
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
static pg_crc32 crc32_sz(char *buf, int size)
Definition: hstore_gist.c:70
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
uintptr_t Datum
Definition: postgres.h:367
#define ISALLTRUE(x)
Definition: hstore_gist.c:46
#define GETBIT(x, i)
Definition: hstore_gist.c:33
Definition: hstore.h:18
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:267
#define DatumGetPointer(X)
Definition: postgres.h:549
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define STRPTR(x)
Definition: hstore.h:76
#define HStoreExistsAnyStrategyNumber
Definition: hstore.h:182
#define elog(elevel,...)
Definition: elog.h:226
int i
Definition: c.h:549
char * BITVECP
Definition: hstore_gist.c:20
#define ARRPTR(x)
Definition: cube.c:25
#define HStoreOldContainsStrategyNumber
Definition: hstore.h:184

◆ ghstore_decompress()

Datum ghstore_decompress ( PG_FUNCTION_ARGS  )

Definition at line 177 of file hstore_gist.c.

References PG_GETARG_POINTER, and PG_RETURN_POINTER.

Referenced by ghstore_out().

178 {
180 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271

◆ ghstore_in()

Datum ghstore_in ( PG_FUNCTION_ARGS  )

Definition at line 87 of file hstore_gist.c.

References elog, ERROR, and PG_RETURN_DATUM.

Referenced by crc32_sz().

88 {
89  elog(ERROR, "Not implemented");
90  PG_RETURN_DATUM(0);
91 }
#define ERROR
Definition: elog.h:43
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define elog(elevel,...)
Definition: elog.h:226

◆ ghstore_out()

Datum ghstore_out ( PG_FUNCTION_ARGS  )

Definition at line 94 of file hstore_gist.c.

References elog, ERROR, ghstore_compress(), ghstore_consistent(), ghstore_decompress(), ghstore_penalty(), ghstore_picksplit(), ghstore_same(), ghstore_union(), PG_FUNCTION_INFO_V1(), and PG_RETURN_DATUM.

Referenced by crc32_sz().

95 {
96  elog(ERROR, "Not implemented");
97  PG_RETURN_DATUM(0);
98 }
#define ERROR
Definition: elog.h:43
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define elog(elevel,...)
Definition: elog.h:226

◆ ghstore_penalty()

Datum ghstore_penalty ( PG_FUNCTION_ARGS  )

Definition at line 305 of file hstore_gist.c.

References DatumGetPointer, hemdist(), GISTENTRY::key, newval, PG_GETARG_POINTER, and PG_RETURN_POINTER.

Referenced by ghstore_out().

306 {
307  GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
308  GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
309  float *penalty = (float *) PG_GETARG_POINTER(2);
310  GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
311  GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
312 
313  *penalty = hemdist(origval, newval);
314  PG_RETURN_POINTER(penalty);
315 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
static int hemdist(GISTTYPE *a, GISTTYPE *b)
Definition: hstore_gist.c:243
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
Datum key
Definition: gist.h:131
#define newval
#define DatumGetPointer(X)
Definition: postgres.h:549

◆ ghstore_picksplit()

Datum ghstore_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 332 of file hstore_gist.c.

References ALLISTRUE, comparecost(), SPLITCOST::cost, FirstOffsetNumber, GISTTYPE::flag, GETENTRY, GETSIGN, GTHDRSIZE, hemdist(), i, ISALLTRUE, LOOPBYTE, MemSet, GistEntryVector::n, OffsetNumberNext, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, SPLITCOST::pos, qsort, SET_VARSIZE, SIGLEN, 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.

Referenced by ghstore_out().

333 {
335  OffsetNumber maxoff = entryvec->n - 2;
336 
338  OffsetNumber k,
339  j;
340  GISTTYPE *datum_l,
341  *datum_r;
342  BITVECP union_l,
343  union_r;
344  int32 size_alpha,
345  size_beta;
346  int32 size_waste,
347  waste = -1;
348  int32 nbytes;
349  OffsetNumber seed_1 = 0,
350  seed_2 = 0;
351  OffsetNumber *left,
352  *right;
353  BITVECP ptr;
354  int i;
355  SPLITCOST *costvector;
356  GISTTYPE *_k,
357  *_j;
358 
359  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
360  v->spl_left = (OffsetNumber *) palloc(nbytes);
361  v->spl_right = (OffsetNumber *) palloc(nbytes);
362 
363  for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
364  {
365  _k = GETENTRY(entryvec, k);
366  for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
367  {
368  size_waste = hemdist(_k, GETENTRY(entryvec, j));
369  if (size_waste > waste)
370  {
371  waste = size_waste;
372  seed_1 = k;
373  seed_2 = j;
374  }
375  }
376  }
377 
378  left = v->spl_left;
379  v->spl_nleft = 0;
380  right = v->spl_right;
381  v->spl_nright = 0;
382 
383  if (seed_1 == 0 || seed_2 == 0)
384  {
385  seed_1 = 1;
386  seed_2 = 2;
387  }
388 
389  /* form initial .. */
390  if (ISALLTRUE(GETENTRY(entryvec, seed_1)))
391  {
392  datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
393  SET_VARSIZE(datum_l, GTHDRSIZE);
394  datum_l->flag = ALLISTRUE;
395  }
396  else
397  {
398  datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
399  SET_VARSIZE(datum_l, GTHDRSIZE + SIGLEN);
400  datum_l->flag = 0;
401  memcpy((void *) GETSIGN(datum_l), (void *) GETSIGN(GETENTRY(entryvec, seed_1)), sizeof(BITVEC))
402  ;
403  }
404  if (ISALLTRUE(GETENTRY(entryvec, seed_2)))
405  {
406  datum_r = (GISTTYPE *) palloc(GTHDRSIZE);
407  SET_VARSIZE(datum_r, GTHDRSIZE);
408  datum_r->flag = ALLISTRUE;
409  }
410  else
411  {
412  datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
413  SET_VARSIZE(datum_r, GTHDRSIZE + SIGLEN);
414  datum_r->flag = 0;
415  memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC));
416  }
417 
418  maxoff = OffsetNumberNext(maxoff);
419  /* sort before ... */
420  costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
421  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
422  {
423  costvector[j - 1].pos = j;
424  _j = GETENTRY(entryvec, j);
425  size_alpha = hemdist(datum_l, _j);
426  size_beta = hemdist(datum_r, _j);
427  costvector[j - 1].cost = abs(size_alpha - size_beta);
428  }
429  qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
430 
431  union_l = GETSIGN(datum_l);
432  union_r = GETSIGN(datum_r);
433 
434  for (k = 0; k < maxoff; k++)
435  {
436  j = costvector[k].pos;
437  if (j == seed_1)
438  {
439  *left++ = j;
440  v->spl_nleft++;
441  continue;
442  }
443  else if (j == seed_2)
444  {
445  *right++ = j;
446  v->spl_nright++;
447  continue;
448  }
449  _j = GETENTRY(entryvec, j);
450  size_alpha = hemdist(datum_l, _j);
451  size_beta = hemdist(datum_r, _j);
452 
453  if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.0001))
454  {
455  if (ISALLTRUE(datum_l) || ISALLTRUE(_j))
456  {
457  if (!ISALLTRUE(datum_l))
458  MemSet((void *) union_l, 0xff, sizeof(BITVEC));
459  }
460  else
461  {
462  ptr = GETSIGN(_j);
463  LOOPBYTE
464  union_l[i] |= ptr[i];
465  }
466  *left++ = j;
467  v->spl_nleft++;
468  }
469  else
470  {
471  if (ISALLTRUE(datum_r) || ISALLTRUE(_j))
472  {
473  if (!ISALLTRUE(datum_r))
474  MemSet((void *) union_r, 0xff, sizeof(BITVEC));
475  }
476  else
477  {
478  ptr = GETSIGN(_j);
479  LOOPBYTE
480  union_r[i] |= ptr[i];
481  }
482  *right++ = j;
483  v->spl_nright++;
484  }
485  }
486 
487  *right = *left = FirstOffsetNumber;
488 
489  v->spl_ldatum = PointerGetDatum(datum_l);
490  v->spl_rdatum = PointerGetDatum(datum_r);
491 
493 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
static int hemdist(GISTTYPE *a, GISTTYPE *b)
Definition: hstore_gist.c:243
OffsetNumber pos
Definition: hstore_gist.c:320
#define GETSIGN(x)
Definition: hstore_gist.c:51
#define PointerGetDatum(X)
Definition: postgres.h:556
OffsetNumber * spl_left
Definition: gist.h:113
Datum spl_rdatum
Definition: gist.h:120
int32 n
Definition: gist.h:206
#define ALLISTRUE
Definition: hstore_gist.c:44
#define MemSet(start, val, len)
Definition: c.h:955
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
int32 flag
Definition: hstore_gist.c:40
int spl_nleft
Definition: gist.h:114
signed int int32
Definition: c.h:346
int32 cost
Definition: hstore_gist.c:321
uint16 OffsetNumber
Definition: off.h:24
#define WISH_F(a, b, c)
Definition: hstore_gist.c:66
#define GTHDRSIZE
Definition: hstore_gist.c:48
int spl_nright
Definition: gist.h:119
char BITVEC[SIGLEN]
Definition: hstore_gist.c:19
#define FirstOffsetNumber
Definition: off.h:27
#define GETENTRY(vec, pos)
Definition: hstore_gist.c:64
#define ISALLTRUE(x)
Definition: hstore_gist.c:46
Datum spl_ldatum
Definition: gist.h:115
static int comparecost(const void *a, const void *b)
Definition: hstore_gist.c:325
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
OffsetNumber * spl_right
Definition: gist.h:118
void * palloc(Size size)
Definition: mcxt.c:949
#define LOOPBYTE
Definition: hstore_gist.c:22
int i
#define SIGLEN
Definition: hstore_gist.c:16
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
char * BITVECP
Definition: hstore_gist.c:20
#define qsort(a, b, c, d)
Definition: port.h:492

◆ ghstore_same()

Datum ghstore_same ( PG_FUNCTION_ARGS  )

Definition at line 183 of file hstore_gist.c.

References GETSIGN, i, ISALLTRUE, LOOPBYTE, PG_GETARG_POINTER, and PG_RETURN_POINTER.

Referenced by ghstore_out().

184 {
185  GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
186  GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
187  bool *result = (bool *) PG_GETARG_POINTER(2);
188 
189  if (ISALLTRUE(a) && ISALLTRUE(b))
190  *result = true;
191  else if (ISALLTRUE(a))
192  *result = false;
193  else if (ISALLTRUE(b))
194  *result = false;
195  else
196  {
197  int32 i;
198  BITVECP sa = GETSIGN(a),
199  sb = GETSIGN(b);
200 
201  *result = true;
202  LOOPBYTE
203  {
204  if (sa[i] != sb[i])
205  {
206  *result = false;
207  break;
208  }
209  }
210  }
211  PG_RETURN_POINTER(result);
212 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define GETSIGN(x)
Definition: hstore_gist.c:51
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
signed int int32
Definition: c.h:346
#define ISALLTRUE(x)
Definition: hstore_gist.c:46
#define LOOPBYTE
Definition: hstore_gist.c:22
int i
char * BITVECP
Definition: hstore_gist.c:20

◆ ghstore_union()

Datum ghstore_union ( PG_FUNCTION_ARGS  )

Definition at line 272 of file hstore_gist.c.

References ALLISTRUE, CALCGTSIZE, flag(), GISTTYPE::flag, GETENTRY, GETSIGN, i, ISALLTRUE, MemSet, GistEntryVector::n, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, SET_VARSIZE, and unionkey().

Referenced by ghstore_out().

273 {
275  int32 len = entryvec->n;
276 
277  int *size = (int *) PG_GETARG_POINTER(1);
278  BITVEC base;
279  int32 i;
280  int32 flag = 0;
281  GISTTYPE *result;
282 
283  MemSet((void *) base, 0, sizeof(BITVEC));
284  for (i = 0; i < len; i++)
285  {
286  if (unionkey(base, GETENTRY(entryvec, i)))
287  {
288  flag = ALLISTRUE;
289  break;
290  }
291  }
292 
293  len = CALCGTSIZE(flag);
294  result = (GISTTYPE *) palloc(len);
295  SET_VARSIZE(result, len);
296  result->flag = flag;
297  if (!ISALLTRUE(result))
298  memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
299  *size = len;
300 
301  PG_RETURN_POINTER(result);
302 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define GETSIGN(x)
Definition: hstore_gist.c:51
int32 n
Definition: gist.h:206
#define ALLISTRUE
Definition: hstore_gist.c:44
#define MemSet(start, val, len)
Definition: c.h:955
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
int32 flag
Definition: hstore_gist.c:40
signed int int32
Definition: c.h:346
char BITVEC[SIGLEN]
Definition: hstore_gist.c:19
char * flag(int b)
Definition: test-ctype.c:33
static int32 unionkey(BITVECP sbase, GISTTYPE *add)
Definition: hstore_gist.c:259
#define GETENTRY(vec, pos)
Definition: hstore_gist.c:64
#define ISALLTRUE(x)
Definition: hstore_gist.c:46
#define CALCGTSIZE(flag)
Definition: hstore_gist.c:49
void * palloc(Size size)
Definition: mcxt.c:949
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ hemdist()

static int hemdist ( GISTTYPE a,
GISTTYPE b 
)
static

Definition at line 243 of file hstore_gist.c.

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

Referenced by ghstore_penalty(), and ghstore_picksplit().

244 {
245  if (ISALLTRUE(a))
246  {
247  if (ISALLTRUE(b))
248  return 0;
249  else
250  return SIGLENBIT - sizebitvec(GETSIGN(b));
251  }
252  else if (ISALLTRUE(b))
253  return SIGLENBIT - sizebitvec(GETSIGN(a));
254 
255  return hemdistsign(GETSIGN(a), GETSIGN(b));
256 }
#define GETSIGN(x)
Definition: hstore_gist.c:51
#define SIGLENBIT
Definition: hstore_gist.c:17
static int32 sizebitvec(BITVECP sign)
Definition: hstore_gist.c:215
#define ISALLTRUE(x)
Definition: hstore_gist.c:46
static int hemdistsign(BITVECP a, BITVECP b)
Definition: hstore_gist.c:229

◆ hemdistsign()

static int hemdistsign ( BITVECP  a,
BITVECP  b 
)
static

Definition at line 229 of file hstore_gist.c.

References GETBIT, i, and LOOPBIT.

Referenced by hemdist().

230 {
231  int i,
232  dist = 0;
233 
234  LOOPBIT
235  {
236  if (GETBIT(a, i) != GETBIT(b, i))
237  dist++;
238  }
239  return dist;
240 }
#define LOOPBIT
Definition: hstore_gist.c:25
#define GETBIT(x, i)
Definition: hstore_gist.c:33
int i

◆ PG_FUNCTION_INFO_V1() [1/9]

PG_FUNCTION_INFO_V1 ( ghstore_in  )

Referenced by crc32_sz(), and ghstore_out().

◆ PG_FUNCTION_INFO_V1() [2/9]

PG_FUNCTION_INFO_V1 ( ghstore_out  )

◆ PG_FUNCTION_INFO_V1() [3/9]

PG_FUNCTION_INFO_V1 ( ghstore_consistent  )

◆ PG_FUNCTION_INFO_V1() [4/9]

PG_FUNCTION_INFO_V1 ( ghstore_compress  )

◆ PG_FUNCTION_INFO_V1() [5/9]

PG_FUNCTION_INFO_V1 ( ghstore_decompress  )

◆ PG_FUNCTION_INFO_V1() [6/9]

PG_FUNCTION_INFO_V1 ( ghstore_penalty  )

◆ PG_FUNCTION_INFO_V1() [7/9]

PG_FUNCTION_INFO_V1 ( ghstore_picksplit  )

◆ PG_FUNCTION_INFO_V1() [8/9]

PG_FUNCTION_INFO_V1 ( ghstore_union  )

◆ PG_FUNCTION_INFO_V1() [9/9]

PG_FUNCTION_INFO_V1 ( ghstore_same  )

◆ sizebitvec()

static int32 sizebitvec ( BITVECP  sign)
static

Definition at line 215 of file hstore_gist.c.

References i, LOOPBYTE, and SUMBIT.

Referenced by hemdist().

216 {
217  int32 size = 0,
218  i;
219 
220  LOOPBYTE
221  {
222  size += SUMBIT(sign);
223  sign = (BITVECP) (((char *) sign) + 1);
224  }
225  return size;
226 }
#define SUMBIT(val)
Definition: hstore_gist.c:53
signed int int32
Definition: c.h:346
char sign
Definition: informix.c:688
#define LOOPBYTE
Definition: hstore_gist.c:22
int i
char * BITVECP
Definition: hstore_gist.c:20

◆ unionkey()

static int32 unionkey ( BITVECP  sbase,
GISTTYPE add 
)
static

Definition at line 259 of file hstore_gist.c.

References GETSIGN, i, ISALLTRUE, and LOOPBYTE.

Referenced by ghstore_union().

260 {
261  int32 i;
262  BITVECP sadd = GETSIGN(add);
263 
264  if (ISALLTRUE(add))
265  return 1;
266  LOOPBYTE
267  sbase[i] |= sadd[i];
268  return 0;
269 }
#define GETSIGN(x)
Definition: hstore_gist.c:51
signed int int32
Definition: c.h:346
#define ISALLTRUE(x)
Definition: hstore_gist.c:46
#define LOOPBYTE
Definition: hstore_gist.c:22
int i
char * BITVECP
Definition: hstore_gist.c:20