2 * contrib/btree_gist/btree_bit.c
6 #include "btree_gist.h"
7 #include "btree_utils_var.h"
8 #include "utils/fmgrprotos.h"
9 #include "utils/varbit.h"
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 */
26 gbt_bitgt(const void *a
, const void *b
, Oid collation
, FmgrInfo
*flinfo
)
28 return DatumGetBool(DirectFunctionCall2(bitgt
,
34 gbt_bitge(const void *a
, const void *b
, Oid collation
, FmgrInfo
*flinfo
)
36 return DatumGetBool(DirectFunctionCall2(bitge
,
42 gbt_biteq(const void *a
, const void *b
, Oid collation
, FmgrInfo
*flinfo
)
44 return DatumGetBool(DirectFunctionCall2(biteq
,
50 gbt_bitle(const void *a
, const void *b
, Oid collation
, FmgrInfo
*flinfo
)
52 return DatumGetBool(DirectFunctionCall2(bitle
,
58 gbt_bitlt(const void *a
, const void *b
, Oid collation
, FmgrInfo
*flinfo
)
60 return DatumGetBool(DirectFunctionCall2(bitlt
,
66 gbt_bitcmp(const void *a
, const void *b
, Oid collation
, FmgrInfo
*flinfo
)
68 return DatumGetInt32(DirectFunctionCall2(byteacmp
,
75 gbt_bit_xfrm(bytea
*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
));
94 gbt_bit_l2n(GBT_VARKEY
*leaf
, FmgrInfo
*flinfo
)
96 GBT_VARKEY
*out
= leaf
;
97 GBT_VARKEY_R r
= gbt_var_key_readable(leaf
);
100 o
= gbt_bit_xfrm(r
.lower
);
101 r
.upper
= r
.lower
= o
;
102 out
= gbt_var_key_copy(&r
);
108 static const gbtree_vinfo tinfo
=
123 /**************************************************
125 **************************************************/
128 gbt_bit_compress(PG_FUNCTION_ARGS
)
130 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
132 PG_RETURN_POINTER(gbt_var_compress(entry
, &tinfo
));
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);
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 */
151 if (GIST_LEAF(entry
))
152 retval
= gbt_var_consistent(&r
, query
, strategy
, PG_GET_COLLATION(),
153 true, &tinfo
, fcinfo
->flinfo
);
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
);
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
));
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
);
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
);
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
));