PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hstore_gist.c File Reference
#include "postgres.h"
#include "access/gist.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
#include "utils/pg_crc.h"
#include "hstore.h"
Include dependency graph for hstore_gist.c:

Go to the source code of this file.

Data Structures

struct  GISTTYPE
 
struct  SPLITCOST
 

Macros

#define BITBYTE   8
 
#define SIGLENINT   4 /* >122 => key will toast, so very slow!!! */
 
#define SIGLEN   ( sizeof(int)*SIGLENINT )
 
#define SIGLENBIT   (SIGLEN*BITBYTE)
 
#define SIGPTR(x)   ( (BITVECP) ARR_DATA_PTR(x) )
 
#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

#define BITBYTE   8

Definition at line 14 of file hstore_gist.c.

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

Definition at line 34 of file hstore_gist.c.

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

Definition at line 36 of file hstore_gist.c.

Referenced by ghstore_consistent(), and hemdistsign().

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

Definition at line 33 of file hstore_gist.c.

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

Definition at line 67 of file hstore_gist.c.

Referenced by ghstore_picksplit(), and ghstore_union().

#define GTHDRSIZE   (VARHDRSZ + sizeof(int32))

Definition at line 51 of file hstore_gist.c.

Referenced by g_intbig_picksplit(), and ghstore_picksplit().

#define HASH (   sign,
  val 
)    SETBIT((sign), HASHVAL(val))
#define HASHVAL (   val)    (((unsigned int)(val)) % SIGLENBIT)
#define LOOPBIT   for(i=0;i<SIGLENBIT;i++)

Definition at line 28 of file hstore_gist.c.

Referenced by hemdistsign().

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

Definition at line 35 of file hstore_gist.c.

#define SIGLEN   ( sizeof(int)*SIGLENINT )

Definition at line 16 of file hstore_gist.c.

Referenced by g_intbig_picksplit(), ghstore_picksplit(), ltree_picksplit(), and ltree_union().

#define SIGLENBIT   (SIGLEN*BITBYTE)

Definition at line 17 of file hstore_gist.c.

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

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

Definition at line 15 of file hstore_gist.c.

#define SIGPTR (   x)    ( (BITVECP) ARR_DATA_PTR(x) )

Definition at line 22 of file hstore_gist.c.

#define SUMBIT (   val)
Value:
( \
)
#define GETBITBYTE(x, i)
Definition: hstore_gist.c:33
long val
Definition: informix.c:689

Definition at line 56 of file hstore_gist.c.

Referenced by sizebitvec().

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

Definition at line 69 of file hstore_gist.c.

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

Typedef Documentation

typedef char BITVEC[SIGLEN]

Definition at line 19 of file hstore_gist.c.

Definition at line 20 of file hstore_gist.c.

Function Documentation

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

Definition at line 328 of file hstore_gist.c.

Referenced by ghstore_picksplit().

329 {
330  return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
331 }
static pg_crc32 crc32_sz ( char *  buf,
int  size 
)
static

Definition at line 73 of file hstore_gist.c.

References COMP_TRADITIONAL_CRC32, FIN_TRADITIONAL_CRC32, and INIT_TRADITIONAL_CRC32.

Referenced by ghstore_compress(), and ghstore_consistent().

74 {
75  pg_crc32 crc;
76 
78  COMP_TRADITIONAL_CRC32(crc, buf, size);
80 
81  return crc;
82 }
#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:65
#define FIN_TRADITIONAL_CRC32(crc)
Definition: pg_crc.h:47
uint32 pg_crc32
Definition: pg_crc.h:37
Datum ghstore_compress ( PG_FUNCTION_ARGS  )

Definition at line 112 of file hstore_gist.c.

References ALLISTRUE, ARRPTR, CALCGTSIZE, crc32_sz(), DatumGetHStoreP, DatumGetPointer, FALSE, 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.

