PostgreSQL Source Code  git master
hstore_gist.c File Reference
#include "postgres.h"
#include "access/gist.h"
#include "access/reloptions.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  GistHstoreOptions
 
struct  GISTTYPE
 
struct  SPLITCOST
 

Macros

#define BITBYTE   8
 
#define SIGLEN_DEFAULT   (sizeof(int32) * 4)
 
#define SIGLEN_MAX   GISTMaxIndexKeySize
 
#define SIGLENBIT(siglen)   ((siglen) * BITBYTE)
 
#define GET_SIGLEN()
 
#define LOOPBYTE(siglen)    for (i = 0; i < (siglen); i++)
 
#define LOOPBIT(siglen)    for (i = 0; i < SIGLENBIT(siglen); 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, siglen)   (((unsigned int)(val)) % SIGLENBIT(siglen))
 
#define HASH(sign, val, siglen)   SETBIT((sign), HASHVAL(val, siglen))
 
#define ALLISTRUE   0x04
 
#define ISALLTRUE(x)   ( ((GISTTYPE*)x)->flag & ALLISTRUE )
 
#define GTHDRSIZE   (VARHDRSZ + sizeof(int32))
 
#define CALCGTSIZE(flag, siglen)   ( 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 * 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)
 
static GISTTYPEghstore_alloc (bool allistrue, int siglen, BITVECP sign)
 
 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)
 
 PG_FUNCTION_INFO_V1 (ghstore_options)
 
Datum ghstore_compress (PG_FUNCTION_ARGS)
 
Datum ghstore_decompress (PG_FUNCTION_ARGS)
 
Datum ghstore_same (PG_FUNCTION_ARGS)
 
static int32 sizebitvec (BITVECP sign, int siglen)
 
static int hemdistsign (BITVECP a, BITVECP b, int siglen)
 
static int hemdist (GISTTYPE *a, GISTTYPE *b, int siglen)
 
static int32 unionkey (BITVECP sbase, GISTTYPE *add, int siglen)
 
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)
 
Datum ghstore_options (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ ALLISTRUE

#define ALLISTRUE   0x04

Definition at line 54 of file hstore_gist.c.

◆ BITBYTE

#define BITBYTE   8

Definition at line 21 of file hstore_gist.c.

◆ CALCGTSIZE

#define CALCGTSIZE (   flag,
  siglen 
)    ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : (siglen)) )

Definition at line 59 of file hstore_gist.c.

◆ CLRBIT

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

Definition at line 41 of file hstore_gist.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: hstore_gist.c:22

Definition at line 25 of file hstore_gist.c.

◆ GETBIT

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

Definition at line 43 of file hstore_gist.c.

◆ GETBITBYTE

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

Definition at line 40 of file hstore_gist.c.

◆ GETBYTE

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

Definition at line 39 of file hstore_gist.c.

◆ GETENTRY

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

Definition at line 74 of file hstore_gist.c.

◆ GETSIGN

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

Definition at line 61 of file hstore_gist.c.

◆ GTHDRSIZE

#define GTHDRSIZE   (VARHDRSZ + sizeof(int32))

Definition at line 58 of file hstore_gist.c.

◆ HASH

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

Definition at line 45 of file hstore_gist.c.

◆ HASHVAL

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

Definition at line 44 of file hstore_gist.c.

◆ ISALLTRUE

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

Definition at line 56 of file hstore_gist.c.

◆ LOOPBIT

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

Definition at line 35 of file hstore_gist.c.

◆ LOOPBYTE

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

Definition at line 32 of file hstore_gist.c.

◆ SETBIT

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

Definition at line 42 of file hstore_gist.c.

◆ SIGLEN_DEFAULT

#define SIGLEN_DEFAULT   (sizeof(int32) * 4)

Definition at line 22 of file hstore_gist.c.

◆ SIGLEN_MAX

#define SIGLEN_MAX   GISTMaxIndexKeySize

Definition at line 23 of file hstore_gist.c.

◆ SIGLENBIT

