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  rix
 

Macros

#define NEXTVAL(x)   ( (lquery*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) )
 
#define ISEQ(a, b)   ( (a)->numlevel == (b)->numlevel && ltree_compare(a,b)==0 )
 
#define GETENTRY(vec, pos)   ((ltree_gist *) DatumGetPointer((vec)->vector[(pos)].key))
 

Typedefs

typedef struct rix RIX
 

Functions

 PG_FUNCTION_INFO_V1 (ltree_gist_in)
 
 PG_FUNCTION_INFO_V1 (ltree_gist_out)
 
Datum ltree_gist_in (PG_FUNCTION_ARGS)
 
Datum ltree_gist_out (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (ltree_compress)
 
 PG_FUNCTION_INFO_V1 (ltree_decompress)
 
 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)
 
Datum ltree_compress (PG_FUNCTION_ARGS)
 
Datum ltree_decompress (PG_FUNCTION_ARGS)
 
Datum ltree_same (PG_FUNCTION_ARGS)
 
static void hashing (BITVECP sign, ltree *t)
 
Datum ltree_union (PG_FUNCTION_ARGS)
 
Datum ltree_penalty (PG_FUNCTION_ARGS)
 
static int treekey_cmp (const void *a, const void *b)
 
Datum ltree_picksplit (PG_FUNCTION_ARGS)
 
static bool gist_isparent (ltree_gist *key, ltree *query)
 
static ltreecopy_ltree (ltree *src)
 
static bool gist_ischild (ltree_gist *key, ltree *query)
 
static bool gist_qe (ltree_gist *key, lquery *query)
 
static int gist_tqcmp (ltree *t, lquery *q)
 
static bool gist_between (ltree_gist *key, lquery *query)
 
static bool checkcondition_bit (void *checkval, ITEM *val)
 
static bool gist_qtxt (ltree_gist *key, ltxtquery *query)
 
static bool arrq_cons (ltree_gist *key, ArrayType *_query)
 
Datum ltree_consistent (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

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

Definition at line 45 of file ltree_gist.c.

Referenced by ltree_picksplit(), and ltree_union().

#define ISEQ (   a,
 
)    ( (a)->numlevel == (b)->numlevel && ltree_compare(a,b)==0 )

Definition at line 44 of file ltree_gist.c.

Referenced by ltree_picksplit(), ltree_same(), and ltree_union().

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

Definition at line 13 of file ltree_gist.c.

Referenced by arrq_cons().

Typedef Documentation

typedef struct rix RIX

Function Documentation

static bool arrq_cons ( ltree_gist key,
ArrayType _query 
)
static

Definition at line 582 of file ltree_gist.c.

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

Referenced by ltree_consistent().

583 {
584  lquery *query = (lquery *) ARR_DATA_PTR(_query);
585  int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
586 
587  if (ARR_NDIM(_query) > 1)
588  ereport(ERROR,
589  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
590  errmsg("array must be one-dimensional")));
591  if (array_contains_nulls(_query))
592  ereport(ERROR,
593  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
594  errmsg("array must not contain nulls")));
595 
596  while (num > 0)
597  {
598  if (gist_qe(key, query) && gist_between(key, query))
599  return true;
600  num--;
601  query = NEXTVAL(query);
602  }
603  return false;
604 }
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:279
#define ARR_DATA_PTR(a)
Definition: array.h:307
#define ereport(elevel, rest)
Definition: elog.h:122
static bool gist_qe(ltree_gist *key, lquery *query)
Definition: ltree_gist.c:480
static bool gist_between(ltree_gist *key, lquery *query)
Definition: ltree_gist.c:548
#define NEXTVAL(x)
Definition: ltree_gist.c:13
#define ARR_NDIM(a)
Definition: array.h:275
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3516
static bool checkcondition_bit ( void *  checkval,
ITEM val 
)
static

Definition at line 563 of file ltree_gist.c.

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

Referenced by gist_qtxt().

564 {
565  return (FLG_CANLOOKSIGN(val->flag)) ? GETBIT(checkval, HASHVAL(val->val)) : true;
566 }
uint8 flag
Definition: ltree.h:96
#define true
Definition: c.h:206
#define GETBIT(x, i)
Definition: blutils.c:34
int32 val
Definition: _int.h:129
#define HASHVAL(val)
Definition: hstore_gist.c:37
#define FLG_CANLOOKSIGN(x)
Definition: ltree.h:65
static ltree* copy_ltree ( ltree src)
static

Definition at line 446 of file ltree_gist.c.

References palloc0(), and VARSIZE.

Referenced by gist_ischild().

447 {
448  ltree *dst = (ltree *) palloc0(VARSIZE(src));
449 
450  memcpy(dst, src, VARSIZE(src));
451  return dst;
452 }
#define VARSIZE(PTR)
Definition: postgres.h:304
Definition: ltree.h:19
void * palloc0(Size size)
Definition: mcxt.c:877
static bool gist_between ( ltree_gist key,
lquery query 
)
static

