PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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)
28 {
30  PointerGetDatum(a),
31  PointerGetDatum(b)));
32 }
33 
34 static bool
35 gbt_bitge(const void *a, const void *b, Oid collation)
36 {
38  PointerGetDatum(a),
39  PointerGetDatum(b)));
40 }
41 
42 static bool
43 gbt_biteq(const void *a, const void *b, Oid collation)
44 {
46  PointerGetDatum(a),
47  PointerGetDatum(b)));
48 }
49 
50 static bool
51 gbt_bitle(const void *a, const void *b, Oid collation)
52 {
54  PointerGetDatum(a),
55  PointerGetDatum(b)));
56 }
57 
58 static bool
59 gbt_bitlt(const void *a, const void *b, Oid collation)
60 {
62  PointerGetDatum(a),
63  PointerGetDatum(b)));
64 }
65 
66 static int32
67 gbt_bitcmp(const void *a, const void *b, Oid collation)
68 {
70  PointerGetDatum(a),
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((void *) VARDATA(out), (void *) 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 
110 static const gbtree_vinfo tinfo =
111 {
112  gbt_t_bit,
113  0,
114  TRUE,
115  gbt_bitgt,
116  gbt_bitge,
117  gbt_biteq,
118  gbt_bitle,
119  gbt_bitlt,
120  gbt_bitcmp,
122 };
123 
124 
125 /**************************************************
126  * Bit ops
127  **************************************************/
128 
129 Datum
131 {
132  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
133 
134  PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
135 }
136 
137 Datum
139 {
140  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
141  void *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
143 
144  /* Oid subtype = PG_GETARG_OID(3); */
145  bool *recheck = (bool *) PG_GETARG_POINTER(4);
146  bool retval;
147  GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
149 
150  /* All cases served by this function are exact */
151  *recheck = false;
152 
153  if (GIST_LEAF(entry))
154  retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
155  TRUE, &tinfo);
156  else
157  {
158  bytea *q = gbt_bit_xfrm((bytea *) query);
159 
160  retval = gbt_var_consistent(&r, q, strategy, PG_GET_COLLATION(),
161  FALSE, &tinfo);
162  }
163  PG_RETURN_BOOL(retval);
164 }
165 
166 
167 
168 Datum
170 {
172  int32 *size = (int *) PG_GETARG_POINTER(1);
173 
175  &tinfo));
176 }
177 
178 
179 Datum
181 {
184 
185  gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
186  &tinfo);
188 }
189 
190 Datum
192 {
193  Datum d1 = PG_GETARG_DATUM(0);
194  Datum d2 = PG_GETARG_DATUM(1);
195  bool *result = (bool *) PG_GETARG_POINTER(2);
196 
197  *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo);
198  PG_RETURN_POINTER(result);
199 }
200 
201 
202 Datum
204 {
207  float *result = (float *) PG_GETARG_POINTER(2);
208 
210  &tinfo));
211 }
#define GIST_LEAF(entry)
Definition: gist.h:133
static bool gbt_bitle(const void *a, const void *b, Oid collation)
Definition: btree_bit.c:51
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define VARBITS(PTR)
Definition: varbit.h:51
Datum bitle(PG_FUNCTION_ARGS)
Definition: varbit.c:883
#define VARDATA(PTR)
Definition: postgres.h:305
#define DatumGetInt32(X)
Definition: postgres.h:480
#define VARBITBYTES(PTR)
Definition: varbit.h:53
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define VARHDRSZ
Definition: c.h:441
bool gbt_var_same(Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo)
uint16 StrategyNumber
Definition: stratnum.h:22
bool gbt_var_consistent(GBT_VARKEY_R *key, const void *query, StrategyNumber strategy, Oid collation, bool is_leaf, const gbtree_vinfo *tinfo)
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define INTALIGN(LEN)
Definition: c.h:581
unsigned int Oid
Definition: postgres_ext.h:31
Datum bitge(PG_FUNCTION_ARGS)
Definition: varbit.c:913
#define PG_GET_COLLATION()
Definition: fmgr.h:155
Datum gbt_bit_penalty(PG_FUNCTION_ARGS)
Definition: btree_bit.c:203
signed int int32
Definition: c.h:253
static bool gbt_bitgt(const void *a, const void *b, Oid collation)
Definition: btree_bit.c:27
float * gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n, Oid collation, const gbtree_vinfo *tinfo)
Datum gbt_bit_compress(PG_FUNCTION_ARGS)
Definition: btree_bit.c:130
void pfree(void *pointer)
Definition: mcxt.c:992
Datum gbt_bit_consistent(PG_FUNCTION_ARGS)
Definition: btree_bit.c:138
#define FALSE
Definition: c.h:218
Datum bitlt(PG_FUNCTION_ARGS)
Definition: varbit.c:868
#define DatumGetByteaP(X)
Definition: fmgr.h:246
Datum key
Definition: gist.h:123
#define DatumGetBool(X)
Definition: postgres.h:401
static bool gbt_biteq(const void *a, const void *b, Oid collation)
Definition: btree_bit.c:43
PG_FUNCTION_INFO_V1(gbt_bit_compress)
GIST_SPLITVEC * gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, Oid collation, const gbtree_vinfo *tinfo)
static int32 gbt_bitcmp(const void *a, const void *b, Oid collation)
Definition: btree_bit.c:67
Datum gbt_bit_union(PG_FUNCTION_ARGS)
Definition: btree_bit.c:169
Datum bitgt(PG_FUNCTION_ARGS)
Definition: varbit.c:898
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
GBT_VARKEY * gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation, const gbtree_vinfo *tinfo)
Datum biteq(PG_FUNCTION_ARGS)
Definition: varbit.c:820
GISTENTRY * gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
static bytea * gbt_bit_xfrm(bytea *leaf)
Definition: btree_bit.c:76
Datum byteacmp(PG_FUNCTION_ARGS)
Definition: varlena.c:3496
static const gbtree_vinfo tinfo
Definition: btree_bit.c:110
GBT_VARKEY * gbt_var_key_copy(const GBT_VARKEY_R *u)
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:228
#define DatumGetPointer(X)
Definition: postgres.h:557
static bool gbt_bitge(const void *a, const void *b, Oid collation)
Definition: btree_bit.c:35
void * palloc(Size size)
Definition: mcxt.c:891
static bool gbt_bitlt(const void *a, const void *b, Oid collation)
Definition: btree_bit.c:59
Datum gbt_bit_same(PG_FUNCTION_ARGS)
Definition: btree_bit.c:191
Definition: c.h:435
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
#define TRUE
Definition: c.h:214
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
static GBT_VARKEY * gbt_bit_l2n(GBT_VARKEY *leaf)
Definition: btree_bit.c:95
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum gbt_bit_picksplit(PG_FUNCTION_ARGS)
Definition: btree_bit.c:180