More logically order libpq func. includes, e.g., group GUC vals
[pgsql.git] / contrib / btree_gist / btree_bit.c
blob9d5e01a1977208abe84574a57e5bf43e688155ca
1 /*
2 * contrib/btree_gist/btree_bit.c
3 */
4 #include "postgres.h"
6 #include "btree_gist.h"
7 #include "btree_utils_var.h"
8 #include "utils/fmgrprotos.h"
9 #include "utils/varbit.h"
13 ** Bit ops
15 PG_FUNCTION_INFO_V1(gbt_bit_compress);
16 PG_FUNCTION_INFO_V1(gbt_bit_union);
17 PG_FUNCTION_INFO_V1(gbt_bit_picksplit);
18 PG_FUNCTION_INFO_V1(gbt_bit_consistent);
19 PG_FUNCTION_INFO_V1(gbt_bit_penalty);
20 PG_FUNCTION_INFO_V1(gbt_bit_same);
23 /* define for comparison */
25 static bool
26 gbt_bitgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
28 return DatumGetBool(DirectFunctionCall2(bitgt,
29 PointerGetDatum(a),
30 PointerGetDatum(b)));
33 static bool
34 gbt_bitge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
36 return DatumGetBool(DirectFunctionCall2(bitge,
37 PointerGetDatum(a),
38 PointerGetDatum(b)));
41 static bool
42 gbt_biteq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
44 return DatumGetBool(DirectFunctionCall2(biteq,
45 PointerGetDatum(a),
46 PointerGetDatum(b)));
49 static bool
50 gbt_bitle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
52 return DatumGetBool(DirectFunctionCall2(bitle,
53 PointerGetDatum(a),
54 PointerGetDatum(b)));
57 static bool
58 gbt_bitlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
60 return DatumGetBool(DirectFunctionCall2(bitlt,
61 PointerGetDatum(a),
62 PointerGetDatum(b)));
65 static int32
66 gbt_bitcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
68 return DatumGetInt32(DirectFunctionCall2(byteacmp,
69 PointerGetDatum(a),
70 PointerGetDatum(b)));
74 static bytea *
75 gbt_bit_xfrm(bytea *leaf)
77 bytea *out = leaf;
78 int sz = VARBITBYTES(leaf) + VARHDRSZ;
79 int padded_sz = INTALIGN(sz);
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;
93 static GBT_VARKEY *
94 gbt_bit_l2n(GBT_VARKEY *leaf, FmgrInfo *flinfo)
96 GBT_VARKEY *out = leaf;
97 GBT_VARKEY_R r = gbt_var_key_readable(leaf);
98 bytea *o;
100 o = gbt_bit_xfrm(r.lower);
101 r.upper = r.lower = o;
102 out = gbt_var_key_copy(&r);
103 pfree(o);
105 return out;
108 static const gbtree_vinfo tinfo =
110 gbt_t_bit,
112 true,
113 gbt_bitgt,
114 gbt_bitge,
115 gbt_biteq,
116 gbt_bitle,
117 gbt_bitlt,
118 gbt_bitcmp,
119 gbt_bit_l2n
123 /**************************************************
124 * Bit ops
125 **************************************************/
127 Datum
128 gbt_bit_compress(PG_FUNCTION_ARGS)
130 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132 PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
135 Datum
136 gbt_bit_consistent(PG_FUNCTION_ARGS)
138 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
139 void *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
140 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
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);
148 /* All cases served by this function are exact */
149 *recheck = false;
151 if (GIST_LEAF(entry))
152 retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
153 true, &tinfo, fcinfo->flinfo);
154 else
156 bytea *q = gbt_bit_xfrm((bytea *) query);
158 retval = gbt_var_consistent(&r, q, strategy, PG_GET_COLLATION(),
159 false, &tinfo, fcinfo->flinfo);
161 PG_RETURN_BOOL(retval);
166 Datum
167 gbt_bit_union(PG_FUNCTION_ARGS)
169 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
170 int32 *size = (int *) PG_GETARG_POINTER(1);
172 PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
173 &tinfo, fcinfo->flinfo));
177 Datum
178 gbt_bit_picksplit(PG_FUNCTION_ARGS)
180 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
181 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
183 gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
184 &tinfo, fcinfo->flinfo);
185 PG_RETURN_POINTER(v);
188 Datum
189 gbt_bit_same(PG_FUNCTION_ARGS)
191 Datum d1 = PG_GETARG_DATUM(0);
192 Datum d2 = PG_GETARG_DATUM(1);
193 bool *result = (bool *) PG_GETARG_POINTER(2);
195 *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
196 PG_RETURN_POINTER(result);
200 Datum
201 gbt_bit_penalty(PG_FUNCTION_ARGS)
203 GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
204 GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
205 float *result = (float *) PG_GETARG_POINTER(2);
207 PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
208 &tinfo, fcinfo->flinfo));