PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
_ltree_gist.c File Reference
#include "postgres.h"
#include "access/gist.h"
#include "access/stratnum.h"
#include "crc32.h"
#include "ltree.h"
Include dependency graph for _ltree_gist.c:

Go to the source code of this file.

Data Structures

struct  SPLITCOST
 

Macros

#define GETENTRY(vec, pos)   ((ltree_gist *) DatumGetPointer((vec)->vector[(pos)].key))
 
#define NEXTVAL(x)   ( (ltree*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) )
 
#define WISH_F(a, b, c)   (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
 

Functions

 PG_FUNCTION_INFO_V1 (_ltree_compress)
 
 PG_FUNCTION_INFO_V1 (_ltree_same)
 
 PG_FUNCTION_INFO_V1 (_ltree_union)
 
 PG_FUNCTION_INFO_V1 (_ltree_penalty)
 
 PG_FUNCTION_INFO_V1 (_ltree_picksplit)
 
 PG_FUNCTION_INFO_V1 (_ltree_consistent)
 
static void hashing (BITVECP sign, ltree *t)
 
Datum _ltree_compress (PG_FUNCTION_ARGS)
 
Datum _ltree_same (PG_FUNCTION_ARGS)
 
static int32 unionkey (BITVECP sbase, ltree_gist *add)
 
Datum _ltree_union (PG_FUNCTION_ARGS)
 
static int32 sizebitvec (BITVECP sign)
 
static int hemdistsign (BITVECP a, BITVECP b)
 
static int hemdist (ltree_gist *a, ltree_gist *b)
 
Datum _ltree_penalty (PG_FUNCTION_ARGS)
 
static int comparecost (const void *a, const void *b)
 
Datum _ltree_picksplit (PG_FUNCTION_ARGS)
 
static bool gist_te (ltree_gist *key, ltree *query)
 
static bool checkcondition_bit (void *checkval, ITEM *val)
 
static bool gist_qtxt (ltree_gist *key, ltxtquery *query)
 
static bool gist_qe (ltree_gist *key, lquery *query)
 
static bool _arrq_cons (ltree_gist *key, ArrayType *_query)
 
Datum _ltree_consistent (PG_FUNCTION_ARGS)
 

Variables

static const uint8 number_of_ones [256]
 

Macro Definition Documentation

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

Definition at line 23 of file _ltree_gist.c.

Referenced by _ltree_picksplit(), and _ltree_union().

#define NEXTVAL (   x)    ( (ltree*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) )

Definition at line 24 of file _ltree_gist.c.

Referenced by _arrq_cons(), and _ltree_compress().

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

Definition at line 46 of file _ltree_gist.c.

Referenced by _ltree_picksplit().

Function Documentation

static bool _arrq_cons ( ltree_gist key,
ArrayType _query 
)
static

Definition at line 520 of file _ltree_gist.c.

References ARR_DATA_PTR, ARR_DIMS, ARR_NDIM, array_contains_nulls(), ArrayGetNItems(), ereport, errcode(), errmsg(), ERROR, gist_qe(), and NEXTVAL.

Referenced by _ltree_consistent().

521 {
522  lquery *query = (lquery *) ARR_DATA_PTR(_query);
523  int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
524 
525  if (ARR_NDIM(_query) > 1)
526  ereport(ERROR,
527  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
528  errmsg("array must be one-dimensional")));
529  if (array_contains_nulls(_query))
530  ereport(ERROR,
531  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
532  errmsg("array must not contain nulls")));
533 
534  while (num > 0)
535  {
536  if (gist_qe(key, query))
537  return true;
538  num--;
539  query = (lquery *) NEXTVAL(query);
540  }
541  return false;
542 }
Definition: ltree.h:69
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:275
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ereport(elevel, rest)
Definition: elog.h:122
#define NEXTVAL(x)
Definition: _ltree_gist.c:24
static bool gist_qe(ltree_gist *key, lquery *query)
Definition: _ltree_gist.c:481
#define ARR_NDIM(a)
Definition: array.h:271
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3542
Datum _ltree_compress ( PG_FUNCTION_ARGS  )

Definition at line 66 of file _ltree_gist.c.

