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 "hstore.h"
#include "utils/pg_crc.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 13 of file hstore_gist.c.

◆ CALCGTSIZE

◆ CLRBIT

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

Definition at line 30 of file hstore_gist.c.

◆ GETBIT

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

Definition at line 32 of file hstore_gist.c.

Referenced by ghstore_consistent(), and hemdistsign().

◆ GETBITBYTE

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

Definition at line 29 of file hstore_gist.c.

◆ GETBYTE

◆ GETENTRY

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

Definition at line 63 of file hstore_gist.c.

Referenced by ghstore_picksplit(), and ghstore_union().

◆ GETSIGN

◆ GTHDRSIZE

#define GTHDRSIZE   (VARHDRSZ + sizeof(int32))

Definition at line 47 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 24 of file hstore_gist.c.

Referenced by hemdistsign().

◆ LOOPBYTE

◆ SETBIT

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

Definition at line 31 of file hstore_gist.c.

◆ SIGLEN

#define SIGLEN   ( sizeof(int)*SIGLENINT )

◆ SIGLENBIT

#define SIGLENBIT   (SIGLEN*BITBYTE)

Definition at line 16 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 14 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:664

Definition at line 52 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 65 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 18 of file hstore_gist.c.

◆ BITVECP

typedef char* BITVECP

Definition at line 19 of file hstore_gist.c.

Function Documentation

◆ comparecost()

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

Definition at line 324 of file hstore_gist.c.

Referenced by ghstore_picksplit().

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

◆ crc32_sz()

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

Definition at line 69 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().

70 {
71  pg_crc32 crc;
72 
74  COMP_TRADITIONAL_CRC32(crc, buf, size);
76 
77  return crc;
78 }
#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:67
#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 108 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().

109 {
110  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
111  GISTENTRY *retval = entry;
112 
113  if (entry->leafkey)
114  {
115  GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
116  HStore *val = DatumGetHStoreP(entry->key);
117  HEntry *hsent = ARRPTR(val);
118  char *ptr = STRPTR(val);
119  int count = HS_COUNT(val);
120  int i;
121 
122  SET_VARSIZE(res, CALCGTSIZE(0));
123 
124  for (i = 0; i < count; ++i)
125  {
126  int h;
127 
128  h = crc32_sz((char *) HSTORE_KEY(hsent, ptr, i),
129  HSTORE_KEYLEN(hsent, i));
130  HASH(GETSIGN(res), h);
131  if (!HSTORE_VALISNULL(hsent, i))
132  {
133  h = crc32_sz((char *) HSTORE_VAL(hsent, ptr, i),
134  HSTORE_VALLEN(hsent, i));
135  HASH(GETSIGN(res), h);
136  }
137  }
138 
139  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
140  gistentryinit(*retval, PointerGetDatum(res),
141  entry->rel, entry->page,
142  entry->offset,
143  false);
144  }
145  else if (!ISALLTRUE(DatumGetPointer(entry->key)))
146  {
147  int32 i;
148  GISTTYPE *res;
150 
151  LOOPBYTE
152  {
153  if ((sign[i] & 0xff) != 0xff)
154  PG_RETURN_POINTER(retval);
155  }
156 
157  res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
159  res->flag = ALLISTRUE;
160 
161  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
162  gistentryinit(*retval, PointerGetDatum(res),
163  entry->rel, entry->page,
164  entry->offset,
165  false);
166  }
167 
168  PG_RETURN_POINTER(retval);
169 }
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:50
#define PointerGetDatum(X)
Definition: postgres.h:556
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define ALLISTRUE
Definition: hstore_gist.c:43
#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:34
int32 flag
Definition: hstore_gist.c:39
signed int int32
Definition: c.h:347
Page page
Definition: gist.h:133
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
char sign
Definition: informix.c:668
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:69
#define ISALLTRUE(x)
Definition: hstore_gist.c:45
#define CALCGTSIZE(flag)
Definition: hstore_gist.c:48
#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:21
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
char * BITVECP
Definition: hstore_gist.c:19
#define ARRPTR(x)
Definition: cube.c:24
OffsetNumber offset
Definition: gist.h:134
long val
Definition: informix.c:664

◆ ghstore_consistent()

Datum ghstore_consistent ( PG_FUNCTION_ARGS  )

Definition at line 496 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().