Definition at line 548 of file ltree_gist.c.

References lquery::firstgood, gist_tqcmp(), LTG_GETLNODE, and LTG_GETRNODE.

Referenced by arrq_cons(), and ltree_consistent().

549 {
550  if (query->firstgood == 0)
551  return true;
552 
553  if (gist_tqcmp(LTG_GETLNODE(key), query) > 0)
554  return false;
555 
556  if (gist_tqcmp(LTG_GETRNODE(key), query) < 0)
557  return false;
558 
559  return true;
560 }
uint16 firstgood
Definition: ltree.h:73
#define LTG_GETRNODE(x)
Definition: ltree.h:238
#define LTG_GETLNODE(x)
Definition: ltree.h:237
static int gist_tqcmp(ltree *t, lquery *q)
Definition: ltree_gist.c:519
static bool gist_ischild ( ltree_gist key,
ltree query 
)
static

Definition at line 455 of file ltree_gist.c.

References copy_ltree(), LTG_GETLNODE, LTG_GETRNODE, ltree_compare(), ltree::numlevel, and pfree().

Referenced by ltree_consistent().

456 {
457  ltree *left = copy_ltree(LTG_GETLNODE(key));
458  ltree *right = copy_ltree(LTG_GETRNODE(key));
459  bool res = true;
460 
461  if (left->numlevel > query->numlevel)
462  left->numlevel = query->numlevel;
463 
464  if (ltree_compare(query, left) < 0)
465  res = false;
466 
467  if (right->numlevel > query->numlevel)
468  right->numlevel = query->numlevel;
469 
470  if (res && ltree_compare(query, right) > 0)
471  res = false;
472 
473  pfree(left);
474  pfree(right);
475 
476  return res;
477 }
void pfree(void *pointer)
Definition: mcxt.c:949
static ltree * copy_ltree(ltree *src)
Definition: ltree_gist.c:446
#define LTG_GETRNODE(x)
Definition: ltree.h:238
#define LTG_GETLNODE(x)
Definition: ltree.h:237
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42
static bool gist_isparent ( ltree_gist key,
ltree query 
)
static

Definition at line 426 of file ltree_gist.c.

References i, LTG_GETLNODE, LTG_GETRNODE, ltree_compare(), and ltree::numlevel.

Referenced by ltree_consistent().

427 {
428  int32 numlevel = query->numlevel;
429  int i;
430 
431  for (i = query->numlevel; i >= 0; i--)
432  {
433  query->numlevel = i;
434  if (ltree_compare(query, LTG_GETLNODE(key)) >= 0 && ltree_compare(query, LTG_GETRNODE(key)) <= 0)
435  {
436  query->numlevel = numlevel;
437  return true;
438  }
439  }
440 
441  query->numlevel = numlevel;
442  return false;
443 }
signed int int32
Definition: c.h:246
#define LTG_GETRNODE(x)
Definition: ltree.h:238
#define LTG_GETLNODE(x)
Definition: ltree.h:237
uint16 numlevel
Definition: ltree.h:22
int i
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42
static bool gist_qe ( ltree_gist key,
lquery query 
)
static

Definition at line 480 of file ltree_gist.c.

References GETBIT, HASHVAL, 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().

481 {
482  lquery_level *curq = LQUERY_FIRST(query);
483  BITVECP sign = LTG_SIGN(key);
484  int qlen = query->numlevel;
485 
486  if (LTG_ISALLTRUE(key))
487  return true;
488 
489  while (qlen > 0)
490  {
491  if (curq->numvar && LQL_CANLOOKSIGN(curq))
492  {
493  bool isexist = false;
494  int vlen = curq->numvar;
495  lquery_variant *curv = LQL_FIRST(curq);
496 
497  while (vlen > 0)
498  {
499  if (GETBIT(sign, HASHVAL(curv->val)))
500  {
501  isexist = true;
502  break;
503  }
504  curv = LVAR_NEXT(curv);
505  vlen--;
506  }
507  if (!isexist)
508  return false;
509  }
510 
511  curq = LQL_NEXT(curq);
512  qlen--;
513  }
514 
515  return true;
516 }
#define LQUERY_FIRST(x)
Definition: ltree.h:79
#define LTG_ISALLTRUE(x)
Definition: ltree.h:231
#define LTG_SIGN(x)
Definition: ltree.h:228
#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 GETBIT(x, i)
Definition: blutils.c:34
char sign
Definition: informix.c:693
#define HASHVAL(val)
Definition: hstore_gist.c:37
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 569 of file ltree_gist.c.

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

Referenced by ltree_consistent().

570 {
571  if (LTG_ISALLTRUE(key))
572  return true;
573 
574  return ltree_execute(
575  GETQUERY(query),
576  (void *) LTG_SIGN(key), false,
578  );
579 }
#define LTG_ISALLTRUE(x)
Definition: ltree.h:231
#define LTG_SIGN(x)
Definition: ltree.h:228
#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:563
static int gist_tqcmp ( ltree t,
lquery q 
)
static

