#include "postgres.h"
#include "btree_gist.h"
#include "btree_utils_num.h"
#include "utils/cash.h"
#include "utils/date.h"
#include "utils/timestamp.h"
Go to the source code of this file.
Functions | |
GISTENTRY * | gbt_num_compress (GISTENTRY *retval, GISTENTRY *entry, const gbtree_ninfo *tinfo) |
void * | gbt_num_union (GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo) |
bool | gbt_num_same (const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo) |
void | gbt_num_bin_union (Datum *u, GBT_NUMKEY *e, const gbtree_ninfo *tinfo) |
bool | gbt_num_consistent (const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo) |
float8 | gbt_num_distance (const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo) |
GIST_SPLITVEC * | gbt_num_picksplit (const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo) |
void gbt_num_bin_union | ( | Datum * | u, | |
GBT_NUMKEY * | e, | |||
const gbtree_ninfo * | tinfo | |||
) |
Definition at line 160 of file btree_utils_num.c.
References DatumGetPointer, gbtree_ninfo::f_gt, gbtree_ninfo::f_lt, GBT_NUMKEY_R::lower, palloc(), PointerGetDatum, gbtree_ninfo::size, and GBT_NUMKEY_R::upper.
Referenced by gbt_num_picksplit().
{ GBT_NUMKEY_R rd; rd.lower = &e[0]; rd.upper = &e[tinfo->size]; if (!DatumGetPointer(*u)) { *u = PointerGetDatum(palloc(2 * tinfo->size)); memcpy((void *) &(((GBT_NUMKEY *) DatumGetPointer(*u))[0]), (void *) rd.lower, tinfo->size); memcpy((void *) &(((GBT_NUMKEY *) DatumGetPointer(*u))[tinfo->size]), (void *) rd.upper, tinfo->size); } else { GBT_NUMKEY_R ur; ur.lower = &(((GBT_NUMKEY *) DatumGetPointer(*u))[0]); ur.upper = &(((GBT_NUMKEY *) DatumGetPointer(*u))[tinfo->size]); if ((*tinfo->f_gt) ((void *) ur.lower, (void *) rd.lower)) memcpy((void *) ur.lower, (void *) rd.lower, tinfo->size); if ((*tinfo->f_lt) ((void *) ur.upper, (void *) rd.upper)) memcpy((void *) ur.upper, (void *) rd.upper, tinfo->size); } }
GISTENTRY* gbt_num_compress | ( | GISTENTRY * | retval, | |
GISTENTRY * | entry, | |||
const gbtree_ninfo * | tinfo | |||
) |
Definition at line 14 of file btree_utils_num.c.
References DatumGetCash, DatumGetDateADT, DatumGetFloat4, DatumGetFloat8, DatumGetInt16, DatumGetInt32, DatumGetInt64, DatumGetObjectId, DatumGetPointer, DatumGetTimeADT, DatumGetTimestamp, FALSE, gbt_t_cash, gbt_t_date, gbt_t_float4, gbt_t_float8, gbt_t_int2, gbt_t_int4, gbt_t_int8, gbt_t_oid, gbt_t_time, gbt_t_ts, gistentryinit, GISTENTRY::key, GISTENTRY::leafkey, GISTENTRY::offset, GISTENTRY::page, palloc(), palloc0(), PointerGetDatum, GISTENTRY::rel, gbtree_ninfo::size, gbtree_ninfo::t, and tm.
Referenced by gbt_cash_compress(), gbt_date_compress(), gbt_float4_compress(), gbt_float8_compress(), gbt_int2_compress(), gbt_int4_compress(), gbt_int8_compress(), gbt_macad_compress(), gbt_oid_compress(), gbt_time_compress(), and gbt_ts_compress().
{ if (entry->leafkey) { union { int16 i2; int32 i4; int64 i8; float4 f4; float8 f8; DateADT dt; TimeADT tm; Timestamp ts; Cash ch; } v; GBT_NUMKEY *r = (GBT_NUMKEY *) palloc0(2 * tinfo->size); void *leaf = NULL; switch (tinfo->t) { case gbt_t_int2: v.i2 = DatumGetInt16(entry->key); leaf = &v.i2; break; case gbt_t_int4: v.i4 = DatumGetInt32(entry->key); leaf = &v.i4; break; case gbt_t_int8: v.i8 = DatumGetInt64(entry->key); leaf = &v.i8; break; case gbt_t_oid: v.i4 = DatumGetObjectId(entry->key); leaf = &v.i4; break; case gbt_t_float4: v.f4 = DatumGetFloat4(entry->key); leaf = &v.f4; break; case gbt_t_float8: v.f8 = DatumGetFloat8(entry->key); leaf = &v.f8; break; case gbt_t_date: v.dt = DatumGetDateADT(entry->key); leaf = &v.dt; break; case gbt_t_time: v.tm = DatumGetTimeADT(entry->key); leaf = &v.tm; break; case gbt_t_ts: v.ts = DatumGetTimestamp(entry->key); leaf = &v.ts; break; case gbt_t_cash: v.ch = DatumGetCash(entry->key); leaf = &v.ch; break; default: leaf = DatumGetPointer(entry->key); } memcpy((void *) &r[0], leaf, tinfo->size); memcpy((void *) &r[tinfo->size], leaf, tinfo->size); retval = palloc(sizeof(GISTENTRY)); gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page, entry->offset, FALSE); } else retval = entry; return retval; }
bool gbt_num_consistent | ( | const GBT_NUMKEY_R * | key, | |
const void * | query, | |||
const StrategyNumber * | strategy, | |||
bool | is_leaf, | |||
const gbtree_ninfo * | tinfo | |||
) |
Definition at line 196 of file btree_utils_num.c.
References BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, BtreeGistNotEqualStrategyNumber, gbtree_ninfo::f_eq, gbtree_ninfo::f_ge, gbtree_ninfo::f_gt, gbtree_ninfo::f_le, gbtree_ninfo::f_lt, GBT_NUMKEY_R::lower, and GBT_NUMKEY_R::upper.
Referenced by gbt_cash_consistent(), gbt_date_consistent(), gbt_float4_consistent(), gbt_float8_consistent(), gbt_inet_consistent(), gbt_int2_consistent(), gbt_int4_consistent(), gbt_int8_consistent(), gbt_intv_consistent(), gbt_macad_consistent(), gbt_oid_consistent(), gbt_time_consistent(), gbt_timetz_consistent(), gbt_ts_consistent(), and gbt_tstz_consistent().
{ bool retval; switch (*strategy) { case BTLessEqualStrategyNumber: retval = (*tinfo->f_ge) (query, key->lower); break; case BTLessStrategyNumber: if (is_leaf) retval = (*tinfo->f_gt) (query, key->lower); else retval = (*tinfo->f_ge) (query, key->lower); break; case BTEqualStrategyNumber: if (is_leaf) retval = (*tinfo->f_eq) (query, key->lower); else retval = ((*tinfo->f_le) (key->lower, query) && (*tinfo->f_le) (query, key->upper)) ? true : false; break; case BTGreaterStrategyNumber: if (is_leaf) retval = (*tinfo->f_lt) (query, key->upper); else retval = (*tinfo->f_le) (query, key->upper); break; case BTGreaterEqualStrategyNumber: retval = (*tinfo->f_le) (query, key->upper); break; case BtreeGistNotEqualStrategyNumber: retval = (!((*tinfo->f_eq) (query, key->lower) && (*tinfo->f_eq) (query, key->upper))) ? true : false; break; default: retval = false; } return (retval); }
float8 gbt_num_distance | ( | const GBT_NUMKEY_R * | key, | |
const void * | query, | |||
bool | is_leaf, | |||
const gbtree_ninfo * | tinfo | |||
) |
Definition at line 247 of file btree_utils_num.c.
References elog, ERROR, gbtree_ninfo::f_dist, gbtree_ninfo::f_ge, gbtree_ninfo::f_le, GBT_NUMKEY_R::lower, NULL, gbtree_ninfo::t, and GBT_NUMKEY_R::upper.
Referenced by gbt_cash_distance(), gbt_date_distance(), gbt_float4_distance(), gbt_float8_distance(), gbt_int2_distance(), gbt_int4_distance(), gbt_int8_distance(), gbt_intv_distance(), gbt_oid_distance(), gbt_time_distance(), gbt_ts_distance(), and gbt_tstz_distance().
{ float8 retval; if (tinfo->f_dist == NULL) elog(ERROR, "KNN search is not supported for btree_gist type %d", (int) tinfo->t); if (tinfo->f_le(query, key->lower)) retval = tinfo->f_dist(query, key->lower); else if (tinfo->f_ge(query, key->upper)) retval = tinfo->f_dist(query, key->upper); else retval = 0.0; return retval; }
GIST_SPLITVEC* gbt_num_picksplit | ( | const GistEntryVector * | entryvec, | |
GIST_SPLITVEC * | v, | |||
const gbtree_ninfo * | tinfo | |||
) |
Definition at line 269 of file btree_utils_num.c.
References DatumGetPointer, gbtree_ninfo::f_cmp, FirstOffsetNumber, gbt_num_bin_union(), Nsrt::i, i, GISTENTRY::key, GistEntryVector::n, OffsetNumberNext, palloc(), PointerGetDatum, qsort, GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, Nsrt::t, and GistEntryVector::vector.
Referenced by gbt_cash_picksplit(), gbt_date_picksplit(), gbt_float4_picksplit(), gbt_float8_picksplit(), gbt_inet_picksplit(), gbt_int2_picksplit(), gbt_int4_picksplit(), gbt_int8_picksplit(), gbt_intv_picksplit(), gbt_macad_picksplit(), gbt_oid_picksplit(), gbt_time_picksplit(), and gbt_ts_picksplit().
{ OffsetNumber i, maxoff = entryvec->n - 1; Nsrt *arr; int nbytes; arr = (Nsrt *) palloc((maxoff + 1) * sizeof(Nsrt)); nbytes = (maxoff + 2) * sizeof(OffsetNumber); v->spl_left = (OffsetNumber *) palloc(nbytes); v->spl_right = (OffsetNumber *) palloc(nbytes); v->spl_ldatum = PointerGetDatum(0); v->spl_rdatum = PointerGetDatum(0); v->spl_nleft = 0; v->spl_nright = 0; /* Sort entries */ for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) { arr[i].t = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[i].key)); arr[i].i = i; } qsort((void *) &arr[FirstOffsetNumber], maxoff - FirstOffsetNumber + 1, sizeof(Nsrt), tinfo->f_cmp); /* We do simply create two parts */ for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) { if (i <= (maxoff - FirstOffsetNumber + 1) / 2) { gbt_num_bin_union(&v->spl_ldatum, arr[i].t, tinfo); v->spl_left[v->spl_nleft] = arr[i].i; v->spl_nleft++; } else { gbt_num_bin_union(&v->spl_rdatum, arr[i].t, tinfo); v->spl_right[v->spl_nright] = arr[i].i; v->spl_nright++; } } return v; }
bool gbt_num_same | ( | const GBT_NUMKEY * | a, | |
const GBT_NUMKEY * | b, | |||
const gbtree_ninfo * | tinfo | |||
) |
Definition at line 138 of file btree_utils_num.c.
References gbtree_ninfo::f_eq, GBT_NUMKEY_R::lower, gbtree_ninfo::size, and GBT_NUMKEY_R::upper.
Referenced by gbt_cash_same(), gbt_date_same(), gbt_float4_same(), gbt_float8_same(), gbt_inet_same(), gbt_int2_same(), gbt_int4_same(), gbt_int8_same(), gbt_intv_same(), gbt_macad_same(), gbt_oid_same(), gbt_time_same(), and gbt_ts_same().
{ GBT_NUMKEY_R b1, b2; b1.lower = &(((GBT_NUMKEY *) a)[0]); b1.upper = &(((GBT_NUMKEY *) a)[tinfo->size]); b2.lower = &(((GBT_NUMKEY *) b)[0]); b2.upper = &(((GBT_NUMKEY *) b)[tinfo->size]); if ( (*tinfo->f_eq) (b1.lower, b2.lower) && (*tinfo->f_eq) (b1.upper, b2.upper) ) return TRUE; return FALSE; }
void* gbt_num_union | ( | GBT_NUMKEY * | out, | |
const GistEntryVector * | entryvec, | |||
const gbtree_ninfo * | tinfo | |||
) |
Definition at line 100 of file btree_utils_num.c.
References cur, DatumGetPointer, gbtree_ninfo::f_gt, gbtree_ninfo::f_lt, i, GISTENTRY::key, GBT_NUMKEY_R::lower, GistEntryVector::n, gbtree_ninfo::size, GBT_NUMKEY_R::upper, and GistEntryVector::vector.
Referenced by gbt_cash_union(), gbt_date_union(), gbt_float4_union(), gbt_float8_union(), gbt_inet_union(), gbt_int2_union(), gbt_int4_union(), gbt_int8_union(), gbt_intv_union(), gbt_macad_union(), gbt_oid_union(), gbt_time_union(), and gbt_ts_union().
{ int i, numranges; GBT_NUMKEY *cur; GBT_NUMKEY_R o, c; numranges = entryvec->n; cur = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[0].key)); o.lower = &((GBT_NUMKEY *) out)[0]; o.upper = &((GBT_NUMKEY *) out)[tinfo->size]; memcpy((void *) out, (void *) cur, 2 * tinfo->size); for (i = 1; i < numranges; i++) { cur = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[i].key)); c.lower = &cur[0]; c.upper = &cur[tinfo->size]; if ((*tinfo->f_gt) (o.lower, c.lower)) /* out->lower > cur->lower */ memcpy((void *) o.lower, (void *) c.lower, tinfo->size); if ((*tinfo->f_lt) (o.upper, c.upper)) /* out->upper < cur->upper */ memcpy((void *) o.upper, (void *) c.upper, tinfo->size); } return out; }