#define SIGLENBIT (   siglen)    ((siglen) * BITBYTE)

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

◆ WISH_F

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

Definition at line 76 of file hstore_gist.c.

Typedef Documentation

◆ BITVECP

typedef char* BITVECP

Definition at line 30 of file hstore_gist.c.

Function Documentation

◆ comparecost()

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

Definition at line 357 of file hstore_gist.c.

358 {
359  return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
360 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69

References a, and b.

Referenced by ghstore_picksplit().

◆ crc32_sz()

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

Definition at line 80 of file hstore_gist.c.

81 {
82  pg_crc32 crc;
83 
87 
88  return crc;
89 }
return crc
uint32 pg_crc32
Definition: pg_crc.h:37
#define FIN_TRADITIONAL_CRC32(crc)
Definition: pg_crc.h:47
#define INIT_TRADITIONAL_CRC32(crc)
Definition: pg_crc.h:46
#define COMP_TRADITIONAL_CRC32(crc, data, len)
Definition: pg_crc.h:48
static char * buf
Definition: pg_test_fsync.c:67

References buf, COMP_TRADITIONAL_CRC32, crc, FIN_TRADITIONAL_CRC32, and INIT_TRADITIONAL_CRC32.

Referenced by ghstore_compress(), and ghstore_consistent().

◆ ghstore_alloc()

static GISTTYPE* ghstore_alloc ( bool  allistrue,
int  siglen,
BITVECP  sign 
)
static

Definition at line 117 of file hstore_gist.c.

118 {
119  int flag = allistrue ? ALLISTRUE : 0;
120  int size = CALCGTSIZE(flag, siglen);
121  GISTTYPE *res = palloc(size);
122 
123  SET_VARSIZE(res, size);
124  res->flag = flag;
125 
126  if (!allistrue)
127  {
128  if (sign)
129  memcpy(GETSIGN(res), sign, siglen);
130  else
131  memset(GETSIGN(res), 0, siglen);
132  }
133 
134  return res;
135 }
#define ALLISTRUE
Definition: hstore_gist.c:54
#define CALCGTSIZE(flag, siglen)
Definition: hstore_gist.c:59
#define GETSIGN(x)
Definition: hstore_gist.c:61
char sign
Definition: informix.c:668
void * palloc(Size size)
Definition: mcxt.c:1226
char * flag(int b)
Definition: test-ctype.c:33
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

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

Referenced by ghstore_compress(), ghstore_picksplit(), and ghstore_union().

◆ ghstore_compress()

Datum ghstore_compress ( PG_FUNCTION_ARGS  )

Definition at line 147 of file hstore_gist.c.

148 {
149  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
150  int siglen = GET_SIGLEN();
151  GISTENTRY *retval = entry;
152 
153  if (entry->leafkey)
154  {
155  GISTTYPE *res = ghstore_alloc(false, siglen, NULL);
156  HStore *val = DatumGetHStoreP(entry->key);
157  HEntry *hsent = ARRPTR(val);
158  char *ptr = STRPTR(val);
159  int count = HS_COUNT(val);
160  int i;
161 
162  for (i = 0; i < count; ++i)
163  {
164  int h;
165 
166  h = crc32_sz((char *) HSTORE_KEY(hsent, ptr, i),
167  HSTORE_KEYLEN(hsent, i));
168  HASH(GETSIGN(res), h, siglen);
169  if (!HSTORE_VALISNULL(hsent, i))
170  {
171  h = crc32_sz((char *) HSTORE_VAL(hsent, ptr, i),
172  HSTORE_VALLEN(hsent, i));
173  HASH(GETSIGN(res), h, siglen);
174  }
175  }
176 
177  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
179  entry->rel, entry->page,
180  entry->offset,
181  false);
182  }
183  else if (!ISALLTRUE(DatumGetPointer(entry->key)))
184  {
185  int32 i;
186  GISTTYPE *res;
188 
189  LOOPBYTE(siglen)
190  {
191  if ((sign[i] & 0xff) != 0xff)
192  PG_RETURN_POINTER(retval);
193  }
194 
195  res = ghstore_alloc(true, siglen, NULL);
196 
197  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
199  entry->rel, entry->page,
200  entry->offset,
201  false);
202  }
203 
204  PG_RETURN_POINTER(retval);
205 }
signed int int32
Definition: c.h:483
#define ARRPTR(x)
Definition: cube.c:25
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:242
#define DatumGetHStoreP(d)
Definition: hstore.h:152
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
#define STRPTR(x)
Definition: hstore.h:76
#define LOOPBYTE(siglen)
Definition: hstore_gist.c:32
static pg_crc32 crc32_sz(char *buf, int size)
Definition: hstore_gist.c:80
#define ISALLTRUE(x)
Definition: hstore_gist.c:56
static GISTTYPE * ghstore_alloc(bool allistrue, int siglen, BITVECP sign)
Definition: hstore_gist.c:117
char * BITVECP
Definition: hstore_gist.c:30
#define GET_SIGLEN()
Definition: hstore_gist.c:25
#define HASH(sign, val, siglen)
Definition: hstore_gist.c:45
int i
Definition: isn.c:73
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
OffsetNumber offset
Definition: gist.h:161
Datum key
Definition: gist.h:158
Page page
Definition: gist.h:160
Relation rel
Definition: gist.h:159
bool leafkey
Definition: gist.h:162
Definition: hstore.h:19
Definition: hstore.h:45

