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 "common/int.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 (const 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 55 of file hstore_gist.c.

◆ BITBYTE

#define BITBYTE   8

Definition at line 22 of file hstore_gist.c.

◆ CALCGTSIZE

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

Definition at line 60 of file hstore_gist.c.

◆ CLRBIT

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

Definition at line 42 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:23

Definition at line 26 of file hstore_gist.c.

◆ GETBIT

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

Definition at line 44 of file hstore_gist.c.

◆ GETBITBYTE

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

Definition at line 41 of file hstore_gist.c.

◆ GETBYTE

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

Definition at line 40 of file hstore_gist.c.

◆ GETENTRY

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

Definition at line 75 of file hstore_gist.c.

◆ GETSIGN

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

Definition at line 62 of file hstore_gist.c.

◆ GTHDRSIZE

#define GTHDRSIZE   (VARHDRSZ + sizeof(int32))

Definition at line 59 of file hstore_gist.c.

◆ HASH

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

Definition at line 46 of file hstore_gist.c.

◆ HASHVAL

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

Definition at line 45 of file hstore_gist.c.

◆ ISALLTRUE

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

Definition at line 57 of file hstore_gist.c.

◆ LOOPBIT

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

Definition at line 36 of file hstore_gist.c.

◆ LOOPBYTE

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

Definition at line 33 of file hstore_gist.c.

◆ SETBIT

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

Definition at line 43 of file hstore_gist.c.

◆ SIGLEN_DEFAULT

#define SIGLEN_DEFAULT   (sizeof(int32) * 4)

Definition at line 23 of file hstore_gist.c.

◆ SIGLEN_MAX

#define SIGLEN_MAX   GISTMaxIndexKeySize

Definition at line 24 of file hstore_gist.c.

◆ SIGLENBIT

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

Definition at line 25 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:689

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

Typedef Documentation

◆ BITVECP

typedef char* BITVECP

Definition at line 31 of file hstore_gist.c.

Function Documentation

◆ comparecost()

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

Definition at line 358 of file hstore_gist.c.

359{
360 return pg_cmp_s32(((const SPLITCOST *) a)->cost,
361 ((const SPLITCOST *) b)->cost);
362}
static int pg_cmp_s32(int32 a, int32 b)
Definition: int.h:646
int b
Definition: isn.c:69
int a
Definition: isn.c:68

References a, b, and pg_cmp_s32().

Referenced by ghstore_picksplit().

◆ crc32_sz()

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

Definition at line 81 of file hstore_gist.c.

82{
84
88
89 return crc;
90}
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:72
static pg_noinline void Size size
Definition: slab.c:607

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

Referenced by ghstore_compress(), and ghstore_consistent().

◆ ghstore_alloc()

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

Definition at line 118 of file hstore_gist.c.

119{
120 int flag = allistrue ? ALLISTRUE : 0;
121 int size = CALCGTSIZE(flag, siglen);
123
125 res->flag = flag;
126
127 if (!allistrue)
128 {
129 if (sign)
130 memcpy(GETSIGN(res), sign, siglen);
131 else
132 memset(GETSIGN(res), 0, siglen);
133 }
134
135 return res;
136}
#define ALLISTRUE
Definition: hstore_gist.c:55
#define CALCGTSIZE(flag, siglen)
Definition: hstore_gist.c:60
#define GETSIGN(x)
Definition: hstore_gist.c:62
char sign
Definition: informix.c:693
void * palloc(Size size)
Definition: mcxt.c:1317
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, sign, and size.

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

◆ ghstore_compress()

Datum ghstore_compress ( PG_FUNCTION_ARGS  )

Definition at line 148 of file hstore_gist.c.

149{
150 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
151 int siglen = GET_SIGLEN();
152 GISTENTRY *retval = entry;
153
154 if (entry->leafkey)
155 {
156 GISTTYPE *res = ghstore_alloc(false, siglen, NULL);
157 HStore *val = DatumGetHStoreP(entry->key);
158 HEntry *hsent = ARRPTR(val);
159 char *ptr = STRPTR(val);
160 int count = HS_COUNT(val);
161 int i;
162
163 for (i = 0; i < count; ++i)
164 {
165 int h;
166
167 h = crc32_sz((char *) HSTORE_KEY(hsent, ptr, i),
168 HSTORE_KEYLEN(hsent, i));
169 HASH(GETSIGN(res), h, siglen);
170 if (!HSTORE_VALISNULL(hsent, i))
171 {
172 h = crc32_sz((char *) HSTORE_VAL(hsent, ptr, i),
173 HSTORE_VALLEN(hsent, i));
174 HASH(GETSIGN(res), h, siglen);
175 }
176 }
177
178 retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
180 entry->rel, entry->page,
181 entry->offset,
182 false);
183 }
184 else if (!ISALLTRUE(DatumGetPointer(entry->key)))
185 {
186 int32 i;
187 GISTTYPE *res;
189
190 LOOPBYTE(siglen)
191 {
192 if ((sign[i] & 0xff) != 0xff)
193 PG_RETURN_POINTER(retval);
194 }
195
196 res = ghstore_alloc(true, siglen, NULL);
197
198 retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
200 entry->rel, entry->page,
201 entry->offset,
202 false);
203 }
204
205 PG_RETURN_POINTER(retval);
206}
int32_t int32
Definition: c.h:484
#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:245
#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:33
#define ISALLTRUE(x)
Definition: hstore_gist.c:57
char * BITVECP
Definition: hstore_gist.c:31
#define GET_SIGLEN()
Definition: hstore_gist.c:26
static pg_crc32 crc32_sz(const char *buf, int size)
Definition: hstore_gist.c:81
static GISTTYPE * ghstore_alloc(bool allistrue, int siglen, BITVECP sign)
Definition: hstore_gist.c:118
#define HASH(sign, val, siglen)
Definition: hstore_gist.c:46
int i
Definition: isn.c:72
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
OffsetNumber offset
Definition: gist.h:164
Datum key
Definition: gist.h:161
Page page
Definition: gist.h:163
Relation rel
Definition: gist.h:162
bool leafkey
Definition: gist.h:165
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 509 of file hstore_gist.c.

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

References PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ ghstore_in()

Datum ghstore_in ( PG_FUNCTION_ARGS  )

Definition at line 98 of file hstore_gist.c.

99{
101 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
102 errmsg("cannot accept a value of type %s", "ghstore")));
103
104 PG_RETURN_VOID(); /* keep compiler quiet */
105}
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#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 614 of file hstore_gist.c.

615{
617
619 add_local_int_reloption(relopts, "siglen",
620 "signature length in bytes",
622 offsetof(GistHstoreOptions, siglen));
623
625}
#define SIGLEN_MAX
Definition: hstore_gist.c:24
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

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 108 of file hstore_gist.c.

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

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

◆ ghstore_penalty()

Datum ghstore_penalty ( PG_FUNCTION_ARGS  )

Definition at line 337 of file hstore_gist.c.

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

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 366 of file hstore_gist.c.

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

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 219 of file hstore_gist.c.

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

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 310 of file hstore_gist.c.

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

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

◆ hemdist()

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

Definition at line 281 of file hstore_gist.c.

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

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 267 of file hstore_gist.c.

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

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 253 of file hstore_gist.c.

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

References i, LOOPBYTE, sign, size, and SUMBIT.

Referenced by hemdist().

◆ unionkey()

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

Definition at line 297 of file hstore_gist.c.

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

References GETSIGN, i, ISALLTRUE, and LOOPBYTE.

Referenced by ghstore_union().