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

◆ BITBYTE

#define BITBYTE   8

Definition at line 21 of file hstore_gist.c.

◆ CALCGTSIZE

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

◆ CLRBIT

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

Definition at line 41 of file hstore_gist.c.

◆ GET_SIGLEN

◆ GETBIT

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

Definition at line 43 of file hstore_gist.c.

Referenced by ghstore_consistent(), and hemdistsign().

◆ GETBITBYTE

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

Definition at line 40 of file hstore_gist.c.

◆ GETBYTE

◆ GETENTRY

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

Definition at line 74 of file hstore_gist.c.

Referenced by ghstore_picksplit(), and ghstore_union().

◆ GETSIGN

◆ 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))

◆ HASHVAL

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

◆ ISALLTRUE

◆ LOOPBIT

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

Definition at line 35 of file hstore_gist.c.

Referenced by hemdistsign().

◆ LOOPBYTE

◆ 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.

Referenced by g_intbig_options(), ghstore_options(), gtrgm_options(), and ltree_gist_options().

◆ SIGLEN_MAX

#define SIGLEN_MAX   GISTMaxIndexKeySize

Definition at line 23 of file hstore_gist.c.

Referenced by g_intbig_options(), ghstore_options(), gtrgm_options(), and ltree_gist_options().

◆ SIGLENBIT

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

Definition at line 24 of file hstore_gist.c.

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

◆ 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.

Referenced by sizebitvec().

◆ 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.

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

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

Referenced by ghstore_picksplit().

352 {
353  return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
354 }

◆ crc32_sz()

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

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

81 {
82  pg_crc32 crc;
83 
85  COMP_TRADITIONAL_CRC32(crc, buf, size);
87 
88  return crc;
89 }
#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_alloc()

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

Definition at line 111 of file hstore_gist.c.

References ALLISTRUE, CALCGTSIZE, flag(), GISTTYPE::flag, GETSIGN, ghstore_compress(), ghstore_consistent(), ghstore_decompress(), ghstore_options(), ghstore_penalty(), ghstore_picksplit(), ghstore_same(), ghstore_union(), palloc(), PG_FUNCTION_INFO_V1(), and SET_VARSIZE.

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

112 {
113  int flag = allistrue ? ALLISTRUE : 0;
114  int size = CALCGTSIZE(flag, siglen);
115  GISTTYPE *res = palloc(size);
116 
117  SET_VARSIZE(res, size);
118  res->flag = flag;
119 
120  if (!allistrue)
121  {
122  if (sign)
123  memcpy(GETSIGN(res), sign, siglen);
124  else
125  memset(GETSIGN(res), 0, siglen);
126  }
127 
128  return res;
129 }
#define GETSIGN(x)
Definition: hstore_gist.c:61
#define ALLISTRUE
Definition: hstore_gist.c:54
int32 flag
Definition: hstore_gist.c:50
#define CALCGTSIZE(flag, siglen)
Definition: hstore_gist.c:59
char sign
Definition: informix.c:668
char * flag(int b)
Definition: test-ctype.c:33
void * palloc(Size size)
Definition: mcxt.c:950
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ ghstore_compress()

Datum ghstore_compress ( PG_FUNCTION_ARGS  )

Definition at line 141 of file hstore_gist.c.

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, sign, STRPTR, and val.

Referenced by ghstore_alloc().