References ARRPTR, crc32_sz(), DatumGetHStoreP, DatumGetPointer(), GET_SIGLEN, GETSIGN, ghstore_alloc(), 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(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum(), GISTENTRY::rel, res, sign, STRPTR, and val.

◆ ghstore_consistent()

Datum ghstore_consistent ( PG_FUNCTION_ARGS  )

Definition at line 507 of file hstore_gist.c.

508 {
509  GISTTYPE *entry = (GISTTYPE *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
511 
512  /* Oid subtype = PG_GETARG_OID(3); */
513  bool *recheck = (bool *) PG_GETARG_POINTER(4);
514  int siglen = GET_SIGLEN();
515  bool res = true;
516  BITVECP sign;
517 
518  /* All cases served by this function are inexact */
519  *recheck = true;
520 
521  if (ISALLTRUE(entry))
522  PG_RETURN_BOOL(true);
523 
524  sign = GETSIGN(entry);
525 
526  if (strategy == HStoreContainsStrategyNumber ||
528  {
529  HStore *query = PG_GETARG_HSTORE_P(1);
530  HEntry *qe = ARRPTR(query);
531  char *qv = STRPTR(query);
532  int count = HS_COUNT(query);
533  int i;
534 
535  for (i = 0; res && i < count; ++i)
536  {
537  int crc = crc32_sz((char *) HSTORE_KEY(qe, qv, i),
538  HSTORE_KEYLEN(qe, i));
539 
540  if (GETBIT(sign, HASHVAL(crc, siglen)))
541  {
542  if (!HSTORE_VALISNULL(qe, i))
543  {
544  crc = crc32_sz((char *) HSTORE_VAL(qe, qv, i),
545  HSTORE_VALLEN(qe, i));
546  if (!GETBIT(sign, HASHVAL(crc, siglen)))
547  res = false;
548  }
549  }
550  else
551  res = false;
552  }
553  }
554  else if (strategy == HStoreExistsStrategyNumber)
555  {
556  text *query = PG_GETARG_TEXT_PP(1);
557  int crc = crc32_sz(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query));
558 
559  res = (GETBIT(sign, HASHVAL(crc, siglen))) ? true : false;
560  }
561  else if (strategy == HStoreExistsAllStrategyNumber)
562  {
563  ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
564  Datum *key_datums;
565  bool *key_nulls;
566  int key_count;
567  int i;
568 
569  deconstruct_array_builtin(query, TEXTOID, &key_datums, &key_nulls, &key_count);
570 
571  for (i = 0; res && i < key_count; ++i)
572  {
573  int crc;
574 
575  if (key_nulls[i])
576  continue;
577  crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
578  if (!(GETBIT(sign, HASHVAL(crc, siglen))))
579  res = false;
580  }
581  }
582  else if (strategy == HStoreExistsAnyStrategyNumber)
583  {
584  ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
585  Datum *key_datums;
586  bool *key_nulls;
587  int key_count;
588  int i;
589 
590  deconstruct_array_builtin(query, TEXTOID, &key_datums, &key_nulls, &key_count);
591 
592  res = false;
593 
594  for (i = 0; !res && i < key_count; ++i)
595  {
596  int crc;
597 
598  if (key_nulls[i])
599  continue;
600  crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
601  if (GETBIT(sign, HASHVAL(crc, siglen)))
602  res = true;
603  }
604  }
605  else
606  elog(ERROR, "Unsupported strategy number: %d", strategy);
607 
609 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3644
#define VARHDRSZ
Definition: c.h:681
#define ERROR
Definition: elog.h:39
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define HStoreExistsAllStrategyNumber
Definition: hstore.h:183
#define HStoreExistsStrategyNumber
Definition: hstore.h:181
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
#define HStoreOldContainsStrategyNumber
Definition: hstore.h:184
#define HStoreExistsAnyStrategyNumber
Definition: hstore.h:182
#define HStoreContainsStrategyNumber
Definition: hstore.h:180
#define HASHVAL(val, siglen)
Definition: hstore_gist.c:44
#define GETBIT(x, i)
Definition: hstore_gist.c:43
uintptr_t Datum
Definition: postgres.h:64
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: c.h:676
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE(PTR)
Definition: varatt.h:279
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References ARRPTR, crc, crc32_sz(), DatumGetPointer(), deconstruct_array_builtin(), elog(), ERROR, GET_SIGLEN, 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, res, sign, STRPTR, VARDATA, VARDATA_ANY, VARHDRSZ, VARSIZE, and VARSIZE_ANY_EXHDR.

◆ ghstore_decompress()

Datum ghstore_decompress ( PG_FUNCTION_ARGS  )

Definition at line 212 of file hstore_gist.c.

213 {
215 }

References PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ ghstore_in()

Datum ghstore_in ( PG_FUNCTION_ARGS  )

Definition at line 97 of file hstore_gist.c.

98 {
99  ereport(ERROR,
100  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
101  errmsg("cannot accept a value of type %s", "ghstore")));
102 
103  PG_RETURN_VOID(); /* keep compiler quiet */
104 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_RETURN_VOID()
Definition: fmgr.h:349

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

◆ ghstore_options()

Datum ghstore_options ( PG_FUNCTION_ARGS  )

Definition at line 612 of file hstore_gist.c.

613 {
615 
616  init_local_reloptions(relopts, sizeof(GistHstoreOptions));
617  add_local_int_reloption(relopts, "siglen",
618  "signature length in bytes",
620  offsetof(GistHstoreOptions, siglen));
621 
622  PG_RETURN_VOID();
623 }
#define SIGLEN_MAX
Definition: hstore_gist.c:23
void init_local_reloptions(local_relopts *relopts, Size relopt_struct_size)
Definition: reloptions.c:736
void add_local_int_reloption(local_relopts *relopts, const char *name, const char *desc, int default_val, int min_val, int max_val, int offset)
Definition: reloptions.c:920

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

◆ ghstore_out()

Datum ghstore_out ( PG_FUNCTION_ARGS  )

Definition at line 107 of file hstore_gist.c.

108 {
109  ereport(ERROR,
110  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
111  errmsg("cannot display a value of type %s", "ghstore")));
112 
113  PG_RETURN_VOID(); /* keep compiler quiet */
114 }

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

◆ ghstore_penalty()

Datum ghstore_penalty ( PG_FUNCTION_ARGS  )

Definition at line 336 of file hstore_gist.c.

337 {
338  GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
339  GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
340  float *penalty = (float *) PG_GETARG_POINTER(2);
341  int siglen = GET_SIGLEN();
342  GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
343  GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
344 
345  *penalty = hemdist(origval, newval, siglen);
346  PG_RETURN_POINTER(penalty);
347 }
#define newval
static int hemdist(GISTTYPE *a, GISTTYPE *b, int siglen)
Definition: hstore_gist.c:280

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

◆ ghstore_picksplit()

Datum ghstore_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 364 of file hstore_gist.c.

365 {
367  OffsetNumber maxoff = entryvec->n - 2;
368 
370  int siglen = GET_SIGLEN();
371  OffsetNumber k,
372  j;
373  GISTTYPE *datum_l,
374  *datum_r;
375  BITVECP union_l,
376  union_r;
377  int32 size_alpha,
378  size_beta;
379  int32 size_waste,
380  waste = -1;
381  int32 nbytes;
382  OffsetNumber seed_1 = 0,
383  seed_2 = 0;
384  OffsetNumber *left,
385  *right;
386  BITVECP ptr;
387  int i;
388  SPLITCOST *costvector;
389  GISTTYPE *_k,
390  *_j;
391 
392  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
393  v->spl_left = (OffsetNumber *) palloc(nbytes);
394  v->spl_right = (OffsetNumber *) palloc(nbytes);
395 
396  for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
397  {
398  _k = GETENTRY(entryvec, k);
399  for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
400  {
401  size_waste = hemdist(_k, GETENTRY(entryvec, j), siglen);
402  if (size_waste > waste)
403  {
404  waste = size_waste;
405  seed_1 = k;
406  seed_2 = j;
407  }
408  }
409  }
410 
411  left = v->spl_left;
412  v->spl_nleft = 0;
413  right = v->spl_right;
414  v->spl_nright = 0;
415 
416  if (seed_1 == 0 || seed_2 == 0)
417  {
418  seed_1 = 1;
419  seed_2 = 2;
420  }
421 
422  /* form initial .. */
423  datum_l = ghstore_alloc(ISALLTRUE(GETENTRY(entryvec, seed_1)), siglen,
424  GETSIGN(GETENTRY(entryvec, seed_1)));
425  datum_r = ghstore_alloc(ISALLTRUE(GETENTRY(entryvec, seed_2)), siglen,
426  GETSIGN(GETENTRY(entryvec, seed_2)));
427 
428  maxoff = OffsetNumberNext(maxoff);
429  /* sort before ... */
430  costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
431  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
432  {
433  costvector[j - 1].pos = j;
434  _j = GETENTRY(entryvec, j);
435  size_alpha = hemdist(datum_l, _j, siglen);
436  size_beta = hemdist(datum_r, _j, siglen);
437  costvector[j - 1].cost = abs(size_alpha - size_beta);
438  }
439  qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
440 
441  union_l = GETSIGN(datum_l);
442  union_r = GETSIGN(datum_r);
443 
444  for (k = 0; k < maxoff; k++)
445  {
446  j = costvector[k].pos;
447  if (j == seed_1)
448  {
449  *left++ = j;
450  v->spl_nleft++;
451  continue;
452  }
453  else if (j == seed_2)
454  {
455  *right++ = j;
456  v->spl_nright++;
457  continue;
458  }
459  _j = GETENTRY(entryvec, j);
460  size_alpha = hemdist(datum_l, _j, siglen);
461  size_beta = hemdist(datum_r, _j, siglen);
462 
463  if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.0001))
464  {
465  if (ISALLTRUE(datum_l) || ISALLTRUE(_j))
466  {
467  if (!ISALLTRUE(datum_l))
468  memset(union_l, 0xff, siglen);
469  }
470  else
471  {
472  ptr = GETSIGN(_j);
473  LOOPBYTE(siglen)
474  union_l[i] |= ptr[i];
475  }
476  *left++ = j;
477  v->spl_nleft++;
478  }
479  else
480  {
481  if (ISALLTRUE(datum_r) || ISALLTRUE(_j))
482  {
483  if (!ISALLTRUE(datum_r))
484  memset(union_r, 0xff, siglen);
485  }
486  else
487  {
488  ptr = GETSIGN(_j);
489  LOOPBYTE(siglen)
490  union_r[i] |= ptr[i];
491  }
492  *right++ = j;
493  v->spl_nright++;
494  }
495  }
496 
497  *right = *left = FirstOffsetNumber;
498 
499  v->spl_ldatum = PointerGetDatum(datum_l);
500  v->spl_rdatum = PointerGetDatum(datum_r);
501 
503 }
#define WISH_F(a, b, c)
Definition: hstore_gist.c:76
#define GETENTRY(vec, pos)
Definition: hstore_gist.c:74
static int comparecost(const void *a, const void *b)
Definition: hstore_gist.c:357
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
#define qsort(a, b, c, d)
Definition: port.h:445
int spl_nleft
Definition: gist.h:141
OffsetNumber * spl_right
Definition: gist.h:145
Datum spl_ldatum
Definition: gist.h:142
Datum spl_rdatum
Definition: gist.h:147
int spl_nright
Definition: gist.h:146
OffsetNumber * spl_left
Definition: gist.h:140
int32 n
Definition: gist.h:233
int32 cost
Definition: hstore_gist.c:353
OffsetNumber pos
Definition: hstore_gist.c:352

