Debian Codegraph @

contrib/intarray/_intbig_gist.c

raw
1 /*
2 * contrib/intarray/_intbig_gist.c
3 */
4 #include "postgres.h"
5
6 #include <math.h>
7
8 #include "_int.h"
9 #include "access/gist.h"
10 #include "access/reloptions.h"
11 #include "access/stratnum.h"
12 #include "common/int.h"
13 #include "port/pg_bitutils.h"
14
15 #define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
16 /*
17 ** _intbig methods
18 */
19 PG_FUNCTION_INFO_V1(g_intbig_consistent);
20 PG_FUNCTION_INFO_V1(g_intbig_compress);
21 PG_FUNCTION_INFO_V1(g_intbig_decompress);
22 PG_FUNCTION_INFO_V1(g_intbig_penalty);
23 PG_FUNCTION_INFO_V1(g_intbig_picksplit);
24 PG_FUNCTION_INFO_V1(g_intbig_union);
25 PG_FUNCTION_INFO_V1(g_intbig_same);
26 PG_FUNCTION_INFO_V1(g_intbig_options);
27
28 PG_FUNCTION_INFO_V1(_intbig_in);
29 PG_FUNCTION_INFO_V1(_intbig_out);
30
31 Datum
32 _intbig_in(PG_FUNCTION_ARGS)
33 {
34 ereport(ERROR,
35 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
36 errmsg("cannot accept a value of type %s", "intbig_gkey")));
37
38 PG_RETURN_VOID(); /* keep compiler quiet */
39 }
40
41 Datum
42 _intbig_out(PG_FUNCTION_ARGS)
43 {
44 ereport(ERROR,
45 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
46 errmsg("cannot display a value of type %s", "intbig_gkey")));
47
48 PG_RETURN_VOID(); /* keep compiler quiet */
49 }
50
51 static GISTTYPE *
52 _intbig_alloc(bool allistrue, int siglen, BITVECP sign)
53 {
54 int flag = allistrue ? ALLISTRUE : 0;
55 int size = CALCGTSIZE(flag, siglen);
56 GISTTYPE *res = (GISTTYPE *) palloc(size);
57
58 SET_VARSIZE(res, size);
59 res->flag = flag;
60
61 if (!allistrue)
62 {
63 if (sign)
64 memcpy(GETSIGN(res), sign, siglen);
65 else
66 memset(GETSIGN(res), 0, siglen);
67 }
68
69 return res;
70 }
71
72
73 /*********************************************************************
74 ** intbig functions
75 *********************************************************************/
76 static bool
77 _intbig_overlap(GISTTYPE *a, ArrayType *b, int siglen)
78 {
79 int num = ARRNELEMS(b);
80 int32 *ptr = ARRPTR(b);
81
82 CHECKARRVALID(b);
83
84 while (num--)
85 {
86 if (GETBIT(GETSIGN(a), HASHVAL(*ptr, siglen)))
87 return true;
88 ptr++;
89 }
90
91 return false;
92 }
93
94 static bool
95 _intbig_contains(GISTTYPE *a, ArrayType *b, int siglen)
96 {
97 int num = ARRNELEMS(b);
98 int32 *ptr = ARRPTR(b);
99
100 CHECKARRVALID(b);
101
102 while (num--)
103 {
104 if (!GETBIT(GETSIGN(a), HASHVAL(*ptr, siglen)))
105 return false;
106 ptr++;
107 }
108
109 return true;
110 }
111
112 Datum
113 g_intbig_same(PG_FUNCTION_ARGS)
114 {
115 GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
116 GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
117 bool *result = (bool *) PG_GETARG_POINTER(2);
118 int siglen = GET_SIGLEN();
119
120 if (ISALLTRUE(a) && ISALLTRUE(b))
121 *result = true;
122 else if (ISALLTRUE(a))
123 *result = false;
124 else if (ISALLTRUE(b))
125 *result = false;
126 else
127 {
128 int32 i;
129 BITVECP sa = GETSIGN(a),
130 sb = GETSIGN(b);
131
132 *result = true;
133 LOOPBYTE(siglen)
134 {
135 if (sa[i] != sb[i])
136 {
137 *result = false;
138 break;
139 }
140 }
141 }
142 PG_RETURN_POINTER(result);
143 }
144
145 Datum
146 g_intbig_compress(PG_FUNCTION_ARGS)
147 {
148 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
149 int siglen = GET_SIGLEN();
150
151 if (entry->leafkey)
152 {
153 GISTENTRY *retval;
154 ArrayType *in = DatumGetArrayTypeP(entry->key);
155 int32 *ptr;
156 int num;
157 GISTTYPE *res = _intbig_alloc(false, siglen, NULL);
158
159 CHECKARRVALID(in);
160 if (ARRISEMPTY(in))
161 {
162 ptr = NULL;
163 num = 0;
164 }
165 else
166 {
167 ptr = ARRPTR(in);
168 num = ARRNELEMS(in);
169 }
170
171 while (num--)
172 {
173 HASH(GETSIGN(res), *ptr, siglen);
174 ptr++;
175 }
176
177 retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
178 gistentryinit(*retval, PointerGetDatum(res),
179 entry->rel, entry->page,
180 entry->offset, false);
181
182 PG_RETURN_POINTER(retval);
183 }
184 else if (!ISALLTRUE(DatumGetPointer(entry->key)))
185 {
186 GISTENTRY *retval;
187 int i;
188 BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
189 GISTTYPE *res;
190
191 LOOPBYTE(siglen)
192 {
193 if ((sign[i] & 0xff) != 0xff)
194 PG_RETURN_POINTER(entry);
195 }
196
197 res = _intbig_alloc(true, siglen, sign);
198 retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
199 gistentryinit(*retval, PointerGetDatum(res),
200 entry->rel, entry->page,
201 entry->offset, false);
202
203 PG_RETURN_POINTER(retval);
204 }
205
206 PG_RETURN_POINTER(entry);
207 }
208
209
210 static int32
211 sizebitvec(BITVECP sign, int siglen)
212 {
213 return pg_popcount(sign, siglen);
214 }
215
216 static int
217 hemdistsign(BITVECP a, BITVECP b, int siglen)
218 {
219 int i,
220 diff,
221 dist = 0;
222
223 LOOPBYTE(siglen)
224 {
225 diff = (unsigned char) (a[i] ^ b[i]);
226 /* Using the popcount functions here isn't likely to win */
227 dist += pg_number_of_ones[diff];
228 }
229 return dist;
230 }
231
232 static int
233 hemdist(GISTTYPE *a, GISTTYPE *b, int siglen)
234 {
235 if (ISALLTRUE(a))
236 {
237 if (ISALLTRUE(b))
238 return 0;
239 else
240 return SIGLENBIT(siglen) - sizebitvec(GETSIGN(b), siglen);
241 }
242 else if (ISALLTRUE(b))
243 return SIGLENBIT(siglen) - sizebitvec(GETSIGN(a), siglen);
244
245 return hemdistsign(GETSIGN(a), GETSIGN(b), siglen);
246 }
247
248 Datum
249 g_intbig_decompress(PG_FUNCTION_ARGS)
250 {
251 PG_RETURN_DATUM(PG_GETARG_DATUM(0));
252 }
253
254 static int32
255 unionkey(BITVECP sbase, GISTTYPE *add, int siglen)
256 {
257 int32 i;
258 BITVECP sadd = GETSIGN(add);
259
260 if (ISALLTRUE(add))
261 return 1;
262 LOOPBYTE(siglen)
263 sbase[i] |= sadd[i];
264 return 0;
265 }
266
267 Datum
268 g_intbig_union(PG_FUNCTION_ARGS)
269 {
270 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
271 int *size = (int *) PG_GETARG_POINTER(1);
272 int siglen = GET_SIGLEN();
273 int32 i;
274 GISTTYPE *result = _intbig_alloc(false, siglen, NULL);
275 BITVECP base = GETSIGN(result);
276
277 for (i = 0; i < entryvec->n; i++)
278 {
279 if (unionkey(base, GETENTRY(entryvec, i), siglen))
280 {
281 result->flag |= ALLISTRUE;
282 SET_VARSIZE(result, CALCGTSIZE(ALLISTRUE, siglen));
283 break;
284 }
285 }
286
287 *size = VARSIZE(result);
288
289 PG_RETURN_POINTER(result);
290 }
291
292 Datum
293 g_intbig_penalty(PG_FUNCTION_ARGS)
294 {
295 GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
296 GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
297 float *penalty = (float *) PG_GETARG_POINTER(2);
298 GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
299 GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
300 int siglen = GET_SIGLEN();
301
302 *penalty = hemdist(origval, newval, siglen);
303 PG_RETURN_POINTER(penalty);
304 }
305
306
307 typedef struct
308 {
309 OffsetNumber pos;
310 int32 cost;
311 } SPLITCOST;
312
313 static int
314 comparecost(const void *a, const void *b)
315 {
316 return pg_cmp_s32(((const SPLITCOST *) a)->cost,
317 ((const SPLITCOST *) b)->cost);
318 }
319
320
321 Datum
322 g_intbig_picksplit(PG_FUNCTION_ARGS)
323 {
324 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
325 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
326 int siglen = GET_SIGLEN();
327 OffsetNumber k,
328 j;
329 GISTTYPE *datum_l,
330 *datum_r;
331 BITVECP union_l,
332 union_r;
333 int32 size_alpha,
334 size_beta;
335 int32 size_waste,
336 waste = -1;
337 int32 nbytes;
338 OffsetNumber seed_1 = 0,
339 seed_2 = 0;
340 OffsetNumber *left,
341 *right;
342 OffsetNumber maxoff;
343 BITVECP ptr;
344 int i;
345 SPLITCOST *costvector;
346 GISTTYPE *_k,
347 *_j;
348
349 maxoff = entryvec->n - 2;
350 nbytes = (maxoff + 2) * sizeof(OffsetNumber);
351 v->spl_left = (OffsetNumber *) palloc(nbytes);
352 v->spl_right = (OffsetNumber *) palloc(nbytes);
353
354 for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
355 {
356 _k = GETENTRY(entryvec, k);
357 for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
358 {
359 size_waste = hemdist(_k, GETENTRY(entryvec, j), siglen);
360 if (size_waste > waste)
361 {
362 waste = size_waste;
363 seed_1 = k;
364 seed_2 = j;
365 }
366 }
367 }
368
369 left = v->spl_left;
370 v->spl_nleft = 0;
371 right = v->spl_right;
372 v->spl_nright = 0;
373
374 if (seed_1 == 0 || seed_2 == 0)
375 {
376 seed_1 = 1;
377 seed_2 = 2;
378 }
379
380 /* form initial .. */
381 datum_l = _intbig_alloc(ISALLTRUE(GETENTRY(entryvec, seed_1)), siglen,
382 GETSIGN(GETENTRY(entryvec, seed_1)));
383 datum_r = _intbig_alloc(ISALLTRUE(GETENTRY(entryvec, seed_2)), siglen,
384 GETSIGN(GETENTRY(entryvec, seed_2)));
385
386 maxoff = OffsetNumberNext(maxoff);
387 /* sort before ... */
388 costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
389 for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
390 {
391 costvector[j - 1].pos = j;
392 _j = GETENTRY(entryvec, j);
393 size_alpha = hemdist(datum_l, _j, siglen);
394 size_beta = hemdist(datum_r, _j, siglen);
395 costvector[j - 1].cost = abs(size_alpha - size_beta);
396 }
397 qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
398
399 union_l = GETSIGN(datum_l);
400 union_r = GETSIGN(datum_r);
401
402 for (k = 0; k < maxoff; k++)
403 {
404 j = costvector[k].pos;
405 if (j == seed_1)
406 {
407 *left++ = j;
408 v->spl_nleft++;
409 continue;
410 }
411 else if (j == seed_2)
412 {
413 *right++ = j;
414 v->spl_nright++;
415 continue;
416 }
417 _j = GETENTRY(entryvec, j);
418 size_alpha = hemdist(datum_l, _j, siglen);
419 size_beta = hemdist(datum_r, _j, siglen);
420
421 if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.00001))
422 {
423 if (ISALLTRUE(datum_l) || ISALLTRUE(_j))
424 {
425 if (!ISALLTRUE(datum_l))
426 memset(union_l, 0xff, siglen);
427 }
428 else
429 {
430 ptr = GETSIGN(_j);
431 LOOPBYTE(siglen)
432 union_l[i] |= ptr[i];
433 }
434 *left++ = j;
435 v->spl_nleft++;
436 }
437 else
438 {
439 if (ISALLTRUE(datum_r) || ISALLTRUE(_j))
440 {
441 if (!ISALLTRUE(datum_r))
442 memset(union_r, 0xff, siglen);
443 }
444 else
445 {
446 ptr = GETSIGN(_j);
447 LOOPBYTE(siglen)
448 union_r[i] |= ptr[i];
449 }
450 *right++ = j;
451 v->spl_nright++;
452 }
453 }
454
455 *right = *left = FirstOffsetNumber;
456 pfree(costvector);
457
458 v->spl_ldatum = PointerGetDatum(datum_l);
459 v->spl_rdatum = PointerGetDatum(datum_r);
460
461 PG_RETURN_POINTER(v);
462 }
463
464 Datum
465 g_intbig_consistent(PG_FUNCTION_ARGS)
466 {
467 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
468 ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
469 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
470
471 /* Oid subtype = PG_GETARG_OID(3); */
472 bool *recheck = (bool *) PG_GETARG_POINTER(4);
473 int siglen = GET_SIGLEN();
474 bool retval;
475
476 /* All cases served by this function are inexact */
477 *recheck = true;
478
479 if (ISALLTRUE(DatumGetPointer(entry->key)))
480 PG_RETURN_BOOL(true);
481
482 if (strategy == BooleanSearchStrategy)
483 {
484 retval = signconsistent((QUERYTYPE *) query,
485 GETSIGN(DatumGetPointer(entry->key)),
486 siglen,
487 false);
488 PG_FREE_IF_COPY(query, 1);
489 PG_RETURN_BOOL(retval);
490 }
491
492 CHECKARRVALID(query);
493
494 switch (strategy)
495 {
496 case RTOverlapStrategyNumber:
497 retval = _intbig_overlap((GISTTYPE *) DatumGetPointer(entry->key),
498 query, siglen);
499 break;
500 case RTSameStrategyNumber:
501 if (GIST_LEAF(entry))
502 {
503 int i,
504 num = ARRNELEMS(query);
505 int32 *ptr = ARRPTR(query);
506 BITVECP dq = palloc0(siglen),
507 de;
508
509 while (num--)
510 {
511 HASH(dq, *ptr, siglen);
512 ptr++;
513 }
514
515 de = GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
516 retval = true;
517 LOOPBYTE(siglen)
518 {
519 if (de[i] != dq[i])
520 {
521 retval = false;
522 break;
523 }
524 }
525
526 pfree(dq);
527 }
528 else
529 retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key),
530 query, siglen);
531 break;
532 case RTContainsStrategyNumber:
533 case RTOldContainsStrategyNumber:
534 retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key),
535 query, siglen);
536 break;
537 case RTContainedByStrategyNumber:
538 case RTOldContainedByStrategyNumber:
539
540 /*
541 * This code is unreachable as of intarray 1.4, because the <@
542 * operator has been removed from the opclass. We keep it for now
543 * to support older versions of the SQL definitions.
544 */
545 if (GIST_LEAF(entry))
546 {
547 int i,
548 num = ARRNELEMS(query);
549 int32 *ptr = ARRPTR(query);
550 BITVECP dq = palloc0(siglen),
551 de;
552
553 while (num--)
554 {
555 HASH(dq, *ptr, siglen);
556 ptr++;
557 }
558
559 de = GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
560 retval = true;
561 LOOPBYTE(siglen)
562 {
563 if (de[i] & ~dq[i])
564 {
565 retval = false;
566 break;
567 }
568 }
569 }
570 else
571 {
572 /*
573 * Unfortunately, because empty arrays could be anywhere in
574 * the index, we must search the whole tree.
575 */
576 retval = true;
577 }
578 break;
579 default:
580 retval = false;
581 }
582 PG_FREE_IF_COPY(query, 1);
583 PG_RETURN_BOOL(retval);
584 }
585
586 Datum
587 g_intbig_options(PG_FUNCTION_ARGS)
588 {
589 local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
590
591 init_local_reloptions(relopts, sizeof(GISTIntArrayBigOptions));
592 add_local_int_reloption(relopts, "siglen",
593 "signature length in bytes",
594 SIGLEN_DEFAULT, 1, SIGLEN_MAX,
595 offsetof(GISTIntArrayBigOptions, siglen));
596
597 PG_RETURN_VOID();
598 }