142 {
143  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
144  int siglen = GET_SIGLEN();
145  GISTENTRY *retval = entry;
146 
147  if (entry->leafkey)
148  {
149  GISTTYPE *res = ghstore_alloc(false, siglen, NULL);
150  HStore *val = DatumGetHStoreP(entry->key);
151  HEntry *hsent = ARRPTR(val);
152  char *ptr = STRPTR(val);
153  int count = HS_COUNT(val);
154  int i;
155 
156  for (i = 0; i < count; ++i)
157  {
158  int h;
159 
160  h = crc32_sz((char *) HSTORE_KEY(hsent, ptr, i),
161  HSTORE_KEYLEN(hsent, i));
162  HASH(GETSIGN(res), h, siglen);
163  if (!HSTORE_VALISNULL(hsent, i))
164  {
165  h = crc32_sz((char *) HSTORE_VAL(hsent, ptr, i),
166  HSTORE_VALLEN(hsent, i));
167  HASH(GETSIGN(res), h, siglen);
168  }
169  }
170 
171  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
172  gistentryinit(*retval, PointerGetDatum(res),
173  entry->rel, entry->page,
174  entry->offset,
175  false);
176  }
177  else if (!ISALLTRUE(DatumGetPointer(entry->key)))
178  {
179  int32 i;
180  GISTTYPE *res;
182 
183  LOOPBYTE(siglen)
184  {
185  if ((sign[i] & 0xff) != 0xff)
186  PG_RETURN_POINTER(retval);
187  }
188 
189  res = ghstore_alloc(true, siglen, NULL);
190 
191  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
192  gistentryinit(*retval, PointerGetDatum(res),
193  entry->rel, entry->page,
194  entry->offset,
195  false);
196  }
197 
198  PG_RETURN_POINTER(retval);
199 }
Relation rel
Definition: gist.h:153
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
Definition: hstore.h:44
#define DatumGetHStoreP(d)
Definition: hstore.h:152
#define LOOPBYTE(siglen)
Definition: hstore_gist.c:32
#define GETSIGN(x)
Definition: hstore_gist.c:61
#define PointerGetDatum(X)
Definition: postgres.h:556
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
signed int int32
Definition: c.h:417
Page page
Definition: gist.h:154
#define HASH(sign, val, siglen)
Definition: hstore_gist.c:45
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
static GISTTYPE * ghstore_alloc(bool allistrue, int siglen, BITVECP sign)
Definition: hstore_gist.c:111
#define GET_SIGLEN()
Definition: hstore_gist.c:25
char sign
Definition: informix.c:668
Datum key
Definition: gist.h:152
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
bool leafkey
Definition: gist.h:156
static pg_crc32 crc32_sz(char *buf, int size)
Definition: hstore_gist.c:80
#define ISALLTRUE(x)
Definition: hstore_gist.c:56
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:236
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:950
#define STRPTR(x)
Definition: hstore.h:76
int i
char * BITVECP
Definition: hstore_gist.c:30
#define ARRPTR(x)
Definition: cube.c:24
OffsetNumber offset
Definition: gist.h:155
long val
Definition: informix.c:664

◆ ghstore_consistent()

Datum ghstore_consistent ( PG_FUNCTION_ARGS  )

Definition at line 501 of file hstore_gist.c.

References ARRPTR, crc32_sz(), DatumGetPointer, deconstruct_array(), 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, sign, STRPTR, VARDATA, VARDATA_ANY, VARHDRSZ, VARSIZE, and VARSIZE_ANY_EXHDR.

Referenced by ghstore_alloc().

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

◆ ghstore_decompress()

Datum ghstore_decompress ( PG_FUNCTION_ARGS  )

Definition at line 206 of file hstore_gist.c.

References PG_GETARG_POINTER, and PG_RETURN_POINTER.

Referenced by ghstore_alloc().