References comparecost(), SPLITCOST::cost, FirstOffsetNumber, GET_SIGLEN, GETENTRY, GETSIGN, ghstore_alloc(), hemdist(), i, ISALLTRUE, j, LOOPBYTE, GistEntryVector::n, OffsetNumberNext, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum(), SPLITCOST::pos, qsort, 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.

◆ ghstore_same()

Datum ghstore_same ( PG_FUNCTION_ARGS  )

Definition at line 218 of file hstore_gist.c.

219 {
222  bool *result = (bool *) PG_GETARG_POINTER(2);
223  int siglen = GET_SIGLEN();
224 
225 
226  if (ISALLTRUE(a) && ISALLTRUE(b))
227  *result = true;
228  else if (ISALLTRUE(a))
229  *result = false;
230  else if (ISALLTRUE(b))
231  *result = false;
232  else
233  {
234  int32 i;
235  BITVECP sa = GETSIGN(a),
236  sb = GETSIGN(b);
237 
238  *result = true;
239  LOOPBYTE(siglen)
240  {
241  if (sa[i] != sb[i])
242  {
243  *result = false;
244  break;
245  }
246  }
247  }
248  PG_RETURN_POINTER(result);
249 }

References a, b, GET_SIGLEN, GETSIGN, i, ISALLTRUE, LOOPBYTE, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ ghstore_union()

