4 #include "btree_gist.h"
5 #include "btree_utils_var.h"
6 #include "utils/builtins.h"
7 #include "utils/varbit.h"
13 PG_FUNCTION_INFO_V1(gbt_bit_compress
);
14 PG_FUNCTION_INFO_V1(gbt_bit_union
);
15 PG_FUNCTION_INFO_V1(gbt_bit_picksplit
);
16 PG_FUNCTION_INFO_V1(gbt_bit_consistent
);
17 PG_FUNCTION_INFO_V1(gbt_bit_penalty
);
18 PG_FUNCTION_INFO_V1(gbt_bit_same
);
20 Datum
gbt_bit_compress(PG_FUNCTION_ARGS
);
21 Datum
gbt_bit_union(PG_FUNCTION_ARGS
);
22 Datum
gbt_bit_picksplit(PG_FUNCTION_ARGS
);
23 Datum
gbt_bit_consistent(PG_FUNCTION_ARGS
);
24 Datum
gbt_bit_penalty(PG_FUNCTION_ARGS
);
25 Datum
gbt_bit_same(PG_FUNCTION_ARGS
);
29 /* define for comparison */
32 gbt_bitgt(const void *a
, const void *b
)
34 return (DatumGetBool(DirectFunctionCall2(bitgt
, PointerGetDatum(a
), PointerGetDatum(b
))));
38 gbt_bitge(const void *a
, const void *b
)
40 return (DatumGetBool(DirectFunctionCall2(bitge
, PointerGetDatum(a
), PointerGetDatum(b
))));
44 gbt_biteq(const void *a
, const void *b
)
46 return (DatumGetBool(DirectFunctionCall2(biteq
, PointerGetDatum(a
), PointerGetDatum(b
))));
50 gbt_bitle(const void *a
, const void *b
)
52 return (DatumGetBool(DirectFunctionCall2(bitle
, PointerGetDatum(a
), PointerGetDatum(b
))));
56 gbt_bitlt(const void *a
, const void *b
)
58 return (DatumGetBool(DirectFunctionCall2(bitlt
, PointerGetDatum(a
), PointerGetDatum(b
))));
62 gbt_bitcmp(const bytea
*a
, const bytea
*b
)
65 (DatumGetInt32(DirectFunctionCall2(byteacmp
, PointerGetDatum(a
), PointerGetDatum(b
))));
70 gbt_bit_xfrm(bytea
*leaf
)
73 int s
= INTALIGN(VARBITBYTES(leaf
) + VARHDRSZ
);
77 memcpy((void *) VARDATA(out
), (void *) VARBITS(leaf
), VARBITBYTES(leaf
));
85 gbt_bit_l2n(GBT_VARKEY
* leaf
)
88 GBT_VARKEY
*out
= leaf
;
89 GBT_VARKEY_R r
= gbt_var_key_readable(leaf
);
92 o
= gbt_bit_xfrm(r
.lower
);
93 r
.upper
= r
.lower
= o
;
94 out
= gbt_var_key_copy(&r
, TRUE
);
101 static const gbtree_vinfo tinfo
=
116 /**************************************************
118 **************************************************/
121 gbt_bit_compress(PG_FUNCTION_ARGS
)
123 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
125 PG_RETURN_POINTER(gbt_var_compress(entry
, &tinfo
));
129 gbt_bit_consistent(PG_FUNCTION_ARGS
)
131 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
132 void *query
= (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
133 StrategyNumber strategy
= (StrategyNumber
) PG_GETARG_UINT16(2);
134 /* Oid subtype = PG_GETARG_OID(3); */
135 bool *recheck
= (bool *) PG_GETARG_POINTER(4);
137 GBT_VARKEY
*key
= (GBT_VARKEY
*) DatumGetPointer(entry
->key
);
138 GBT_VARKEY_R r
= gbt_var_key_readable(key
);
140 /* All cases served by this function are exact */
143 if (GIST_LEAF(entry
))
144 retval
= gbt_var_consistent(&r
, query
, &strategy
, TRUE
, &tinfo
);
147 bytea
*q
= gbt_bit_xfrm((bytea
*) query
);
149 retval
= gbt_var_consistent(&r
, (void *) q
, &strategy
, FALSE
, &tinfo
);
151 PG_RETURN_BOOL(retval
);
157 gbt_bit_union(PG_FUNCTION_ARGS
)
159 GistEntryVector
*entryvec
= (GistEntryVector
*) PG_GETARG_POINTER(0);
160 int32
*size
= (int *) PG_GETARG_POINTER(1);
162 PG_RETURN_POINTER(gbt_var_union(entryvec
, size
, &tinfo
));
167 gbt_bit_picksplit(PG_FUNCTION_ARGS
)
169 GistEntryVector
*entryvec
= (GistEntryVector
*) PG_GETARG_POINTER(0);
170 GIST_SPLITVEC
*v
= (GIST_SPLITVEC
*) PG_GETARG_POINTER(1);
172 gbt_var_picksplit(entryvec
, v
, &tinfo
);
173 PG_RETURN_POINTER(v
);
177 gbt_bit_same(PG_FUNCTION_ARGS
)
179 Datum d1
= PG_GETARG_DATUM(0);
180 Datum d2
= PG_GETARG_DATUM(1);
181 bool *result
= (bool *) PG_GETARG_POINTER(2);
183 PG_RETURN_POINTER(gbt_var_same(result
, d1
, d2
, &tinfo
));
188 gbt_bit_penalty(PG_FUNCTION_ARGS
)
190 GISTENTRY
*o
= (GISTENTRY
*) PG_GETARG_POINTER(0);
191 GISTENTRY
*n
= (GISTENTRY
*) PG_GETARG_POINTER(1);
192 float *result
= (float *) PG_GETARG_POINTER(2);
194 PG_RETURN_POINTER(gbt_var_penalty(result
, o
, n
, &tinfo
));