PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
btree_utils_var.c File Reference
#include "postgres.h"
#include "btree_gist.h"
#include <math.h>
#include <limits.h>
#include <float.h>
#include "btree_utils_var.h"
#include "utils/pg_locale.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for btree_utils_var.c:

Go to the source code of this file.

Data Structures

struct  Vsrt
 
struct  gbt_vsrt_arg
 

Functions

 PG_FUNCTION_INFO_V1 (gbt_var_decompress)
 
 PG_FUNCTION_INFO_V1 (gbt_var_fetch)
 
Datum gbt_var_decompress (PG_FUNCTION_ARGS)
 
GBT_VARKEY_R gbt_var_key_readable (const GBT_VARKEY *k)
 
static GBT_VARKEYgbt_var_key_from_datum (const struct varlena *u)
 
GBT_VARKEYgbt_var_key_copy (const GBT_VARKEY_R *u)
 
static GBT_VARKEYgbt_var_leaf2node (GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
static int32 gbt_var_node_cp_len (const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
 
static bool gbt_bytea_pf_match (const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)
 
static bool gbt_var_node_pf_match (const GBT_VARKEY_R *node, const bytea *query, const gbtree_vinfo *tinfo)
 
static GBT_VARKEYgbt_var_node_truncate (const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
 
void gbt_var_bin_union (Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
GISTENTRYgbt_var_compress (GISTENTRY *entry, const gbtree_vinfo *tinfo)
 
Datum gbt_var_fetch (PG_FUNCTION_ARGS)
 
GBT_VARKEYgbt_var_union (const GistEntryVector *entryvec, int32 *size, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
bool gbt_var_same (Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
float * gbt_var_penalty (float *res, const GISTENTRY *o, const GISTENTRY *n, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
static int gbt_vsrt_cmp (const void *a, const void *b, void *arg)
 
GIST_SPLITVECgbt_var_picksplit (const GistEntryVector *entryvec, GIST_SPLITVEC *v, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 
bool gbt_var_consistent (GBT_VARKEY_R *key, const void *query, StrategyNumber strategy, Oid collation, bool is_leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
 

Function Documentation

static bool gbt_bytea_pf_match ( const bytea pf,
const bytea query,
const gbtree_vinfo tinfo 
)
static

Definition at line 170 of file btree_utils_var.c.

References FALSE, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by gbt_var_node_pf_match(), and gbt_var_penalty().

171 {
172  bool out = FALSE;
173  int32 qlen = VARSIZE(query) - VARHDRSZ;
174  int32 nlen = VARSIZE(pf) - VARHDRSZ;
175 
176  if (nlen <= qlen)
177  {
178  char *q = VARDATA(query);
179  char *n = VARDATA(pf);
180 
181  out = (memcmp(q, n, nlen) == 0);
182  }
183 
184  return out;
185 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:439
signed int int32
Definition: c.h:246
#define FALSE
Definition: c.h:219
void gbt_var_bin_union ( Datum u,
GBT_VARKEY e,
Oid  collation,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 235 of file btree_utils_var.c.

References DatumGetPointer, gbtree_vinfo::f_cmp, gbt_var_key_copy(), gbt_var_key_readable(), gbt_var_leaf2node(), GBT_VARKEY_R::lower, PointerGetDatum, and GBT_VARKEY_R::upper.

Referenced by gbt_numeric_penalty(), gbt_var_penalty(), gbt_var_picksplit(), and gbt_var_union().

237 {
239  GBT_VARKEY_R nr;
240 
241  if (eo.lower == eo.upper) /* leaf */
242  {
243  GBT_VARKEY *tmp;
244 
245  tmp = gbt_var_leaf2node(e, tinfo, flinfo);
246  if (tmp != e)
247  eo = gbt_var_key_readable(tmp);
248  }
249 
250  if (DatumGetPointer(*u))
251  {
253  bool update = false;
254 
255  nr.lower = ro.lower;
256  nr.upper = ro.upper;
257 
258  if (tinfo->f_cmp(ro.lower, eo.lower, collation, flinfo) > 0)
259  {
260  nr.lower = eo.lower;
261  update = true;
262  }
263 
264  if (tinfo->f_cmp(ro.upper, eo.upper, collation, flinfo) < 0)
265  {
266  nr.upper = eo.upper;
267  update = true;
268  }
269 
270  if (update)
272  }
273  else
274  {
275  nr.lower = eo.lower;
276  nr.upper = eo.upper;
278  }
279 }
static GBT_VARKEY * gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
#define PointerGetDatum(X)
Definition: postgres.h:562
int32(* f_cmp)(const void *, const void *, Oid, FmgrInfo *)
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
GBT_VARKEY * gbt_var_key_copy(const GBT_VARKEY_R *u)
#define DatumGetPointer(X)
Definition: postgres.h:555
Definition: c.h:433
GISTENTRY* gbt_var_compress ( GISTENTRY entry,
const gbtree_vinfo tinfo 
)

Definition at line 283 of file btree_utils_var.c.

References gbt_var_key_from_datum(), gistentryinit, GISTENTRY::key, GISTENTRY::leafkey, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_DETOAST_DATUM, PointerGetDatum, GISTENTRY::rel, and TRUE.

Referenced by gbt_bit_compress(), gbt_bpchar_compress(), gbt_bytea_compress(), gbt_numeric_compress(), and gbt_text_compress().

284 {
285  GISTENTRY *retval;
286 
287  if (entry->leafkey)
288  {
289  struct varlena *leaf = PG_DETOAST_DATUM(entry->key);
290  GBT_VARKEY *r;
291 
292  r = gbt_var_key_from_datum(leaf);
293 
294  retval = palloc(sizeof(GISTENTRY));
295  gistentryinit(*retval, PointerGetDatum(r),
296  entry->rel, entry->page,
297  entry->offset, TRUE);
298  }
299  else
300  retval = entry;
301 
302  return retval;
303 }
Relation rel
Definition: gist.h:124
#define PointerGetDatum(X)
Definition: postgres.h:562
static GBT_VARKEY * gbt_var_key_from_datum(const struct varlena *u)
Page page
Definition: gist.h:125
Datum key
Definition: gist.h:123
bool leafkey
Definition: gist.h:127
#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
Definition: c.h:433
#define TRUE
Definition: c.h:215
OffsetNumber offset
Definition: gist.h:126
bool gbt_var_consistent ( GBT_VARKEY_R key,
const void *  query,
StrategyNumber  strategy,
Oid  collation,
bool  is_leaf,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 556 of file btree_utils_var.c.

References BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, BtreeGistNotEqualStrategyNumber, gbtree_vinfo::f_cmp, gbtree_vinfo::f_eq, gbtree_vinfo::f_ge, gbtree_vinfo::f_gt, gbtree_vinfo::f_le, gbtree_vinfo::f_lt, FALSE, gbt_var_node_pf_match(), GBT_VARKEY_R::lower, and GBT_VARKEY_R::upper.

Referenced by gbt_bit_consistent(), gbt_bpchar_consistent(), gbt_bytea_consistent(), gbt_numeric_consistent(), and gbt_text_consistent().

563 {
564  bool retval = FALSE;
565 
566  switch (strategy)
567  {
569  if (is_leaf)
570  retval = tinfo->f_ge(query, key->lower, collation, flinfo);
571  else
572  retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
573  || gbt_var_node_pf_match(key, query, tinfo);
574  break;
576  if (is_leaf)
577  retval = tinfo->f_gt(query, key->lower, collation, flinfo);
578  else
579  retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
580  || gbt_var_node_pf_match(key, query, tinfo);
581  break;
583  if (is_leaf)
584  retval = tinfo->f_eq(query, key->lower, collation, flinfo);
585  else
586  retval =
587  (tinfo->f_cmp(key->lower, query, collation, flinfo) <= 0 &&
588  tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0) ||
589  gbt_var_node_pf_match(key, query, tinfo);
590  break;
592  if (is_leaf)
593  retval = tinfo->f_lt(query, key->upper, collation, flinfo);
594  else
595  retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
596  || gbt_var_node_pf_match(key, query, tinfo);
597  break;
599  if (is_leaf)
600  retval = tinfo->f_le(query, key->upper, collation, flinfo);
601  else
602  retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
603  || gbt_var_node_pf_match(key, query, tinfo);
604  break;
606  retval = !(tinfo->f_eq(query, key->lower, collation, flinfo) &&
607  tinfo->f_eq(query, key->upper, collation, flinfo));
608  break;
609  default:
610  retval = FALSE;
611  }
612 
613  return retval;
614 }
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
static bool gbt_var_node_pf_match(const GBT_VARKEY_R *node, const bytea *query, const gbtree_vinfo *tinfo)
bool(* f_lt)(const void *, const void *, Oid, FmgrInfo *)
bool(* f_gt)(const void *, const void *, Oid, FmgrInfo *)
int32(* f_cmp)(const void *, const void *, Oid, FmgrInfo *)
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
bool(* f_le)(const void *, const void *, Oid, FmgrInfo *)
bool(* f_ge)(const void *, const void *, Oid, FmgrInfo *)
#define FALSE
Definition: c.h:219
bool(* f_eq)(const void *, const void *, Oid, FmgrInfo *)
#define BtreeGistNotEqualStrategyNumber
Definition: btree_gist.h:10
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
Datum gbt_var_decompress ( PG_FUNCTION_ARGS  )

Definition at line 37 of file btree_utils_var.c.

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

38 {
39  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
40  GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
41 
42  if (key != (GBT_VARKEY *) DatumGetPointer(entry->key))
43  {
44  GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
45 
46  gistentryinit(*retval, PointerGetDatum(key),
47  entry->rel, entry->page,
48  entry->offset, FALSE);
49 
50  PG_RETURN_POINTER(retval);
51  }
52 
53  PG_RETURN_POINTER(entry);
54 }
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
#define DatumGetPointer(X)
Definition: postgres.h:555
void * palloc(Size size)
Definition: mcxt.c:848
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:205
Definition: c.h:433
OffsetNumber offset
Definition: gist.h:126
Datum gbt_var_fetch ( PG_FUNCTION_ARGS  )

Definition at line 307 of file btree_utils_var.c.

References gbt_var_key_readable(), gistentryinit, GISTENTRY::key, GBT_VARKEY_R::lower, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_DETOAST_DATUM, PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, GISTENTRY::rel, and TRUE.

308 {
309  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
310  GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
312  GISTENTRY *retval;
313 
314  retval = palloc(sizeof(GISTENTRY));
315  gistentryinit(*retval, PointerGetDatum(r.lower),
316  entry->rel, entry->page,
317  entry->offset, TRUE);
318 
319  PG_RETURN_POINTER(retval);
320 }
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
Datum key
Definition: gist.h:123
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
#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
Definition: c.h:433
#define TRUE
Definition: c.h:215
OffsetNumber offset
Definition: gist.h:126
GBT_VARKEY* gbt_var_key_copy ( const GBT_VARKEY_R u)

Definition at line 91 of file btree_utils_var.c.

References INTALIGN, GBT_VARKEY_R::lower, palloc0(), SET_VARSIZE, GBT_VARKEY_R::upper, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by gbt_bit_l2n(), gbt_numeric_penalty(), gbt_var_bin_union(), and gbt_var_union().

92 {
93  int32 lowersize = VARSIZE(u->lower);
94  int32 uppersize = VARSIZE(u->upper);
95  GBT_VARKEY *r;
96 
97  r = (GBT_VARKEY *) palloc0(INTALIGN(lowersize) + uppersize + VARHDRSZ);
98  memcpy(VARDATA(r), u->lower, lowersize);
99  memcpy(VARDATA(r) + INTALIGN(lowersize), u->upper, uppersize);
100  SET_VARSIZE(r, INTALIGN(lowersize) + uppersize + VARHDRSZ);
101 
102  return r;
103 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:439
#define INTALIGN(LEN)
Definition: c.h:573
signed int int32
Definition: c.h:246
void * palloc0(Size size)
Definition: mcxt.c:877
Definition: c.h:433
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
static GBT_VARKEY* gbt_var_key_from_datum ( const struct varlena u)
static

Definition at line 75 of file btree_utils_var.c.

References palloc(), SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by gbt_var_compress().

76 {
77  int32 lowersize = VARSIZE(u);
78  GBT_VARKEY *r;
79 
80  r = (GBT_VARKEY *) palloc(lowersize + VARHDRSZ);
81  memcpy(VARDATA(r), u, lowersize);
82  SET_VARSIZE(r, lowersize + VARHDRSZ);
83 
84  return r;
85 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:439
signed int int32
Definition: c.h:246
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:433
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
GBT_VARKEY_R gbt_var_key_readable ( const GBT_VARKEY k)

Definition at line 58 of file btree_utils_var.c.

References INTALIGN, GBT_VARKEY_R::lower, GBT_VARKEY_R::upper, VARHDRSZ, and VARSIZE.

Referenced by gbt_bit_consistent(), gbt_bit_l2n(), gbt_bpchar_consistent(), gbt_bytea_consistent(), gbt_numeric_consistent(), gbt_numeric_penalty(), gbt_text_consistent(), gbt_var_bin_union(), gbt_var_fetch(), gbt_var_node_cp_len(), gbt_var_node_truncate(), gbt_var_penalty(), gbt_var_picksplit(), gbt_var_same(), gbt_var_union(), and gbt_vsrt_cmp().

59 {
60  GBT_VARKEY_R r;
61 
62  r.lower = (bytea *) &(((char *) k)[VARHDRSZ]);
63  if (VARSIZE(k) > (VARHDRSZ + (VARSIZE(r.lower))))
64  r.upper = (bytea *) &(((char *) k)[VARHDRSZ + INTALIGN(VARSIZE(r.lower))]);
65  else
66  r.upper = r.lower;
67  return r;
68 }
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:439
#define INTALIGN(LEN)
Definition: c.h:573
Definition: c.h:433
static GBT_VARKEY* gbt_var_leaf2node ( GBT_VARKEY leaf,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)
static

Definition at line 107 of file btree_utils_var.c.

References gbtree_vinfo::f_l2n.

Referenced by gbt_var_bin_union(), gbt_var_penalty(), and gbt_var_picksplit().

108 {
109  GBT_VARKEY *out = leaf;
110 
111  if (tinfo->f_l2n)
112  out = tinfo->f_l2n(leaf, flinfo);
113 
114  return out;
115 }
GBT_VARKEY *(* f_l2n)(GBT_VARKEY *, FmgrInfo *flinfo)
Definition: c.h:433
static int32 gbt_var_node_cp_len ( const GBT_VARKEY node,
const gbtree_vinfo tinfo 
)
static

Definition at line 122 of file btree_utils_var.c.

References gbtree_vinfo::eml, gbt_var_key_readable(), i, GBT_VARKEY_R::lower, Min, pg_mblen(), GBT_VARKEY_R::upper, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by gbt_var_penalty(), gbt_var_picksplit(), and gbt_var_union().

123 {
125  int32 i = 0;
126  int32 l = 0;
127  int32 t1len = VARSIZE(r.lower) - VARHDRSZ;
128  int32 t2len = VARSIZE(r.upper) - VARHDRSZ;
129  int32 ml = Min(t1len, t2len);
130  char *p1 = VARDATA(r.lower);
131  char *p2 = VARDATA(r.upper);
132 
133  if (ml == 0)
134  return 0;
135 
136  while (i < ml)
137  {
138  if (tinfo->eml > 1 && l == 0)
139  {
140  if ((l = pg_mblen(p1)) != pg_mblen(p2))
141  {
142  return i;
143  }
144  }
145  if (*p1 != *p2)
146  {
147  if (tinfo->eml > 1)
148  {
149  return (i - l + 1);
150  }
151  else
152  {
153  return i;
154  }
155  }
156 
157  p1++;
158  p2++;
159  l--;
160  i++;
161  }
162  return ml; /* lower == upper */
163 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:439
#define Min(x, y)
Definition: c.h:812
signed int int32
Definition: c.h:246
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760
int i
static bool gbt_var_node_pf_match ( const GBT_VARKEY_R node,
const bytea query,
const gbtree_vinfo tinfo 
)
static

Definition at line 192 of file btree_utils_var.c.

References gbt_bytea_pf_match(), GBT_VARKEY_R::lower, gbtree_vinfo::trnc, and GBT_VARKEY_R::upper.

Referenced by gbt_var_consistent().

193 {
194  return (tinfo->trnc && (
195  gbt_bytea_pf_match(node->lower, query, tinfo) ||
196  gbt_bytea_pf_match(node->upper, query, tinfo)
197  ));
198 }
static bool gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)
static GBT_VARKEY* gbt_var_node_truncate ( const GBT_VARKEY node,
int32  cpf_length,
const gbtree_vinfo tinfo 
)
static

Definition at line 206 of file btree_utils_var.c.

References gbt_var_key_readable(), INTALIGN, GBT_VARKEY_R::lower, Min, palloc0(), SET_VARSIZE, GBT_VARKEY_R::upper, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by gbt_var_picksplit(), and gbt_var_union().

207 {
208  GBT_VARKEY *out = NULL;
210  int32 len1 = VARSIZE(r.lower) - VARHDRSZ;
211  int32 len2 = VARSIZE(r.upper) - VARHDRSZ;
212  int32 si;
213  char *out2;
214 
215  len1 = Min(len1, (cpf_length + 1));
216  len2 = Min(len2, (cpf_length + 1));
217 
218  si = 2 * VARHDRSZ + INTALIGN(len1 + VARHDRSZ) + len2;
219  out = (GBT_VARKEY *) palloc0(si);
220  SET_VARSIZE(out, si);
221 
222  memcpy(VARDATA(out), r.lower, len1 + VARHDRSZ);
223  SET_VARSIZE(VARDATA(out), len1 + VARHDRSZ);
224 
225  out2 = VARDATA(out) + INTALIGN(len1 + VARHDRSZ);
226  memcpy(out2, r.upper, len2 + VARHDRSZ);
227  SET_VARSIZE(out2, len2 + VARHDRSZ);
228 
229  return out;
230 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:439
#define Min(x, y)
Definition: c.h:812
#define INTALIGN(LEN)
Definition: c.h:573
signed int int32
Definition: c.h:246
void * palloc0(Size size)
Definition: mcxt.c:877
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
Definition: c.h:433
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
float* gbt_var_penalty ( float *  res,
const GISTENTRY o,
const GISTENTRY n,
Oid  collation,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 380 of file btree_utils_var.c.

References Abs, DatumGetPointer, gbtree_vinfo::f_cmp, gbt_bytea_pf_match(), gbt_var_bin_union(), gbt_var_key_readable(), gbt_var_leaf2node(), gbt_var_node_cp_len(), GISTENTRY::key, GBT_VARKEY_R::lower, tupleDesc::natts, PointerGetDatum, RelationData::rd_att, GISTENTRY::rel, GBT_VARKEY_R::upper, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by gbt_bit_penalty(), gbt_bytea_penalty(), and gbt_text_penalty().

382 {
383  GBT_VARKEY *orge = (GBT_VARKEY *) DatumGetPointer(o->key);
384  GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
385  GBT_VARKEY_R ok,
386  nk;
387 
388  *res = 0.0;
389 
390  nk = gbt_var_key_readable(newe);
391  if (nk.lower == nk.upper) /* leaf */
392  {
393  GBT_VARKEY *tmp;
394 
395  tmp = gbt_var_leaf2node(newe, tinfo, flinfo);
396  if (tmp != newe)
397  nk = gbt_var_key_readable(tmp);
398  }
399  ok = gbt_var_key_readable(orge);
400 
401  if ((VARSIZE(ok.lower) - VARHDRSZ) == 0 && (VARSIZE(ok.upper) - VARHDRSZ) == 0)
402  *res = 0.0;
403  else if (!((tinfo->f_cmp(nk.lower, ok.lower, collation, flinfo) >= 0 ||
404  gbt_bytea_pf_match(ok.lower, nk.lower, tinfo)) &&
405  (tinfo->f_cmp(nk.upper, ok.upper, collation, flinfo) <= 0 ||
406  gbt_bytea_pf_match(ok.upper, nk.upper, tinfo))))
407  {
408  Datum d = PointerGetDatum(0);
409  double dres;
410  int32 ol,
411  ul;
412 
413  gbt_var_bin_union(&d, orge, collation, tinfo, flinfo);
414  ol = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
415  gbt_var_bin_union(&d, newe, collation, tinfo, flinfo);
416  ul = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
417 
418  if (ul < ol)
419  {
420  dres = (ol - ul); /* reduction of common prefix len */
421  }
422  else
423  {
425  unsigned char tmp[4];
426 
427  tmp[0] = (unsigned char) (((VARSIZE(ok.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.lower)[ul]));
428  tmp[1] = (unsigned char) (((VARSIZE(uk.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.lower)[ul]));
429  tmp[2] = (unsigned char) (((VARSIZE(ok.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.upper)[ul]));
430  tmp[3] = (unsigned char) (((VARSIZE(uk.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.upper)[ul]));
431  dres = Abs(tmp[0] - tmp[1]) + Abs(tmp[3] - tmp[2]);
432  dres /= 256.0;
433  }
434 
435  *res += FLT_MIN;
436  *res += (float) (dres / ((double) (ol + 1)));
437  *res *= (FLT_MAX / (o->rel->rd_att->natts + 1));
438  }
439 
440  return res;
441 }
Relation rel
Definition: gist.h:124
static GBT_VARKEY * gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define PointerGetDatum(X)
Definition: postgres.h:562
#define VARHDRSZ
Definition: c.h:439
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
int32(* f_cmp)(const void *, const void *, Oid, FmgrInfo *)
static int32 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
int natts
Definition: tupdesc.h:73
signed int int32
Definition: c.h:246
#define Abs(x)
Definition: c.h:818
Datum key
Definition: gist.h:123
static bool gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)
uintptr_t Datum
Definition: postgres.h:372
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
TupleDesc rd_att
Definition: rel.h:115
#define DatumGetPointer(X)
Definition: postgres.h:555
Definition: c.h:433
GIST_SPLITVEC* gbt_var_picksplit ( const GistEntryVector entryvec,
GIST_SPLITVEC v,
Oid  collation,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 460 of file btree_utils_var.c.

References gbt_vsrt_arg::collation, cur, DatumGetPointer, FirstOffsetNumber, gbt_vsrt_arg::flinfo, gbt_var_bin_union(), gbt_var_key_readable(), gbt_var_leaf2node(), gbt_var_node_cp_len(), gbt_var_node_truncate(), gbt_vsrt_cmp(), Vsrt::i, i, GISTENTRY::key, GBT_VARKEY_R::lower, Max, GistEntryVector::n, OffsetNumberNext, palloc(), PointerGetDatum, qsort_arg(), GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, Vsrt::t, gbt_vsrt_arg::tinfo, tinfo, gbtree_vinfo::trnc, GBT_VARKEY_R::upper, and GistEntryVector::vector.

Referenced by gbt_bit_picksplit(), gbt_bytea_picksplit(), gbt_numeric_picksplit(), and gbt_text_picksplit().

462 {
463  OffsetNumber i,
464  maxoff = entryvec->n - 1;
465  Vsrt *arr;
466  int svcntr = 0,
467  nbytes;
468  char *cur;
469  GBT_VARKEY **sv = NULL;
470  gbt_vsrt_arg varg;
471 
472  arr = (Vsrt *) palloc((maxoff + 1) * sizeof(Vsrt));
473  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
474  v->spl_left = (OffsetNumber *) palloc(nbytes);
475  v->spl_right = (OffsetNumber *) palloc(nbytes);
476  v->spl_ldatum = PointerGetDatum(0);
477  v->spl_rdatum = PointerGetDatum(0);
478  v->spl_nleft = 0;
479  v->spl_nright = 0;
480 
481  sv = palloc(sizeof(bytea *) * (maxoff + 1));
482 
483  /* Sort entries */
484 
485  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
486  {
487  GBT_VARKEY_R ro;
488 
489  cur = (char *) DatumGetPointer(entryvec->vector[i].key);
490  ro = gbt_var_key_readable((GBT_VARKEY *) cur);
491  if (ro.lower == ro.upper) /* leaf */
492  {
493  sv[svcntr] = gbt_var_leaf2node((GBT_VARKEY *) cur, tinfo, flinfo);
494  arr[i].t = sv[svcntr];
495  if (sv[svcntr] != (GBT_VARKEY *) cur)
496  svcntr++;
497  }
498  else
499  arr[i].t = (GBT_VARKEY *) cur;
500  arr[i].i = i;
501  }
502 
503  /* sort */
504  varg.tinfo = tinfo;
505  varg.collation = collation;
506  varg.flinfo = flinfo;
507  qsort_arg((void *) &arr[FirstOffsetNumber],
508  maxoff - FirstOffsetNumber + 1,
509  sizeof(Vsrt),
510  gbt_vsrt_cmp,
511  (void *) &varg);
512 
513  /* We do simply create two parts */
514 
515  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
516  {
517  if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
518  {
519  gbt_var_bin_union(&v->spl_ldatum, arr[i].t, collation, tinfo, flinfo);
520  v->spl_left[v->spl_nleft] = arr[i].i;
521  v->spl_nleft++;
522  }
523  else
524  {
525  gbt_var_bin_union(&v->spl_rdatum, arr[i].t, collation, tinfo, flinfo);
526  v->spl_right[v->spl_nright] = arr[i].i;
527  v->spl_nright++;
528  }
529  }
530 
531  /* Truncate (=compress) key */
532  if (tinfo->trnc)
533  {
536  GBT_VARKEY *dl;
537  GBT_VARKEY *dr;
538 
539  ll = Max(ll, lr);
540  ll++;
541 
544  v->spl_ldatum = PointerGetDatum(dl);
545  v->spl_rdatum = PointerGetDatum(dr);
546  }
547 
548  return v;
549 }
static GBT_VARKEY * gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
#define PointerGetDatum(X)
Definition: postgres.h:562
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
OffsetNumber * spl_left
Definition: gist.h:105
Datum spl_rdatum
Definition: gist.h:112
int32 n
Definition: gist.h:160
struct cursor * cur
Definition: ecpg.c:28
static int gbt_vsrt_cmp(const void *a, const void *b, void *arg)
static int32 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
int spl_nleft
Definition: gist.h:106
signed int int32
Definition: c.h:246
uint16 OffsetNumber
Definition: off.h:24
GISTENTRY vector[FLEXIBLE_ARRAY_MEMBER]
Definition: gist.h:161
int spl_nright
Definition: gist.h:111
Datum key
Definition: gist.h:123
#define FirstOffsetNumber
Definition: off.h:27
GBT_VARKEY * t
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Definition: qsort_arg.c:113
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
static GBT_VARKEY * gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
#define Max(x, y)
Definition: c.h:806
Datum spl_ldatum
Definition: gist.h:107
static const gbtree_vinfo tinfo
Definition: btree_bit.c:110
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
OffsetNumber * spl_right
Definition: gist.h:110
#define DatumGetPointer(X)
Definition: postgres.h:555
FmgrInfo * flinfo
void * palloc(Size size)
Definition: mcxt.c:848
int i
const gbtree_vinfo * tinfo
Definition: c.h:433
bool gbt_var_same ( Datum  d1,
Datum  d2,
Oid  collation,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 363 of file btree_utils_var.c.

References DatumGetPointer, gbtree_vinfo::f_cmp, gbt_var_key_readable(), GBT_VARKEY_R::lower, and GBT_VARKEY_R::upper.

Referenced by gbt_bit_same(), gbt_bytea_same(), gbt_numeric_same(), and gbt_text_same().

365 {
366  GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1);
367  GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2);
368  GBT_VARKEY_R r1,
369  r2;
370 
371  r1 = gbt_var_key_readable(t1);
372  r2 = gbt_var_key_readable(t2);
373 
374  return (tinfo->f_cmp(r1.lower, r2.lower, collation, flinfo) == 0 &&
375  tinfo->f_cmp(r1.upper, r2.upper, collation, flinfo) == 0);
376 }
int32(* f_cmp)(const void *, const void *, Oid, FmgrInfo *)
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
#define DatumGetPointer(X)
Definition: postgres.h:555
Definition: c.h:433
GBT_VARKEY* gbt_var_union ( const GistEntryVector entryvec,
int32 size,
Oid  collation,
const gbtree_vinfo tinfo,
FmgrInfo flinfo 
)

Definition at line 324 of file btree_utils_var.c.

References cur, DatumGetPointer, gbt_var_bin_union(), gbt_var_key_copy(), gbt_var_key_readable(), gbt_var_node_cp_len(), gbt_var_node_truncate(), i, GISTENTRY::key, GistEntryVector::n, PointerGetDatum, gbtree_vinfo::trnc, and GistEntryVector::vector.

Referenced by gbt_bit_union(), gbt_bytea_union(), gbt_numeric_union(), and gbt_text_union().

326 {
327  int i = 0,
328  numranges = entryvec->n;
329  GBT_VARKEY *cur;
330  Datum out;
331  GBT_VARKEY_R rk;
332 
333  *size = sizeof(GBT_VARKEY);
334 
335  cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[0].key);
336  rk = gbt_var_key_readable(cur);
337  out = PointerGetDatum(gbt_var_key_copy(&rk));
338 
339  for (i = 1; i < numranges; i++)
340  {
341  cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[i].key);
342  gbt_var_bin_union(&out, cur, collation, tinfo, flinfo);
343  }
344 
345 
346  /* Truncate (=compress) key */
347  if (tinfo->trnc)
348  {
349  int32 plen;
350  GBT_VARKEY *trc = NULL;
351 
352  plen = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(out), tinfo);
353  trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo);
354 
355  out = PointerGetDatum(trc);
356  }
357 
358  return ((GBT_VARKEY *) DatumGetPointer(out));
359 }
#define PointerGetDatum(X)
Definition: postgres.h:562
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
int32 n
Definition: gist.h:160
struct cursor * cur
Definition: ecpg.c:28
bytea GBT_VARKEY
static int32 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
signed int int32
Definition: c.h:246
GISTENTRY vector[FLEXIBLE_ARRAY_MEMBER]
Definition: gist.h:161
Datum key
Definition: gist.h:123
uintptr_t Datum
Definition: postgres.h:372
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
static GBT_VARKEY * gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
GBT_VARKEY * gbt_var_key_copy(const GBT_VARKEY_R *u)
#define DatumGetPointer(X)
Definition: postgres.h:555
int i
Definition: c.h:433
static int gbt_vsrt_cmp ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 445 of file btree_utils_var.c.

References gbt_vsrt_arg::collation, gbtree_vinfo::f_cmp, gbt_vsrt_arg::flinfo, gbt_var_key_readable(), GBT_VARKEY_R::lower, gbt_vsrt_arg::tinfo, and GBT_VARKEY_R::upper.

Referenced by gbt_var_picksplit().

446 {
447  GBT_VARKEY_R ar = gbt_var_key_readable(((const Vsrt *) a)->t);
448  GBT_VARKEY_R br = gbt_var_key_readable(((const Vsrt *) b)->t);
449  const gbt_vsrt_arg *varg = (const gbt_vsrt_arg *) arg;
450  int res;
451 
452  res = varg->tinfo->f_cmp(ar.lower, br.lower, varg->collation, varg->flinfo);
453  if (res == 0)
454  return varg->tinfo->f_cmp(ar.upper, br.upper, varg->collation, varg->flinfo);
455 
456  return res;
457 }
int32(* f_cmp)(const void *, const void *, Oid, FmgrInfo *)
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
FmgrInfo * flinfo
const gbtree_vinfo * tinfo
void * arg
PG_FUNCTION_INFO_V1 ( gbt_var_decompress  )
PG_FUNCTION_INFO_V1 ( gbt_var_fetch  )