Datum ghstore_union ( PG_FUNCTION_ARGS  )

Definition at line 309 of file hstore_gist.c.

310 {
312  int32 len = entryvec->n;
313 
314  int *size = (int *) PG_GETARG_POINTER(1);
315  int siglen = GET_SIGLEN();
316  int32 i;
317  GISTTYPE *result = ghstore_alloc(false, siglen, NULL);
318  BITVECP base = GETSIGN(result);
319 
320  for (i = 0; i < len; i++)
321  {
322  if (unionkey(base, GETENTRY(entryvec, i), siglen))
323  {
324  result->flag |= ALLISTRUE;
325  SET_VARSIZE(result, CALCGTSIZE(ALLISTRUE, siglen));
326  break;
327  }
328  }
329 
330  *size = VARSIZE(result);
331 
332  PG_RETURN_POINTER(result);
333 }
static int32 unionkey(BITVECP sbase, GISTTYPE *add, int siglen)
Definition: hstore_gist.c:296
const void size_t len
int32 flag
Definition: hstore_gist.c:50

References ALLISTRUE, CALCGTSIZE, GISTTYPE::flag, GET_SIGLEN, GETENTRY, GETSIGN, ghstore_alloc(), i, len, GistEntryVector::n, PG_GETARG_POINTER, PG_RETURN_POINTER, SET_VARSIZE, unionkey(), and VARSIZE.