113 {
114  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
115  GISTENTRY *retval = entry;
116 
117  if (entry->leafkey)
118  {
119  GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
120  HStore *val = DatumGetHStoreP(entry->key);
121  HEntry *hsent = ARRPTR(val);
122  char *ptr = STRPTR(val);
123  int count = HS_COUNT(val);
124  int i;
125 
126  SET_VARSIZE(res, CALCGTSIZE(0));
127 
128  for (i = 0; i < count; ++i)
129  {
130  int h;
131 
132  h = crc32_sz((char *) HSTORE_KEY(hsent, ptr, i),
133  HSTORE_KEYLEN(hsent, i));
134  HASH(GETSIGN(res), h);
135  if (!HSTORE_VALISNULL(hsent, i))
136  {
137  h = crc32_sz((char *) HSTORE_VAL(hsent, ptr, i),
138  HSTORE_VALLEN(hsent, i));
139  HASH(GETSIGN(res), h);
140  }
141  }
142 
143  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
144  gistentryinit(*retval, PointerGetDatum(res),
145  entry->rel, entry->page,
146  entry->offset,
147  FALSE);
148  }
149  else if (!ISALLTRUE(DatumGetPointer(entry->key)))
150  {
151  int32 i;
152  GISTTYPE *res;
154 
155  LOOPBYTE
156  {
157  if ((sign[i] & 0xff) != 0xff)
158  PG_RETURN_POINTER(retval);
159  }
160 
161  res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
163  res->flag = ALLISTRUE;
164 
165  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
166  gistentryinit(*retval, PointerGetDatum(res),
167  entry->rel, entry->page,
168  entry->offset,
169  FALSE);
170  }
171 
172  PG_RETURN_POINTER(retval);
173 }
Relation rel
Definition: gist.h:124
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
#define DatumGetHStoreP(d)
Definition: hstore.h:152
#define GETSIGN(x)
Definition: hstore_gist.c:54
#define PointerGetDatum(X)
Definition: postgres.h:564
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define ALLISTRUE
Definition: hstore_gist.c:47
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define HASH(sign, val)
Definition: hstore_gist.c:38
int32 flag
Definition: hstore_gist.c:43
signed int int32
Definition: c.h:253
Page page
Definition: gist.h:125
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define FALSE
Definition: c.h:218
char sign
Definition: informix.c:693
Datum key
Definition: gist.h:123
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
bool leafkey
Definition: gist.h:127
void * palloc0(Size size)
Definition: mcxt.c:920
static pg_crc32 crc32_sz(char *buf, int size)
Definition: hstore_gist.c:73
#define ISALLTRUE(x)
Definition: hstore_gist.c:49
#define CALCGTSIZE(flag)
Definition: hstore_gist.c:52
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:169
Definition: hstore.h:18
#define DatumGetPointer(X)
Definition: postgres.h:557
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * palloc(Size size)
Definition: mcxt.c:891
#define STRPTR(x)
Definition: hstore.h:76
#define LOOPBYTE
Definition: hstore_gist.c:25
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
char * BITVECP
Definition: hstore_gist.c:20
#define ARRPTR(x)
Definition: cube.c:26
OffsetNumber offset
Definition: gist.h:126
long val
Definition: informix.c:689
Datum ghstore_consistent ( PG_FUNCTION_ARGS  )

Definition at line 500 of file hstore_gist.c.

References ARRPTR, crc32_sz(), DatumGetPointer, deconstruct_array(), elog, ERROR, FALSE, 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_HS, PG_GETARG_POINTER, PG_GETARG_TEXT_PP, PG_GETARG_UINT16, PG_RETURN_BOOL, sign, STRPTR, TEXTOID, TRUE, VARDATA, VARDATA_ANY, VARHDRSZ, VARSIZE, and VARSIZE_ANY_EXHDR.

