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