207 {
209 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276

◆ ghstore_in()

Datum ghstore_in ( PG_FUNCTION_ARGS  )

Definition at line 97 of file hstore_gist.c.

References elog, ERROR, and PG_RETURN_DATUM.

Referenced by crc32_sz().

98 {
99  elog(ERROR, "Not implemented");
100  PG_RETURN_DATUM(0);
101 }
#define ERROR
Definition: elog.h:43
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define elog(elevel,...)
Definition: elog.h:228

◆ ghstore_options()

Datum ghstore_options ( PG_FUNCTION_ARGS  )

Definition at line 610 of file hstore_gist.c.

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

Referenced by ghstore_alloc().

611 {
613 
614  init_local_reloptions(relopts, sizeof(GistHstoreOptions));
615  add_local_int_reloption(relopts, "siglen",
616  "signature length in bytes",
618  offsetof(GistHstoreOptions, siglen));
619 
620  PG_RETURN_VOID();
621 }
void init_local_reloptions(local_relopts *opts, Size relopt_struct_size)
Definition: reloptions.c:710
#define SIGLEN_MAX
Definition: hstore_gist.c:23
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
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:894
#define SIGLEN_DEFAULT
Definition: hstore_gist.c:22
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define offsetof(type, field)
Definition: c.h:723

◆ ghstore_out()

Datum ghstore_out ( PG_FUNCTION_ARGS  )

Definition at line 104 of file hstore_gist.c.

References elog, ERROR, and PG_RETURN_DATUM.

Referenced by crc32_sz().

105 {
106  elog(ERROR, "Not implemented");
107  PG_RETURN_DATUM(0);
108 }
#define ERROR
Definition: elog.h:43
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define elog(elevel,...)
Definition: elog.h:228

◆ ghstore_penalty()

Datum ghstore_penalty ( PG_FUNCTION_ARGS  )

Definition at line 330 of file hstore_gist.c.

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

Referenced by ghstore_alloc().

331 {
332  GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
333  GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
334  float *penalty = (float *) PG_GETARG_POINTER(2);
335  int siglen = GET_SIGLEN();
336  GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
337  GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
338 
339  *penalty = hemdist(origval, newval, siglen);
340  PG_RETURN_POINTER(penalty);
341 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define GET_SIGLEN()
Definition: hstore_gist.c:25
Datum key
Definition: gist.h:152
#define newval
#define DatumGetPointer(X)
Definition: postgres.h:549
static int hemdist(GISTTYPE *a, GISTTYPE *b, int siglen)
Definition: hstore_gist.c:274

◆ ghstore_picksplit()

Datum ghstore_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 358 of file hstore_gist.c.

References comparecost(), SPLITCOST::cost, FirstOffsetNumber, GET_SIGLEN, GETENTRY, GETSIGN, ghstore_alloc(), hemdist(), i, ISALLTRUE, LOOPBYTE, MemSet, 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.

Referenced by ghstore_alloc().

359 {
361  OffsetNumber maxoff = entryvec->n - 2;
362 
364  int siglen = GET_SIGLEN();
365  OffsetNumber k,
366  j;
367  GISTTYPE *datum_l,
368  *datum_r;
369  BITVECP union_l,
370  union_r;
371  int32 size_alpha,
372  size_beta;
373  int32 size_waste,
374  waste = -1;
375  int32 nbytes;
376  OffsetNumber seed_1 = 0,
377  seed_2 = 0;
378  OffsetNumber *left,
379  *right;
380  BITVECP ptr;
381  int i;
382  SPLITCOST *costvector;
383  GISTTYPE *_k,
384  *_j;
385 
386  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
387  v->spl_left = (OffsetNumber *) palloc(nbytes);
388  v->spl_right = (OffsetNumber *) palloc(nbytes);
389 
390  for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
391  {
392  _k = GETENTRY(entryvec, k);
393  for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
394  {
395  size_waste = hemdist(_k, GETENTRY(entryvec, j), siglen);
396  if (size_waste > waste)
397  {
398  waste = size_waste;
399  seed_1 = k;
400  seed_2 = j;
401  }
402  }
403  }
404 
405  left = v->spl_left;
406  v->spl_nleft = 0;
407  right = v->spl_right;
408  v->spl_nright = 0;
409 
410  if (seed_1 == 0 || seed_2 == 0)
411  {
412  seed_1 = 1;
413  seed_2 = 2;
414  }
415 
416  /* form initial .. */
417  datum_l = ghstore_alloc(ISALLTRUE(GETENTRY(entryvec, seed_1)), siglen,
418  GETSIGN(GETENTRY(entryvec, seed_1)));
419  datum_r = ghstore_alloc(ISALLTRUE(GETENTRY(entryvec, seed_2)), siglen,
420  GETSIGN(GETENTRY(entryvec, seed_2)));
421 
422  maxoff = OffsetNumberNext(maxoff);
423  /* sort before ... */
424  costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
425  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
426  {
427  costvector[j - 1].pos = j;
428  _j = GETENTRY(entryvec, j);
429  size_alpha = hemdist(datum_l, _j, siglen);
430  size_beta = hemdist(datum_r, _j, siglen);
431  costvector[j - 1].cost = abs(size_alpha - size_beta);
432  }
433  qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
434 
435  union_l = GETSIGN(datum_l);
436  union_r = GETSIGN(datum_r);
437 
438  for (k = 0; k < maxoff; k++)
439  {
440  j = costvector[k].pos;
441  if (j == seed_1)
442  {
443  *left++ = j;
444  v->spl_nleft++;
445  continue;
446  }
447  else if (j == seed_2)
448  {
449  *right++ = j;
450  v->spl_nright++;
451  continue;
452  }
453  _j = GETENTRY(entryvec, j);
454  size_alpha = hemdist(datum_l, _j, siglen);
455  size_beta = hemdist(datum_r, _j, siglen);
456 
457  if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.0001))
458  {
459  if (ISALLTRUE(datum_l) || ISALLTRUE(_j))
460  {
461  if (!ISALLTRUE(datum_l))
462  MemSet((void *) union_l, 0xff, siglen);
463  }
464  else
465  {
466  ptr = GETSIGN(_j);
467  LOOPBYTE(siglen)
468  union_l[i] |= ptr[i];
469  }
470  *left++ = j;
471  v->spl_nleft++;
472  }
473  else
474  {
475  if (ISALLTRUE(datum_r) || ISALLTRUE(_j))
476  {
477  if (!ISALLTRUE(datum_r))
478  MemSet((void *) union_r, 0xff, siglen);
479  }
480  else
481  {
482  ptr = GETSIGN(_j);
483  LOOPBYTE(siglen)
484  union_r[i] |= ptr[i];
485  }
486  *right++ = j;
487  v->spl_nright++;
488  }
489  }
490 
491  *right = *left = FirstOffsetNumber;
492 
493  v->spl_ldatum = PointerGetDatum(datum_l);
494  v->spl_rdatum = PointerGetDatum(datum_r);
495 
497 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
OffsetNumber pos
Definition: hstore_gist.c:346
#define LOOPBYTE(siglen)
Definition: hstore_gist.c:32
#define GETSIGN(x)
Definition: hstore_gist.c:61
#define PointerGetDatum(X)
Definition: postgres.h:556
OffsetNumber * spl_left
Definition: gist.h:134
Datum spl_rdatum
Definition: gist.h:141
int32 n
Definition: gist.h:227
#define MemSet(start, val, len)
Definition: c.h:1004
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
int spl_nleft
Definition: gist.h:135
signed int int32
Definition: c.h:417
int32 cost
Definition: hstore_gist.c:347
uint16 OffsetNumber
Definition: off.h:24
static GISTTYPE * ghstore_alloc(bool allistrue, int siglen, BITVECP sign)
Definition: hstore_gist.c:111
#define WISH_F(a, b, c)
Definition: hstore_gist.c:76
#define GET_SIGLEN()
Definition: hstore_gist.c:25
int spl_nright
Definition: gist.h:140
#define FirstOffsetNumber
Definition: off.h:27
#define GETENTRY(vec, pos)
Definition: hstore_gist.c:74
#define ISALLTRUE(x)
Definition: hstore_gist.c:56
Datum spl_ldatum
Definition: gist.h:136
static int comparecost(const void *a, const void *b)
Definition: hstore_gist.c:351
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
OffsetNumber * spl_right
Definition: gist.h:139
void * palloc(Size size)
Definition: mcxt.c:950
int i
char * BITVECP
Definition: hstore_gist.c:30
#define qsort(a, b, c, d)
Definition: port.h:497
static int hemdist(GISTTYPE *a, GISTTYPE *b, int siglen)
Definition: hstore_gist.c:274

◆ ghstore_same()

Datum ghstore_same ( PG_FUNCTION_ARGS  )

Definition at line 212 of file hstore_gist.c.

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

Referenced by ghstore_alloc().

213 {
214  GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
215  GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
216  bool *result = (bool *) PG_GETARG_POINTER(2);
217  int siglen = GET_SIGLEN();
218 
219 
220  if (ISALLTRUE(a) && ISALLTRUE(b))
221  *result = true;
222  else if (ISALLTRUE(a))
223  *result = false;
224  else if (ISALLTRUE(b))
225  *result = false;
226  else
227  {
228  int32 i;
229  BITVECP sa = GETSIGN(a),
230  sb = GETSIGN(b);
231 
232  *result = true;
233  LOOPBYTE(siglen)
234  {
235  if (sa[i] != sb[i])
236  {
237  *result = false;
238  break;
239  }
240  }
241  }
242  PG_RETURN_POINTER(result);
243 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define LOOPBYTE(siglen)
Definition: hstore_gist.c:32
#define GETSIGN(x)
Definition: hstore_gist.c:61
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
signed int int32
Definition: c.h:417
#define GET_SIGLEN()
Definition: hstore_gist.c:25
#define ISALLTRUE(x)
Definition: hstore_gist.c:56
int i
char * BITVECP
Definition: hstore_gist.c:30

◆ ghstore_union()

Datum ghstore_union ( PG_FUNCTION_ARGS  )

Definition at line 303 of file hstore_gist.c.

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

Referenced by ghstore_alloc().

304 {
306  int32 len = entryvec->n;
307 
308  int *size = (int *) PG_GETARG_POINTER(1);
309  int siglen = GET_SIGLEN();
310  int32 i;
311  GISTTYPE *result = ghstore_alloc(false, siglen, NULL);
312  BITVECP base = GETSIGN(result);
313 
314  for (i = 0; i < len; i++)
315  {
316  if (unionkey(base, GETENTRY(entryvec, i), siglen))
317  {
318  result->flag |= ALLISTRUE;
319  SET_VARSIZE(result, CALCGTSIZE(ALLISTRUE, siglen));
320  break;
321  }
322  }
323 
324  *size = VARSIZE(result);
325 
326  PG_RETURN_POINTER(result);
327 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define VARSIZE(PTR)
Definition: postgres.h:303
#define GETSIGN(x)
Definition: hstore_gist.c:61
int32 n
Definition: gist.h:227
#define ALLISTRUE
Definition: hstore_gist.c:54
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
int32 flag
Definition: hstore_gist.c:50
signed int int32
Definition: c.h:417
#define CALCGTSIZE(flag, siglen)
Definition: hstore_gist.c:59
static GISTTYPE * ghstore_alloc(bool allistrue, int siglen, BITVECP sign)
Definition: hstore_gist.c:111
#define GET_SIGLEN()
Definition: hstore_gist.c:25
#define GETENTRY(vec, pos)
Definition: hstore_gist.c:74
static int32 unionkey(BITVECP sbase, GISTTYPE *add, int siglen)
Definition: hstore_gist.c:290
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
char * BITVECP
Definition: hstore_gist.c:30

◆ hemdist()

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

Definition at line 274 of file hstore_gist.c.

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

Referenced by ghstore_penalty(), and ghstore_picksplit().

275 {
276  if (ISALLTRUE(a))
277  {
278  if (ISALLTRUE(b))
279  return 0;
280  else
281  return SIGLENBIT(siglen) - sizebitvec(GETSIGN(b), siglen);
282  }
283  else if (ISALLTRUE(b))
284  return SIGLENBIT(siglen) - sizebitvec(GETSIGN(a), siglen);
285 
286  return hemdistsign(GETSIGN(a), GETSIGN(b), siglen);
287 }
static int32 sizebitvec(BITVECP sign, int siglen)
Definition: hstore_gist.c:246
#define GETSIGN(x)
Definition: hstore_gist.c:61
static int hemdistsign(BITVECP a, BITVECP b, int siglen)
Definition: hstore_gist.c:260
#define ISALLTRUE(x)
Definition: hstore_gist.c:56
#define SIGLENBIT(siglen)
Definition: hstore_gist.c:24

◆ hemdistsign()

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

Definition at line 260 of file hstore_gist.c.

References GETBIT, i, and LOOPBIT.

Referenced by hemdist().

261 {
262  int i,
263  dist = 0;
264 
265  LOOPBIT(siglen)
266  {
267  if (GETBIT(a, i) != GETBIT(b, i))
268  dist++;
269  }
270  return dist;
271 }
#define LOOPBIT(siglen)
Definition: hstore_gist.c:35
#define GETBIT(x, i)
Definition: hstore_gist.c:43
int i

◆ PG_FUNCTION_INFO_V1() [1/10]

PG_FUNCTION_INFO_V1 ( ghstore_in  )

Referenced by crc32_sz(), and ghstore_alloc().

◆ PG_FUNCTION_INFO_V1() [2/10]

PG_FUNCTION_INFO_V1 ( ghstore_out  )

◆ PG_FUNCTION_INFO_V1() [3/10]

PG_FUNCTION_INFO_V1 ( ghstore_consistent  )

◆ PG_FUNCTION_INFO_V1() [4/10]

PG_FUNCTION_INFO_V1 ( ghstore_compress  )

◆ PG_FUNCTION_INFO_V1() [5/10]

PG_FUNCTION_INFO_V1 ( ghstore_decompress  )

◆ PG_FUNCTION_INFO_V1() [6/10]

PG_FUNCTION_INFO_V1 ( ghstore_penalty  )

◆ PG_FUNCTION_INFO_V1() [7/10]

PG_FUNCTION_INFO_V1 ( ghstore_picksplit  )

◆ PG_FUNCTION_INFO_V1() [8/10]

PG_FUNCTION_INFO_V1 ( ghstore_union  )

◆ PG_FUNCTION_INFO_V1() [9/10]

PG_FUNCTION_INFO_V1 ( ghstore_same  )

◆ PG_FUNCTION_INFO_V1() [10/10]

PG_FUNCTION_INFO_V1 ( ghstore_options  )

◆ sizebitvec()

static int32 sizebitvec ( BITVECP  sign,
int  siglen 
)
static

Definition at line 246 of file hstore_gist.c.

References i, LOOPBYTE, and SUMBIT.

Referenced by hemdist().

247 {
248  int32 size = 0,
249  i;
250 
251  LOOPBYTE(siglen)
252  {
253  size += SUMBIT(sign);
254  sign = (BITVECP) (((char *) sign) + 1);
255  }
256  return size;
257 }
#define SUMBIT(val)
Definition: hstore_gist.c:63
#define LOOPBYTE(siglen)
Definition: hstore_gist.c:32
signed int int32
Definition: c.h:417
char sign
Definition: informix.c:668
int i
char * BITVECP
Definition: hstore_gist.c:30

◆ unionkey()

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

Definition at line 290 of file hstore_gist.c.

References GETSIGN, i, ISALLTRUE, and LOOPBYTE.

Referenced by ghstore_union().

291 {
292  int32 i;
293  BITVECP sadd = GETSIGN(add);
294 
295  if (ISALLTRUE(add))
296  return 1;
297  LOOPBYTE(siglen)
298  sbase[i] |= sadd[i];
299  return 0;
300 }
#define LOOPBYTE(siglen)
Definition: hstore_gist.c:32
#define GETSIGN(x)
Definition: hstore_gist.c:61
signed int int32
Definition: c.h:417
#define ISALLTRUE(x)
Definition: hstore_gist.c:56
int i
char * BITVECP
Definition: hstore_gist.c:30