]> granicus.if.org Git - postgresql/blob - contrib/btree_gist/btree_text.c
Whack btree_gist code around until it has some small hope of building
[postgresql] / contrib / btree_gist / btree_text.c
1 #include "btree_gist.h"
2 #include "btree_utils_var.h"
3 #include "utils/builtins.h"
4
5 /*
6 ** Text ops
7 */
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);
16
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);
25
26
27 /* define for comparison */
28
29 static bool     gbt_textgt     (const void *a, const void *b)
30 {
31   return ( DatumGetBool(DirectFunctionCall2( text_gt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
32 }
33
34 static bool     gbt_textge     (const void *a, const void *b)
35 {
36   return ( DatumGetBool(DirectFunctionCall2( text_ge ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
37 }
38
39 static bool     gbt_texteq     (const void *a, const void *b)
40 {
41   return ( DatumGetBool(DirectFunctionCall2( texteq ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
42 }
43
44 static bool     gbt_textle     (const void *a, const void *b)
45 {
46   return ( DatumGetBool(DirectFunctionCall2( text_le ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
47 }
48
49 static bool     gbt_textlt     (const void *a, const void *b)
50 {
51   return ( DatumGetBool(DirectFunctionCall2( text_lt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
52 }
53
54 static int32 gbt_textcmp ( const bytea * a , const bytea * b )
55 {
56   return strcmp( VARDATA(a), VARDATA(b) );
57 }
58   
59
60 /*
61  * Converts data of leaf using strxfrm ( locale support )
62 */
63
64 static bytea *
65 gbt_text_xfrm ( bytea * leaf )
66 {
67     bytea  * out = leaf;
68     int32  ilen = VARSIZE (leaf) - VARHDRSZ;
69     int32  olen ;
70     char   * sin;
71     char   * sou;
72
73         sin = palloc(ilen + 1);
74     memcpy (sin, (void*) VARDATA(leaf) ,ilen );
75     sin[ilen]   = '\0';
76
77     olen        = strxfrm ( NULL, &sin[0], 0 ) + 1;
78     sou         = palloc ( olen );
79     olen        = strxfrm ( sou , &sin[0] , olen );
80     olen       += VARHDRSZ;
81     out         = palloc ( olen + 1 );
82     out->vl_len = olen+1;
83     memcpy( (void*) VARDATA(out), sou, olen-VARHDRSZ );
84     ((char*)out)[olen]   = '\0';
85
86     pfree(sou);
87     pfree(sin);
88
89     return out;
90 }
91
92
93 static GBT_VARKEY * gbt_text_l2n ( GBT_VARKEY * leaf )
94 {
95  
96   GBT_VARKEY   *out = leaf ;
97   GBT_VARKEY_R r    = gbt_var_key_readable ( leaf );
98   bytea * o ;
99
100   o   = gbt_text_xfrm ( r.lower );
101   r.lower = r.upper = o;
102   out = gbt_var_key_copy ( &r , TRUE );
103   pfree(o);
104
105   return out;
106
107 }
108
109
110
111
112
113 static const gbtree_vinfo tinfo =
114 {
115   gbt_t_text,
116   TRUE,
117   TRUE,
118   gbt_textgt,
119   gbt_textge,
120   gbt_texteq,
121   gbt_textle,
122   gbt_textlt,
123   gbt_textcmp,
124   gbt_text_l2n
125 };
126
127
128
129 /**************************************************
130  * Text ops
131  **************************************************/
132
133
134 Datum
135 gbt_text_compress (PG_FUNCTION_ARGS)
136 {
137   GISTENTRY        *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
138   PG_RETURN_POINTER ( gbt_var_compress( entry, &tinfo ) );
139 }
140
141 Datum
142 gbt_bpchar_compress (PG_FUNCTION_ARGS)
143 {
144
145   GISTENTRY        *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
146   GISTENTRY        * retval ;
147
148   if (entry->leafkey)
149   {
150
151     Datum          d = DirectFunctionCall1 ( rtrim1, entry->key );
152     GISTENTRY * trim = palloc(sizeof(GISTENTRY));
153
154     gistentryinit(*trim, d ,
155               entry->rel, entry->page,
156               entry->offset, VARSIZE(DatumGetPointer(d)), TRUE);
157     retval = gbt_var_compress( trim , &tinfo ) ;
158
159     pfree ( trim );
160     pfree ( DatumGetPointer(d) );
161   } else
162     retval = entry;
163
164   PG_RETURN_POINTER ( retval );
165 }
166
167
168
169 Datum
170 gbt_text_consistent(PG_FUNCTION_ARGS)
171 {
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);
178   bool    retval          = FALSE;
179   GBT_VARKEY_R          r =  gbt_var_key_readable ( key );
180
181   if ( GIST_LEAF(entry) )
182   {
183     retval = gbt_var_consistent( &r, query, &strategy, TRUE, &tinfo );
184   } else {
185     bytea * q = gbt_text_xfrm ( ( bytea * ) query );
186     retval    = gbt_var_consistent( &r, (void*)q, &strategy, FALSE, &tinfo );
187     if ( q != query )
188       pfree(q);
189   }
190
191   if ( ktst != key ){
192     pfree ( key );
193   }
194   if ( qtst != query ){
195     pfree ( query );
196   }
197
198   PG_RETURN_BOOL(retval);
199 }
200
201
202 Datum
203 gbt_bpchar_consistent(PG_FUNCTION_ARGS)
204 {
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);
212   bool    retval          = FALSE;
213   GBT_VARKEY_R          r =  gbt_var_key_readable ( key );
214
215   if ( GIST_LEAF(entry) )
216   {
217     retval = gbt_var_consistent( &r, trim , &strategy, TRUE, &tinfo );
218   } else {
219     bytea * q = gbt_text_xfrm ( ( bytea * ) trim );
220     retval    = gbt_var_consistent( &r, (void*)q, &strategy, FALSE, &tinfo );
221     if ( q != trim )
222       pfree(q);
223   }
224
225   pfree(trim);
226
227   if ( ktst != key ){
228     pfree ( key );
229   }
230   if ( qtst != query ){
231     pfree ( query );
232   }
233   PG_RETURN_BOOL(retval);
234 }
235
236
237
238
239 Datum
240 gbt_text_union(PG_FUNCTION_ARGS)
241 {
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 ) );
245 }
246  
247
248 Datum
249 gbt_text_picksplit(PG_FUNCTION_ARGS)
250 {
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);
255 }
256
257 Datum
258 gbt_text_same(PG_FUNCTION_ARGS)
259 {
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 ));
264 }
265
266
267 Datum
268 gbt_text_penalty(PG_FUNCTION_ARGS)
269 {
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 ) );
274 }
275