Definition at line 519 of file ltree_gist.c.

References lquery::firstgood, ltree_level::len, lquery_variant::len, LEVEL_NEXT, LQL_FIRST, LQL_NEXT, LQUERY_FIRST, LTREE_FIRST, Min, ltree_level::name, lquery_variant::name, and ltree::numlevel.

Referenced by gist_between().

520 {
521  ltree_level *al = LTREE_FIRST(t);
522  lquery_level *ql = LQUERY_FIRST(q);
523  lquery_variant *bl;
524  int an = t->numlevel;
525  int bn = q->firstgood;
526  int res = 0;
527 
528  while (an > 0 && bn > 0)
529  {
530  bl = LQL_FIRST(ql);
531  if ((res = memcmp(al->name, bl->name, Min(al->len, bl->len))) == 0)
532  {
533  if (al->len != bl->len)
534  return al->len - bl->len;
535  }
536  else
537  return res;
538  an--;
539  bn--;
540  al = LEVEL_NEXT(al);
541  ql = LQL_NEXT(ql);
542  }
543 
544  return Min(t->numlevel, q->firstgood) - q->firstgood;
545 }
uint16 firstgood
Definition: ltree.h:73
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
#define LQUERY_FIRST(x)
Definition: ltree.h:79
uint16 len
Definition: ltree.h:35
uint16 len
Definition: ltree.h:12
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:37
#define Min(x, y)
Definition: c.h:812
#define LQL_FIRST(x)
Definition: ltree.h:59
#define LQL_NEXT(x)
Definition: ltree.h:58
#define LEVEL_NEXT(x)
Definition: ltree.h:17
uint16 numlevel
Definition: ltree.h:22
#define LTREE_FIRST(x)
Definition: ltree.h:27
static void hashing ( BITVECP  sign,
ltree t 
)
static

Definition at line 135 of file ltree_gist.c.

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

Referenced by ltree_picksplit(), and ltree_union().

136 {
137  int tlen = t->numlevel;
139  int hash;
140 
141  while (tlen > 0)
142  {
143  hash = ltree_crc32_sz(cur->name, cur->len);
144  HASH(sign, hash);
145  cur = LEVEL_NEXT(cur);
146  tlen--;
147  }
148 }
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
uint16 len
Definition: ltree.h:12
struct cursor * cur
Definition: ecpg.c:28
#define HASH(sign, val)
Definition: hstore_gist.c:38
unsigned int ltree_crc32_sz(char *buf, int size)
Definition: crc32.c:23
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
Datum ltree_compress ( PG_FUNCTION_ARGS  )

Definition at line 48 of file ltree_gist.c.

References DatumGetLtreeP, FALSE, ltree_gist::flag, gistentryinit, GISTENTRY::key, GISTENTRY::leafkey, LTG_HDRSIZE, LTG_NODE, LTG_ONENODE, GISTENTRY::offset, GISTENTRY::page, palloc(), palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, GISTENTRY::rel, SET_VARSIZE, val, and VARSIZE.

49 {
50  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
51  GISTENTRY *retval = entry;
52 
53  if (entry->leafkey)
54  { /* ltree */
55  ltree_gist *key;
56  ltree *val = DatumGetLtreeP(entry->key);
57  int32 len = LTG_HDRSIZE + VARSIZE(val);
58 
59  key = (ltree_gist *) palloc0(len);
60  SET_VARSIZE(key, len);
61  key->flag = LTG_ONENODE;
62  memcpy((void *) LTG_NODE(key), (void *) val, VARSIZE(val));
63 
64  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
65  gistentryinit(*retval, PointerGetDatum(key),
66  entry->rel, entry->page,
67  entry->offset, FALSE);
68  }
69  PG_RETURN_POINTER(retval);
70 }
Relation rel
Definition: gist.h:124
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define VARSIZE(PTR)
Definition: postgres.h:304
#define PointerGetDatum(X)
Definition: postgres.h:562
uint32 flag
Definition: ltree.h:219
#define LTG_NODE(x)
Definition: ltree.h:229
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
signed int int32
Definition: c.h:246
Page page
Definition: gist.h:125
#define FALSE
Definition: c.h:219
Datum key
Definition: gist.h:123
Definition: ltree.h:19
bool leafkey
Definition: gist.h:127
#define DatumGetLtreeP(X)
Definition: ltree.h:169
void * palloc0(Size size)
Definition: mcxt.c:877
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:169
void * palloc(Size size)
Definition: mcxt.c:848
#define LTG_ONENODE
Definition: ltree.h:223
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define LTG_HDRSIZE
Definition: ltree.h:227
OffsetNumber offset
Definition: gist.h:126
long val
Definition: informix.c:689
Datum ltree_consistent ( PG_FUNCTION_ARGS  )

Definition at line 607 of file ltree_gist.c.

