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/builtins.h"
9 #include "utils/bytea.h"
10 #include "utils/varbit.h"
11 
12 
13 /*
14 ** Bit ops
15 */
16 PG_FUNCTION_INFO_V1(gbt_bit_compress);
17 PG_FUNCTION_INFO_V1(gbt_bit_union);
18 PG_FUNCTION_INFO_V1(gbt_bit_picksplit);
19 PG_FUNCTION_INFO_V1(gbt_bit_consistent);
20 PG_FUNCTION_INFO_V1(gbt_bit_penalty);
21 PG_FUNCTION_INFO_V1(gbt_bit_same);
22 
23 
24 /* define for comparison */
25 
26 static bool
gbt_bitgt(const void * a,const void * b,Oid collation,FmgrInfo * flinfo)27 gbt_bitgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
28 {
29 	return DatumGetBool(DirectFunctionCall2(bitgt,
30 											PointerGetDatum(a),
31 											PointerGetDatum(b)));
32 }
33 
34 static bool
gbt_bitge(const void * a,const void * b,Oid collation,FmgrInfo * flinfo)35 gbt_bitge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
36 {
37 	return DatumGetBool(DirectFunctionCall2(bitge,
38 											PointerGetDatum(a),
39 											PointerGetDatum(b)));
40 }
41 
42 static bool
gbt_biteq(const void * a,const void * b,Oid collation,FmgrInfo * flinfo)43 gbt_biteq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
44 {
45 	return DatumGetBool(DirectFunctionCall2(biteq,
46 											PointerGetDatum(a),
47 											PointerGetDatum(b)));
48 }
49 
50 static bool
gbt_bitle(const void * a,const void * b,Oid collation,FmgrInfo * flinfo)51 gbt_bitle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
52 {
53 	return DatumGetBool(DirectFunctionCall2(bitle,
54 											PointerGetDatum(a),
55 											PointerGetDatum(b)));
56 }
57 
58 static bool
gbt_bitlt(const void * a,const void * b,Oid collation,FmgrInfo * flinfo)59 gbt_bitlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
60 {
61 	return DatumGetBool(DirectFunctionCall2(bitlt,
62 											PointerGetDatum(a),
63 											PointerGetDatum(b)));
64 }
65 
66 static int32
gbt_bitcmp(const void * a,const void * b,Oid collation,FmgrInfo * flinfo)67 gbt_bitcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
68 {
69 	return DatumGetInt32(DirectFunctionCall2(byteacmp,
70 											 PointerGetDatum(a),
71 											 PointerGetDatum(b)));
72 }
73 
74 
75 static bytea *
gbt_bit_xfrm(bytea * leaf)76 gbt_bit_xfrm(bytea *leaf)
77 {
78 	bytea	   *out = leaf;
79 	int			sz = VARBITBYTES(leaf) + VARHDRSZ;
80 	int			padded_sz = INTALIGN(sz);
81 
82 	out = (bytea *) palloc(padded_sz);
83 	/* initialize the padding bytes to zero */
84 	while (sz < padded_sz)
85 		((char *) out)[sz++] = 0;
86 	SET_VARSIZE(out, padded_sz);
87 	memcpy((void *) VARDATA(out), (void *) VARBITS(leaf), VARBITBYTES(leaf));
88 	return out;
89 }
90 
91 
92 
93 
94 static GBT_VARKEY *
gbt_bit_l2n(GBT_VARKEY * leaf,FmgrInfo * flinfo)95 gbt_bit_l2n(GBT_VARKEY *leaf, FmgrInfo *flinfo)
96 {
97 	GBT_VARKEY *out = leaf;
98 	GBT_VARKEY_R r = gbt_var_key_readable(leaf);
99 	bytea	   *o;
100 
101 	o = gbt_bit_xfrm(r.lower);
102 	r.upper = r.lower = o;
103 	out = gbt_var_key_copy(&r);
104 	pfree(o);
105 
106 	return out;
107 
108 }
109 
110 static const gbtree_vinfo tinfo =
111 {
112 	gbt_t_bit,
113 	0,
114 	true,
115 	gbt_bitgt,
116 	gbt_bitge,
117 	gbt_biteq,
118 	gbt_bitle,
119 	gbt_bitlt,
120 	gbt_bitcmp,
121 	gbt_bit_l2n
122 };
123 
124 
125 /**************************************************
126  * Bit ops
127  **************************************************/
128 
129 Datum
gbt_bit_compress(PG_FUNCTION_ARGS)130 gbt_bit_compress(PG_FUNCTION_ARGS)
131 {
132 	GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
133 
134 	PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
135 }
136 
137 Datum
gbt_bit_consistent(PG_FUNCTION_ARGS)138 gbt_bit_consistent(PG_FUNCTION_ARGS)
139 {
140 	GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
141 	void	   *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
142 	StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
143 
144 	/* Oid		subtype = PG_GETARG_OID(3); */
145 	bool	   *recheck = (bool *) PG_GETARG_POINTER(4);
146 	bool		retval;
147 	GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
148 	GBT_VARKEY_R r = gbt_var_key_readable(key);
149 
150 	/* All cases served by this function are exact */
151 	*recheck = false;
152 
153 	if (GIST_LEAF(entry))
154 		retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
155 									true, &tinfo, fcinfo->flinfo);
156 	else
157 	{
158 		bytea	   *q = gbt_bit_xfrm((bytea *) query);
159 
160 		retval = gbt_var_consistent(&r, q, strategy, PG_GET_COLLATION(),
161 									false, &tinfo, fcinfo->flinfo);
162 	}
163 	PG_RETURN_BOOL(retval);
164 }
165 
166 
167 
168 Datum
gbt_bit_union(PG_FUNCTION_ARGS)169 gbt_bit_union(PG_FUNCTION_ARGS)
170 {
171 	GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
172 	int32	   *size = (int *) PG_GETARG_POINTER(1);
173 
174 	PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
175 									&tinfo, fcinfo->flinfo));
176 }
177 
178 
179 Datum
gbt_bit_picksplit(PG_FUNCTION_ARGS)180 gbt_bit_picksplit(PG_FUNCTION_ARGS)
181 {
182 	GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
183 	GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
184 
185 	gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
186 					  &tinfo, fcinfo->flinfo);
187 	PG_RETURN_POINTER(v);
188 }
189 
190 Datum
gbt_bit_same(PG_FUNCTION_ARGS)191 gbt_bit_same(PG_FUNCTION_ARGS)
192 {
193 	Datum		d1 = PG_GETARG_DATUM(0);
194 	Datum		d2 = PG_GETARG_DATUM(1);
195 	bool	   *result = (bool *) PG_GETARG_POINTER(2);
196 
197 	*result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
198 	PG_RETURN_POINTER(result);
199 }
200 
201 
202 Datum
gbt_bit_penalty(PG_FUNCTION_ARGS)203 gbt_bit_penalty(PG_FUNCTION_ARGS)
204 {
205 	GISTENTRY  *o = (GISTENTRY *) PG_GETARG_POINTER(0);
206 	GISTENTRY  *n = (GISTENTRY *) PG_GETARG_POINTER(1);
207 	float	   *result = (float *) PG_GETARG_POINTER(2);
208 
209 	PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
210 									  &tinfo, fcinfo->flinfo));
211 }
212