References ALOOPBYTE, ARR_DATA_PTR, ARR_DIMS, ARR_NDIM, array_contains_nulls(), ArrayGetNItems(), ASIGLEN, DatumGetArrayTypeP, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, FALSE, ltree_gist::flag, gistentryinit, hashing(), i, GISTENTRY::key, GISTENTRY::leafkey, LTG_ALLTRUE, LTG_HDRSIZE, LTG_ISALLTRUE, LTG_SIGN, MemSet, NEXTVAL, GISTENTRY::offset, GISTENTRY::page, palloc(), palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, GISTENTRY::rel, SET_VARSIZE, sign, and val.

67 {
68  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
69  GISTENTRY *retval = entry;
70 
71  if (entry->leafkey)
72  { /* ltree */
73  ltree_gist *key;
75  int32 len = LTG_HDRSIZE + ASIGLEN;
76  int num = ArrayGetNItems(ARR_NDIM(val), ARR_DIMS(val));
77  ltree *item = (ltree *) ARR_DATA_PTR(val);
78 
79  if (ARR_NDIM(val) > 1)
80  ereport(ERROR,
81  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
82  errmsg("array must be one-dimensional")));
83  if (array_contains_nulls(val))
84  ereport(ERROR,
85  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
86  errmsg("array must not contain nulls")));
87 
88  key = (ltree_gist *) palloc0(len);
89  SET_VARSIZE(key, len);
90  key->flag = 0;
91 
92  MemSet(LTG_SIGN(key), 0, ASIGLEN);
93  while (num > 0)
94  {
95  hashing(LTG_SIGN(key), item);
96  num--;
97  item = NEXTVAL(item);
98  }
99 
100  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
101  gistentryinit(*retval, PointerGetDatum(key),
102  entry->rel, entry->page,
103  entry->offset, FALSE);
104  }
105  else if (!LTG_ISALLTRUE(entry->key))
106  {
107  int32 i,
108  len;
109  ltree_gist *key;
110 
112 
113  ALOOPBYTE
114  {
115  if ((sign[i] & 0xff) != 0xff)
116  PG_RETURN_POINTER(retval);
117  }
118  len = LTG_HDRSIZE;
119  key = (ltree_gist *) palloc0(len);
120  SET_VARSIZE(key, len);
121  key->flag = LTG_ALLTRUE;
122 
123  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
124  gistentryinit(*retval, PointerGetDatum(key),
125  entry->rel, entry->page,
126  entry->offset, FALSE);
127  }
128  PG_RETURN_POINTER(retval);
129 }
Relation rel
Definition: gist.h:124
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define LTG_ISALLTRUE(x)
Definition: ltree.h:222
#define PointerGetDatum(X)
Definition: postgres.h:562
#define LTG_SIGN(x)
Definition: ltree.h:219
uint32 flag
Definition: ltree.h:210
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:858
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
signed int int32
Definition: c.h:256
Page page
Definition: gist.h:125
#define ERROR
Definition: elog.h:43
#define FALSE
Definition: c.h:221
#define ARR_DIMS(a)
Definition: array.h:275
static void hashing(BITVECP sign, ltree *t)
Definition: _ltree_gist.c:50
char sign
Definition: informix.c:693
Datum key
Definition: gist.h:123
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define LTG_ALLTRUE
Definition: ltree.h:215
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: ltree.h:19
bool leafkey
Definition: gist.h:127
void * palloc0(Size size)
Definition: mcxt.c:878
#define NEXTVAL(x)
Definition: _ltree_gist.c:24
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:169
#define ASIGLEN
Definition: ltree.h:235
#define ALOOPBYTE
Definition: ltree.h:239
#define ARR_NDIM(a)
Definition: array.h:271
#define DatumGetPointer(X)
Definition: postgres.h:555
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
char * BITVECP
Definition: hstore_gist.c:20
#define LTG_HDRSIZE
Definition: ltree.h:218
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3542
OffsetNumber offset
Definition: gist.h:126
long val
Definition: informix.c:689
#define DatumGetArrayTypeP(X)
Definition: array.h:242
Datum _ltree_consistent ( PG_FUNCTION_ARGS  )

Definition at line 545 of file _ltree_gist.c.