References arrq_cons(), BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, DatumGetBool, DatumGetPointer, DirectFunctionCall2, elog, ERROR, gist_between(), gist_ischild(), gist_isparent(), GIST_LEAF, gist_qe(), gist_qtxt(), inner_isparent(), GISTENTRY::key, lt_q_regex(), LTG_GETLNODE, LTG_GETRNODE, LTG_NODE, ltq_regex(), ltree_compare(), ltxtq_exec(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_LQUERY_P, PG_GETARG_LTREE_P, PG_GETARG_LTREE_P_COPY, PG_GETARG_LTXTQUERY_P, PG_GETARG_POINTER, PG_GETARG_UINT16, PG_RETURN_BOOL, and PointerGetDatum.

608 {
609  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
611 
612  /* Oid subtype = PG_GETARG_OID(3); */
613  bool *recheck = (bool *) PG_GETARG_POINTER(4);
614  ltree_gist *key = (ltree_gist *) DatumGetPointer(entry->key);
615  void *query = NULL;
616  bool res = false;
617 
618  /* All cases served by this function are exact */
619  *recheck = false;
620 
621  switch (strategy)
622  {
624  query = PG_GETARG_LTREE_P(1);
625  res = (GIST_LEAF(entry)) ?
626  (ltree_compare((ltree *) query, LTG_NODE(key)) > 0)
627  :
628  (ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0);
629  break;
631  query = PG_GETARG_LTREE_P(1);
632  res = (ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0);
633  break;
635  query = PG_GETARG_LTREE_P(1);
636  if (GIST_LEAF(entry))
637  res = (ltree_compare((ltree *) query, LTG_NODE(key)) == 0);
638  else
639  res = (
640  ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0
641  &&
642  ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0
643  );
644  break;
646  query = PG_GETARG_LTREE_P(1);
647  res = (ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0);
648  break;
650  query = PG_GETARG_LTREE_P(1);
651  res = (GIST_LEAF(entry)) ?
652  (ltree_compare((ltree *) query, LTG_GETRNODE(key)) < 0)
653  :
654  (ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0);
655  break;
656  case 10:
657  query = PG_GETARG_LTREE_P_COPY(1);
658  res = (GIST_LEAF(entry)) ?
659  inner_isparent((ltree *) query, LTG_NODE(key))
660  :
661  gist_isparent(key, (ltree *) query);
662  break;
663  case 11:
664  query = PG_GETARG_LTREE_P(1);
665  res = (GIST_LEAF(entry)) ?
666  inner_isparent(LTG_NODE(key), (ltree *) query)
667  :
668  gist_ischild(key, (ltree *) query);
669  break;
670  case 12:
671  case 13:
672  query = PG_GETARG_LQUERY_P(1);
673  if (GIST_LEAF(entry))
676  PointerGetDatum((lquery *) query)
677  ));
678  else
679  res = (gist_qe(key, (lquery *) query) && gist_between(key, (lquery *) query));
680  break;
681  case 14:
682  case 15:
683  query = PG_GETARG_LTXTQUERY_P(1);
684  if (GIST_LEAF(entry))
687  PointerGetDatum((ltxtquery *) query)
688  ));
689  else
690  res = gist_qtxt(key, (ltxtquery *) query);
691  break;
692  case 16:
693  case 17:
694  query = PG_GETARG_ARRAYTYPE_P(1);
695  if (GIST_LEAF(entry))
698  PointerGetDatum((ArrayType *) query)
699  ));
700  else
701  res = arrq_cons(key, (ArrayType *) query);
702  break;
703  default:
704  /* internal error */
705  elog(ERROR, "unrecognized StrategyNumber: %d", strategy);
706  }
707 
708  PG_FREE_IF_COPY(query, 1);
709  PG_RETURN_BOOL(res);
710 }
#define GIST_LEAF(entry)
Definition: gist.h:133
#define PG_GETARG_LTREE_P_COPY(n)
Definition: ltree.h:172
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define PointerGetDatum(X)
Definition: postgres.h:562
bool inner_isparent(const ltree *c, const ltree *p)
Definition: ltree_op.c:136
Definition: ltree.h:69
static bool gist_ischild(ltree_gist *key, ltree *query)
Definition: ltree_gist.c:455
uint16 StrategyNumber
Definition: stratnum.h:22
#define LTG_NODE(x)
Definition: ltree.h:229
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static bool gist_isparent(ltree_gist *key, ltree *query)
Definition: ltree_gist.c:426
#define PG_GETARG_LQUERY_P(n)
Definition: ltree.h:176
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
static bool arrq_cons(ltree_gist *key, ArrayType *_query)
Definition: ltree_gist.c:582
#define ERROR
Definition: elog.h:43
#define LTG_GETRNODE(x)
Definition: ltree.h:238
#define LTG_GETLNODE(x)
Definition: ltree.h:237
Datum ltq_regex(PG_FUNCTION_ARGS)
Definition: lquery_op.c:303
Datum key
Definition: gist.h:123
#define DatumGetBool(X)
Definition: postgres.h:399
Definition: ltree.h:19
Datum lt_q_regex(PG_FUNCTION_ARGS)
Definition: lquery_op.c:339
static bool gist_qe(ltree_gist *key, lquery *query)
Definition: ltree_gist.c:480
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static bool gist_between(ltree_gist *key, lquery *query)
Definition: ltree_gist.c:548
Datum ltxtq_exec(PG_FUNCTION_ARGS)
Definition: ltxtquery_op.c:87
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_GETARG_LTXTQUERY_P(n)
Definition: ltree.h:181
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:237
#define DatumGetPointer(X)
Definition: postgres.h:555
static bool gist_qtxt(ltree_gist *key, ltxtquery *query)
Definition: ltree_gist.c:569
#define elog
Definition: elog.h:219
#define BTLessStrategyNumber
Definition: stratnum.h:29
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
Datum ltree_decompress ( PG_FUNCTION_ARGS  )