501 {
502  GISTTYPE *entry = (GISTTYPE *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
504 
505  /* Oid subtype = PG_GETARG_OID(3); */
506  bool *recheck = (bool *) PG_GETARG_POINTER(4);
507  bool res = true;
508  BITVECP sign;
509 
510  /* All cases served by this function are inexact */
511  *recheck = true;
512 
513  if (ISALLTRUE(entry))
514  PG_RETURN_BOOL(true);
515 
516  sign = GETSIGN(entry);
517 
518  if (strategy == HStoreContainsStrategyNumber ||
520  {
521  HStore *query = PG_GETARG_HS(1);
522  HEntry *qe = ARRPTR(query);
523  char *qv = STRPTR(query);
524  int count = HS_COUNT(query);
525  int i;
526 
527  for (i = 0; res && i < count; ++i)
528  {
529  int crc = crc32_sz((char *) HSTORE_KEY(qe, qv, i),
530  HSTORE_KEYLEN(qe, i));
531 
532  if (GETBIT(sign, HASHVAL(crc)))
533  {
534  if (!HSTORE_VALISNULL(qe, i))
535  {
536  crc = crc32_sz((char *) HSTORE_VAL(qe, qv, i),
537  HSTORE_VALLEN(qe, i));
538  if (!GETBIT(sign, HASHVAL(crc)))
539  res = false;
540  }
541  }
542  else
543  res = false;
544  }
545  }
546  else if (strategy == HStoreExistsStrategyNumber)
547  {
548  text *query = PG_GETARG_TEXT_PP(1);
549  int crc = crc32_sz(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query));
550 
551  res = (GETBIT(sign, HASHVAL(crc))) ? true : false;
552  }
553  else if (strategy == HStoreExistsAllStrategyNumber)
554  {
555  ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
556  Datum *key_datums;
557  bool *key_nulls;
558  int key_count;
559  int i;
560 
561  deconstruct_array(query,
562  TEXTOID, -1, false, 'i',
563  &key_datums, &key_nulls, &key_count);
564 
565  for (i = 0; res && i < key_count; ++i)
566  {
567  int crc;
568 
569  if (key_nulls[i])
570  continue;
571  crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
572  if (!(GETBIT(sign, HASHVAL(crc))))
573  res = FALSE;
574  }
575  }
576  else if (strategy == HStoreExistsAnyStrategyNumber)
577  {
578  ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
579  Datum *key_datums;
580  bool *key_nulls;
581  int key_count;
582  int i;
583 
584  deconstruct_array(query,
585  TEXTOID, -1, false, 'i',
586  &key_datums, &key_nulls, &key_count);
587 
588  res = FALSE;
589 
590  for (i = 0; !res && i < key_count; ++i)
591  {
592  int crc;
593 
594  if (key_nulls[i])
595  continue;
596  crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
597  if (GETBIT(sign, HASHVAL(crc)))
598  res = TRUE;
599  }
600  }
601  else
602  elog(ERROR, "Unsupported strategy number: %d", strategy);
603 
604  PG_RETURN_BOOL(res);
605 }
Definition: hstore.h:44
#define HStoreContainsStrategyNumber
Definition: hstore.h:180
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define VARDATA(PTR)
Definition: postgres.h:305
#define TEXTOID
Definition: pg_type.h:324
#define VARSIZE(PTR)
Definition: postgres.h:306
#define GETSIGN(x)
Definition: hstore_gist.c:54
#define VARHDRSZ
Definition: c.h:441
#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:232
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define ERROR
Definition: elog.h:43
#define FALSE
Definition: c.h:218
#define HStoreExistsStrategyNumber
Definition: hstore.h:181
char sign
Definition: informix.c:693
#define HStoreExistsAllStrategyNumber
Definition: hstore.h:183
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define HASHVAL(val)
Definition: hstore_gist.c:37
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
static pg_crc32 crc32_sz(char *buf, int size)
Definition: hstore_gist.c:73
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
#define ISALLTRUE(x)
Definition: hstore_gist.c:49
#define GETBIT(x, i)
Definition: hstore_gist.c:36
Definition: hstore.h:18
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:228
#define DatumGetPointer(X)
Definition: postgres.h:557
#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:3475
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
#define STRPTR(x)
Definition: hstore.h:76
#define HStoreExistsAnyStrategyNumber
Definition: hstore.h:182
int i
Definition: c.h:435
#define TRUE
Definition: c.h:214
char * BITVECP
Definition: hstore_gist.c:20
#define ARRPTR(x)
Definition: cube.c:26
#define elog
Definition: elog.h:219
#define HStoreOldContainsStrategyNumber
Definition: hstore.h:184
Datum ghstore_decompress ( PG_FUNCTION_ARGS  )

Definition at line 180 of file hstore_gist.c.

References PG_GETARG_POINTER, and PG_RETURN_POINTER.

181 {
183 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
Datum ghstore_in ( PG_FUNCTION_ARGS  )

Definition at line 90 of file hstore_gist.c.

References elog, ERROR, and PG_RETURN_DATUM.

91 {
92  elog(ERROR, "Not implemented");
93  PG_RETURN_DATUM(0);
94 }
#define ERROR
Definition: elog.h:43
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
#define elog
Definition: elog.h:219
Datum ghstore_out ( PG_FUNCTION_ARGS  )

Definition at line 97 of file hstore_gist.c.

References elog, ERROR, and PG_RETURN_DATUM.

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:297
#define elog
Definition: elog.h:219
Datum ghstore_penalty ( PG_FUNCTION_ARGS  )

Definition at line 308 of file hstore_gist.c.

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

309 {
310  GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
311  GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
312  float *penalty = (float *) PG_GETARG_POINTER(2);
313  GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
314  GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
315 
316  *penalty = hemdist(origval, newval);
317  PG_RETURN_POINTER(penalty);
318 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
static int hemdist(GISTTYPE *a, GISTTYPE *b)
Definition: hstore_gist.c:246
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
Datum key
Definition: gist.h:123
#define newval
#define DatumGetPointer(X)
Definition: postgres.h:557
Datum ghstore_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 335 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.

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

Definition at line 186 of file hstore_gist.c.

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

187 {
188  GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
189  GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
190  bool *result = (bool *) PG_GETARG_POINTER(2);
191 
192  if (ISALLTRUE(a) && ISALLTRUE(b))
193  *result = true;
194  else if (ISALLTRUE(a))
195  *result = false;
196  else if (ISALLTRUE(b))
197  *result = false;
198  else
199  {
200  int32 i;
201  BITVECP sa = GETSIGN(a),
202  sb = GETSIGN(b);
203 
204  *result = true;
205  LOOPBYTE
206  {
207  if (sa[i] != sb[i])
208  {
209  *result = false;
210  break;
211  }
212  }
213  }
214  PG_RETURN_POINTER(result);
215 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define GETSIGN(x)
Definition: hstore_gist.c:54
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
signed int int32
Definition: c.h:253
#define ISALLTRUE(x)
Definition: hstore_gist.c:49
#define LOOPBYTE
Definition: hstore_gist.c:25
int i
char * BITVECP
Definition: hstore_gist.c:20
Datum ghstore_union ( PG_FUNCTION_ARGS  )

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

276 {
278  int32 len = entryvec->n;
279 
280  int *size = (int *) PG_GETARG_POINTER(1);
281  BITVEC base;
282  int32 i;
283  int32 flag = 0;
284  GISTTYPE *result;
285 
286  MemSet((void *) base, 0, sizeof(BITVEC));
287  for (i = 0; i < len; i++)
288  {
289  if (unionkey(base, GETENTRY(entryvec, i)))
290  {
291  flag = ALLISTRUE;
292  break;
293  }
294  }
295 
296  len = CALCGTSIZE(flag);
297  result = (GISTTYPE *) palloc(len);
298  SET_VARSIZE(result, len);
299  result->flag = flag;
300  if (!ISALLTRUE(result))
301  memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
302  *size = len;
303 
304  PG_RETURN_POINTER(result);
305 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define GETSIGN(x)
Definition: hstore_gist.c:54
int32 n
Definition: gist.h:160
#define ALLISTRUE
Definition: hstore_gist.c:47
#define MemSet(start, val, len)
Definition: c.h:853
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
int32 flag
Definition: hstore_gist.c:43
signed int int32
Definition: c.h:253
char BITVEC[SIGLEN]
Definition: hstore_gist.c:19
char * flag(int b)
Definition: test-ctype.c:33
static int32 unionkey(BITVECP sbase, GISTTYPE *add)
Definition: hstore_gist.c:262
#define GETENTRY(vec, pos)
Definition: hstore_gist.c:67
#define ISALLTRUE(x)
Definition: hstore_gist.c:49
#define CALCGTSIZE(flag)
Definition: hstore_gist.c:52
void * palloc(Size size)
Definition: mcxt.c:891
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
static int hemdist ( GISTTYPE a,
GISTTYPE b 
)
static

Definition at line 246 of file hstore_gist.c.

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

Referenced by ghstore_penalty(), and ghstore_picksplit().

247 {
248  if (ISALLTRUE(a))
249  {
250  if (ISALLTRUE(b))
251  return 0;
252  else
253  return SIGLENBIT - sizebitvec(GETSIGN(b));
254  }
255  else if (ISALLTRUE(b))
256  return SIGLENBIT - sizebitvec(GETSIGN(a));
257 
258  return hemdistsign(GETSIGN(a), GETSIGN(b));
259 }
#define GETSIGN(x)
Definition: hstore_gist.c:54
#define SIGLENBIT
Definition: hstore_gist.c:17
static int32 sizebitvec(BITVECP sign)
Definition: hstore_gist.c:218
#define ISALLTRUE(x)
Definition: hstore_gist.c:49
static int hemdistsign(BITVECP a, BITVECP b)
Definition: hstore_gist.c:232
static int hemdistsign ( BITVECP  a,
BITVECP  b 
)
static

Definition at line 232 of file hstore_gist.c.

References GETBIT, i, and LOOPBIT.

Referenced by hemdist().

233 {
234  int i,
235  dist = 0;
236 
237  LOOPBIT
238  {
239  if (GETBIT(a, i) != GETBIT(b, i))
240  dist++;
241  }
242  return dist;
243 }
#define LOOPBIT
Definition: hstore_gist.c:28
#define GETBIT(x, i)
Definition: hstore_gist.c:36
int i
PG_FUNCTION_INFO_V1 ( ghstore_in  )
PG_FUNCTION_INFO_V1 ( ghstore_out  )
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  )
static int32 sizebitvec ( BITVECP  sign)
static

Definition at line 218 of file hstore_gist.c.

References i, LOOPBYTE, and SUMBIT.

Referenced by hemdist().

219 {
220  int32 size = 0,
221  i;
222 
223  LOOPBYTE
224  {
225  size += SUMBIT(sign);
226  sign = (BITVECP) (((char *) sign) + 1);
227  }
228  return size;
229 }
#define SUMBIT(val)
Definition: hstore_gist.c:56
signed int int32
Definition: c.h:253
char sign
Definition: informix.c:693
#define LOOPBYTE
Definition: hstore_gist.c:25
int i
char * BITVECP
Definition: hstore_gist.c:20
static int32 unionkey ( BITVECP  sbase,
GISTTYPE add 
)
static

Definition at line 262 of file hstore_gist.c.

References GETSIGN, i, ISALLTRUE, and LOOPBYTE.

Referenced by ghstore_union().

263 {
264  int32 i;
265  BITVECP sadd = GETSIGN(add);
266 
267  if (ISALLTRUE(add))
268  return 1;
269  LOOPBYTE
270  sbase[i] |= sadd[i];
271  return 0;
272 }
#define GETSIGN(x)
Definition: hstore_gist.c:54
signed int int32
Definition: c.h:253
#define ISALLTRUE(x)
Definition: hstore_gist.c:49
#define LOOPBYTE
Definition: hstore_gist.c:25
int i
char * BITVECP
Definition: hstore_gist.c:20