#include "postgres.h"
#include "access/gist.h"
#include "access/skey.h"
#include "crc32.h"
#include "ltree.h"
Go to the source code of this file.
Data Structures | |
struct | SPLITCOST |
Defines | |
#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) | |
Datum | _ltree_compress (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (_ltree_same) | |
Datum | _ltree_same (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (_ltree_union) | |
Datum | _ltree_union (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (_ltree_penalty) | |
Datum | _ltree_penalty (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (_ltree_picksplit) | |
Datum | _ltree_picksplit (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (_ltree_consistent) | |
Datum | _ltree_consistent (PG_FUNCTION_ARGS) |
static void | hashing (BITVECP sign, ltree *t) |
static int32 | unionkey (BITVECP sbase, ltree_gist *add) |
static int32 | sizebitvec (BITVECP sign) |
static int | hemdistsign (BITVECP a, BITVECP b) |
static int | hemdist (ltree_gist *a, ltree_gist *b) |
static int | comparecost (const void *a, const void *b) |
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) |
Variables | |
static const uint8 | number_of_ones [256] |
#define GETENTRY | ( | vec, | ||
pos | ||||
) | ((ltree_gist *) DatumGetPointer((vec)->vector[(pos)].key)) |
Definition at line 34 of file _ltree_gist.c.
Referenced by _ltree_picksplit(), and _ltree_union().
#define NEXTVAL | ( | x | ) | ( (ltree*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) ) |
Definition at line 35 of file _ltree_gist.c.
Referenced by _arrq_cons(), and _ltree_compress().
Definition at line 57 of file _ltree_gist.c.
Referenced by _ltree_picksplit().
static bool _arrq_cons | ( | ltree_gist * | key, | |
ArrayType * | _query | |||
) | [static] |
Definition at line 531 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().
{ lquery *query = (lquery *) ARR_DATA_PTR(_query); int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query)); if (ARR_NDIM(_query) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must be one-dimensional"))); if (array_contains_nulls(_query)) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("array must not contain nulls"))); while (num > 0) { if (gist_qe(key, query)) return true; num--; query = (lquery *) NEXTVAL(query); } return false; }
Datum _ltree_compress | ( | PG_FUNCTION_ARGS | ) |
Definition at line 77 of file _ltree_gist.c.
References ALOOPBYTE, ARR_DATA_PTR, ARR_DIMS, ARR_NDIM, array_contains_nulls(), ArrayGetNItems(), DatumGetArrayTypeP, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, FALSE, ltree_gist::flag, gistentryinit, hashing(), i, GISTENTRY::key, GISTENTRY::leafkey, LTG_HDRSIZE, LTG_ISALLTRUE, LTG_SIGN, MemSet, NEXTVAL, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, GISTENTRY::rel, SET_VARSIZE, sign, and val.
{ GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); GISTENTRY *retval = entry; if (entry->leafkey) { /* ltree */ ltree_gist *key; ArrayType *val = DatumGetArrayTypeP(entry->key); int32 len = LTG_HDRSIZE + ASIGLEN; int num = ArrayGetNItems(ARR_NDIM(val), ARR_DIMS(val)); ltree *item = (ltree *) ARR_DATA_PTR(val); if (ARR_NDIM(val) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must be one-dimensional"))); if (array_contains_nulls(val)) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("array must not contain nulls"))); key = (ltree_gist *) palloc(len); SET_VARSIZE(key, len); key->flag = 0; MemSet(LTG_SIGN(key), 0, ASIGLEN); while (num > 0) { hashing(LTG_SIGN(key), item); num--; item = NEXTVAL(item); } retval = (GISTENTRY *) palloc(sizeof(GISTENTRY)); gistentryinit(*retval, PointerGetDatum(key), entry->rel, entry->page, entry->offset, FALSE); } else if (!LTG_ISALLTRUE(entry->key)) { int32 i, len; ltree_gist *key; BITVECP sign = LTG_SIGN(DatumGetPointer(entry->key)); ALOOPBYTE { if ((sign[i] & 0xff) != 0xff) PG_RETURN_POINTER(retval); } len = LTG_HDRSIZE; key = (ltree_gist *) palloc(len); SET_VARSIZE(key, len); key->flag = LTG_ALLTRUE; retval = (GISTENTRY *) palloc(sizeof(GISTENTRY)); gistentryinit(*retval, PointerGetDatum(key), entry->rel, entry->page, entry->offset, FALSE); } PG_RETURN_POINTER(retval); }
Datum _ltree_consistent | ( | PG_FUNCTION_ARGS | ) |
Definition at line 556 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.
{ GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); char *query = (char *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1))); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); /* Oid subtype = PG_GETARG_OID(3); */ bool *recheck = (bool *) PG_GETARG_POINTER(4); ltree_gist *key = (ltree_gist *) DatumGetPointer(entry->key); bool res = false; /* All cases served by this function are inexact */ *recheck = true; switch (strategy) { case 10: case 11: res = gist_te(key, (ltree *) query); break; case 12: case 13: res = gist_qe(key, (lquery *) query); break; case 14: case 15: res = gist_qtxt(key, (ltxtquery *) query); break; case 16: case 17: res = _arrq_cons(key, (ArrayType *) query); break; default: /* internal error */ elog(ERROR, "unrecognized StrategyNumber: %d", strategy); } PG_FREE_IF_COPY(query, 1); PG_RETURN_BOOL(res); }
Datum _ltree_penalty | ( | PG_FUNCTION_ARGS | ) |
Definition at line 264 of file _ltree_gist.c.
References DatumGetPointer, hemdist(), PG_GETARG_POINTER, and PG_RETURN_POINTER.
{ ltree_gist *origval = (ltree_gist *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key); ltree_gist *newval = (ltree_gist *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key); float *penalty = (float *) PG_GETARG_POINTER(2); *penalty = hemdist(origval, newval); PG_RETURN_POINTER(penalty); }
Datum _ltree_picksplit | ( | PG_FUNCTION_ARGS | ) |
Definition at line 287 of file _ltree_gist.c.
References Abs, ALOOPBYTE, ASIGLEN, comparecost(), SPLITCOST::cost, FirstOffsetNumber, ltree_gist::flag, GETENTRY, hemdist(), i, NODE::left, LTG_HDRSIZE, LTG_ISALLTRUE, LTG_SIGN, MemSet, GistEntryVector::n, OffsetNumberNext, palloc(), 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.
{ GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1); OffsetNumber k, j; ltree_gist *datum_l, *datum_r; BITVECP union_l, union_r; int32 size_alpha, size_beta; int32 size_waste, waste = -1; int32 nbytes; OffsetNumber seed_1 = 0, seed_2 = 0; OffsetNumber *left, *right; OffsetNumber maxoff; BITVECP ptr; int i; SPLITCOST *costvector; ltree_gist *_k, *_j; maxoff = entryvec->n - 2; nbytes = (maxoff + 2) * sizeof(OffsetNumber); v->spl_left = (OffsetNumber *) palloc(nbytes); v->spl_right = (OffsetNumber *) palloc(nbytes); for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k)) { _k = GETENTRY(entryvec, k); for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j)) { size_waste = hemdist(_k, GETENTRY(entryvec, j)); if (size_waste > waste) { waste = size_waste; seed_1 = k; seed_2 = j; } } } left = v->spl_left; v->spl_nleft = 0; right = v->spl_right; v->spl_nright = 0; if (seed_1 == 0 || seed_2 == 0) { seed_1 = 1; seed_2 = 2; } /* form initial .. */ if (LTG_ISALLTRUE(GETENTRY(entryvec, seed_1))) { datum_l = (ltree_gist *) palloc(LTG_HDRSIZE); SET_VARSIZE(datum_l, LTG_HDRSIZE); datum_l->flag = LTG_ALLTRUE; } else { datum_l = (ltree_gist *) palloc(LTG_HDRSIZE + ASIGLEN); SET_VARSIZE(datum_l, LTG_HDRSIZE + ASIGLEN); datum_l->flag = 0; memcpy((void *) LTG_SIGN(datum_l), (void *) LTG_SIGN(GETENTRY(entryvec, seed_1)), sizeof(ABITVEC)); } if (LTG_ISALLTRUE(GETENTRY(entryvec, seed_2))) { datum_r = (ltree_gist *) palloc(LTG_HDRSIZE); SET_VARSIZE(datum_r, LTG_HDRSIZE); datum_r->flag = LTG_ALLTRUE; } else { datum_r = (ltree_gist *) palloc(LTG_HDRSIZE + ASIGLEN); SET_VARSIZE(datum_r, LTG_HDRSIZE + ASIGLEN); datum_r->flag = 0; memcpy((void *) LTG_SIGN(datum_r), (void *) LTG_SIGN(GETENTRY(entryvec, seed_2)), sizeof(ABITVEC)); } maxoff = OffsetNumberNext(maxoff); /* sort before ... */ costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff); for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j)) { costvector[j - 1].pos = j; _j = GETENTRY(entryvec, j); size_alpha = hemdist(datum_l, _j); size_beta = hemdist(datum_r, _j); costvector[j - 1].cost = Abs(size_alpha - size_beta); } qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost); union_l = LTG_SIGN(datum_l); union_r = LTG_SIGN(datum_r); for (k = 0; k < maxoff; k++) { j = costvector[k].pos; if (j == seed_1) { *left++ = j; v->spl_nleft++; continue; } else if (j == seed_2) { *right++ = j; v->spl_nright++; continue; } _j = GETENTRY(entryvec, j); size_alpha = hemdist(datum_l, _j); size_beta = hemdist(datum_r, _j); if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.00001)) { if (LTG_ISALLTRUE(datum_l) || LTG_ISALLTRUE(_j)) { if (!LTG_ISALLTRUE(datum_l)) MemSet((void *) union_l, 0xff, sizeof(ABITVEC)); } else { ptr = LTG_SIGN(_j); ALOOPBYTE union_l[i] |= ptr[i]; } *left++ = j; v->spl_nleft++; } else { if (LTG_ISALLTRUE(datum_r) || LTG_ISALLTRUE(_j)) { if (!LTG_ISALLTRUE(datum_r)) MemSet((void *) union_r, 0xff, sizeof(ABITVEC)); } else { ptr = LTG_SIGN(_j); ALOOPBYTE union_r[i] |= ptr[i]; } *right++ = j; v->spl_nright++; } } *right = *left = FirstOffsetNumber; v->spl_ldatum = PointerGetDatum(datum_l); v->spl_rdatum = PointerGetDatum(datum_r); PG_RETURN_POINTER(v); }
Datum _ltree_same | ( | PG_FUNCTION_ARGS | ) |
Definition at line 143 of file _ltree_gist.c.
References ALOOPBYTE, i, LTG_ISALLTRUE, LTG_SIGN, PG_GETARG_POINTER, and PG_RETURN_POINTER.
{ ltree_gist *a = (ltree_gist *) PG_GETARG_POINTER(0); ltree_gist *b = (ltree_gist *) PG_GETARG_POINTER(1); bool *result = (bool *) PG_GETARG_POINTER(2); if (LTG_ISALLTRUE(a) && LTG_ISALLTRUE(b)) *result = true; else if (LTG_ISALLTRUE(a)) *result = false; else if (LTG_ISALLTRUE(b)) *result = false; else { int32 i; BITVECP sa = LTG_SIGN(a), sb = LTG_SIGN(b); *result = true; ALOOPBYTE { if (sa[i] != sb[i]) { *result = false; break; } } } PG_RETURN_POINTER(result); }
Datum _ltree_union | ( | PG_FUNCTION_ARGS | ) |
Definition at line 189 of file _ltree_gist.c.
References ASIGLEN, ltree_gist::flag, flag(), GETENTRY, i, LTG_HDRSIZE, LTG_ISALLTRUE, LTG_SIGN, MemSet, GistEntryVector::n, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, SET_VARSIZE, and unionkey().
{ GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); int *size = (int *) PG_GETARG_POINTER(1); ABITVEC base; int32 i, len; int32 flag = 0; ltree_gist *result; MemSet((void *) base, 0, sizeof(ABITVEC)); for (i = 0; i < entryvec->n; i++) { if (unionkey(base, GETENTRY(entryvec, i))) { flag = LTG_ALLTRUE; break; } } len = LTG_HDRSIZE + ((flag & LTG_ALLTRUE) ? 0 : ASIGLEN); result = (ltree_gist *) palloc(len); SET_VARSIZE(result, len); result->flag = flag; if (!LTG_ISALLTRUE(result)) memcpy((void *) LTG_SIGN(result), (void *) base, sizeof(ABITVEC)); *size = len; PG_RETURN_POINTER(result); }
Definition at line 473 of file _ltree_gist.c.
References AHASHVAL, ITEM::flag, FLG_CANLOOKSIGN, GETBIT, and ITEM::val.
Referenced by gist_qtxt().
static int comparecost | ( | const void * | a, | |
const void * | b | |||
) | [static] |
Definition at line 281 of file _ltree_gist.c.
Referenced by _ltree_picksplit().
static bool gist_qe | ( | ltree_gist * | key, | |
lquery * | query | |||
) | [static] |
Definition at line 492 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().
{ lquery_level *curq = LQUERY_FIRST(query); BITVECP sign = LTG_SIGN(key); int qlen = query->numlevel; if (LTG_ISALLTRUE(key)) return true; while (qlen > 0) { if (curq->numvar && LQL_CANLOOKSIGN(curq)) { bool isexist = false; int vlen = curq->numvar; lquery_variant *curv = LQL_FIRST(curq); while (vlen > 0) { if (GETBIT(sign, AHASHVAL(curv->val))) { isexist = true; break; } curv = LVAR_NEXT(curv); vlen--; } if (!isexist) return false; } curq = LQL_NEXT(curq); qlen--; } return true; }
static bool gist_qtxt | ( | ltree_gist * | key, | |
ltxtquery * | query | |||
) | [static] |
Definition at line 479 of file _ltree_gist.c.
References checkcondition_bit(), GETQUERY, LTG_ISALLTRUE, LTG_SIGN, and ltree_execute().
Referenced by _ltree_consistent().
{ if (LTG_ISALLTRUE(key)) return true; return ltree_execute( GETQUERY(query), (void *) LTG_SIGN(key), false, checkcondition_bit ); }
static bool gist_te | ( | ltree_gist * | key, | |
ltree * | query | |||
) | [static] |
Definition at line 450 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().
{ ltree_level *curq = LTREE_FIRST(query); BITVECP sign = LTG_SIGN(key); int qlen = query->numlevel; unsigned int hv; if (LTG_ISALLTRUE(key)) return true; while (qlen > 0) { hv = ltree_crc32_sz(curq->name, curq->len); if (!GETBIT(sign, AHASHVAL(hv))) return false; curq = LEVEL_NEXT(curq); qlen--; } return true; }
Definition at line 61 of file _ltree_gist.c.
References AHASH, cur, ltree_level::len, LEVEL_NEXT, ltree_crc32_sz(), LTREE_FIRST, ltree_level::name, and ltree::numlevel.
Referenced by _ltree_compress().
{ int tlen = t->numlevel; ltree_level *cur = LTREE_FIRST(t); int hash; while (tlen > 0) { hash = ltree_crc32_sz(cur->name, cur->len); AHASH(sign, hash); cur = LEVEL_NEXT(cur); tlen--; } }
static int hemdist | ( | ltree_gist * | a, | |
ltree_gist * | b | |||
) | [static] |
Definition at line 247 of file _ltree_gist.c.
References ASIGLENBIT, hemdistsign(), LTG_ISALLTRUE, LTG_SIGN, and sizebitvec().
Referenced by _ltree_penalty(), and _ltree_picksplit().
{ if (LTG_ISALLTRUE(a)) { if (LTG_ISALLTRUE(b)) return 0; else return ASIGLENBIT - sizebitvec(LTG_SIGN(b)); } else if (LTG_ISALLTRUE(b)) return ASIGLENBIT - sizebitvec(LTG_SIGN(a)); return hemdistsign(LTG_SIGN(a), LTG_SIGN(b)); }
Definition at line 232 of file _ltree_gist.c.
References ALOOPBYTE, i, and number_of_ones.
Referenced by hemdist().
{ int i, diff, dist = 0; ALOOPBYTE { diff = (unsigned char) (a[i] ^ b[i]); dist += number_of_ones[diff]; } return dist; }
PG_FUNCTION_INFO_V1 | ( | _ltree_union | ) |
PG_FUNCTION_INFO_V1 | ( | _ltree_consistent | ) |
PG_FUNCTION_INFO_V1 | ( | _ltree_penalty | ) |
PG_FUNCTION_INFO_V1 | ( | _ltree_picksplit | ) |
PG_FUNCTION_INFO_V1 | ( | _ltree_compress | ) |
PG_FUNCTION_INFO_V1 | ( | _ltree_same | ) |
Definition at line 221 of file _ltree_gist.c.
References ALOOPBYTE, i, and number_of_ones.
Referenced by hemdist().
static int32 unionkey | ( | BITVECP | sbase, | |
ltree_gist * | add | |||
) | [static] |
Definition at line 175 of file _ltree_gist.c.
References ALOOPBYTE, i, LTG_ISALLTRUE, and LTG_SIGN.
Referenced by _ltree_union().
const uint8 number_of_ones[256] [static] |
{ 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 38 of file _ltree_gist.c.
Referenced by hemdistsign(), and sizebitvec().