Definition at line 73 of file ltree_gist.c.

References FALSE, gistentryinit, GISTENTRY::key, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_DETOAST_DATUM, PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, and GISTENTRY::rel.

74 {
75  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
76  ltree_gist *key = (ltree_gist *) PG_DETOAST_DATUM(entry->key);
77 
78  if (PointerGetDatum(key) != entry->key)
79  {
80  GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
81 
82  gistentryinit(*retval, PointerGetDatum(key),
83  entry->rel, entry->page,
84  entry->offset, FALSE);
85  PG_RETURN_POINTER(retval);
86  }
87  PG_RETURN_POINTER(entry);
88 }
Relation rel
Definition: gist.h:124
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
Page page
Definition: gist.h:125
#define FALSE
Definition: c.h:219
Datum key
Definition: gist.h:123
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:169
void * palloc(Size size)
Definition: mcxt.c:848
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:205
OffsetNumber offset
Definition: gist.h:126
Datum ltree_gist_in ( PG_FUNCTION_ARGS  )

Definition at line 19 of file ltree_gist.c.

References ereport, errcode(), errmsg(), ERROR, and PG_RETURN_DATUM.

20 {
21  ereport(ERROR,
22  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
23  errmsg("ltree_gist_in() not implemented")));
24  PG_RETURN_DATUM(0);
25 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum ltree_gist_out ( PG_FUNCTION_ARGS  )

Definition at line 28 of file ltree_gist.c.

References ereport, errcode(), errmsg(), ERROR, and PG_RETURN_DATUM.

29 {
30  ereport(ERROR,
31  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
32  errmsg("ltree_gist_out() not implemented")));
33  PG_RETURN_DATUM(0);
34 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum ltree_penalty ( PG_FUNCTION_ARGS  )

Definition at line 235 of file ltree_gist.c.

References DatumGetPointer, LTG_GETLNODE, LTG_GETRNODE, ltree_compare(), Max, newval, PG_GETARG_POINTER, and PG_RETURN_POINTER.

