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 3626136 : compareint(const void *va, const void *vb)
127 : {
128 3626136 : int32 a = *((const int32 *) va);
129 3626136 : int32 b = *((const int32 *) vb);
130 :
131 3626136 : return pg_cmp_s32(a, b);
132 : }
133 :
134 : static void
135 74812 : makesign(BITVECP sign, SignTSVector *a, int siglen)
136 : {
137 : int32 k,
138 74812 : len = ARRNELEM(a);
139 74812 : int32 *ptr = GETARR(a);
140 :
141 74812 : MemSet(sign, 0, siglen);
142 4182846 : for (k = 0; k < len; k++)
143 4108034 : HASH(sign, ptr[k], siglen);
144 74812 : }
145 :
146 : static SignTSVector *
147 19430 : gtsvector_alloc(int flag, int len, BITVECP sign)
148 : {
149 19430 : int size = CALCGTSIZE(flag, len);
150 19430 : SignTSVector *res = palloc(size);
151 :
152 19430 : SET_VARSIZE(res, size);
153 19430 : res->flag = flag;
154 :
155 19430 : if ((flag & (SIGNKEY | ALLISTRUE)) == SIGNKEY && sign)
156 816 : memcpy(GETSIGN(res), sign, len);
157 :
158 19430 : return res;
159 : }
160 :
161 :
162 : Datum
163 15708 : gtsvector_compress(PG_FUNCTION_ARGS)
164 : {
165 15708 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
166 15708 : int siglen = GET_SIGLEN();
167 15708 : GISTENTRY *retval = entry;
168 :
169 15708 : if (entry->leafkey)
170 : { /* tsvector */
171 9144 : TSVector val = DatumGetTSVector(entry->key);
172 9144 : SignTSVector *res = gtsvector_alloc(ARRKEY, val->size, NULL);
173 : int32 len;
174 : int32 *arr;
175 9144 : WordEntry *ptr = ARRPTR(val);
176 9144 : char *words = STRPTR(val);
177 :
178 9144 : arr = GETARR(res);
179 9144 : len = val->size;
180 527544 : while (len--)
181 : {
182 : pg_crc32 c;
183 :
184 518400 : INIT_LEGACY_CRC32(c);
185 1555200 : COMP_LEGACY_CRC32(c, words + ptr->pos, ptr->len);
186 518400 : FIN_LEGACY_CRC32(c);
187 :
188 518400 : *arr = *(int32 *) &c;
189 518400 : arr++;
190 518400 : ptr++;
191 : }
192 :
193 9144 : qsort(GETARR(res), val->size, sizeof(int), compareint);
194 9144 : len = qunique(GETARR(res), val->size, sizeof(int), compareint);
195 9144 : 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 9144 : 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 9144 : retval = palloc_object(GISTENTRY);
216 9144 : gistentryinit(*retval, PointerGetDatum(res),
217 : entry->rel, entry->page,
218 : entry->offset, false);
219 : }
220 6564 : else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
221 6564 : !ISALLTRUE(DatumGetPointer(entry->key)))
222 : {
223 : int32 i;
224 : SignTSVector *res;
225 6564 : BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
226 :
227 6936 : LOOPBYTE(siglen)
228 : {
229 6564 : if ((sign[i] & 0xff) != 0xff)
230 6192 : PG_RETURN_POINTER(retval);
231 : }
232 :
233 372 : res = gtsvector_alloc(SIGNKEY | ALLISTRUE, siglen, sign);
234 372 : retval = palloc_object(GISTENTRY);
235 372 : gistentryinit(*retval, PointerGetDatum(res),
236 : entry->rel, entry->page,
237 : entry->offset, false);
238 : }
239 9516 : PG_RETURN_POINTER(retval);
240 : }
241 :
242 : Datum
243 403522 : 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 403522 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
250 403522 : SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(entry->key);
251 :
252 403522 : 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 403522 : 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 415586 : checkcondition_arr(void *checkval, QueryOperand *val, ExecPhraseData *data)
277 : {
278 415586 : int32 *StopLow = ((CHKVAL *) checkval)->arrb;
279 415586 : 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 415586 : if (val->prefix)
288 24384 : return TS_MAYBE;
289 :
290 2514440 : while (StopLow < StopHigh)
291 : {
292 2171702 : StopMiddle = StopLow + (StopHigh - StopLow) / 2;
293 2171702 : if (*StopMiddle == val->valcrc)
294 48464 : return TS_MAYBE;
295 2123238 : else if (*StopMiddle < val->valcrc)
296 902112 : StopLow = StopMiddle + 1;
297 : else
298 1221126 : StopHigh = StopMiddle;
299 : }
300 :
301 342738 : 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 15872 : checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
309 : {
310 15872 : void *key = (SignTSVector *) checkval;
311 :
312 : /*
313 : * we are not able to find a prefix in signature tree
314 : */
315 15872 : if (val->prefix)
316 708 : return TS_MAYBE;
317 :
318 15164 : if (GETBIT(GETSIGN(key), HASHVAL(val->valcrc, GETSIGLEN(key))))
319 14216 : return TS_MAYBE;
320 : else
321 948 : return TS_NO;
322 : }
323 :
324 : Datum
325 301326 : gtsvector_consistent(PG_FUNCTION_ARGS)
326 : {
327 301326 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
328 301326 : 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 301326 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
334 301326 : SignTSVector *key = (SignTSVector *) DatumGetPointer(entry->key);
335 :
336 : /* All cases served by this function are inexact */
337 301326 : *recheck = true;
338 :
339 301326 : if (!query->size)
340 0 : PG_RETURN_BOOL(false);
341 :
342 301326 : if (ISSIGNKEY(key))
343 : {
344 13422 : if (ISALLTRUE(key))
345 4800 : PG_RETURN_BOOL(true);
346 :
347 8622 : 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 287904 : chkval.arrb = GETARR(key);
357 287904 : chkval.arre = chkval.arrb + ARRNELEM(key);
358 287904 : 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 15376 : unionkey(BITVECP sbase, SignTSVector *add, int siglen)
367 : {
368 : int32 i;
369 :
370 15376 : if (ISSIGNKEY(add))
371 : {
372 9098 : BITVECP sadd = GETSIGN(add);
373 :
374 9098 : if (ISALLTRUE(add))
375 2820 : return 1;
376 :
377 : Assert(GETSIGLEN(add) == siglen);
378 :
379 2029630 : LOOPBYTE(siglen)
380 2023352 : sbase[i] |= sadd[i];
381 : }
382 : else
383 : {
384 6278 : int32 *ptr = GETARR(add);
385 :
386 369362 : for (i = 0; i < ARRNELEM(add); i++)
387 363084 : HASH(sbase, ptr[i], siglen);
388 : }
389 12556 : return 0;
390 : }
391 :
392 :
393 : Datum
394 9098 : gtsvector_union(PG_FUNCTION_ARGS)
395 : {
396 9098 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
397 9098 : int *size = (int *) PG_GETARG_POINTER(1);
398 9098 : int siglen = GET_SIGLEN();
399 9098 : SignTSVector *result = gtsvector_alloc(SIGNKEY, siglen, NULL);
400 9098 : BITVECP base = GETSIGN(result);
401 : int32 i;
402 :
403 9098 : memset(base, 0, siglen);
404 :
405 21654 : for (i = 0; i < entryvec->n; i++)
406 : {
407 15376 : if (unionkey(base, GETENTRY(entryvec, i), siglen))
408 : {
409 2820 : result->flag |= ALLISTRUE;
410 2820 : SET_VARSIZE(result, CALCGTSIZE(result->flag, siglen));
411 2820 : break;
412 : }
413 : }
414 :
415 9098 : *size = VARSIZE(result);
416 :
417 9098 : PG_RETURN_POINTER(result);
418 : }
419 :
420 : Datum
421 9098 : gtsvector_same(PG_FUNCTION_ARGS)
422 : {
423 9098 : SignTSVector *a = (SignTSVector *) PG_GETARG_POINTER(0);
424 9098 : SignTSVector *b = (SignTSVector *) PG_GETARG_POINTER(1);
425 9098 : bool *result = (bool *) PG_GETARG_POINTER(2);
426 9098 : int siglen = GET_SIGLEN();
427 :
428 9098 : if (ISSIGNKEY(a))
429 : { /* then b also ISSIGNKEY */
430 9098 : if (ISALLTRUE(a) && ISALLTRUE(b))
431 2820 : *result = true;
432 6278 : else if (ISALLTRUE(a))
433 0 : *result = false;
434 6278 : else if (ISALLTRUE(b))
435 0 : *result = false;
436 : else
437 : {
438 : int32 i;
439 6278 : BITVECP sa = GETSIGN(a),
440 6278 : sb = GETSIGN(b);
441 :
442 : Assert(GETSIGLEN(a) == siglen && GETSIGLEN(b) == siglen);
443 :
444 6278 : *result = true;
445 494504 : LOOPBYTE(siglen)
446 : {
447 493974 : if (sa[i] != sb[i])
448 : {
449 5748 : *result = false;
450 5748 : 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 9098 : PG_RETURN_POINTER(result);
479 : }
480 :
481 : static int32
482 10702 : sizebitvec(BITVECP sign, int siglen)
483 : {
484 10702 : return pg_popcount(sign, siglen);
485 : }
486 :
487 : static int
488 279380 : hemdistsign(BITVECP a, BITVECP b, int siglen)
489 : {
490 : int i,
491 : diff,
492 279380 : dist = 0;
493 :
494 51798412 : LOOPBYTE(siglen)
495 : {
496 51519032 : diff = (unsigned char) (a[i] ^ b[i]);
497 : /* Using the popcount functions here isn't likely to win */
498 51519032 : dist += pg_number_of_ones[diff];
499 : }
500 279380 : 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 62298 : gtsvector_penalty(PG_FUNCTION_ARGS)
526 : {
527 62298 : GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
528 62298 : GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
529 62298 : float *penalty = (float *) PG_GETARG_POINTER(2);
530 62298 : int siglen = GET_SIGLEN();
531 62298 : SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
532 62298 : SignTSVector *newval = (SignTSVector *) DatumGetPointer(newentry->key);
533 62298 : BITVECP orig = GETSIGN(origval);
534 :
535 62298 : *penalty = 0.0;
536 :
537 62298 : if (ISARRKEY(newval))
538 : {
539 62298 : BITVECP sign = palloc(siglen);
540 :
541 62298 : makesign(sign, newval, siglen);
542 :
543 62298 : if (ISALLTRUE(origval))
544 : {
545 10702 : int siglenbit = SIGLENBIT(siglen);
546 :
547 10702 : *penalty =
548 10702 : (float) (siglenbit - sizebitvec(sign, siglen)) /
549 10702 : (float) (siglenbit + 1);
550 : }
551 : else
552 51596 : *penalty = hemdistsign(sign, orig, siglen);
553 :
554 62298 : pfree(sign);
555 : }
556 : else
557 0 : *penalty = hemdist(origval, newval);
558 62298 : PG_RETURN_POINTER(penalty);
559 : }
560 :
561 : typedef struct
562 : {
563 : bool allistrue;
564 : BITVECP sign;
565 : } CACHESIGN;
566 :
567 : static void
568 12604 : fillcache(CACHESIGN *item, SignTSVector *key, int siglen)
569 : {
570 12604 : item->allistrue = false;
571 12604 : if (ISARRKEY(key))
572 12514 : makesign(item->sign, key, siglen);
573 90 : else if (ISALLTRUE(key))
574 0 : item->allistrue = true;
575 : else
576 90 : memcpy(item->sign, GETSIGN(key), siglen);
577 12604 : }
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 31006 : comparecost(const void *va, const void *vb)
588 : {
589 31006 : const SPLITCOST *a = (const SPLITCOST *) va;
590 31006 : const SPLITCOST *b = (const SPLITCOST *) vb;
591 :
592 31006 : return pg_cmp_s32(a->cost, b->cost);
593 : }
594 :
595 :
596 : static int
597 204208 : hemdistcache(CACHESIGN *a, CACHESIGN *b, int siglen)
598 : {
599 204208 : 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 204208 : else if (b->allistrue)
607 0 : return SIGLENBIT(siglen) - sizebitvec(a->sign, siglen);
608 :
609 204208 : return hemdistsign(a->sign, b->sign, siglen);
610 : }
611 :
612 : Datum
613 408 : gtsvector_picksplit(PG_FUNCTION_ARGS)
614 : {
615 408 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
616 408 : GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
617 408 : 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 408 : waste = -1;
628 : int32 nbytes;
629 408 : OffsetNumber seed_1 = 0,
630 408 : 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 408 : maxoff = entryvec->n - 2;
641 408 : nbytes = (maxoff + 2) * sizeof(OffsetNumber);
642 408 : v->spl_left = (OffsetNumber *) palloc(nbytes);
643 408 : v->spl_right = (OffsetNumber *) palloc(nbytes);
644 :
645 408 : cache = palloc_array(CACHESIGN, maxoff + 2);
646 408 : cache_sign = palloc(siglen * (maxoff + 2));
647 :
648 13420 : for (j = 0; j < maxoff + 2; j++)
649 13012 : cache[j].sign = &cache_sign[siglen * j];
650 :
651 408 : fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber),
652 : siglen);
653 :
654 12196 : for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
655 : {
656 190788 : for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
657 : {
658 179000 : if (k == FirstOffsetNumber)
659 11788 : fillcache(&cache[j], GETENTRY(entryvec, j), siglen);
660 :
661 179000 : size_waste = hemdistcache(&(cache[j]), &(cache[k]), siglen);
662 179000 : if (size_waste > waste)
663 : {
664 2588 : waste = size_waste;
665 2588 : seed_1 = k;
666 2588 : seed_2 = j;
667 : }
668 : }
669 : }
670 :
671 408 : left = v->spl_left;
672 408 : v->spl_nleft = 0;
673 408 : right = v->spl_right;
674 408 : v->spl_nright = 0;
675 :
676 408 : if (seed_1 == 0 || seed_2 == 0)
677 : {
678 0 : seed_1 = 1;
679 0 : seed_2 = 2;
680 : }
681 :
682 : /* form initial .. */
683 408 : datum_l = gtsvector_alloc(SIGNKEY | (cache[seed_1].allistrue ? ALLISTRUE : 0),
684 408 : siglen, cache[seed_1].sign);
685 408 : datum_r = gtsvector_alloc(SIGNKEY | (cache[seed_2].allistrue ? ALLISTRUE : 0),
686 408 : siglen, cache[seed_2].sign);
687 408 : union_l = GETSIGN(datum_l);
688 408 : union_r = GETSIGN(datum_r);
689 408 : maxoff = OffsetNumberNext(maxoff);
690 408 : fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen);
691 : /* sort before ... */
692 408 : costvector = palloc_array(SPLITCOST, maxoff);
693 13012 : for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
694 : {
695 12604 : costvector[j - 1].pos = j;
696 12604 : size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]), siglen);
697 12604 : size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]), siglen);
698 12604 : costvector[j - 1].cost = abs(size_alpha - size_beta);
699 : }
700 408 : qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
701 :
702 13012 : for (k = 0; k < maxoff; k++)
703 : {
704 12604 : j = costvector[k].pos;
705 12604 : if (j == seed_1)
706 : {
707 408 : *left++ = j;
708 408 : v->spl_nleft++;
709 408 : continue;
710 : }
711 12196 : else if (j == seed_2)
712 : {
713 408 : *right++ = j;
714 408 : v->spl_nright++;
715 408 : continue;
716 : }
717 :
718 11788 : 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 11788 : size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l), siglen);
731 :
732 11788 : 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 11788 : size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r), siglen);
745 :
746 11788 : if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
747 : {
748 5864 : 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 5864 : ptr = cache[j].sign;
756 975988 : LOOPBYTE(siglen)
757 970124 : union_l[i] |= ptr[i];
758 : }
759 5864 : *left++ = j;
760 5864 : v->spl_nleft++;
761 : }
762 : else
763 : {
764 5924 : 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 5924 : ptr = cache[j].sign;
772 965728 : LOOPBYTE(siglen)
773 959804 : union_r[i] |= ptr[i];
774 : }
775 5924 : *right++ = j;
776 5924 : v->spl_nright++;
777 : }
778 : }
779 :
780 408 : *right = *left = FirstOffsetNumber;
781 408 : v->spl_ldatum = PointerGetDatum(datum_l);
782 408 : v->spl_rdatum = PointerGetDatum(datum_r);
783 :
784 408 : 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 354 : gtsvector_options(PG_FUNCTION_ARGS)
802 : {
803 354 : local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
804 :
805 354 : init_local_reloptions(relopts, sizeof(GistTsVectorOptions));
806 354 : add_local_int_reloption(relopts, "siglen", "signature length",
807 : SIGLEN_DEFAULT, 1, SIGLEN_MAX,
808 : offsetof(GistTsVectorOptions, siglen));
809 :
810 354 : PG_RETURN_VOID();
811 : }
|