◆ hemdist()

static int hemdist ( GISTTYPE a,
GISTTYPE b,
int  siglen 
)
static

Definition at line 280 of file hstore_gist.c.

281 {
282  if (ISALLTRUE(a))
283  {
284  if (ISALLTRUE(b))
285  return 0;
286  else
287  return SIGLENBIT(siglen) - sizebitvec(GETSIGN(b), siglen);
288  }
289  else if (ISALLTRUE(b))
290  return SIGLENBIT(siglen) - sizebitvec(GETSIGN(a), siglen);
291 
292  return hemdistsign(GETSIGN(a), GETSIGN(b), siglen);
293 }
static int32 sizebitvec(BITVECP sign, int siglen)
Definition: hstore_gist.c:252
static int hemdistsign(BITVECP a, BITVECP b, int siglen)
Definition: hstore_gist.c:266
#define SIGLENBIT(siglen)
Definition: hstore_gist.c:24

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

Referenced by ghstore_penalty(), and ghstore_picksplit().

◆ hemdistsign()

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

Definition at line 266 of file hstore_gist.c.

267 {
268  int i,
269  dist = 0;
270 
271  LOOPBIT(siglen)
272  {
273  if (GETBIT(a, i) != GETBIT(b, i))
274  dist++;
275  }
276  return dist;
277 }
#define LOOPBIT(siglen)
Definition: hstore_gist.c:35