236 {
237  ltree_gist *origval = (ltree_gist *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
239  float *penalty = (float *) PG_GETARG_POINTER(2);
240  int32 cmpr,
241  cmpl;
242 
243  cmpl = ltree_compare(LTG_GETLNODE(origval), LTG_GETLNODE(newval));
244  cmpr = ltree_compare(LTG_GETRNODE(newval), LTG_GETRNODE(origval));
245 
246  *penalty = Max(cmpl, 0) + Max(cmpr, 0);
247 
248  PG_RETURN_POINTER(penalty);
249 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
signed int int32
Definition: c.h:246
#define LTG_GETRNODE(x)
Definition: ltree.h:238
#define LTG_GETLNODE(x)
Definition: ltree.h:237
#define Max(x, y)
Definition: c.h:806
#define newval
#define DatumGetPointer(X)
Definition: postgres.h:555
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42
Datum ltree_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 269 of file ltree_gist.c.

References FirstOffsetNumber, ltree_gist::flag, GETENTRY, hashing(), i, rix::index, ISEQ, LOOPBYTE, LTG_ALLTRUE, LTG_GETLNODE, LTG_GETRNODE, LTG_HDRSIZE, LTG_ISALLTRUE, LTG_ISONENODE, LTG_LNODE, LTG_NODE, LTG_NORIGHT, LTG_RNODE, LTG_SIGN, ltree_compare(), GistEntryVector::n, OffsetNumberNext, palloc(), palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, qsort, rix::r, SET_VARSIZE, SIGLEN, GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, treekey_cmp(), and VARSIZE.

270 {
273  OffsetNumber j;
274  int32 i;
275  RIX *array;
276  OffsetNumber maxoff;
277  int nbytes;
278  int size;
279  ltree *lu_l,
280  *lu_r,
281  *ru_l,
282  *ru_r;
283  ltree_gist *lu,
284  *ru;
285  BITVEC ls,
286  rs;
287  bool lisat = false,
288  risat = false,
289  isleqr;
290 
291  memset((void *) ls, 0, sizeof(BITVEC));
292  memset((void *) rs, 0, sizeof(BITVEC));
293  maxoff = entryvec->n - 1;
294  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
295  v->spl_left = (OffsetNumber *) palloc(nbytes);
296  v->spl_right = (OffsetNumber *) palloc(nbytes);
297  v->spl_nleft = 0;
298  v->spl_nright = 0;
299  array = (RIX *) palloc(sizeof(RIX) * (maxoff + 1));
300 
301  /* copy the data into RIXes, and sort the RIXes */
302  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
303  {
304  array[j].index = j;
305  lu = GETENTRY(entryvec, j); /* use as tmp val */
306  array[j].r = LTG_GETLNODE(lu);
307  }
308 
309  qsort((void *) &array[FirstOffsetNumber], maxoff - FirstOffsetNumber + 1,
310  sizeof(RIX), treekey_cmp);
311 
312  lu_l = lu_r = ru_l = ru_r = NULL;
313  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
314  {
315  lu = GETENTRY(entryvec, array[j].index); /* use as tmp val */
316  if (j <= (maxoff - FirstOffsetNumber + 1) / 2)
317  {
318  v->spl_left[v->spl_nleft] = array[j].index;
319  v->spl_nleft++;
320  if (lu_r == NULL || ltree_compare(LTG_GETRNODE(lu), lu_r) > 0)
321  lu_r = LTG_GETRNODE(lu);
322  if (LTG_ISONENODE(lu))
323  hashing(ls, LTG_NODE(lu));
324  else
325  {
326  if (lisat || LTG_ISALLTRUE(lu))
327  lisat = true;
328  else
329  {
330  BITVECP sc = LTG_SIGN(lu);
331 
332  LOOPBYTE
333  ((unsigned char *) ls)[i] |= sc[i];
334  }
335  }
336  }
337  else
338  {
339  v->spl_right[v->spl_nright] = array[j].index;
340  v->spl_nright++;
341  if (ru_r == NULL || ltree_compare(LTG_GETRNODE(lu), ru_r) > 0)
342  ru_r = LTG_GETRNODE(lu);
343  if (LTG_ISONENODE(lu))
344  hashing(rs, LTG_NODE(lu));
345  else
346  {
347  if (risat || LTG_ISALLTRUE(lu))
348  risat = true;
349  else
350  {
351  BITVECP sc = LTG_SIGN(lu);
352 
353  LOOPBYTE
354  ((unsigned char *) rs)[i] |= sc[i];
355  }
356  }
357  }
358  }
359 
360  if (lisat == false)
361  {
362  lisat = true;
363  LOOPBYTE
364  {
365  if (((unsigned char *) ls)[i] != 0xff)
366  {
367  lisat = false;
368  break;
369  }
370  }
371  }
372 
373  if (risat == false)
374  {
375  risat = true;
376  LOOPBYTE
377  {
378  if (((unsigned char *) rs)[i] != 0xff)
379  {
380  risat = false;
381  break;
382  }
383  }
384  }
385 
386  lu_l = LTG_GETLNODE(GETENTRY(entryvec, array[FirstOffsetNumber].index));
387  isleqr = (lu_l == lu_r || ISEQ(lu_l, lu_r)) ? true : false;
388  size = LTG_HDRSIZE + ((lisat) ? 0 : SIGLEN) + VARSIZE(lu_l) + ((isleqr) ? 0 : VARSIZE(lu_r));
389  lu = (ltree_gist *) palloc0(size);
390  SET_VARSIZE(lu, size);
391  lu->flag = 0;
392  if (lisat)
393  lu->flag |= LTG_ALLTRUE;
394  else
395  memcpy((void *) LTG_SIGN(lu), ls, SIGLEN);
396  memcpy((void *) LTG_LNODE(lu), (void *) lu_l, VARSIZE(lu_l));
397  if (isleqr)
398  lu->flag |= LTG_NORIGHT;
399  else
400  memcpy((void *) LTG_RNODE(lu), (void *) lu_r, VARSIZE(lu_r));
401 
402 
403  ru_l = LTG_GETLNODE(GETENTRY(entryvec, array[1 + ((maxoff - FirstOffsetNumber + 1) / 2)].index));
404  isleqr = (ru_l == ru_r || ISEQ(ru_l, ru_r)) ? true : false;
405  size = LTG_HDRSIZE + ((risat) ? 0 : SIGLEN) + VARSIZE(ru_l) + ((isleqr) ? 0 : VARSIZE(ru_r));
406  ru = (ltree_gist *) palloc0(size);
407  SET_VARSIZE(ru, size);
408  ru->flag = 0;
409  if (risat)
410  ru->flag |= LTG_ALLTRUE;
411  else
412  memcpy((void *) LTG_SIGN(ru), rs, SIGLEN);
413  memcpy((void *) LTG_LNODE(ru), (void *) ru_l, VARSIZE(ru_l));
414  if (isleqr)
415  ru->flag |= LTG_NORIGHT;
416  else
417  memcpy((void *) LTG_RNODE(ru), (void *) ru_r, VARSIZE(ru_r));
418 
419  v->spl_ldatum = PointerGetDatum(lu);
420  v->spl_rdatum = PointerGetDatum(ru);
421 
423 }
static void hashing(BITVECP sign, ltree *t)
Definition: ltree_gist.c:135
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
ltree * r
Definition: ltree_gist.c:255
#define LTG_ISALLTRUE(x)
Definition: ltree.h:231
#define VARSIZE(PTR)
Definition: postgres.h:304
#define PointerGetDatum(X)
Definition: postgres.h:562
#define LTG_SIGN(x)
Definition: ltree.h:228
uint32 flag
Definition: ltree.h:219
OffsetNumber * spl_left
Definition: gist.h:105
Datum spl_rdatum
Definition: gist.h:112
int32 n
Definition: gist.h:160
#define LTG_NODE(x)
Definition: ltree.h:229
#define LTG_RNODE(x)
Definition: ltree.h:235
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define LTG_NORIGHT
Definition: ltree.h:225
int spl_nleft
Definition: gist.h:106
signed int int32
Definition: c.h:246
uint16 OffsetNumber
Definition: off.h:24
Definition: type.h:89
#define LTG_GETRNODE(x)
Definition: ltree.h:238
int spl_nright
Definition: gist.h:111
#define LTG_GETLNODE(x)
Definition: ltree.h:237
char BITVEC[SIGLEN]
Definition: hstore_gist.c:19
#define FirstOffsetNumber
Definition: off.h:27
#define LTG_ALLTRUE
Definition: ltree.h:224
static int treekey_cmp(const void *a, const void *b)
Definition: ltree_gist.c:259
#define GETENTRY(vec, pos)
Definition: ltree_gist.c:45
Definition: ltree.h:19
#define ISEQ(a, b)
Definition: ltree_gist.c:44
void * palloc0(Size size)
Definition: mcxt.c:877
#define LTG_LNODE(x)
Definition: ltree.h:233
#define LTG_ISONENODE(x)
Definition: ltree.h:230
Datum spl_ldatum
Definition: gist.h:107
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
OffsetNumber * spl_right
Definition: gist.h:110
void * palloc(Size size)
Definition: mcxt.c:848
#define LOOPBYTE
Definition: hstore_gist.c:25
int i
#define SIGLEN
Definition: hstore_gist.c:16
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
char * BITVECP
Definition: hstore_gist.c:20
#define qsort(a, b, c, d)
Definition: port.h:447
#define LTG_HDRSIZE
Definition: ltree.h:227
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42
int index
Definition: ltree_gist.c:254
Datum ltree_same ( PG_FUNCTION_ARGS  )

Definition at line 91 of file ltree_gist.c.

References i, ISEQ, LOOPBYTE, LTG_ISALLTRUE, LTG_ISONENODE, LTG_LNODE, LTG_NODE, LTG_RNODE, LTG_SIGN, PG_GETARG_POINTER, and PG_RETURN_POINTER.

92 {
95  bool *result = (bool *) PG_GETARG_POINTER(2);
96 
97  *result = false;
98  if (LTG_ISONENODE(a) != LTG_ISONENODE(b))
99  PG_RETURN_POINTER(result);
100 
101  if (LTG_ISONENODE(a))
102  *result = (ISEQ(LTG_NODE(a), LTG_NODE(b))) ? true : false;
103  else
104  {
105  int32 i;
106  BITVECP sa = LTG_SIGN(a),
107  sb = LTG_SIGN(b);
108 
109  if (LTG_ISALLTRUE(a) != LTG_ISALLTRUE(b))
110  PG_RETURN_POINTER(result);
111 
112  if (!ISEQ(LTG_LNODE(a), LTG_LNODE(b)))
113  PG_RETURN_POINTER(result);
114  if (!ISEQ(LTG_RNODE(a), LTG_RNODE(b)))
115  PG_RETURN_POINTER(result);
116 
117  *result = true;
118  if (!LTG_ISALLTRUE(a))
119  {
120  LOOPBYTE
121  {
122  if (sa[i] != sb[i])
123  {
124  *result = false;
125  break;
126  }
127  }
128  }
129  }
130 
131  PG_RETURN_POINTER(result);
132 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define LTG_ISALLTRUE(x)
Definition: ltree.h:231
#define LTG_SIGN(x)
Definition: ltree.h:228
#define LTG_NODE(x)
Definition: ltree.h:229
#define LTG_RNODE(x)
Definition: ltree.h:235
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
signed int int32
Definition: c.h:246
#define ISEQ(a, b)
Definition: ltree_gist.c:44
#define LTG_LNODE(x)
Definition: ltree.h:233
#define LTG_ISONENODE(x)
Definition: ltree.h:230
#define LOOPBYTE
Definition: hstore_gist.c:25
int i
char * BITVECP
Definition: hstore_gist.c:20
Datum ltree_union ( PG_FUNCTION_ARGS  )

Definition at line 151 of file ltree_gist.c.

References cur, ltree_gist::flag, GETENTRY, hashing(), i, ISEQ, LOOPBYTE, LTG_ALLTRUE, LTG_HDRSIZE, LTG_ISALLTRUE, LTG_ISONENODE, LTG_LNODE, LTG_NODE, LTG_NORIGHT, LTG_RNODE, LTG_SIGN, ltree_compare(), MemSet, GistEntryVector::n, palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, SET_VARSIZE, SIGLEN, and VARSIZE.

152 {
154  int *size = (int *) PG_GETARG_POINTER(1);
155  BITVEC base;
156  int32 i,
157  j;
158  ltree_gist *result,
159  *cur;
160  ltree *left = NULL,
161  *right = NULL,
162  *curtree;
163  bool isalltrue = false;
164  bool isleqr;
165 
166  MemSet((void *) base, 0, sizeof(BITVEC));
167  for (j = 0; j < entryvec->n; j++)
168  {
169  cur = GETENTRY(entryvec, j);
170  if (LTG_ISONENODE(cur))
171  {
172  curtree = LTG_NODE(cur);
173  hashing(base, curtree);
174  if (!left || ltree_compare(left, curtree) > 0)
175  left = curtree;
176  if (!right || ltree_compare(right, curtree) < 0)
177  right = curtree;
178  }
179  else
180  {
181  if (isalltrue || LTG_ISALLTRUE(cur))
182  isalltrue = true;
183  else
184  {
185  BITVECP sc = LTG_SIGN(cur);
186 
187  LOOPBYTE
188  ((unsigned char *) base)[i] |= sc[i];
189  }
190 
191  curtree = LTG_LNODE(cur);
192  if (!left || ltree_compare(left, curtree) > 0)
193  left = curtree;
194  curtree = LTG_RNODE(cur);
195  if (!right || ltree_compare(right, curtree) < 0)
196  right = curtree;
197  }
198  }
199 
200  if (isalltrue == false)
201  {
202  isalltrue = true;
203  LOOPBYTE
204  {
205  if (((unsigned char *) base)[i] != 0xff)
206  {
207  isalltrue = false;
208  break;
209  }
210  }
211  }
212 
213  isleqr = (left == right || ISEQ(left, right)) ? true : false;
214  *size = LTG_HDRSIZE + ((isalltrue) ? 0 : SIGLEN) + VARSIZE(left) + ((isleqr) ? 0 : VARSIZE(right));
215 
216  result = (ltree_gist *) palloc0(*size);
217  SET_VARSIZE(result, *size);
218  result->flag = 0;
219 
220  if (isalltrue)
221  result->flag |= LTG_ALLTRUE;
222  else
223  memcpy((void *) LTG_SIGN(result), base, SIGLEN);
224 
225  memcpy((void *) LTG_LNODE(result), (void *) left, VARSIZE(left));
226  if (isleqr)
227  result->flag |= LTG_NORIGHT;
228  else
229  memcpy((void *) LTG_RNODE(result), (void *) right, VARSIZE(right));
230 
231  PG_RETURN_POINTER(result);
232 }
static void hashing(BITVECP sign, ltree *t)
Definition: ltree_gist.c:135
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define LTG_ISALLTRUE(x)
Definition: ltree.h:231
#define VARSIZE(PTR)
Definition: postgres.h:304
#define LTG_SIGN(x)
Definition: ltree.h:228
uint32 flag
Definition: ltree.h:219
int32 n
Definition: gist.h:160
struct cursor * cur
Definition: ecpg.c:28
#define LTG_NODE(x)
Definition: ltree.h:229
#define LTG_RNODE(x)
Definition: ltree.h:235
#define MemSet(start, val, len)
Definition: c.h:863
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define LTG_NORIGHT
Definition: ltree.h:225
signed int int32
Definition: c.h:246
char BITVEC[SIGLEN]
Definition: hstore_gist.c:19
#define LTG_ALLTRUE
Definition: ltree.h:224
#define GETENTRY(vec, pos)
Definition: ltree_gist.c:45
Definition: ltree.h:19
#define ISEQ(a, b)
Definition: ltree_gist.c:44
void * palloc0(Size size)
Definition: mcxt.c:877
#define LTG_LNODE(x)
Definition: ltree.h:233
#define LTG_ISONENODE(x)
Definition: ltree.h:230
#define LOOPBYTE
Definition: hstore_gist.c:25
int i
#define SIGLEN
Definition: hstore_gist.c:16
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
char * BITVECP
Definition: hstore_gist.c:20
#define LTG_HDRSIZE
Definition: ltree.h:227
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42
PG_FUNCTION_INFO_V1 ( ltree_gist_in  )
PG_FUNCTION_INFO_V1 ( ltree_gist_out  )
PG_FUNCTION_INFO_V1 ( ltree_compress  )
PG_FUNCTION_INFO_V1 ( ltree_decompress  )
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 int treekey_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 259 of file ltree_gist.c.

References ltree_compare().

Referenced by ltree_picksplit().

260 {
261  return ltree_compare(
262  ((const RIX *) a)->r,
263  ((const RIX *) b)->r
264  );
265 }
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42