PostgreSQL Source Code git master
Loading...
Searching...
No Matches
btree_bit.c
Go to the documentation of this file.
1/*
2 * contrib/btree_gist/btree_bit.c
3 *
4 * Support for bit and varbit types (which act the same for our purposes).
5 *
6 * Leaf-page keys are bit/varbit values, but internal-page keys are just
7 * bytea values containing the first N bytes of the represented bitstring.
8 * (In particular, they lack the bit_len field of a bit/varbit Datum.)
9 */
10#include "postgres.h"
11
12#include "btree_gist.h"
13#include "btree_utils_var.h"
14#include "utils/fmgrprotos.h"
15#include "utils/sortsupport.h"
16#include "utils/varbit.h"
17#include "varatt.h"
18
19/* GiST support functions */
28
29
30/* define for comparison */
31
32static bool
33gbt_bitgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
34{
38}
39
40static bool
41gbt_bitge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
42{
46}
47
48static bool
49gbt_biteq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
50{
54}
55
56static bool
57gbt_bitle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
58{
62}
63
64static bool
65gbt_bitlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
66{
70}
71
72/*
73 * Notice we use byteacmp here, not bitcmp as you might expect.
74 * That's because internal-page keys are bytea.
75 */
76static int32
77gbt_bitcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
78{
82}
83
84
85/*
86 * Convert a leaf-page bit/varbit value to internal-page form.
87 *
88 * The important change here is to remove the bit_len field so that
89 * what we have left looks like a bytea.
90 *
91 * The business with padding to INTALIGN length appears entirely historical,
92 * but we can't remove that without breaking on-disk compatibility.
93 * For example, if the lower bound of some leaf page is the 20-bit string
94 * of all ones, with data contents FFFFF0, this code pads it to bytea FFFFF000
95 * in the internal key representing that page. If, when we search the index
96 * for that value, we did not again pad to FFFFF000, then byteacmp would
97 * say that the query is strictly less than the lower bound so we would not
98 * descend to that leaf page.
99 */
100static bytea *
102{
103 bytea *out;
104 int sz = VARBITBYTES(leaf) + VARHDRSZ;
105 int padded_sz = INTALIGN(sz);
106
107 out = (bytea *) palloc(padded_sz);
108 /* initialize the padding bytes to zero */
109 while (sz < padded_sz)
110 ((char *) out)[sz++] = 0;
113 return out;
114}
115
116/*
117 * Convert a GBT_VARKEY representation of a leaf key to a palloc'd
118 * GBT_VARKEY representation of an internal key.
119 * We assume lower == upper since it's a leaf key.
120 */
121static GBT_VARKEY *
123{
124 GBT_VARKEY *out;
126 bytea *o;
127
128 o = gbt_bit_xfrm((VarBit *) r.lower);
129 r.upper = r.lower = o;
130 out = gbt_var_key_copy(&r);
131 pfree(o);
132
133 return out;
134}
135
136static const gbtree_vinfo tinfo =
137{
138 gbt_t_bit,
139 true, /* internal keys can be truncated */
140 gbt_bitgt,
141 gbt_bitge,
142 gbt_biteq,
143 gbt_bitle,
144 gbt_bitlt,
146 gbt_bit_l2n /* leaf to internal transformation */
147};
148
149
150/**************************************************
151 * GiST support functions
152 **************************************************/
153
154Datum
161
162Datum
164{
165 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
166 VarBit *query = PG_GETARG_VARBIT_P(1);
168#ifdef NOT_USED
169 Oid subtype = PG_GETARG_OID(3);
170#endif
171 bool *recheck = (bool *) PG_GETARG_POINTER(4);
172 bool retval;
173 GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
175
176 /* All cases served by this function are exact */
177 *recheck = false;
178
179 if (GIST_LEAF(entry))
180 retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
181 true, &tinfo, fcinfo->flinfo);
182 else
183 {
184 /* Must convert to internal form to compare to internal-page entries */
185 bytea *q = gbt_bit_xfrm(query);
186
187 retval = gbt_var_consistent(&r, q, strategy, PG_GET_COLLATION(),
188 false, &tinfo, fcinfo->flinfo);
189 }
190 PG_RETURN_BOOL(retval);
191}
192
193Datum
202
203Datum
213
214Datum
216{
217 Datum d1 = PG_GETARG_DATUM(0);
218 Datum d2 = PG_GETARG_DATUM(1);
219 bool *result = (bool *) PG_GETARG_POINTER(2);
220
221 *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
223}
224
225Datum
227{
230 float *result = (float *) PG_GETARG_POINTER(2);
231
233 &tinfo, fcinfo->flinfo));
234}
235
236static int
238{
241
245
246 /* for leaf items we expect lower == upper, so only compare lower */
248 PointerGetDatum(arg1.lower),
249 PointerGetDatum(arg2.lower));
250
253
254 return DatumGetInt32(result);
255}
256
257Datum
267
268Datum
static bool gbt_bitge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition btree_bit.c:41
Datum gbt_bit_same(PG_FUNCTION_ARGS)
Definition btree_bit.c:215
static bool gbt_bitgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition btree_bit.c:33
static int32 gbt_bitcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition btree_bit.c:77
static bytea * gbt_bit_xfrm(VarBit *leaf)
Definition btree_bit.c:101
Datum gbt_bit_sortsupport(PG_FUNCTION_ARGS)
Definition btree_bit.c:258
Datum gbt_varbit_sortsupport(PG_FUNCTION_ARGS)
Definition btree_bit.c:269
static bool gbt_bitle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition btree_bit.c:57
static const gbtree_vinfo tinfo
Definition btree_bit.c:136
Datum gbt_bit_consistent(PG_FUNCTION_ARGS)
Definition btree_bit.c:163
Datum gbt_bit_penalty(PG_FUNCTION_ARGS)
Definition btree_bit.c:226
Datum gbt_bit_compress(PG_FUNCTION_ARGS)
Definition btree_bit.c:155
static bool gbt_biteq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition btree_bit.c:49
static bool gbt_bitlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
Definition btree_bit.c:65
static GBT_VARKEY * gbt_bit_l2n(GBT_VARKEY *leaf, FmgrInfo *flinfo)
Definition btree_bit.c:122
static int gbt_bit_ssup_cmp(Datum x, Datum y, SortSupport ssup)
Definition btree_bit.c:237
Datum gbt_bit_union(PG_FUNCTION_ARGS)
Definition btree_bit.c:194
Datum gbt_bit_picksplit(PG_FUNCTION_ARGS)
Definition btree_bit.c:204
@ 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(const GBT_VARKEY_R *key, const void *query, StrategyNumber strategy, Oid collation, bool is_leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
GIST_SPLITVEC * gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY *k)
float * gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
GBT_VARKEY * gbt_var_key_copy(const GBT_VARKEY_R *u)
bool gbt_var_same(Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
#define GBT_FREE_IF_COPY(ptr1, ptr2)
Datum byteacmp(PG_FUNCTION_ARGS)
Definition bytea.c:959
#define INTALIGN(LEN)
Definition c.h:952
#define VARHDRSZ
Definition c.h:840
int32_t int32
Definition c.h:679
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
struct SortSupportData * SortSupport
Definition execnodes.h:61
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:690
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_GETARG_UINT16(n)
Definition fmgr.h:272
#define PG_DETOAST_DATUM(datum)
Definition fmgr.h:240
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_GET_COLLATION()
Definition fmgr.h:198
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define GIST_LEAF(entry)
Definition gist.h:171
int y
Definition isn.c:76
int b
Definition isn.c:74
int x
Definition isn.c:75
int a
Definition isn.c:73
void pfree(void *pointer)
Definition mcxt.c:1619
void * palloc(Size size)
Definition mcxt.c:1390
static bool DatumGetBool(Datum X)
Definition postgres.h:100
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static int32 DatumGetInt32(Datum X)
Definition postgres.h:202
#define PointerGetDatum(X)
Definition postgres.h:354
unsigned int Oid
static int fb(int x)
uint16 StrategyNumber
Definition stratnum.h:22
Datum key
Definition gist.h:161
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition c.h:835
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432
Datum bitcmp(PG_FUNCTION_ARGS)
Definition varbit.c:949
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
#define PG_GETARG_VARBIT_P(n)
Definition varbit.h:62