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