PostgreSQL Source Code git master
Loading...
Searching...
No Matches
hstore_gist.c
Go to the documentation of this file.
1/*
2 * contrib/hstore/hstore_gist.c
3 */
4#include "postgres.h"
5
6#include "access/gist.h"
7#include "access/reloptions.h"
8#include "access/stratnum.h"
9#include "catalog/pg_type.h"
10#include "common/int.h"
11#include "hstore.h"
12#include "utils/pg_crc.h"
13
14/* gist_hstore_ops opclass options */
15typedef struct
16{
17 int32 vl_len_; /* varlena header (do not touch directly!) */
18 int siglen; /* signature length in bytes */
20
21/* bigint defines */
22#define BITBYTE 8
23#define SIGLEN_DEFAULT (sizeof(int32) * 4)
24#define SIGLEN_MAX GISTMaxIndexKeySize
25#define SIGLENBIT(siglen) ((siglen) * BITBYTE)
26#define GET_SIGLEN() (PG_HAS_OPCLASS_OPTIONS() ? \
27 ((GistHstoreOptions *) PG_GET_OPCLASS_OPTIONS())->siglen : \
28 SIGLEN_DEFAULT)
29
30
31typedef char *BITVECP;
32
33#define LOOPBYTE(siglen) \
34 for (i = 0; i < (siglen); i++)
35
36#define LOOPBIT(siglen) \
37 for (i = 0; i < SIGLENBIT(siglen); i++)
38
39/* beware of multiple evaluation of arguments to these macros! */
40#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
41#define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 )
42#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
43#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
44#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
45#define HASHVAL(val, siglen) (((unsigned int)(val)) % SIGLENBIT(siglen))
46#define HASH(sign, val, siglen) SETBIT((sign), HASHVAL(val, siglen))
47
48typedef struct
49{
50 int32 vl_len_; /* varlena header (do not touch directly!) */
53} GISTTYPE;
54
55#define ALLISTRUE 0x04
56
57#define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
58
59#define GTHDRSIZE (VARHDRSZ + sizeof(int32))
60#define CALCGTSIZE(flag, siglen) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : (siglen)) )
61
62#define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
63
64#define SUMBIT(val) ( \
65 GETBITBYTE((val),0) + \
66 GETBITBYTE((val),1) + \
67 GETBITBYTE((val),2) + \
68 GETBITBYTE((val),3) + \
69 GETBITBYTE((val),4) + \
70 GETBITBYTE((val),5) + \
71 GETBITBYTE((val),6) + \
72 GETBITBYTE((val),7) \
73)
74
75#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
76
77#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
78
79/* shorthand for calculating CRC-32 of a single chunk of data. */
80static pg_crc32
81crc32_sz(const char *buf, int size)
82{
84
88
89 return crc;
90}
91
92
95
96
99{
102 errmsg("cannot accept a value of type %s", "ghstore")));
103
104 PG_RETURN_VOID(); /* keep compiler quiet */
105}
106
107Datum
109{
112 errmsg("cannot display a value of type %s", "ghstore")));
113
114 PG_RETURN_VOID(); /* keep compiler quiet */
115}
116
117static GISTTYPE *
118ghstore_alloc(bool allistrue, int siglen, BITVECP sign)
119{
120 int flag = allistrue ? ALLISTRUE : 0;
121 int size = CALCGTSIZE(flag, siglen);
122 GISTTYPE *res = palloc(size);
123
124 SET_VARSIZE(res, size);
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}
137
146
147Datum
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);
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),
169 HASH(GETSIGN(res), h, siglen);
170 if (!HSTORE_VALISNULL(hsent, i))
171 {
172 h = crc32_sz((char *) HSTORE_VAL(hsent, ptr, i),
174 HASH(GETSIGN(res), h, siglen);
175 }
176 }
177
178 retval = palloc_object(GISTENTRY);
179 gistentryinit(*retval, PointerGetDatum(res),
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 = palloc_object(GISTENTRY);
199 gistentryinit(*retval, PointerGetDatum(res),
200 entry->rel, entry->page,
201 entry->offset,
202 false);
203 }
204
205 PG_RETURN_POINTER(retval);
206}
207
208/*
209 * Since type ghstore isn't toastable (and doesn't need to be),
210 * this function can be a no-op.
211 */
212Datum
217
218Datum
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}
251
252static int32
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}
265
266static int
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}
279
280static int
281hemdist(GISTTYPE *a, GISTTYPE *b, int siglen)
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}
295
296static int32
298{
299 int32 i;
301
302 if (ISALLTRUE(add))
303 return 1;
304 LOOPBYTE(siglen)
305 sbase[i] |= sadd[i];
306 return 0;
307}
308
309Datum
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}
335
336Datum
338{
339 GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
341 float *penalty = (float *) PG_GETARG_POINTER(2);
342 int siglen = GET_SIGLEN();
345
346 *penalty = hemdist(origval, newval, siglen);
347 PG_RETURN_POINTER(penalty);
348}
349
350
351typedef struct
352{
355} SPLITCOST;
356
357static int
358comparecost(const void *a, const void *b)
359{
360 return pg_cmp_s32(((const SPLITCOST *) a)->cost,
361 ((const SPLITCOST *) b)->cost);
362}
363
364
365Datum
367{
369 OffsetNumber maxoff = entryvec->n - 2;
370
372 int siglen = GET_SIGLEN();
373 OffsetNumber k,
374 j;
376 *datum_r;
378 union_r;
380 size_beta;
382 waste = -1;
383 int32 nbytes;
385 seed_2 = 0;
386 OffsetNumber *left,
387 *right;
388 BITVECP ptr;
389 int i;
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 .. */
429
430 maxoff = OffsetNumberNext(maxoff);
431 /* sort before ... */
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
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
503
505}
506
507
508Datum
510{
511 GISTTYPE *entry = (GISTTYPE *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
513#ifdef NOT_USED
514 Oid subtype = PG_GETARG_OID(3);
515#endif
516 bool *recheck = (bool *) PG_GETARG_POINTER(4);
517 int siglen = GET_SIGLEN();
518 bool res = true;
520
521 /* All cases served by this function are inexact */
522 *recheck = true;
523
524 if (ISALLTRUE(entry))
525 PG_RETURN_BOOL(true);
526
527 sign = GETSIGN(entry);
528
529 if (strategy == HStoreContainsStrategyNumber ||
531 {
532 HStore *query = PG_GETARG_HSTORE_P(1);
533 HEntry *qe = ARRPTR(query);
534 char *qv = STRPTR(query);
535 int count = HS_COUNT(query);
536 int i;
537
538 for (i = 0; res && i < count; ++i)
539 {
540 int crc = crc32_sz((char *) HSTORE_KEY(qe, qv, i),
541 HSTORE_KEYLEN(qe, i));
542
543 if (GETBIT(sign, HASHVAL(crc, siglen)))
544 {
545 if (!HSTORE_VALISNULL(qe, i))
546 {
547 crc = crc32_sz((char *) HSTORE_VAL(qe, qv, i),
548 HSTORE_VALLEN(qe, i));
549 if (!GETBIT(sign, HASHVAL(crc, siglen)))
550 res = false;
551 }
552 }
553 else
554 res = false;
555 }
556 }
557 else if (strategy == HStoreExistsStrategyNumber)
558 {
559 text *query = PG_GETARG_TEXT_PP(1);
560 int crc = crc32_sz(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query));
561
562 res = (GETBIT(sign, HASHVAL(crc, siglen))) ? true : false;
563 }
564 else if (strategy == HStoreExistsAllStrategyNumber)
565 {
568 bool *key_nulls;
569 int key_count;
570 int i;
571
573
574 for (i = 0; res && i < key_count; ++i)
575 {
576 int crc;
577
578 if (key_nulls[i])
579 continue;
581 if (!(GETBIT(sign, HASHVAL(crc, siglen))))
582 res = false;
583 }
584 }
585 else if (strategy == HStoreExistsAnyStrategyNumber)
586 {
589 bool *key_nulls;
590 int key_count;
591 int i;
592
594
595 res = false;
596
597 for (i = 0; !res && i < key_count; ++i)
598 {
599 int crc;
600
601 if (key_nulls[i])
602 continue;
604 if (GETBIT(sign, HASHVAL(crc, siglen)))
605 res = true;
606 }
607 }
608 else
609 elog(ERROR, "Unsupported strategy number: %d", strategy);
610
611 PG_RETURN_BOOL(res);
612}
613
614Datum
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
#define VARHDRSZ
Definition c.h:711
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:480
int32_t int32
Definition c.h:542
#define ARRPTR(x)
Definition cube.c:28
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_GETARG_UINT16(n)
Definition fmgr.h:272
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define gistentryinit(e, k, r, pg, o, l)
Definition gist.h:245
#define newval
#define HStoreExistsAllStrategyNumber
Definition hstore.h:183
#define HStoreExistsStrategyNumber
Definition hstore.h:181
#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 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 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 HASHVAL(val, siglen)
Definition hstore_gist.c:45
Datum ghstore_options(PG_FUNCTION_ARGS)
#define LOOPBYTE(siglen)
Definition hstore_gist.c:33
#define ALLISTRUE
Definition hstore_gist.c:55
#define WISH_F(a, b, c)
Definition hstore_gist.c:77
Datum ghstore_union(PG_FUNCTION_ARGS)
Datum ghstore_picksplit(PG_FUNCTION_ARGS)
#define GETBIT(x, i)
Definition hstore_gist.c:44
#define LOOPBIT(siglen)
Definition hstore_gist.c:36
static int hemdist(GISTTYPE *a, GISTTYPE *b, int siglen)
static int32 sizebitvec(BITVECP sign, int siglen)
static int hemdistsign(BITVECP a, BITVECP b, int siglen)
Datum ghstore_compress(PG_FUNCTION_ARGS)
Datum ghstore_out(PG_FUNCTION_ARGS)
#define CALCGTSIZE(flag, siglen)
Definition hstore_gist.c:60
#define GETENTRY(vec, pos)
Definition hstore_gist.c:75
Datum ghstore_consistent(PG_FUNCTION_ARGS)
static int32 unionkey(BITVECP sbase, GISTTYPE *add, int siglen)
#define ISALLTRUE(x)
Definition hstore_gist.c:57
#define SIGLEN_MAX
Definition hstore_gist.c:24
#define SIGLEN_DEFAULT
Definition hstore_gist.c:23
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
#define SUMBIT(val)
Definition hstore_gist.c:64
Datum ghstore_penalty(PG_FUNCTION_ARGS)
#define SIGLENBIT(siglen)
Definition hstore_gist.c:25
Datum ghstore_decompress(PG_FUNCTION_ARGS)
Datum ghstore_in(PG_FUNCTION_ARGS)
Definition hstore_gist.c:98
Datum ghstore_same(PG_FUNCTION_ARGS)
static GISTTYPE * ghstore_alloc(bool allistrue, int siglen, BITVECP sign)
#define GETSIGN(x)
Definition hstore_gist.c:62
#define HASH(sign, val, siglen)
Definition hstore_gist.c:46
static int comparecost(const void *a, const void *b)
long val
Definition informix.c:689
char sign
Definition informix.c:693
static int pg_cmp_s32(int32 a, int32 b)
Definition int.h:713
int b
Definition isn.c:74
int a
Definition isn.c:73
int j
Definition isn.c:78
int i
Definition isn.c:77
void * palloc(Size size)
Definition mcxt.c:1387
#define OffsetNumberNext(offsetNumber)
Definition off.h:52
uint16 OffsetNumber
Definition off.h:24
#define FirstOffsetNumber
Definition off.h:27
const void size_t len
const void * data
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[DEFAULT_XLOG_SEG_SIZE]
#define qsort(a, b, c, d)
Definition port.h:495
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
unsigned int Oid
static int fb(int x)
void init_local_reloptions(local_relopts *relopts, Size relopt_struct_size)
Definition reloptions.c:782
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)
uint16 StrategyNumber
Definition stratnum.h:22
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
int32 flag
Definition hstore_gist.c:51
int32 vl_len_
Definition hstore_gist.c:50
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
OffsetNumber pos
Definition c.h:706
char * flag(int b)
Definition test-ctype.c:33
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static Size VARSIZE(const void *PTR)
Definition varatt.h:298
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432