Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * tsgistidx.c
4 : * GiST support functions for tsvector_ops
5 : *
6 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : *
8 : *
9 : * IDENTIFICATION
10 : * src/backend/utils/adt/tsgistidx.c
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 :
15 : #include "postgres.h"
16 :
17 : #include "access/gist.h"
18 : #include "access/heaptoast.h"
19 : #include "access/reloptions.h"
20 : #include "common/int.h"
21 : #include "lib/qunique.h"
22 : #include "port/pg_bitutils.h"
23 : #include "tsearch/ts_utils.h"
24 : #include "utils/fmgrprotos.h"
25 : #include "utils/pg_crc.h"
26 :
27 :
28 : /* tsvector_ops opclass options */
29 : typedef struct
30 : {
31 : int32 vl_len_; /* varlena header (do not touch directly!) */
32 : int siglen; /* signature length */
33 : } GistTsVectorOptions;
34 :
35 : #define SIGLEN_DEFAULT (31 * 4)
36 : #define SIGLEN_MAX GISTMaxIndexKeySize
37 : #define GET_SIGLEN() (PG_HAS_OPCLASS_OPTIONS() ? \
38 : ((GistTsVectorOptions *) PG_GET_OPCLASS_OPTIONS())->siglen : \
39 : SIGLEN_DEFAULT)
40 :
41 : #define SIGLENBIT(siglen) ((siglen) * BITS_PER_BYTE)
42 :
43 : typedef char *BITVECP;
44 :
45 : #define LOOPBYTE(siglen) \
46 : for (i = 0; i < siglen; i++)
47 :
48 : #define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
49 : #define GETBITBYTE(x,i) ( ((char)(x)) >> (i) & 0x01 )
50 : #define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
51 : #define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) )
52 : #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
53 :
54 : #define HASHVAL(val, siglen) (((unsigned int)(val)) % SIGLENBIT(siglen))
55 : #define HASH(sign, val, siglen) SETBIT((sign), HASHVAL(val, siglen))
56 :
57 : #define GETENTRY(vec,pos) ((SignTSVector *) DatumGetPointer((vec)->vector[(pos)].key))
58 :
59 : /*
60 : * type of GiST index key
61 : */
62 :
63 : typedef struct
64 : {
65 : int32 vl_len_; /* varlena header (do not touch directly!) */
66 : int32 flag;
67 : char data[FLEXIBLE_ARRAY_MEMBER];
68 : } SignTSVector;
69 :
70 : #define ARRKEY 0x01
71 : #define SIGNKEY 0x02
72 : #define ALLISTRUE 0x04
73 :
74 : #define ISARRKEY(x) ( ((SignTSVector*)(x))->flag & ARRKEY )
75 : #define ISSIGNKEY(x) ( ((SignTSVector*)(x))->flag & SIGNKEY )
76 : #define ISALLTRUE(x) ( ((SignTSVector*)(x))->flag & ALLISTRUE )
77 :
78 : #define GTHDRSIZE ( VARHDRSZ + sizeof(int32) )
79 : #define CALCGTSIZE(flag, len) ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int32)) : (((flag) & ALLISTRUE) ? 0 : (len)) ) )
80 :
81 : #define GETSIGN(x) ( (BITVECP)( (char*)(x)+GTHDRSIZE ) )
82 : #define GETSIGLEN(x)( VARSIZE(x) - GTHDRSIZE )
83 : #define GETARR(x) ( (int32*)( (char*)(x)+GTHDRSIZE ) )
84 : #define ARRNELEM(x) ( ( VARSIZE(x) - GTHDRSIZE )/sizeof(int32) )
85 :
86 : static int32 sizebitvec(BITVECP sign, int siglen);
87 :
88 : Datum
89 0 : gtsvectorin(PG_FUNCTION_ARGS)
90 : {
91 : /* There's no need to support input of gtsvectors */
92 0 : ereport(ERROR,
93 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
94 : errmsg("cannot accept a value of type %s", "gtsvector")));
95 :
96 : PG_RETURN_VOID(); /* keep compiler quiet */
97 : }
98 :
99 : Datum
100 0 : gtsvectorout(PG_FUNCTION_ARGS)
101 : {
102 0 : SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
103 : char *outbuf;
104 :
105 0 : if (ISARRKEY(key))
106 0 : outbuf = psprintf("%d unique words", (int) ARRNELEM(key));
107 : else
108 : {
109 0 : if (ISALLTRUE(key))
110 0 : outbuf = pstrdup("all true bits");
111 : else
112 : {
113 0 : int siglen = GETSIGLEN(key);
114 0 : int cnttrue = sizebitvec(GETSIGN(key), siglen);
115 :
116 0 : outbuf = psprintf("%d true bits, %d false bits",
117 0 : cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
118 : }
119 : }
120 :
121 0 : PG_FREE_IF_COPY(key, 0);
122 0 : PG_RETURN_POINTER(outbuf);
123 : }
124 :
125 : static int
126 1813068 : compareint(const void *va, const void *vb)
127 : {
128 1813068 : int32 a = *((const int32 *) va);
129 1813068 : int32 b = *((const int32 *) vb);
130 :
131 1813068 : return pg_cmp_s32(a, b);
132 : }
133 :
134 : static void
135 37672 : makesign(BITVECP sign, SignTSVector *a, int siglen)
136 : {
137 : int32 k,
138 37672 : len = ARRNELEM(a);
139 37672 : int32 *ptr = GETARR(a);
140 :
141 37672 : MemSet(sign, 0, siglen);
142 2104573 : for (k = 0; k < len; k++)
143 2066901 : HASH(sign, ptr[k], siglen);
144 37672 : }
145 :
146 : static SignTSVector *
147 9715 : gtsvector_alloc(int flag, int len, BITVECP sign)
148 : {
149 9715 : int size = CALCGTSIZE(flag, len);
150 9715 : SignTSVector *res = palloc(size);
151 :
152 9715 : SET_VARSIZE(res, size);
153 9715 : res->flag = flag;
154 :
155 9715 : if ((flag & (SIGNKEY | ALLISTRUE)) == SIGNKEY && sign)
156 406 : memcpy(GETSIGN(res), sign, len);
157 :
158 9715 : return res;
159 : }
160 :
161 :
162 : Datum
163 7830 : gtsvector_compress(PG_FUNCTION_ARGS)
164 : {
165 7830 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
166 7830 : int siglen = GET_SIGLEN();
167 7830 : GISTENTRY *retval = entry;
168 :
169 7830 : if (entry->leafkey)
170 : { /* tsvector */
171 4572 : TSVector val = DatumGetTSVector(entry->key);
172 4572 : SignTSVector *res = gtsvector_alloc(ARRKEY, val->size, NULL);
173 : int32 len;
174 : int32 *arr;
175 4572 : WordEntry *ptr = ARRPTR(val);
176 4572 : char *words = STRPTR(val);
177 :
178 4572 : arr = GETARR(res);
179 4572 : len = val->size;
180 263772 : while (len--)
181 : {
182 : pg_crc32 c;
183 :
184 259200 : INIT_LEGACY_CRC32(c);
185 777600 : COMP_LEGACY_CRC32(c, words + ptr->pos, ptr->len);
186 259200 : FIN_LEGACY_CRC32(c);
187 :
188 259200 : *arr = *(int32 *) &c;
189 259200 : arr++;
190 259200 : ptr++;
191 : }
192 :
193 4572 : qsort(GETARR(res), val->size, sizeof(int), compareint);
194 4572 : len = qunique(GETARR(res), val->size, sizeof(int), compareint);
195 4572 : if (len != val->size)
196 : {
197 : /*
198 : * there is a collision of hash-function; len is always less than
199 : * val->size
200 : */
201 0 : len = CALCGTSIZE(ARRKEY, len);
202 0 : res = (SignTSVector *) repalloc(res, len);
203 0 : SET_VARSIZE(res, len);
204 : }
205 :
206 : /* make signature, if array is too long */
207 4572 : if (VARSIZE(res) > TOAST_INDEX_TARGET)
208 : {
209 0 : SignTSVector *ressign = gtsvector_alloc(SIGNKEY, siglen, NULL);
210 :
211 0 : makesign(GETSIGN(ressign), res, siglen);
212 0 : res = ressign;
213 : }
214 :
215 4572 : retval = palloc_object(GISTENTRY);
216 4572 : gistentryinit(*retval, PointerGetDatum(res),
217 : entry->rel, entry->page,
218 : entry->offset, false);
219 : }
220 3258 : else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
221 3258 : !ISALLTRUE(DatumGetPointer(entry->key)))
222 : {
223 : int32 i;
224 : SignTSVector *res;
225 3258 : BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
226 :
227 3446 : LOOPBYTE(siglen)
228 : {
229 3258 : if ((sign[i] & 0xff) != 0xff)
230 3070 : PG_RETURN_POINTER(retval);
231 : }
232 :
233 188 : res = gtsvector_alloc(SIGNKEY | ALLISTRUE, siglen, sign);
234 188 : retval = palloc_object(GISTENTRY);
235 188 : gistentryinit(*retval, PointerGetDatum(res),
236 : entry->rel, entry->page,
237 : entry->offset, false);
238 : }
239 4760 : PG_RETURN_POINTER(retval);
240 : }
241 :
242 : Datum
243 203282 : gtsvector_decompress(PG_FUNCTION_ARGS)
244 : {
245 : /*
246 : * We need to detoast the stored value, because the other gtsvector
247 : * support functions don't cope with toasted values.
248 : */
249 203282 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
250 203282 : SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(entry->key);
251 :
252 203282 : if (key != (SignTSVector *) DatumGetPointer(entry->key))
253 : {
254 0 : GISTENTRY *retval = palloc_object(GISTENTRY);
255 :
256 0 : gistentryinit(*retval, PointerGetDatum(key),
257 : entry->rel, entry->page,
258 : entry->offset, false);
259 :
260 0 : PG_RETURN_POINTER(retval);
261 : }
262 :
263 203282 : PG_RETURN_POINTER(entry);
264 : }
265 :
266 : typedef struct
267 : {
268 : int32 *arrb;
269 : int32 *arre;
270 : } CHKVAL;
271 :
272 : /*
273 : * TS_execute callback for matching a tsquery operand to GIST leaf-page data
274 : */
275 : static TSTernaryValue
276 209530 : checkcondition_arr(void *checkval, QueryOperand *val, ExecPhraseData *data)
277 : {
278 209530 : int32 *StopLow = ((CHKVAL *) checkval)->arrb;
279 209530 : int32 *StopHigh = ((CHKVAL *) checkval)->arre;
280 : int32 *StopMiddle;
281 :
282 : /* Loop invariant: StopLow <= val < StopHigh */
283 :
284 : /*
285 : * we are not able to find a prefix by hash value
286 : */
287 209530 : if (val->prefix)
288 12192 : return TS_MAYBE;
289 :
290 1268910 : while (StopLow < StopHigh)
291 : {
292 1095850 : StopMiddle = StopLow + (StopHigh - StopLow) / 2;
293 1095850 : if (*StopMiddle == val->valcrc)
294 24278 : return TS_MAYBE;
295 1071572 : else if (*StopMiddle < val->valcrc)
296 455950 : StopLow = StopMiddle + 1;
297 : else
298 615622 : StopHigh = StopMiddle;
299 : }
300 :
301 173060 : return TS_NO;
302 : }
303 :
304 : /*
305 : * TS_execute callback for matching a tsquery operand to GIST non-leaf data
306 : */
307 : static TSTernaryValue
308 7868 : checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
309 : {
310 7868 : void *key = (SignTSVector *) checkval;
311 :
312 : /*
313 : * we are not able to find a prefix in signature tree
314 : */
315 7868 : if (val->prefix)
316 350 : return TS_MAYBE;
317 :
318 7518 : if (GETBIT(GETSIGN(key), HASHVAL(val->valcrc, GETSIGLEN(key))))
319 7152 : return TS_MAYBE;
320 : else
321 366 : return TS_NO;
322 : }
323 :
324 : Datum
325 151918 : gtsvector_consistent(PG_FUNCTION_ARGS)
326 : {
327 151918 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
328 151918 : TSQuery query = PG_GETARG_TSQUERY(1);
329 : #ifdef NOT_USED
330 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
331 : Oid subtype = PG_GETARG_OID(3);
332 : #endif
333 151918 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
334 151918 : SignTSVector *key = (SignTSVector *) DatumGetPointer(entry->key);
335 :
336 : /* All cases served by this function are inexact */
337 151918 : *recheck = true;
338 :
339 151918 : if (!query->size)
340 0 : PG_RETURN_BOOL(false);
341 :
342 151918 : if (ISSIGNKEY(key))
343 : {
344 6686 : if (ISALLTRUE(key))
345 2425 : PG_RETURN_BOOL(true);
346 :
347 4261 : PG_RETURN_BOOL(TS_execute(GETQUERY(query),
348 : key,
349 : TS_EXEC_PHRASE_NO_POS,
350 : checkcondition_bit));
351 : }
352 : else
353 : { /* only leaf pages */
354 : CHKVAL chkval;
355 :
356 145232 : chkval.arrb = GETARR(key);
357 145232 : chkval.arre = chkval.arrb + ARRNELEM(key);
358 145232 : PG_RETURN_BOOL(TS_execute(GETQUERY(query),
359 : &chkval,
360 : TS_EXEC_PHRASE_NO_POS,
361 : checkcondition_arr));
362 : }
363 : }
364 :
365 : static int32
366 7688 : unionkey(BITVECP sbase, SignTSVector *add, int siglen)
367 : {
368 : int32 i;
369 :
370 7688 : if (ISSIGNKEY(add))
371 : {
372 4549 : BITVECP sadd = GETSIGN(add);
373 :
374 4549 : if (ISALLTRUE(add))
375 1410 : return 1;
376 :
377 : Assert(GETSIGLEN(add) == siglen);
378 :
379 1014815 : LOOPBYTE(siglen)
380 1011676 : sbase[i] |= sadd[i];
381 : }
382 : else
383 : {
384 3139 : int32 *ptr = GETARR(add);
385 :
386 184608 : for (i = 0; i < ARRNELEM(add); i++)
387 181469 : HASH(sbase, ptr[i], siglen);
388 : }
389 6278 : return 0;
390 : }
391 :
392 :
393 : Datum
394 4549 : gtsvector_union(PG_FUNCTION_ARGS)
395 : {
396 4549 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
397 4549 : int *size = (int *) PG_GETARG_POINTER(1);
398 4549 : int siglen = GET_SIGLEN();
399 4549 : SignTSVector *result = gtsvector_alloc(SIGNKEY, siglen, NULL);
400 4549 : BITVECP base = GETSIGN(result);
401 : int32 i;
402 :
403 4549 : memset(base, 0, siglen);
404 :
405 10827 : for (i = 0; i < entryvec->n; i++)
406 : {
407 7688 : if (unionkey(base, GETENTRY(entryvec, i), siglen))
408 : {
409 1410 : result->flag |= ALLISTRUE;
410 1410 : SET_VARSIZE(result, CALCGTSIZE(result->flag, siglen));
411 1410 : break;
412 : }
413 : }
414 :
415 4549 : *size = VARSIZE(result);
416 :
417 4549 : PG_RETURN_POINTER(result);
418 : }
419 :
420 : Datum
421 4549 : gtsvector_same(PG_FUNCTION_ARGS)
422 : {
423 4549 : SignTSVector *a = (SignTSVector *) PG_GETARG_POINTER(0);
424 4549 : SignTSVector *b = (SignTSVector *) PG_GETARG_POINTER(1);
425 4549 : bool *result = (bool *) PG_GETARG_POINTER(2);
426 4549 : int siglen = GET_SIGLEN();
427 :
428 4549 : if (ISSIGNKEY(a))
429 : { /* then b also ISSIGNKEY */
430 4549 : if (ISALLTRUE(a) && ISALLTRUE(b))
431 1410 : *result = true;
432 3139 : else if (ISALLTRUE(a))
433 0 : *result = false;
434 3139 : else if (ISALLTRUE(b))
435 0 : *result = false;
436 : else
437 : {
438 : int32 i;
439 3139 : BITVECP sa = GETSIGN(a),
440 3139 : sb = GETSIGN(b);
441 :
442 : Assert(GETSIGLEN(a) == siglen && GETSIGLEN(b) == siglen);
443 :
444 3139 : *result = true;
445 253251 : LOOPBYTE(siglen)
446 : {
447 252964 : if (sa[i] != sb[i])
448 : {
449 2852 : *result = false;
450 2852 : break;
451 : }
452 : }
453 : }
454 : }
455 : else
456 : { /* a and b ISARRKEY */
457 0 : int32 lena = ARRNELEM(a),
458 0 : lenb = ARRNELEM(b);
459 :
460 0 : if (lena != lenb)
461 0 : *result = false;
462 : else
463 : {
464 0 : int32 *ptra = GETARR(a),
465 0 : *ptrb = GETARR(b);
466 : int32 i;
467 :
468 0 : *result = true;
469 0 : for (i = 0; i < lena; i++)
470 0 : if (ptra[i] != ptrb[i])
471 : {
472 0 : *result = false;
473 0 : break;
474 : }
475 : }
476 : }
477 :
478 4549 : PG_RETURN_POINTER(result);
479 : }
480 :
481 : static int32
482 5321 : sizebitvec(BITVECP sign, int siglen)
483 : {
484 5321 : return pg_popcount(sign, siglen);
485 : }
486 :
487 : static int
488 140126 : hemdistsign(BITVECP a, BITVECP b, int siglen)
489 : {
490 : int i,
491 : diff,
492 140126 : dist = 0;
493 :
494 25911037 : LOOPBYTE(siglen)
495 : {
496 25770911 : diff = (unsigned char) (a[i] ^ b[i]);
497 : /* Using the popcount functions here isn't likely to win */
498 25770911 : dist += pg_number_of_ones[diff];
499 : }
500 140126 : return dist;
501 : }
502 :
503 : static int
504 0 : hemdist(SignTSVector *a, SignTSVector *b)
505 : {
506 0 : int siglena = GETSIGLEN(a);
507 0 : int siglenb = GETSIGLEN(b);
508 :
509 0 : if (ISALLTRUE(a))
510 : {
511 0 : if (ISALLTRUE(b))
512 0 : return 0;
513 : else
514 0 : return SIGLENBIT(siglenb) - sizebitvec(GETSIGN(b), siglenb);
515 : }
516 0 : else if (ISALLTRUE(b))
517 0 : return SIGLENBIT(siglena) - sizebitvec(GETSIGN(a), siglena);
518 :
519 : Assert(siglena == siglenb);
520 :
521 0 : return hemdistsign(GETSIGN(a), GETSIGN(b), siglena);
522 : }
523 :
524 : Datum
525 31424 : gtsvector_penalty(PG_FUNCTION_ARGS)
526 : {
527 31424 : GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
528 31424 : GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
529 31424 : float *penalty = (float *) PG_GETARG_POINTER(2);
530 31424 : int siglen = GET_SIGLEN();
531 31424 : SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
532 31424 : SignTSVector *newval = (SignTSVector *) DatumGetPointer(newentry->key);
533 31424 : BITVECP orig = GETSIGN(origval);
534 :
535 31424 : *penalty = 0.0;
536 :
537 31424 : if (ISARRKEY(newval))
538 : {
539 31424 : BITVECP sign = palloc(siglen);
540 :
541 31424 : makesign(sign, newval, siglen);
542 :
543 31424 : if (ISALLTRUE(origval))
544 : {
545 5321 : int siglenbit = SIGLENBIT(siglen);
546 :
547 5321 : *penalty =
548 5321 : (float) (siglenbit - sizebitvec(sign, siglen)) /
549 5321 : (float) (siglenbit + 1);
550 : }
551 : else
552 26103 : *penalty = hemdistsign(sign, orig, siglen);
553 :
554 31424 : pfree(sign);
555 : }
556 : else
557 0 : *penalty = hemdist(origval, newval);
558 31424 : PG_RETURN_POINTER(penalty);
559 : }
560 :
561 : typedef struct
562 : {
563 : bool allistrue;
564 : BITVECP sign;
565 : } CACHESIGN;
566 :
567 : static void
568 6293 : fillcache(CACHESIGN *item, SignTSVector *key, int siglen)
569 : {
570 6293 : item->allistrue = false;
571 6293 : if (ISARRKEY(key))
572 6248 : makesign(item->sign, key, siglen);
573 45 : else if (ISALLTRUE(key))
574 0 : item->allistrue = true;
575 : else
576 45 : memcpy(item->sign, GETSIGN(key), siglen);
577 6293 : }
578 :
579 : #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
580 : typedef struct
581 : {
582 : OffsetNumber pos;
583 : int32 cost;
584 : } SPLITCOST;
585 :
586 : static int
587 15522 : comparecost(const void *va, const void *vb)
588 : {
589 15522 : const SPLITCOST *a = (const SPLITCOST *) va;
590 15522 : const SPLITCOST *b = (const SPLITCOST *) vb;
591 :
592 15522 : return pg_cmp_s32(a->cost, b->cost);
593 : }
594 :
595 :
596 : static int
597 102249 : hemdistcache(CACHESIGN *a, CACHESIGN *b, int siglen)
598 : {
599 102249 : if (a->allistrue)
600 : {
601 0 : if (b->allistrue)
602 0 : return 0;
603 : else
604 0 : return SIGLENBIT(siglen) - sizebitvec(b->sign, siglen);
605 : }
606 102249 : else if (b->allistrue)
607 0 : return SIGLENBIT(siglen) - sizebitvec(a->sign, siglen);
608 :
609 102249 : return hemdistsign(a->sign, b->sign, siglen);
610 : }
611 :
612 : Datum
613 203 : gtsvector_picksplit(PG_FUNCTION_ARGS)
614 : {
615 203 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
616 203 : GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
617 203 : int siglen = GET_SIGLEN();
618 : OffsetNumber k,
619 : j;
620 : SignTSVector *datum_l,
621 : *datum_r;
622 : BITVECP union_l,
623 : union_r;
624 : int32 size_alpha,
625 : size_beta;
626 : int32 size_waste,
627 203 : waste = -1;
628 : int32 nbytes;
629 203 : OffsetNumber seed_1 = 0,
630 203 : seed_2 = 0;
631 : OffsetNumber *left,
632 : *right;
633 : OffsetNumber maxoff;
634 : BITVECP ptr;
635 : int i;
636 : CACHESIGN *cache;
637 : char *cache_sign;
638 : SPLITCOST *costvector;
639 :
640 203 : maxoff = entryvec->n - 2;
641 203 : nbytes = (maxoff + 2) * sizeof(OffsetNumber);
642 203 : v->spl_left = (OffsetNumber *) palloc(nbytes);
643 203 : v->spl_right = (OffsetNumber *) palloc(nbytes);
644 :
645 203 : cache = palloc_array(CACHESIGN, maxoff + 2);
646 203 : cache_sign = palloc(siglen * (maxoff + 2));
647 :
648 6699 : for (j = 0; j < maxoff + 2; j++)
649 6496 : cache[j].sign = &cache_sign[siglen * j];
650 :
651 203 : fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber),
652 : siglen);
653 :
654 6090 : for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
655 : {
656 95550 : for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
657 : {
658 89663 : if (k == FirstOffsetNumber)
659 5887 : fillcache(&cache[j], GETENTRY(entryvec, j), siglen);
660 :
661 89663 : size_waste = hemdistcache(&(cache[j]), &(cache[k]), siglen);
662 89663 : if (size_waste > waste)
663 : {
664 1277 : waste = size_waste;
665 1277 : seed_1 = k;
666 1277 : seed_2 = j;
667 : }
668 : }
669 : }
670 :
671 203 : left = v->spl_left;
672 203 : v->spl_nleft = 0;
673 203 : right = v->spl_right;
674 203 : v->spl_nright = 0;
675 :
676 203 : if (seed_1 == 0 || seed_2 == 0)
677 : {
678 0 : seed_1 = 1;
679 0 : seed_2 = 2;
680 : }
681 :
682 : /* form initial .. */
683 203 : datum_l = gtsvector_alloc(SIGNKEY | (cache[seed_1].allistrue ? ALLISTRUE : 0),
684 203 : siglen, cache[seed_1].sign);
685 203 : datum_r = gtsvector_alloc(SIGNKEY | (cache[seed_2].allistrue ? ALLISTRUE : 0),
686 203 : siglen, cache[seed_2].sign);
687 203 : union_l = GETSIGN(datum_l);
688 203 : union_r = GETSIGN(datum_r);
689 203 : maxoff = OffsetNumberNext(maxoff);
690 203 : fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen);
691 : /* sort before ... */
692 203 : costvector = palloc_array(SPLITCOST, maxoff);
693 6496 : for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
694 : {
695 6293 : costvector[j - 1].pos = j;
696 6293 : size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]), siglen);
697 6293 : size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]), siglen);
698 6293 : costvector[j - 1].cost = abs(size_alpha - size_beta);
699 : }
700 203 : qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
701 :
702 6496 : for (k = 0; k < maxoff; k++)
703 : {
704 6293 : j = costvector[k].pos;
705 6293 : if (j == seed_1)
706 : {
707 203 : *left++ = j;
708 203 : v->spl_nleft++;
709 203 : continue;
710 : }
711 6090 : else if (j == seed_2)
712 : {
713 203 : *right++ = j;
714 203 : v->spl_nright++;
715 203 : continue;
716 : }
717 :
718 5887 : if (ISALLTRUE(datum_l) || cache[j].allistrue)
719 : {
720 0 : if (ISALLTRUE(datum_l) && cache[j].allistrue)
721 0 : size_alpha = 0;
722 : else
723 0 : size_alpha = SIGLENBIT(siglen) -
724 0 : sizebitvec((cache[j].allistrue) ?
725 : GETSIGN(datum_l) :
726 0 : cache[j].sign,
727 : siglen);
728 : }
729 : else
730 5887 : size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l), siglen);
731 :
732 5887 : if (ISALLTRUE(datum_r) || cache[j].allistrue)
733 : {
734 0 : if (ISALLTRUE(datum_r) && cache[j].allistrue)
735 0 : size_beta = 0;
736 : else
737 0 : size_beta = SIGLENBIT(siglen) -
738 0 : sizebitvec((cache[j].allistrue) ?
739 : GETSIGN(datum_r) :
740 0 : cache[j].sign,
741 : siglen);
742 : }
743 : else
744 5887 : size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r), siglen);
745 :
746 5887 : if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
747 : {
748 2941 : if (ISALLTRUE(datum_l) || cache[j].allistrue)
749 : {
750 0 : if (!ISALLTRUE(datum_l))
751 0 : memset(GETSIGN(datum_l), 0xff, siglen);
752 : }
753 : else
754 : {
755 2941 : ptr = cache[j].sign;
756 482015 : LOOPBYTE(siglen)
757 479074 : union_l[i] |= ptr[i];
758 : }
759 2941 : *left++ = j;
760 2941 : v->spl_nleft++;
761 : }
762 : else
763 : {
764 2946 : if (ISALLTRUE(datum_r) || cache[j].allistrue)
765 : {
766 0 : if (!ISALLTRUE(datum_r))
767 0 : memset(GETSIGN(datum_r), 0xff, siglen);
768 : }
769 : else
770 : {
771 2946 : ptr = cache[j].sign;
772 474225 : LOOPBYTE(siglen)
773 471279 : union_r[i] |= ptr[i];
774 : }
775 2946 : *right++ = j;
776 2946 : v->spl_nright++;
777 : }
778 : }
779 :
780 203 : *right = *left = FirstOffsetNumber;
781 203 : v->spl_ldatum = PointerGetDatum(datum_l);
782 203 : v->spl_rdatum = PointerGetDatum(datum_r);
783 :
784 203 : PG_RETURN_POINTER(v);
785 : }
786 :
787 : /*
788 : * Formerly, gtsvector_consistent was declared in pg_proc.h with arguments
789 : * that did not match the documented conventions for GiST support functions.
790 : * We fixed that, but we still need a pg_proc entry with the old signature
791 : * to support reloading pre-9.6 contrib/tsearch2 opclass declarations.
792 : * This compatibility function should go away eventually.
793 : */
794 : Datum
795 0 : gtsvector_consistent_oldsig(PG_FUNCTION_ARGS)
796 : {
797 0 : return gtsvector_consistent(fcinfo);
798 : }
799 :
800 : Datum
801 177 : gtsvector_options(PG_FUNCTION_ARGS)
802 : {
803 177 : local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
804 :
805 177 : init_local_reloptions(relopts, sizeof(GistTsVectorOptions));
806 177 : add_local_int_reloption(relopts, "siglen", "signature length",
807 : SIGLEN_DEFAULT, 1, SIGLEN_MAX,
808 : offsetof(GistTsVectorOptions, siglen));
809 :
810 177 : PG_RETURN_VOID();
811 : }
|