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