References _arrq_cons(), DatumGetPointer, elog, ERROR, gist_qe(), gist_qtxt(), gist_te(), GISTENTRY::key, PG_DETOAST_DATUM, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_GETARG_UINT16, and PG_RETURN_BOOL.

546 {
547  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
548  char *query = (char *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
550 
551  /* Oid subtype = PG_GETARG_OID(3); */
552  bool *recheck = (bool *) PG_GETARG_POINTER(4);
553  ltree_gist *key = (ltree_gist *) DatumGetPointer(entry->key);
554  bool res = false;
555 
556  /* All cases served by this function are inexact */
557  *recheck = true;
558 
559  switch (strategy)
560  {
561  case 10:
562  case 11:
563  res = gist_te(key, (ltree *) query);
564  break;
565  case 12:
566  case 13:
567  res = gist_qe(key, (lquery *) query);
568  break;
569  case 14:
570  case 15:
571  res = gist_qtxt(key, (ltxtquery *) query);
572  break;
573  case 16:
574  case 17:
575  res = _arrq_cons(key, (ArrayType *) query);
576  break;
577  default:
578  /* internal error */
579  elog(ERROR, "unrecognized StrategyNumber: %d", strategy);
580  }
581  PG_FREE_IF_COPY(query, 1);
582  PG_RETURN_BOOL(res);
583 }
static bool gist_qtxt(ltree_gist *key, ltxtquery *query)
Definition: _ltree_gist.c:468
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
Definition: ltree.h:69
uint16 StrategyNumber
Definition: stratnum.h:22
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define ERROR
Definition: elog.h:43
static bool gist_te(ltree_gist *key, ltree *query)
Definition: _ltree_gist.c:439
Datum key
Definition: gist.h:123
Definition: ltree.h:19
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static bool _arrq_cons(ltree_gist *key, ArrayType *_query)
Definition: _ltree_gist.c:520
static bool gist_qe(ltree_gist *key, lquery *query)
Definition: _ltree_gist.c:481
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:237
#define DatumGetPointer(X)
Definition: postgres.h:555
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:205
#define elog
Definition: elog.h:219
Datum _ltree_penalty ( PG_FUNCTION_ARGS  )

Definition at line 253 of file _ltree_gist.c.

References DatumGetPointer, hemdist(), newval, PG_GETARG_POINTER, and PG_RETURN_POINTER.

254 {
255  ltree_gist *origval = (ltree_gist *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
257  float *penalty = (float *) PG_GETARG_POINTER(2);
258 
259  *penalty = hemdist(origval, newval);
260  PG_RETURN_POINTER(penalty);
261 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static int hemdist(ltree_gist *a, ltree_gist *b)
Definition: _ltree_gist.c:236
#define newval
#define DatumGetPointer(X)
Definition: postgres.h:555
Datum _ltree_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 276 of file _ltree_gist.c.

References Abs, ALOOPBYTE, ASIGLEN, comparecost(), SPLITCOST::cost, FirstOffsetNumber, ltree_gist::flag, GETENTRY, hemdist(), i, NODE::left, LTG_ALLTRUE, LTG_HDRSIZE, LTG_ISALLTRUE, LTG_SIGN, MemSet, GistEntryVector::n, OffsetNumberNext, palloc(), palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, SPLITCOST::pos, qsort, NODE::right, SET_VARSIZE, 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.

277 {
280  OffsetNumber k,
281  j;
282  ltree_gist *datum_l,
283  *datum_r;
284  BITVECP union_l,
285  union_r;
286  int32 size_alpha,
287  size_beta;
288  int32 size_waste,
289  waste = -1;
290  int32 nbytes;
291  OffsetNumber seed_1 = 0,
292  seed_2 = 0;
293  OffsetNumber *left,
294  *right;
295  OffsetNumber maxoff;
296  BITVECP ptr;
297  int i;
298  SPLITCOST *costvector;
299  ltree_gist *_k,
300  *_j;
301 
302  maxoff = entryvec->n - 2;
303  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
304  v->spl_left = (OffsetNumber *) palloc(nbytes);
305  v->spl_right = (OffsetNumber *) palloc(nbytes);
306 
307  for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
308  {
309  _k = GETENTRY(entryvec, k);
310  for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
311  {
312  size_waste = hemdist(_k, GETENTRY(entryvec, j));
313  if (size_waste > waste)
314  {
315  waste = size_waste;
316  seed_1 = k;
317  seed_2 = j;
318  }
319  }
320  }
321 
322  left = v->spl_left;
323  v->spl_nleft = 0;
324  right = v->spl_right;
325  v->spl_nright = 0;
326 
327  if (seed_1 == 0 || seed_2 == 0)
328  {
329  seed_1 = 1;
330  seed_2 = 2;
331  }
332 
333  /* form initial .. */
334  if (LTG_ISALLTRUE(GETENTRY(entryvec, seed_1)))
335  {
336  datum_l = (ltree_gist *) palloc0(LTG_HDRSIZE);
337  SET_VARSIZE(datum_l, LTG_HDRSIZE);
338  datum_l->flag = LTG_ALLTRUE;
339  }
340  else
341  {
342  datum_l = (ltree_gist *) palloc0(LTG_HDRSIZE + ASIGLEN);
343  SET_VARSIZE(datum_l, LTG_HDRSIZE + ASIGLEN);
344  datum_l->flag = 0;
345  memcpy((void *) LTG_SIGN(datum_l), (void *) LTG_SIGN(GETENTRY(entryvec, seed_1)), sizeof(ABITVEC));
346  }
347  if (LTG_ISALLTRUE(GETENTRY(entryvec, seed_2)))
348  {
349  datum_r = (ltree_gist *) palloc0(LTG_HDRSIZE);
350  SET_VARSIZE(datum_r, LTG_HDRSIZE);
351  datum_r->flag = LTG_ALLTRUE;
352  }
353  else
354  {
355  datum_r = (ltree_gist *) palloc0(LTG_HDRSIZE + ASIGLEN);
356  SET_VARSIZE(datum_r, LTG_HDRSIZE + ASIGLEN);
357  datum_r->flag = 0;
358  memcpy((void *) LTG_SIGN(datum_r), (void *) LTG_SIGN(GETENTRY(entryvec, seed_2)), sizeof(ABITVEC));
359  }
360 
361  maxoff = OffsetNumberNext(maxoff);
362  /* sort before ... */
363  costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
364  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
365  {
366  costvector[j - 1].pos = j;
367  _j = GETENTRY(entryvec, j);
368  size_alpha = hemdist(datum_l, _j);
369  size_beta = hemdist(datum_r, _j);
370  costvector[j - 1].cost = Abs(size_alpha - size_beta);
371  }
372  qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
373 
374  union_l = LTG_SIGN(datum_l);
375  union_r = LTG_SIGN(datum_r);
376 
377  for (k = 0; k < maxoff; k++)
378  {
379  j = costvector[k].pos;
380  if (j == seed_1)
381  {
382  *left++ = j;
383  v->spl_nleft++;
384  continue;
385  }
386  else if (j == seed_2)
387  {
388  *right++ = j;
389  v->spl_nright++;
390  continue;
391  }
392  _j = GETENTRY(entryvec, j);
393  size_alpha = hemdist(datum_l, _j);
394  size_beta = hemdist(datum_r, _j);
395 
396  if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.00001))
397  {
398  if (LTG_ISALLTRUE(datum_l) || LTG_ISALLTRUE(_j))
399  {
400  if (!LTG_ISALLTRUE(datum_l))
401  MemSet((void *) union_l, 0xff, sizeof(ABITVEC));
402  }
403  else
404  {
405  ptr = LTG_SIGN(_j);
406  ALOOPBYTE
407  union_l[i] |= ptr[i];
408  }
409  *left++ = j;
410  v->spl_nleft++;
411  }
412  else
413  {
414  if (LTG_ISALLTRUE(datum_r) || LTG_ISALLTRUE(_j))
415  {
416  if (!LTG_ISALLTRUE(datum_r))
417  MemSet((void *) union_r, 0xff, sizeof(ABITVEC));
418  }
419  else
420  {
421  ptr = LTG_SIGN(_j);
422  ALOOPBYTE
423  union_r[i] |= ptr[i];
424  }
425  *right++ = j;
426  v->spl_nright++;
427  }
428  }
429 
430  *right = *left = FirstOffsetNumber;
431 
432  v->spl_ldatum = PointerGetDatum(datum_l);
433  v->spl_rdatum = PointerGetDatum(datum_r);
434 
436 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
OffsetNumber pos
Definition: hstore_gist.c:323
#define GETENTRY(vec, pos)
Definition: _ltree_gist.c:23
#define LTG_ISALLTRUE(x)
Definition: ltree.h:222
#define PointerGetDatum(X)
Definition: postgres.h:562
#define LTG_SIGN(x)
Definition: ltree.h:219
uint32 flag
Definition: ltree.h:210
OffsetNumber * spl_left
Definition: gist.h:105
Datum spl_rdatum
Definition: gist.h:112
int32 n
Definition: gist.h:160
#define MemSet(start, val, len)
Definition: c.h:858
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
int spl_nleft
Definition: gist.h:106
#define WISH_F(a, b, c)
Definition: _ltree_gist.c:46
signed int int32
Definition: c.h:256
int32 cost
Definition: hstore_gist.c:324
uint16 OffsetNumber
Definition: off.h:24
#define Abs(x)
Definition: c.h:813
int spl_nright
Definition: gist.h:111
static int hemdist(ltree_gist *a, ltree_gist *b)
Definition: _ltree_gist.c:236
#define FirstOffsetNumber
Definition: off.h:27
#define LTG_ALLTRUE
Definition: ltree.h:215
void * palloc0(Size size)
Definition: mcxt.c:878
unsigned char ABITVEC[ASIGLEN]
Definition: ltree.h:237
Datum spl_ldatum
Definition: gist.h:107
#define ASIGLEN
Definition: ltree.h:235
#define ALOOPBYTE
Definition: ltree.h:239
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
OffsetNumber * spl_right
Definition: gist.h:110
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
char * BITVECP
Definition: hstore_gist.c:20
static int comparecost(const void *a, const void *b)
Definition: _ltree_gist.c:270
#define qsort(a, b, c, d)
Definition: port.h:443
#define LTG_HDRSIZE
Definition: ltree.h:218
Datum _ltree_same ( PG_FUNCTION_ARGS  )

Definition at line 132 of file _ltree_gist.c.

References ALOOPBYTE, i, LTG_ISALLTRUE, LTG_SIGN, PG_GETARG_POINTER, PG_RETURN_POINTER, and result.

133 {
136  bool *result = (bool *) PG_GETARG_POINTER(2);
137 
138  if (LTG_ISALLTRUE(a) && LTG_ISALLTRUE(b))
139  *result = true;
140  else if (LTG_ISALLTRUE(a))
141  *result = false;
142  else if (LTG_ISALLTRUE(b))
143  *result = false;
144  else
145  {
146  int32 i;
147  BITVECP sa = LTG_SIGN(a),
148  sb = LTG_SIGN(b);
149 
150  *result = true;
151  ALOOPBYTE
152  {
153  if (sa[i] != sb[i])
154  {
155  *result = false;
156  break;
157  }
158  }
159  }
160  PG_RETURN_POINTER(result);
161 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define LTG_ISALLTRUE(x)
Definition: ltree.h:222
#define LTG_SIGN(x)
Definition: ltree.h:219
return result
Definition: formatting.c:1633
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
signed int int32
Definition: c.h:256
#define ALOOPBYTE
Definition: ltree.h:239
int i
char * BITVECP
Definition: hstore_gist.c:20
Datum _ltree_union ( PG_FUNCTION_ARGS  )

Definition at line 178 of file _ltree_gist.c.

References ASIGLEN, flag(), ltree_gist::flag, GETENTRY, i, LTG_ALLTRUE, LTG_HDRSIZE, LTG_ISALLTRUE, LTG_SIGN, MemSet, GistEntryVector::n, palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, result, SET_VARSIZE, and unionkey().

179 {
181  int *size = (int *) PG_GETARG_POINTER(1);
182  ABITVEC base;
183  int32 i,
184  len;
185  int32 flag = 0;
187 
188  MemSet((void *) base, 0, sizeof(ABITVEC));
189  for (i = 0; i < entryvec->n; i++)
190  {
191  if (unionkey(base, GETENTRY(entryvec, i)))
192  {
193  flag = LTG_ALLTRUE;
194  break;
195  }
196  }
197 
198  len = LTG_HDRSIZE + ((flag & LTG_ALLTRUE) ? 0 : ASIGLEN);
199  result = (ltree_gist *) palloc0(len);
200  SET_VARSIZE(result, len);
201  result->flag = flag;
202  if (!LTG_ISALLTRUE(result))
203  memcpy((void *) LTG_SIGN(result), (void *) base, sizeof(ABITVEC));
204  *size = len;
205 
206  PG_RETURN_POINTER(result);
207 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
static int32 unionkey(BITVECP sbase, ltree_gist *add)
Definition: _ltree_gist.c:164
#define GETENTRY(vec, pos)
Definition: _ltree_gist.c:23
#define LTG_ISALLTRUE(x)
Definition: ltree.h:222
#define LTG_SIGN(x)
Definition: ltree.h:219
uint32 flag
Definition: ltree.h:210
int32 n
Definition: gist.h:160
#define MemSet(start, val, len)
Definition: c.h:858
return result
Definition: formatting.c:1633
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
signed int int32
Definition: c.h:256
char * flag(int b)
Definition: test-ctype.c:33
#define LTG_ALLTRUE
Definition: ltree.h:215
void * palloc0(Size size)
Definition: mcxt.c:878
unsigned char ABITVEC[ASIGLEN]
Definition: ltree.h:237
#define ASIGLEN
Definition: ltree.h:235
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define LTG_HDRSIZE
Definition: ltree.h:218
static bool checkcondition_bit ( void *  checkval,
ITEM val 
)
static

Definition at line 462 of file _ltree_gist.c.

References AHASHVAL, ITEM::flag, FLG_CANLOOKSIGN, GETBIT, and ITEM::val.

Referenced by gist_qtxt().

463 {
464  return (FLG_CANLOOKSIGN(val->flag)) ? GETBIT(checkval, AHASHVAL(val->val)) : true;
465 }
uint8 flag
Definition: ltree.h:96
#define AHASHVAL(val)
Definition: ltree.h:242
#define true
Definition: c.h:206
#define GETBIT(x, i)
Definition: blutils.c:34
int32 val
Definition: _int.h:129
#define FLG_CANLOOKSIGN(x)
Definition: ltree.h:65
static int comparecost ( const void *  a,
const void *  b 
)
static

Definition at line 270 of file _ltree_gist.c.

Referenced by _ltree_picksplit().

271 {
272  return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
273 }
static bool gist_qe ( ltree_gist key,
lquery query 
)
static

Definition at line 481 of file _ltree_gist.c.

References AHASHVAL, GETBIT, LQL_CANLOOKSIGN, LQL_FIRST, LQL_NEXT, LQUERY_FIRST, LTG_ISALLTRUE, LTG_SIGN, LVAR_NEXT, lquery::numlevel, lquery_level::numvar, sign, and lquery_variant::val.

Referenced by _arrq_cons(), and _ltree_consistent().

482 {
483  lquery_level *curq = LQUERY_FIRST(query);
484  BITVECP sign = LTG_SIGN(key);
485  int qlen = query->numlevel;
486 
487  if (LTG_ISALLTRUE(key))
488  return true;
489 
490  while (qlen > 0)
491  {
492  if (curq->numvar && LQL_CANLOOKSIGN(curq))
493  {
494  bool isexist = false;
495  int vlen = curq->numvar;
496  lquery_variant *curv = LQL_FIRST(curq);
497 
498  while (vlen > 0)
499  {
500  if (GETBIT(sign, AHASHVAL(curv->val)))
501  {
502  isexist = true;
503  break;
504  }
505  curv = LVAR_NEXT(curv);
506  vlen--;
507  }
508  if (!isexist)
509  return false;
510  }
511 
512  curq = LQL_NEXT(curq);
513  qlen--;
514  }
515 
516  return true;
517 }
#define LQUERY_FIRST(x)
Definition: ltree.h:79
#define LTG_ISALLTRUE(x)
Definition: ltree.h:222
#define LTG_SIGN(x)
Definition: ltree.h:219
#define LQL_FIRST(x)
Definition: ltree.h:59
#define LQL_NEXT(x)
Definition: ltree.h:58
#define LVAR_NEXT(x)
Definition: ltree.h:41
#define AHASHVAL(val)
Definition: ltree.h:242
#define GETBIT(x, i)
Definition: blutils.c:34
char sign
Definition: informix.c:693
int32 val
Definition: ltree.h:34
uint16 numlevel
Definition: ltree.h:72
uint16 numvar
Definition: ltree.h:51
char * BITVECP
Definition: hstore_gist.c:20
#define LQL_CANLOOKSIGN(x)
Definition: ltree.h:67
static bool gist_qtxt ( ltree_gist key,
ltxtquery query 
)
static

Definition at line 468 of file _ltree_gist.c.

References checkcondition_bit(), GETQUERY, LTG_ISALLTRUE, LTG_SIGN, and ltree_execute().

Referenced by _ltree_consistent().

469 {
470  if (LTG_ISALLTRUE(key))
471  return true;
472 
473  return ltree_execute(
474  GETQUERY(query),
475  (void *) LTG_SIGN(key), false,
477  );
478 }
#define LTG_ISALLTRUE(x)
Definition: ltree.h:222
#define LTG_SIGN(x)
Definition: ltree.h:219
#define GETQUERY(x)
Definition: _int.h:142
bool ltree_execute(ITEM *curitem, void *checkval, bool calcnot, bool(*chkcond)(void *checkval, ITEM *val))
Definition: ltxtquery_op.c:20
static bool checkcondition_bit(void *checkval, ITEM *val)
Definition: _ltree_gist.c:462
static bool gist_te ( ltree_gist key,
ltree query 
)
static

Definition at line 439 of file _ltree_gist.c.

References AHASHVAL, GETBIT, ltree_level::len, LEVEL_NEXT, LTG_ISALLTRUE, LTG_SIGN, ltree_crc32_sz(), LTREE_FIRST, ltree_level::name, ltree::numlevel, and sign.

Referenced by _ltree_consistent().

440 {
441  ltree_level *curq = LTREE_FIRST(query);
442  BITVECP sign = LTG_SIGN(key);
443  int qlen = query->numlevel;
444  unsigned int hv;
445 
446  if (LTG_ISALLTRUE(key))
447  return true;
448 
449  while (qlen > 0)
450  {
451  hv = ltree_crc32_sz(curq->name, curq->len);
452  if (!GETBIT(sign, AHASHVAL(hv)))
453  return false;
454  curq = LEVEL_NEXT(curq);
455  qlen--;
456  }
457 
458  return true;
459 }
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
uint16 len
Definition: ltree.h:12
#define LTG_ISALLTRUE(x)
Definition: ltree.h:222
#define LTG_SIGN(x)
Definition: ltree.h:219
#define AHASHVAL(val)
Definition: ltree.h:242
unsigned int ltree_crc32_sz(char *buf, int size)
Definition: crc32.c:23
#define GETBIT(x, i)
Definition: blutils.c:34
char sign
Definition: informix.c:693
#define LEVEL_NEXT(x)
Definition: ltree.h:17
uint16 numlevel
Definition: ltree.h:22
#define LTREE_FIRST(x)
Definition: ltree.h:27
char * BITVECP
Definition: hstore_gist.c:20
static void hashing ( BITVECP  sign,
ltree t 
)
static

Definition at line 50 of file _ltree_gist.c.

References AHASH, cur, hash(), ltree_level::len, LEVEL_NEXT, ltree_crc32_sz(), LTREE_FIRST, ltree_level::name, and ltree::numlevel.

Referenced by _ltree_compress().

51 {
52  int tlen = t->numlevel;
54  int hash;
55 
56  while (tlen > 0)
57  {
58  hash = ltree_crc32_sz(cur->name, cur->len);
59  AHASH(sign, hash);
60  cur = LEVEL_NEXT(cur);
61  tlen--;
62  }
63 }
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
uint16 len
Definition: ltree.h:12
struct cursor * cur
Definition: ecpg.c:28
unsigned int ltree_crc32_sz(char *buf, int size)
Definition: crc32.c:23
#define AHASH(sign, val)
Definition: ltree.h:243
char sign
Definition: informix.c:693
#define LEVEL_NEXT(x)
Definition: ltree.h:17
uint16 numlevel
Definition: ltree.h:22
#define LTREE_FIRST(x)
Definition: ltree.h:27
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541
static int hemdist ( ltree_gist a,
ltree_gist b 
)
static

Definition at line 236 of file _ltree_gist.c.

References ASIGLENBIT, hemdistsign(), LTG_ISALLTRUE, LTG_SIGN, and sizebitvec().

Referenced by _ltree_penalty(), and _ltree_picksplit().

237 {
238  if (LTG_ISALLTRUE(a))
239  {
240  if (LTG_ISALLTRUE(b))
241  return 0;
242  else
243  return ASIGLENBIT - sizebitvec(LTG_SIGN(b));
244  }
245  else if (LTG_ISALLTRUE(b))
246  return ASIGLENBIT - sizebitvec(LTG_SIGN(a));
247 
248  return hemdistsign(LTG_SIGN(a), LTG_SIGN(b));
249 }
#define LTG_ISALLTRUE(x)
Definition: ltree.h:222
#define LTG_SIGN(x)
Definition: ltree.h:219
#define ASIGLENBIT
Definition: ltree.h:236
static int hemdistsign(BITVECP a, BITVECP b)
Definition: _ltree_gist.c:221
static int32 sizebitvec(BITVECP sign)
Definition: _ltree_gist.c:210
static int hemdistsign ( BITVECP  a,
BITVECP  b 
)
static

Definition at line 221 of file _ltree_gist.c.

References ALOOPBYTE, i, and number_of_ones.

Referenced by hemdist().

222 {
223  int i,
224  diff,
225  dist = 0;
226 
227  ALOOPBYTE
228  {
229  diff = (unsigned char) (a[i] ^ b[i]);
230  dist += number_of_ones[diff];
231  }
232  return dist;
233 }
static const uint8 number_of_ones[256]
Definition: _ltree_gist.c:27
#define ALOOPBYTE
Definition: ltree.h:239
int i
PG_FUNCTION_INFO_V1 ( _ltree_compress  )
PG_FUNCTION_INFO_V1 ( _ltree_same  )
PG_FUNCTION_INFO_V1 ( _ltree_union  )
PG_FUNCTION_INFO_V1 ( _ltree_penalty  )
PG_FUNCTION_INFO_V1 ( _ltree_picksplit  )
PG_FUNCTION_INFO_V1 ( _ltree_consistent  )
static int32 sizebitvec ( BITVECP  sign)
static

Definition at line 210 of file _ltree_gist.c.

References ALOOPBYTE, i, and number_of_ones.

Referenced by hemdist().

211 {
212  int32 size = 0,
213  i;
214 
215  ALOOPBYTE
216  size += number_of_ones[(unsigned char) sign[i]];
217  return size;
218 }
signed int int32
Definition: c.h:256
char sign
Definition: informix.c:693
static const uint8 number_of_ones[256]
Definition: _ltree_gist.c:27
#define ALOOPBYTE
Definition: ltree.h:239
int i
static int32 unionkey ( BITVECP  sbase,
ltree_gist add 
)
static

Definition at line 164 of file _ltree_gist.c.

References ALOOPBYTE, i, LTG_ISALLTRUE, and LTG_SIGN.

Referenced by _ltree_union().

165 {
166  int32 i;
167  BITVECP sadd = LTG_SIGN(add);
168 
169  if (LTG_ISALLTRUE(add))
170  return 1;
171 
172  ALOOPBYTE
173  sbase[i] |= sadd[i];
174  return 0;
175 }
#define LTG_ISALLTRUE(x)
Definition: ltree.h:222
#define LTG_SIGN(x)
Definition: ltree.h:219
signed int int32
Definition: c.h:256
#define ALOOPBYTE
Definition: ltree.h:239
int i
char * BITVECP
Definition: hstore_gist.c:20

Variable Documentation

const uint8 number_of_ones[256]
static
Initial value:
= {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
}

Definition at line 27 of file _ltree_gist.c.

Referenced by hemdistsign(), and sizebitvec().