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 )
69 int32 ilen = VARSIZE (leaf) - VARHDRSZ;
73 memcpy ( (void*)&sin[0], (void*) VARDATA(leaf) ,ilen );
75 olen = strxfrm ( NULL, &sin[0], 0 ) + 1;
76 sou = palloc ( olen );
77 olen = strxfrm ( sou , &sin[0] , olen );
79 out = palloc ( olen + 1 );
81 memcpy( (void*) VARDATA(out), sou, olen-VARHDRSZ );
82 ((char*)out)[olen] = '\0';
89 static GBT_VARKEY * gbt_text_l2n ( GBT_VARKEY * leaf )
92 GBT_VARKEY *out = leaf ;
93 GBT_VARKEY_R r = gbt_var_key_readable ( leaf );
96 o = gbt_text_xfrm ( r.lower );
97 r.lower = r.upper = o;
98 out = gbt_var_key_copy ( &r , TRUE );
109 static const gbtree_vinfo tinfo =
125 /**************************************************
127 **************************************************/
131 gbt_text_compress (PG_FUNCTION_ARGS)
133 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
134 PG_RETURN_POINTER ( gbt_var_compress( entry, &tinfo ) );
138 gbt_bpchar_compress (PG_FUNCTION_ARGS)
141 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
147 Datum d = DirectFunctionCall1 ( rtrim1, entry->key );
148 GISTENTRY * trim = palloc(sizeof(GISTENTRY));
150 gistentryinit(*trim, d ,
151 entry->rel, entry->page,
152 entry->offset, VARSIZE(DatumGetPointer(d)), TRUE);
153 retval = gbt_var_compress( trim , &tinfo ) ;
156 pfree ( DatumGetPointer(d) );
160 PG_RETURN_POINTER ( retval );
166 gbt_text_consistent(PG_FUNCTION_ARGS)
168 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
169 GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer ( entry->key ) ;
170 GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer ( PG_DETOAST_DATUM( entry->key ) );
171 void *qtst = ( void * ) DatumGetPointer( PG_GETARG_DATUM(1) );
172 void *query = ( void * ) DatumGetTextP ( PG_GETARG_DATUM(1) );
173 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
175 GBT_VARKEY_R r = gbt_var_key_readable ( key );
177 if ( GIST_LEAF(entry) )
179 retval = gbt_var_consistent( &r, query, &strategy, TRUE, &tinfo );
181 bytea * q = gbt_text_xfrm ( ( bytea * ) query );
182 retval = gbt_var_consistent( &r, (void*)q, &strategy, FALSE, &tinfo );
190 if ( qtst != query ){
194 PG_RETURN_BOOL(retval);
199 gbt_bpchar_consistent(PG_FUNCTION_ARGS)
201 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
202 GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer ( entry->key ) ;
203 GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer ( PG_DETOAST_DATUM( entry->key ) );
204 void *qtst = ( void * ) DatumGetPointer ( PG_GETARG_DATUM(1) );
205 void *query = ( void * ) DatumGetPointer (PG_DETOAST_DATUM( PG_GETARG_DATUM(1) ) );
206 void *trim = ( void * ) DatumGetPointer ( DirectFunctionCall1 ( rtrim1, PointerGetDatum ( query ) ) ) ;
207 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
209 GBT_VARKEY_R r = gbt_var_key_readable ( key );
211 if ( GIST_LEAF(entry) )
213 retval = gbt_var_consistent( &r, trim , &strategy, TRUE, &tinfo );
215 bytea * q = gbt_text_xfrm ( ( bytea * ) trim );
216 retval = gbt_var_consistent( &r, (void*)q, &strategy, FALSE, &tinfo );
226 if ( qtst != query ){
229 PG_RETURN_BOOL(retval);
236 gbt_text_union(PG_FUNCTION_ARGS)
238 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
239 int32 *size = (int *) PG_GETARG_POINTER(1);
240 PG_RETURN_POINTER( gbt_var_union ( entryvec , size , &tinfo ) );
245 gbt_text_picksplit(PG_FUNCTION_ARGS)
247 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
248 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
249 gbt_var_picksplit ( entryvec, v, &tinfo );
250 PG_RETURN_POINTER(v);
254 gbt_text_same(PG_FUNCTION_ARGS)
256 Datum d1 = PG_GETARG_DATUM(0);
257 Datum d2 = PG_GETARG_DATUM(1);
258 bool *result = (bool *) PG_GETARG_POINTER(2);
259 PG_RETURN_POINTER( gbt_var_same ( result, d1 , d2 , &tinfo ));
264 gbt_text_penalty(PG_FUNCTION_ARGS)
266 float *result = (float *) PG_GETARG_POINTER(2);
267 GISTENTRY * o = (GISTENTRY *) PG_GETARG_POINTER(0);
268 GISTENTRY * n = (GISTENTRY *) PG_GETARG_POINTER(1);
269 PG_RETURN_POINTER( gbt_var_penalty ( result ,o , n, &tinfo ) );