PostgreSQL Source Code  git master
btree_bit.c
Go to the documentation of this file.
1 /*
2  * contrib/btree_gist/btree_bit.c
3  */
4 #include "postgres.h"
5 
6 #include "btree_gist.h"
7 #include "btree_utils_var.h"
8 #include "utils/builtins.h"
9 #include "utils/bytea.h"
10 #include "utils/varbit.h"
11 
12 
13 /*
14 ** Bit ops
15 */
22 
23 
24 /* define for comparison */
25 
26 static bool
27 gbt_bitgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
28 {
31  PointerGetDatum(b)));
32 }
33 
34 static bool
35 gbt_bitge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
36 {
39  PointerGetDatum(b)));
40 }
41 
42 static bool
43 gbt_biteq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
44 {
47  PointerGetDatum(b)));
48 }
49 
50 static bool
51 gbt_bitle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
52 {
55  PointerGetDatum(b)));
56 }
57 
58 static bool
59 gbt_bitlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
60 {
63  PointerGetDatum(b)));
64 }
65 
66 static int32
67 gbt_bitcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
68 {
71  PointerGetDatum(b)));
72 }
73 
74 
75 static bytea *
77 {
78  bytea *out = leaf;
79  int sz = VARBITBYTES(leaf) + VARHDRSZ;
80  int padded_sz = INTALIGN(sz);
81 
82  out = (bytea *) palloc(padded_sz);
83  /* initialize the padding bytes to zero */
84  while (sz < padded_sz)
85  ((char *) out)[sz++] = 0;
86  SET_VARSIZE(out, padded_sz);
87  memcpy(VARDATA(out), VARBITS(leaf), VARBITBYTES(leaf));
88  return out;
89 }
90 
91 
92 
93 
94 static GBT_VARKEY *
96 {
97  GBT_VARKEY *out = leaf;
99  bytea *o;
100 
101  o = gbt_bit_xfrm(r.lower);
102  r.upper = r.lower = o;
103  out = gbt_var_key_copy(&r);
104  pfree(o);
105 
106  return out;
107 }
108 
109 static const gbtree_vinfo tinfo =
110 {
111  gbt_t_bit,
112  0,
113  true,
114  gbt_bitgt,
115  gbt_bitge,
116  gbt_biteq,
117  gbt_bitle,
118  gbt_bitlt,
119  gbt_bitcmp,
121 };
122 
123 
124 /**************************************************
125  * Bit ops
126  **************************************************/
127 
128 Datum
130 {
131  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132 
134 }
135 
136 Datum
138 {
139  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140  void *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
142 
143  /* Oid subtype = PG_GETARG_OID(3); */
144  bool *recheck = (bool *) PG_GETARG_POINTER(4);
145  bool retval;
148 
149  /* All cases served by this function are exact */
150  *recheck = false;
151 
152  if (GIST_LEAF(entry))
153  retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
154  true, &tinfo, fcinfo->flinfo);
155  else
156  {
157  bytea *q = gbt_bit_xfrm((bytea *) query);
158 
159  retval = gbt_var_consistent(&r, q, strategy, PG_GET_COLLATION(),
160  false, &tinfo, fcinfo->flinfo);
161  }
162  PG_RETURN_BOOL(retval);
163 }
164 
165 
166 
167 Datum
169 {
171  int32 *size = (int *) PG_GETARG_POINTER(1);
172 
174  &tinfo, fcinfo->flinfo));
175 }
176 
177 
178 Datum
180 {
183 
184  gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
185  &tinfo, fcinfo->flinfo);
187 }
188 
189 Datum
191 {
192  Datum d1 = PG_GETARG_DATUM(0);
193  Datum d2 = PG_GETARG_DATUM(1);
194  bool *result = (bool *) PG_GETARG_POINTER(2);
195 
196  *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
197  PG_RETURN_POINTER(result);
198 }
199 
200 
201 Datum
203 {
206  float *result = (float *) PG_GETARG_POINTER(2);
207 
209  &tinfo, fcinfo->flinfo));
210 }
static bool gbt_bitge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_bit.c:35
Datum gbt_bit_same(PG_FUNCTION_ARGS)
Definition: btree_bit.c:190
static bool gbt_bitgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_bit.c:27
static int32 gbt_bitcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_bit.c:67
PG_FUNCTION_INFO_V1(gbt_bit_compress)
static bool gbt_bitle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_bit.c:51
static const gbtree_vinfo tinfo
Definition: btree_bit.c:109
static bytea * gbt_bit_xfrm(bytea *leaf)
Definition: btree_bit.c:76
static GBT_VARKEY * gbt_bit_l2n(GBT_VARKEY *leaf, FmgrInfo *flinfo)
Definition: btree_bit.c:95
Datum gbt_bit_consistent(PG_FUNCTION_ARGS)
Definition: btree_bit.c:137
Datum gbt_bit_penalty(PG_FUNCTION_ARGS)
Definition: btree_bit.c:202
Datum gbt_bit_compress(PG_FUNCTION_ARGS)
Definition: btree_bit.c:129
static bool gbt_biteq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_bit.c:43
static bool gbt_bitlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition: btree_bit.c:59
Datum gbt_bit_union(PG_FUNCTION_ARGS)
Definition: btree_bit.c:168
Datum gbt_bit_picksplit(PG_FUNCTION_ARGS)
Definition: btree_bit.c:179
@ gbt_t_bit
Definition: btree_gist.h:34
GBT_VARKEY * gbt_var_union(const GistEntryVector *entryvec, int32 *size, 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)
float * gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
GIST_SPLITVEC * gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
GBT_VARKEY * gbt_var_key_copy(const GBT_VARKEY_R *u)
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
bool gbt_var_same(Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
#define INTALIGN(LEN)
Definition: c.h:808
signed int int32
Definition: c.h:494
#define VARHDRSZ
Definition: c.h:692
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define DatumGetByteaP(X)
Definition: fmgr.h:331
#define GIST_LEAF(entry)
Definition: gist.h:170
int b
Definition: isn.c:70
int a
Definition: isn.c:69
void pfree(void *pointer)
Definition: mcxt.c:1520
void * palloc(Size size)
Definition: mcxt.c:1316
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
unsigned int Oid
Definition: postgres_ext.h:31
static pg_noinline void Size size
Definition: slab.c:607
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
Datum key
Definition: gist.h:160
Definition: c.h:687
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305
Datum bitge(PG_FUNCTION_ARGS)
Definition: varbit.c:934
Datum bitle(PG_FUNCTION_ARGS)
Definition: varbit.c:904
Datum biteq(PG_FUNCTION_ARGS)
Definition: varbit.c:841
Datum bitlt(PG_FUNCTION_ARGS)
Definition: varbit.c:889
Datum bitgt(PG_FUNCTION_ARGS)
Definition: varbit.c:919
#define VARBITBYTES(PTR)
Definition: varbit.h:73
#define VARBITS(PTR)
Definition: varbit.h:71
Datum byteacmp(PG_FUNCTION_ARGS)
Definition: varlena.c:3938