1 #include "btree_gist.h"
2 #include "btree_utils_var.h"
3 #include "utils/builtins.h"
8 PG_FUNCTION_INFO_V1(gbt_text_compress);
9 PG_FUNCTION_INFO_V1(gbt_bpchar_compress);
10 PG_FUNCTION_INFO_V1(gbt_text_union);
11 PG_FUNCTION_INFO_V1(gbt_text_picksplit);
12 PG_FUNCTION_INFO_V1(gbt_text_consistent);
13 PG_FUNCTION_INFO_V1(gbt_bpchar_consistent);
14 PG_FUNCTION_INFO_V1(gbt_text_penalty);
15 PG_FUNCTION_INFO_V1(gbt_text_same);
17 Datum gbt_text_compress(PG_FUNCTION_ARGS);
18 Datum gbt_bpchar_compress(PG_FUNCTION_ARGS);
19 Datum gbt_text_union(PG_FUNCTION_ARGS);
20 Datum gbt_text_picksplit(PG_FUNCTION_ARGS);
21 Datum gbt_text_consistent(PG_FUNCTION_ARGS);
22 Datum gbt_bpchar_consistent(PG_FUNCTION_ARGS);
23 Datum gbt_text_penalty(PG_FUNCTION_ARGS);
24 Datum gbt_text_same(PG_FUNCTION_ARGS);
27 /* define for comparison */
29 static bool gbt_textgt (const void *a, const void *b)
31 return ( DatumGetBool(DirectFunctionCall2( text_gt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
34 static bool gbt_textge (const void *a, const void *b)
36 return ( DatumGetBool(DirectFunctionCall2( text_ge ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
39 static bool gbt_texteq (const void *a, const void *b)
41 return ( DatumGetBool(DirectFunctionCall2( texteq ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
44 static bool gbt_textle (const void *a, const void *b)
46 return ( DatumGetBool(DirectFunctionCall2( text_le ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
49 static bool gbt_textlt (const void *a, const void *b)
51 return ( DatumGetBool(DirectFunctionCall2( text_lt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
54 static int32 gbt_textcmp ( const bytea * a , const bytea * b )
56 return strcmp( VARDATA(a), VARDATA(b) );
61 * Converts data of leaf using strxfrm ( locale support )
65 gbt_text_xfrm ( bytea * leaf )
68 int32 ilen = VARSIZE (leaf) - VARHDRSZ;
73 sin = palloc(ilen + 1);
74 memcpy (sin, (void*) VARDATA(leaf) ,ilen );
77 olen = strxfrm ( NULL, &sin[0], 0 ) + 1;
78 sou = palloc ( olen );
79 olen = strxfrm ( sou , &sin[0] , olen );
81 out = palloc ( olen + 1 );
83 memcpy( (void*) VARDATA(out), sou, olen-VARHDRSZ );
84 ((char*)out)[olen] = '\0';
93 static GBT_VARKEY * gbt_text_l2n ( GBT_VARKEY * leaf )
96 GBT_VARKEY *out = leaf ;
97 GBT_VARKEY_R r = gbt_var_key_readable ( leaf );
100 o = gbt_text_xfrm ( r.lower );
101 r.lower = r.upper = o;
102 out = gbt_var_key_copy ( &r , TRUE );
113 static const gbtree_vinfo tinfo =
129 /**************************************************
131 **************************************************/
135 gbt_text_compress (PG_FUNCTION_ARGS)
137 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
138 PG_RETURN_POINTER ( gbt_var_compress( entry, &tinfo ) );
142 gbt_bpchar_compress (PG_FUNCTION_ARGS)
145 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
151 Datum d = DirectFunctionCall1 ( rtrim1, entry->key );
152 GISTENTRY * trim = palloc(sizeof(GISTENTRY));
154 gistentryinit(*trim, d ,
155 entry->rel, entry->page,
156 entry->offset, VARSIZE(DatumGetPointer(d)), TRUE);
157 retval = gbt_var_compress( trim , &tinfo ) ;
160 pfree ( DatumGetPointer(d) );
164 PG_RETURN_POINTER ( retval );
170 gbt_text_consistent(PG_FUNCTION_ARGS)
172 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
173 GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer ( entry->key ) ;
174 GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer ( PG_DETOAST_DATUM( entry->key ) );
175 void *qtst = ( void * ) DatumGetPointer( PG_GETARG_DATUM(1) );
176 void *query = ( void * ) DatumGetTextP ( PG_GETARG_DATUM(1) );
177 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
179 GBT_VARKEY_R r = gbt_var_key_readable ( key );
181 if ( GIST_LEAF(entry) )
183 retval = gbt_var_consistent( &r, query, &strategy, TRUE, &tinfo );
185 bytea * q = gbt_text_xfrm ( ( bytea * ) query );
186 retval = gbt_var_consistent( &r, (void*)q, &strategy, FALSE, &tinfo );
194 if ( qtst != query ){
198 PG_RETURN_BOOL(retval);
203 gbt_bpchar_consistent(PG_FUNCTION_ARGS)
205 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
206 GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer ( entry->key ) ;
207 GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer ( PG_DETOAST_DATUM( entry->key ) );
208 void *qtst = ( void * ) DatumGetPointer ( PG_GETARG_DATUM(1) );
209 void *query = ( void * ) DatumGetPointer (PG_DETOAST_DATUM( PG_GETARG_DATUM(1) ) );
210 void *trim = ( void * ) DatumGetPointer ( DirectFunctionCall1 ( rtrim1, PointerGetDatum ( query ) ) ) ;
211 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
213 GBT_VARKEY_R r = gbt_var_key_readable ( key );
215 if ( GIST_LEAF(entry) )
217 retval = gbt_var_consistent( &r, trim , &strategy, TRUE, &tinfo );
219 bytea * q = gbt_text_xfrm ( ( bytea * ) trim );
220 retval = gbt_var_consistent( &r, (void*)q, &strategy, FALSE, &tinfo );
230 if ( qtst != query ){
233 PG_RETURN_BOOL(retval);
240 gbt_text_union(PG_FUNCTION_ARGS)
242 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
243 int32 *size = (int *) PG_GETARG_POINTER(1);
244 PG_RETURN_POINTER( gbt_var_union ( entryvec , size , &tinfo ) );
249 gbt_text_picksplit(PG_FUNCTION_ARGS)
251 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
252 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
253 gbt_var_picksplit ( entryvec, v, &tinfo );
254 PG_RETURN_POINTER(v);
258 gbt_text_same(PG_FUNCTION_ARGS)
260 Datum d1 = PG_GETARG_DATUM(0);
261 Datum d2 = PG_GETARG_DATUM(1);
262 bool *result = (bool *) PG_GETARG_POINTER(2);
263 PG_RETURN_POINTER( gbt_var_same ( result, d1 , d2 , &tinfo ));
268 gbt_text_penalty(PG_FUNCTION_ARGS)
270 float *result = (float *) PG_GETARG_POINTER(2);
271 GISTENTRY * o = (GISTENTRY *) PG_GETARG_POINTER(0);
272 GISTENTRY * n = (GISTENTRY *) PG_GETARG_POINTER(1);
273 PG_RETURN_POINTER( gbt_var_penalty ( result ,o , n, &tinfo ) );