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