Header And Logo

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

btree_cash.c

Go to the documentation of this file.
00001 /*
00002  * contrib/btree_gist/btree_cash.c
00003  */
00004 #include "postgres.h"
00005 
00006 #include "btree_gist.h"
00007 #include "btree_utils_num.h"
00008 #include "utils/cash.h"
00009 
00010 typedef struct
00011 {
00012     Cash        lower;
00013     Cash        upper;
00014 } cashKEY;
00015 
00016 /*
00017 ** Cash ops
00018 */
00019 PG_FUNCTION_INFO_V1(gbt_cash_compress);
00020 PG_FUNCTION_INFO_V1(gbt_cash_union);
00021 PG_FUNCTION_INFO_V1(gbt_cash_picksplit);
00022 PG_FUNCTION_INFO_V1(gbt_cash_consistent);
00023 PG_FUNCTION_INFO_V1(gbt_cash_distance);
00024 PG_FUNCTION_INFO_V1(gbt_cash_penalty);
00025 PG_FUNCTION_INFO_V1(gbt_cash_same);
00026 
00027 Datum       gbt_cash_compress(PG_FUNCTION_ARGS);
00028 Datum       gbt_cash_union(PG_FUNCTION_ARGS);
00029 Datum       gbt_cash_picksplit(PG_FUNCTION_ARGS);
00030 Datum       gbt_cash_consistent(PG_FUNCTION_ARGS);
00031 Datum       gbt_cash_distance(PG_FUNCTION_ARGS);
00032 Datum       gbt_cash_penalty(PG_FUNCTION_ARGS);
00033 Datum       gbt_cash_same(PG_FUNCTION_ARGS);
00034 
00035 static bool
00036 gbt_cashgt(const void *a, const void *b)
00037 {
00038     return (*((const Cash *) a) > *((const Cash *) b));
00039 }
00040 static bool
00041 gbt_cashge(const void *a, const void *b)
00042 {
00043     return (*((const Cash *) a) >= *((const Cash *) b));
00044 }
00045 static bool
00046 gbt_casheq(const void *a, const void *b)
00047 {
00048     return (*((const Cash *) a) == *((const Cash *) b));
00049 }
00050 static bool
00051 gbt_cashle(const void *a, const void *b)
00052 {
00053     return (*((const Cash *) a) <= *((const Cash *) b));
00054 }
00055 static bool
00056 gbt_cashlt(const void *a, const void *b)
00057 {
00058     return (*((const Cash *) a) < *((const Cash *) b));
00059 }
00060 
00061 static int
00062 gbt_cashkey_cmp(const void *a, const void *b)
00063 {
00064     cashKEY    *ia = (cashKEY *) (((const Nsrt *) a)->t);
00065     cashKEY    *ib = (cashKEY *) (((const Nsrt *) b)->t);
00066 
00067     if (ia->lower == ib->lower)
00068     {
00069         if (ia->upper == ib->upper)
00070             return 0;
00071 
00072         return (ia->upper > ib->upper) ? 1 : -1;
00073     }
00074 
00075     return (ia->lower > ib->lower) ? 1 : -1;
00076 }
00077 
00078 static float8
00079 gbt_cash_dist(const void *a, const void *b)
00080 {
00081     return GET_FLOAT_DISTANCE(Cash, a, b);
00082 }
00083 
00084 
00085 static const gbtree_ninfo tinfo =
00086 {
00087     gbt_t_cash,
00088     sizeof(Cash),
00089     gbt_cashgt,
00090     gbt_cashge,
00091     gbt_casheq,
00092     gbt_cashle,
00093     gbt_cashlt,
00094     gbt_cashkey_cmp,
00095     gbt_cash_dist
00096 };
00097 
00098 
00099 PG_FUNCTION_INFO_V1(cash_dist);
00100 Datum       cash_dist(PG_FUNCTION_ARGS);
00101 Datum
00102 cash_dist(PG_FUNCTION_ARGS)
00103 {
00104     Cash        a = PG_GETARG_CASH(0);
00105     Cash        b = PG_GETARG_CASH(1);
00106     Cash        r;
00107     Cash        ra;
00108 
00109     r = a - b;
00110     ra = Abs(r);
00111 
00112     /* Overflow check. */
00113     if (ra < 0 || (!SAMESIGN(a, b) && !SAMESIGN(r, a)))
00114         ereport(ERROR,
00115                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
00116                  errmsg("money out of range")));
00117 
00118     PG_RETURN_CASH(ra);
00119 }
00120 
00121 /**************************************************
00122  * Cash ops
00123  **************************************************/
00124 
00125 
00126 Datum
00127 gbt_cash_compress(PG_FUNCTION_ARGS)
00128 {
00129     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00130     GISTENTRY  *retval = NULL;
00131 
00132     PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
00133 }
00134 
00135 
00136 Datum
00137 gbt_cash_consistent(PG_FUNCTION_ARGS)
00138 {
00139     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00140     Cash        query = PG_GETARG_CASH(1);
00141     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
00142 
00143     /* Oid      subtype = PG_GETARG_OID(3); */
00144     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
00145     cashKEY    *kkk = (cashKEY *) DatumGetPointer(entry->key);
00146     GBT_NUMKEY_R key;
00147 
00148     /* All cases served by this function are exact */
00149     *recheck = false;
00150 
00151     key.lower = (GBT_NUMKEY *) &kkk->lower;
00152     key.upper = (GBT_NUMKEY *) &kkk->upper;
00153 
00154     PG_RETURN_BOOL(
00155                    gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
00156         );
00157 }
00158 
00159 
00160 Datum
00161 gbt_cash_distance(PG_FUNCTION_ARGS)
00162 {
00163     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00164     Cash        query = PG_GETARG_CASH(1);
00165 
00166     /* Oid      subtype = PG_GETARG_OID(3); */
00167     cashKEY    *kkk = (cashKEY *) DatumGetPointer(entry->key);
00168     GBT_NUMKEY_R key;
00169 
00170     key.lower = (GBT_NUMKEY *) &kkk->lower;
00171     key.upper = (GBT_NUMKEY *) &kkk->upper;
00172 
00173     PG_RETURN_FLOAT8(
00174             gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo)
00175         );
00176 }
00177 
00178 
00179 Datum
00180 gbt_cash_union(PG_FUNCTION_ARGS)
00181 {
00182     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
00183     void       *out = palloc(sizeof(cashKEY));
00184 
00185     *(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
00186     PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
00187 }
00188 
00189 
00190 Datum
00191 gbt_cash_penalty(PG_FUNCTION_ARGS)
00192 {
00193     cashKEY    *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
00194     cashKEY    *newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
00195     float      *result = (float *) PG_GETARG_POINTER(2);
00196 
00197     penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
00198 
00199     PG_RETURN_POINTER(result);
00200 
00201 }
00202 
00203 Datum
00204 gbt_cash_picksplit(PG_FUNCTION_ARGS)
00205 {
00206     PG_RETURN_POINTER(gbt_num_picksplit(
00207                                     (GistEntryVector *) PG_GETARG_POINTER(0),
00208                                       (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
00209                                         &tinfo
00210                                         ));
00211 }
00212 
00213 Datum
00214 gbt_cash_same(PG_FUNCTION_ARGS)
00215 {
00216     cashKEY    *b1 = (cashKEY *) PG_GETARG_POINTER(0);
00217     cashKEY    *b2 = (cashKEY *) PG_GETARG_POINTER(1);
00218     bool       *result = (bool *) PG_GETARG_POINTER(2);
00219 
00220     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
00221     PG_RETURN_POINTER(result);
00222 }