497 {
498  GISTTYPE *entry = (GISTTYPE *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
500 
501  /* Oid subtype = PG_GETARG_OID(3); */
502  bool *recheck = (bool *) PG_GETARG_POINTER(4);
503  bool res = true;
504  BITVECP sign;
505 
506  /* All cases served by this function are inexact */
507  *recheck = true;
508 
509  if (ISALLTRUE(entry))
510  PG_RETURN_BOOL(true);
511 
512  sign = GETSIGN(entry);
513 
514  if (strategy == HStoreContainsStrategyNumber ||
516  {
517  HStore *query = PG_GETARG_HSTORE_P(1);
518  HEntry *qe = ARRPTR(query);
519  char *qv = STRPTR(query);
520  int count = HS_COUNT(query);
521  int i;
522 
523  for (i = 0; res && i < count; ++i)
524  {
525  int crc = crc32_sz((char *) HSTORE_KEY(qe, qv, i),
526  HSTORE_KEYLEN(qe, i));
527 
528  if (GETBIT(sign, HASHVAL(crc)))
529  {
530  if (!HSTORE_VALISNULL(qe, i))
531  {
532  crc = crc32_sz((char *) HSTORE_VAL(qe, qv, i),
533  HSTORE_VALLEN(qe, i));
534  if (!GETBIT(sign, HASHVAL(crc)))
535  res = false;
536  }
537  }
538  else
539  res = false;
540  }
541  }
542  else if (strategy == HStoreExistsStrategyNumber)
543  {
544  text *query = PG_GETARG_TEXT_PP(1);
545  int crc = crc32_sz(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query));
546 
547  res = (GETBIT(sign, HASHVAL(crc))) ? true : false;
548  }
549  else if (strategy == HStoreExistsAllStrategyNumber)
550  {
551  ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
552  Datum *key_datums;
553  bool *key_nulls;
554  int key_count;
555  int i;
556 
557  deconstruct_array(query,
558  TEXTOID, -1, false, 'i',
559  &key_datums, &key_nulls, &key_count);
560 
561  for (i = 0; res && i < key_count; ++i)
562  {
563  int crc;
564 
565  if (key_nulls[i])
566  continue;
567  crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
568  if (!(GETBIT(sign, HASHVAL(crc))))
569  res = false;
570  }
571  }
572  else if (strategy == HStoreExistsAnyStrategyNumber)
573  {
574  ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
575  Datum *key_datums;
576  bool *key_nulls;
577  int key_count;
578  int i;
579 
580  deconstruct_array(query,
581  TEXTOID, -1, false, 'i',
582  &key_datums, &key_nulls, &key_count);
583 
584  res = false;
585 
586  for (i = 0; !res && i < key_count; ++i)
587  {
588  int crc;
589 
590  if (key_nulls[i])
591  continue;
592  crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
593  if (GETBIT(sign, HASHVAL(crc)))
594  res = true;
595  }
596  }
597  else
598  elog(ERROR, "Unsupported strategy number: %d", strategy);
599 
600  PG_RETURN_BOOL(res);
601 }
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:50
#define VARHDRSZ
Definition: c.h:562
#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:668
#define HStoreExistsAllStrategyNumber
Definition: hstore.h:183
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HASHVAL(val)
Definition: hstore_gist.c:33
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
static pg_crc32 crc32_sz(char *buf, int size)
Definition: hstore_gist.c:69
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
uintptr_t Datum
Definition: postgres.h:367
#define ISALLTRUE(x)
Definition: hstore_gist.c:45
#define GETBIT(x, i)
Definition: hstore_gist.c:32
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:228
int i
Definition: c.h:556
char * BITVECP
Definition: hstore_gist.c:19
#define ARRPTR(x)
Definition: cube.c:24
#define HStoreOldContainsStrategyNumber
Definition: hstore.h:184

◆ ghstore_decompress()

Datum ghstore_decompress ( PG_FUNCTION_ARGS  )

Definition at line 176 of file hstore_gist.c.

References PG_GETARG_POINTER, and PG_RETURN_POINTER.

Referenced by ghstore_out().

177 {
179 }
#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 86 of file hstore_gist.c.

References elog, ERROR, and PG_RETURN_DATUM.

Referenced by crc32_sz().

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

◆ ghstore_out()

Datum ghstore_out ( PG_FUNCTION_ARGS  )

Definition at line 93 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().

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

◆ ghstore_penalty()

Datum ghstore_penalty ( PG_FUNCTION_ARGS  )

Definition at line 304 of file hstore_gist.c.

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

Referenced by ghstore_out().

305 {
306  GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
307  GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
308  float *penalty = (float *) PG_GETARG_POINTER(2);
309  GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
310  GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
311 
312  *penalty = hemdist(origval, newval);
313  PG_RETURN_POINTER(penalty);
314 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
static int hemdist(GISTTYPE *a, GISTTYPE *b)
Definition: hstore_gist.c:242
#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 331 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().

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

◆ ghstore_same()

Datum ghstore_same ( PG_FUNCTION_ARGS  )

Definition at line 182 of file hstore_gist.c.

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

Referenced by ghstore_out().

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

◆ ghstore_union()

Datum ghstore_union ( PG_FUNCTION_ARGS  )

Definition at line 271 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().

272 {
274  int32 len = entryvec->n;
275 
276  int *size = (int *) PG_GETARG_POINTER(1);
277  BITVEC base;
278  int32 i;
279  int32 flag = 0;
280  GISTTYPE *result;
281 
282  MemSet((void *) base, 0, sizeof(BITVEC));
283  for (i = 0; i < len; i++)
284  {
285  if (unionkey(base, GETENTRY(entryvec, i)))
286  {
287  flag = ALLISTRUE;
288  break;
289  }
290  }
291 
292  len = CALCGTSIZE(flag);
293  result = (GISTTYPE *) palloc(len);
294  SET_VARSIZE(result, len);
295  result->flag = flag;
296  if (!ISALLTRUE(result))
297  memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
298  *size = len;
299 
300  PG_RETURN_POINTER(result);
301 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define GETSIGN(x)
Definition: hstore_gist.c:50
int32 n
Definition: gist.h:206
#define ALLISTRUE
Definition: hstore_gist.c:43
#define MemSet(start, val, len)
Definition: c.h:962
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
int32 flag
Definition: hstore_gist.c:39
signed int int32
Definition: c.h:347
char BITVEC[SIGLEN]
Definition: hstore_gist.c:18
char * flag(int b)
Definition: test-ctype.c:33
static int32 unionkey(BITVECP sbase, GISTTYPE *add)
Definition: hstore_gist.c:258
#define GETENTRY(vec, pos)
Definition: hstore_gist.c:63
#define ISALLTRUE(x)
Definition: hstore_gist.c:45
#define CALCGTSIZE(flag)
Definition: hstore_gist.c:48
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 242 of file hstore_gist.c.

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

Referenced by ghstore_penalty(), and ghstore_picksplit().

243 {
244  if (ISALLTRUE(a))
245  {
246  if (ISALLTRUE(b))
247  return 0;
248  else
249  return SIGLENBIT - sizebitvec(GETSIGN(b));
250  }
251  else if (ISALLTRUE(b))
252  return SIGLENBIT - sizebitvec(GETSIGN(a));
253 
254  return hemdistsign(GETSIGN(a), GETSIGN(b));
255 }
#define GETSIGN(x)
Definition: hstore_gist.c:50
#define SIGLENBIT
Definition: hstore_gist.c:16
static int32 sizebitvec(BITVECP sign)
Definition: hstore_gist.c:214
#define ISALLTRUE(x)
Definition: hstore_gist.c:45
static int hemdistsign(BITVECP a, BITVECP b)
Definition: hstore_gist.c:228

◆ hemdistsign()

static int hemdistsign ( BITVECP  a,
BITVECP  b 
)
static

Definition at line 228 of file hstore_gist.c.

References GETBIT, i, and LOOPBIT.

Referenced by hemdist().

229 {
230  int i,
231  dist = 0;
232 
233  LOOPBIT
234  {
235  if (GETBIT(a, i) != GETBIT(b, i))
236  dist++;
237  }
238  return dist;
239 }
#define LOOPBIT
Definition: hstore_gist.c:24
#define GETBIT(x, i)
Definition: hstore_gist.c:32
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 214 of file hstore_gist.c.

References i, LOOPBYTE, and SUMBIT.

Referenced by hemdist().

215 {
216  int32 size = 0,
217  i;
218 
219  LOOPBYTE
220  {
221  size += SUMBIT(sign);
222  sign = (BITVECP) (((char *) sign) + 1);
223  }
224  return size;
225 }
#define SUMBIT(val)
Definition: hstore_gist.c:52
signed int int32
Definition: c.h:347
char sign
Definition: informix.c:668
#define LOOPBYTE
Definition: hstore_gist.c:21
int i
char * BITVECP
Definition: hstore_gist.c:19

◆ unionkey()

static int32 unionkey ( BITVECP  sbase,
GISTTYPE add 
)
static

Definition at line 258 of file hstore_gist.c.

References GETSIGN, i, ISALLTRUE, and LOOPBYTE.

Referenced by ghstore_union().

259 {
260  int32 i;
261  BITVECP sadd = GETSIGN(add);
262 
263  if (ISALLTRUE(add))
264  return 1;
265  LOOPBYTE
266  sbase[i] |= sadd[i];
267  return 0;
268 }
#define GETSIGN(x)
Definition: hstore_gist.c:50
signed int int32
Definition: c.h:347
#define ISALLTRUE(x)
Definition: hstore_gist.c:45
#define LOOPBYTE
Definition: hstore_gist.c:21
int i
char * BITVECP
Definition: hstore_gist.c:19