Header And Logo

PostgreSQL
| The world's most advanced open source database.

btree_text.c

Go to the documentation of this file.
00001 /*
00002  * contrib/btree_gist/btree_text.c
00003  */
00004 #include "postgres.h"
00005 
00006 #include "btree_gist.h"
00007 #include "btree_utils_var.h"
00008 #include "utils/builtins.h"
00009 
00010 /*
00011 ** Text ops
00012 */
00013 PG_FUNCTION_INFO_V1(gbt_text_compress);
00014 PG_FUNCTION_INFO_V1(gbt_bpchar_compress);
00015 PG_FUNCTION_INFO_V1(gbt_text_union);
00016 PG_FUNCTION_INFO_V1(gbt_text_picksplit);
00017 PG_FUNCTION_INFO_V1(gbt_text_consistent);
00018 PG_FUNCTION_INFO_V1(gbt_bpchar_consistent);
00019 PG_FUNCTION_INFO_V1(gbt_text_penalty);
00020 PG_FUNCTION_INFO_V1(gbt_text_same);
00021 
00022 Datum       gbt_text_compress(PG_FUNCTION_ARGS);
00023 Datum       gbt_bpchar_compress(PG_FUNCTION_ARGS);
00024 Datum       gbt_text_union(PG_FUNCTION_ARGS);
00025 Datum       gbt_text_picksplit(PG_FUNCTION_ARGS);
00026 Datum       gbt_text_consistent(PG_FUNCTION_ARGS);
00027 Datum       gbt_bpchar_consistent(PG_FUNCTION_ARGS);
00028 Datum       gbt_text_penalty(PG_FUNCTION_ARGS);
00029 Datum       gbt_text_same(PG_FUNCTION_ARGS);
00030 
00031 
00032 /* define for comparison */
00033 
00034 static bool
00035 gbt_textgt(const void *a, const void *b, Oid collation)
00036 {
00037     return DatumGetBool(DirectFunctionCall2Coll(text_gt,
00038                                                 collation,
00039                                                 PointerGetDatum(a),
00040                                                 PointerGetDatum(b)));
00041 }
00042 
00043 static bool
00044 gbt_textge(const void *a, const void *b, Oid collation)
00045 {
00046     return DatumGetBool(DirectFunctionCall2Coll(text_ge,
00047                                                 collation,
00048                                                 PointerGetDatum(a),
00049                                                 PointerGetDatum(b)));
00050 }
00051 
00052 static bool
00053 gbt_texteq(const void *a, const void *b, Oid collation)
00054 {
00055     return DatumGetBool(DirectFunctionCall2Coll(texteq,
00056                                                 collation,
00057                                                 PointerGetDatum(a),
00058                                                 PointerGetDatum(b)));
00059 }
00060 
00061 static bool
00062 gbt_textle(const void *a, const void *b, Oid collation)
00063 {
00064     return DatumGetBool(DirectFunctionCall2Coll(text_le,
00065                                                 collation,
00066                                                 PointerGetDatum(a),
00067                                                 PointerGetDatum(b)));
00068 }
00069 
00070 static bool
00071 gbt_textlt(const void *a, const void *b, Oid collation)
00072 {
00073     return DatumGetBool(DirectFunctionCall2Coll(text_lt,
00074                                                 collation,
00075                                                 PointerGetDatum(a),
00076                                                 PointerGetDatum(b)));
00077 }
00078 
00079 static int32
00080 gbt_textcmp(const void *a, const void *b, Oid collation)
00081 {
00082     return DatumGetInt32(DirectFunctionCall2Coll(bttextcmp,
00083                                                  collation,
00084                                                  PointerGetDatum(a),
00085                                                  PointerGetDatum(b)));
00086 }
00087 
00088 static gbtree_vinfo tinfo =
00089 {
00090     gbt_t_text,
00091     0,
00092     FALSE,
00093     gbt_textgt,
00094     gbt_textge,
00095     gbt_texteq,
00096     gbt_textle,
00097     gbt_textlt,
00098     gbt_textcmp,
00099     NULL
00100 };
00101 
00102 
00103 /**************************************************
00104  * Text ops
00105  **************************************************/
00106 
00107 
00108 Datum
00109 gbt_text_compress(PG_FUNCTION_ARGS)
00110 {
00111     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00112 
00113     if (tinfo.eml == 0)
00114     {
00115         tinfo.eml = pg_database_encoding_max_length();
00116     }
00117 
00118     PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
00119 }
00120 
00121 Datum
00122 gbt_bpchar_compress(PG_FUNCTION_ARGS)
00123 {
00124 
00125     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00126     GISTENTRY  *retval;
00127 
00128     if (tinfo.eml == 0)
00129     {
00130         tinfo.eml = pg_database_encoding_max_length();
00131     }
00132 
00133     if (entry->leafkey)
00134     {
00135 
00136         Datum       d = DirectFunctionCall1(rtrim1, entry->key);
00137         GISTENTRY   trim;
00138 
00139         gistentryinit(trim, d,
00140                       entry->rel, entry->page,
00141                       entry->offset, TRUE);
00142         retval = gbt_var_compress(&trim, &tinfo);
00143     }
00144     else
00145         retval = entry;
00146 
00147     PG_RETURN_POINTER(retval);
00148 }
00149 
00150 
00151 
00152 Datum
00153 gbt_text_consistent(PG_FUNCTION_ARGS)
00154 {
00155     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00156     void       *query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
00157     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
00158 
00159     /* Oid      subtype = PG_GETARG_OID(3); */
00160     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
00161     bool        retval;
00162     GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
00163     GBT_VARKEY_R r = gbt_var_key_readable(key);
00164 
00165     /* All cases served by this function are exact */
00166     *recheck = false;
00167 
00168     if (tinfo.eml == 0)
00169     {
00170         tinfo.eml = pg_database_encoding_max_length();
00171     }
00172 
00173     retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
00174                                 GIST_LEAF(entry), &tinfo);
00175 
00176     PG_RETURN_BOOL(retval);
00177 }
00178 
00179 
00180 Datum
00181 gbt_bpchar_consistent(PG_FUNCTION_ARGS)
00182 {
00183     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00184     void       *query = (void *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
00185     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
00186 
00187     /* Oid      subtype = PG_GETARG_OID(3); */
00188     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
00189     bool        retval;
00190     GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
00191     GBT_VARKEY_R r = gbt_var_key_readable(key);
00192     void       *trim = (void *) DatumGetPointer(DirectFunctionCall1(rtrim1, PointerGetDatum(query)));
00193 
00194     /* All cases served by this function are exact */
00195     *recheck = false;
00196 
00197     if (tinfo.eml == 0)
00198     {
00199         tinfo.eml = pg_database_encoding_max_length();
00200     }
00201 
00202     retval = gbt_var_consistent(&r, trim, strategy, PG_GET_COLLATION(),
00203                                 GIST_LEAF(entry), &tinfo);
00204     PG_RETURN_BOOL(retval);
00205 }
00206 
00207 
00208 Datum
00209 gbt_text_union(PG_FUNCTION_ARGS)
00210 {
00211     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
00212     int32      *size = (int *) PG_GETARG_POINTER(1);
00213 
00214     PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
00215                                     &tinfo));
00216 }
00217 
00218 
00219 Datum
00220 gbt_text_picksplit(PG_FUNCTION_ARGS)
00221 {
00222     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
00223     GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
00224 
00225     gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
00226                       &tinfo);
00227     PG_RETURN_POINTER(v);
00228 }
00229 
00230 Datum
00231 gbt_text_same(PG_FUNCTION_ARGS)
00232 {
00233     Datum       d1 = PG_GETARG_DATUM(0);
00234     Datum       d2 = PG_GETARG_DATUM(1);
00235     bool       *result = (bool *) PG_GETARG_POINTER(2);
00236 
00237     *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo);
00238     PG_RETURN_POINTER(result);
00239 }
00240 
00241 
00242 Datum
00243 gbt_text_penalty(PG_FUNCTION_ARGS)
00244 {
00245     GISTENTRY  *o = (GISTENTRY *) PG_GETARG_POINTER(0);
00246     GISTENTRY  *n = (GISTENTRY *) PG_GETARG_POINTER(1);
00247     float      *result = (float *) PG_GETARG_POINTER(2);
00248 
00249     PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
00250                                       &tinfo));
00251 }