Debian Codegraph @

contrib/btree_gist/btree_bit.c

raw
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/fmgrprotos.h"
9 #include "utils/sortsupport.h"
10 #include "utils/varbit.h"
11
12 /* GiST support functions */
13 PG_FUNCTION_INFO_V1(gbt_bit_compress);
14 PG_FUNCTION_INFO_V1(gbt_bit_union);
15 PG_FUNCTION_INFO_V1(gbt_bit_picksplit);
16 PG_FUNCTION_INFO_V1(gbt_bit_consistent);
17 PG_FUNCTION_INFO_V1(gbt_bit_penalty);
18 PG_FUNCTION_INFO_V1(gbt_bit_same);
19 PG_FUNCTION_INFO_V1(gbt_bit_sortsupport);
20 PG_FUNCTION_INFO_V1(gbt_varbit_sortsupport);
21
22
23 /* define for comparison */
24
25 static bool
26 gbt_bitgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
27 {
28 return DatumGetBool(DirectFunctionCall2(bitgt,
29 PointerGetDatum(a),
30 PointerGetDatum(b)));
31 }
32
33 static bool
34 gbt_bitge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
35 {
36 return DatumGetBool(DirectFunctionCall2(bitge,
37 PointerGetDatum(a),
38 PointerGetDatum(b)));
39 }
40
41 static bool
42 gbt_biteq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
43 {
44 return DatumGetBool(DirectFunctionCall2(biteq,
45 PointerGetDatum(a),
46 PointerGetDatum(b)));
47 }
48
49 static bool
50 gbt_bitle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
51 {
52 return DatumGetBool(DirectFunctionCall2(bitle,
53 PointerGetDatum(a),
54 PointerGetDatum(b)));
55 }
56
57 static bool
58 gbt_bitlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
59 {
60 return DatumGetBool(DirectFunctionCall2(bitlt,
61 PointerGetDatum(a),
62 PointerGetDatum(b)));
63 }
64
65 static int32
66 gbt_bitcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
67 {
68 return DatumGetInt32(DirectFunctionCall2(byteacmp,
69 PointerGetDatum(a),
70 PointerGetDatum(b)));
71 }
72
73
74 static bytea *
75 gbt_bit_xfrm(bytea *leaf)
76 {
77 bytea *out = leaf;
78 int sz = VARBITBYTES(leaf) + VARHDRSZ;
79 int padded_sz = INTALIGN(sz);
80
81 out = (bytea *) palloc(padded_sz);
82 /* initialize the padding bytes to zero */
83 while (sz < padded_sz)
84 ((char *) out)[sz++] = 0;
85 SET_VARSIZE(out, padded_sz);
86 memcpy(VARDATA(out), VARBITS(leaf), VARBITBYTES(leaf));
87 return out;
88 }
89
90
91
92
93 static GBT_VARKEY *
94 gbt_bit_l2n(GBT_VARKEY *leaf, FmgrInfo *flinfo)
95 {
96 GBT_VARKEY *out = leaf;
97 GBT_VARKEY_R r = gbt_var_key_readable(leaf);
98 bytea *o;
99
100 o = gbt_bit_xfrm(r.lower);
101 r.upper = r.lower = o;
102 out = gbt_var_key_copy(&r);
103 pfree(o);
104
105 return out;
106 }
107
108 static const gbtree_vinfo tinfo =
109 {
110 gbt_t_bit,
111 0,
112 true,
113 gbt_bitgt,
114 gbt_bitge,
115 gbt_biteq,
116 gbt_bitle,
117 gbt_bitlt,
118 gbt_bitcmp,
119 gbt_bit_l2n
120 };
121
122
123 /**************************************************
124 * GiST support functions
125 **************************************************/
126
127 Datum
128 gbt_bit_compress(PG_FUNCTION_ARGS)
129 {
130 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
131
132 PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
133 }
134
135 Datum
136 gbt_bit_consistent(PG_FUNCTION_ARGS)
137 {
138 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
139 void *query = DatumGetByteaP(PG_GETARG_DATUM(1));
140 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
141
142 /* Oid subtype = PG_GETARG_OID(3); */
143 bool *recheck = (bool *) PG_GETARG_POINTER(4);
144 bool retval;
145 GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
146 GBT_VARKEY_R r = gbt_var_key_readable(key);
147
148 /* All cases served by this function are exact */
149 *recheck = false;
150
151 if (GIST_LEAF(entry))
152 retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
153 true, &tinfo, fcinfo->flinfo);
154 else
155 {
156 bytea *q = gbt_bit_xfrm((bytea *) query);
157
158 retval = gbt_var_consistent(&r, q, strategy, PG_GET_COLLATION(),
159 false, &tinfo, fcinfo->flinfo);
160 }
161 PG_RETURN_BOOL(retval);
162 }
163
164 Datum
165 gbt_bit_union(PG_FUNCTION_ARGS)
166 {
167 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
168 int32 *size = (int *) PG_GETARG_POINTER(1);
169
170 PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
171 &tinfo, fcinfo->flinfo));
172 }
173
174 Datum
175 gbt_bit_picksplit(PG_FUNCTION_ARGS)
176 {
177 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
178 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
179
180 gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
181 &tinfo, fcinfo->flinfo);
182 PG_RETURN_POINTER(v);
183 }
184
185 Datum
186 gbt_bit_same(PG_FUNCTION_ARGS)
187 {
188 Datum d1 = PG_GETARG_DATUM(0);
189 Datum d2 = PG_GETARG_DATUM(1);
190 bool *result = (bool *) PG_GETARG_POINTER(2);
191
192 *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
193 PG_RETURN_POINTER(result);
194 }
195
196 Datum
197 gbt_bit_penalty(PG_FUNCTION_ARGS)
198 {
199 GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
200 GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
201 float *result = (float *) PG_GETARG_POINTER(2);
202
203 PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
204 &tinfo, fcinfo->flinfo));
205 }
206
207 static int
208 gbt_bit_ssup_cmp(Datum x, Datum y, SortSupport ssup)
209 {
210 GBT_VARKEY *key1 = PG_DETOAST_DATUM(x);
211 GBT_VARKEY *key2 = PG_DETOAST_DATUM(y);
212
213 GBT_VARKEY_R arg1 = gbt_var_key_readable(key1);
214 GBT_VARKEY_R arg2 = gbt_var_key_readable(key2);
215 Datum result;
216
217 /* for leaf items we expect lower == upper, so only compare lower */
218 result = DirectFunctionCall2(bitcmp,
219 PointerGetDatum(arg1.lower),
220 PointerGetDatum(arg2.lower));
221
222 GBT_FREE_IF_COPY(key1, x);
223 GBT_FREE_IF_COPY(key2, y);
224
225 return DatumGetInt32(result);
226 }
227
228 Datum
229 gbt_bit_sortsupport(PG_FUNCTION_ARGS)
230 {
231 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
232
233 ssup->comparator = gbt_bit_ssup_cmp;
234 ssup->ssup_extra = NULL;
235
236 PG_RETURN_VOID();
237 }
238
239 Datum
240 gbt_varbit_sortsupport(PG_FUNCTION_ARGS)
241 {
242 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
243
244 ssup->comparator = gbt_bit_ssup_cmp;
245 ssup->ssup_extra = NULL;
246
247 PG_RETURN_VOID();
248 }