References a, b, GETBIT, i, and LOOPBIT.

Referenced by hemdist().

◆ PG_FUNCTION_INFO_V1() [1/10]

PG_FUNCTION_INFO_V1 ( ghstore_compress  )

◆ PG_FUNCTION_INFO_V1() [2/10]

PG_FUNCTION_INFO_V1 ( ghstore_consistent  )

◆ PG_FUNCTION_INFO_V1() [3/10]

PG_FUNCTION_INFO_V1 ( ghstore_decompress  )

◆ PG_FUNCTION_INFO_V1() [4/10]

PG_FUNCTION_INFO_V1 ( ghstore_in  )

◆ PG_FUNCTION_INFO_V1() [5/10]

PG_FUNCTION_INFO_V1 ( ghstore_options  )

◆ PG_FUNCTION_INFO_V1() [6/10]

PG_FUNCTION_INFO_V1 ( ghstore_out  )

◆ PG_FUNCTION_INFO_V1() [7/10]

PG_FUNCTION_INFO_V1 ( ghstore_penalty  )

◆ PG_FUNCTION_INFO_V1() [8/10]

PG_FUNCTION_INFO_V1 ( ghstore_picksplit  )

◆ PG_FUNCTION_INFO_V1() [9/10]

PG_FUNCTION_INFO_V1 ( ghstore_same  )

◆ PG_FUNCTION_INFO_V1() [10/10]

PG_FUNCTION_INFO_V1 ( ghstore_union  )

◆ sizebitvec()

static int32 sizebitvec ( BITVECP  sign,
int  siglen 
)
static

Definition at line 252 of file hstore_gist.c.

253 {
254  int32 size = 0,
255  i;
256 
257  LOOPBYTE(siglen)
258  {
259  size += SUMBIT(sign);
260  sign = (BITVECP) (((char *) sign) + 1);
261  }
262  return size;
263 }
#define SUMBIT(val)
Definition: hstore_gist.c:63

References i, LOOPBYTE, sign, and SUMBIT.

Referenced by hemdist().

◆ unionkey()

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

Definition at line 296 of file hstore_gist.c.

297 {
298  int32 i;
299  BITVECP sadd = GETSIGN(add);
300 
301  if (ISALLTRUE(add))
302  return 1;
303  LOOPBYTE(siglen)
304  sbase[i] |= sadd[i];
305  return 0;
306 }

References GETSIGN, i, ISALLTRUE, and LOOPBYTE.

Referenced by ghstore_union().