Header And Logo

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

btree_interval.c

Go to the documentation of this file.
00001 /*
00002  * contrib/btree_gist/btree_interval.c
00003  */
00004 #include "postgres.h"
00005 
00006 #include "btree_gist.h"
00007 #include "btree_utils_num.h"
00008 #include "utils/timestamp.h"
00009 
00010 typedef struct
00011 {
00012     Interval    lower,
00013                 upper;
00014 } intvKEY;
00015 
00016 
00017 /*
00018 ** Interval ops
00019 */
00020 PG_FUNCTION_INFO_V1(gbt_intv_compress);
00021 PG_FUNCTION_INFO_V1(gbt_intv_decompress);
00022 PG_FUNCTION_INFO_V1(gbt_intv_union);
00023 PG_FUNCTION_INFO_V1(gbt_intv_picksplit);
00024 PG_FUNCTION_INFO_V1(gbt_intv_consistent);
00025 PG_FUNCTION_INFO_V1(gbt_intv_distance);
00026 PG_FUNCTION_INFO_V1(gbt_intv_penalty);
00027 PG_FUNCTION_INFO_V1(gbt_intv_same);
00028 
00029 Datum       gbt_intv_compress(PG_FUNCTION_ARGS);
00030 Datum       gbt_intv_decompress(PG_FUNCTION_ARGS);
00031 Datum       gbt_intv_union(PG_FUNCTION_ARGS);
00032 Datum       gbt_intv_picksplit(PG_FUNCTION_ARGS);
00033 Datum       gbt_intv_consistent(PG_FUNCTION_ARGS);
00034 Datum       gbt_intv_distance(PG_FUNCTION_ARGS);
00035 Datum       gbt_intv_penalty(PG_FUNCTION_ARGS);
00036 Datum       gbt_intv_same(PG_FUNCTION_ARGS);
00037 
00038 
00039 static bool
00040 gbt_intvgt(const void *a, const void *b)
00041 {
00042     return DatumGetBool(DirectFunctionCall2(interval_gt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
00043 }
00044 
00045 static bool
00046 gbt_intvge(const void *a, const void *b)
00047 {
00048     return DatumGetBool(DirectFunctionCall2(interval_ge, IntervalPGetDatum(a), IntervalPGetDatum(b)));
00049 }
00050 
00051 static bool
00052 gbt_intveq(const void *a, const void *b)
00053 {
00054     return DatumGetBool(DirectFunctionCall2(interval_eq, IntervalPGetDatum(a), IntervalPGetDatum(b)));
00055 }
00056 
00057 static bool
00058 gbt_intvle(const void *a, const void *b)
00059 {
00060     return DatumGetBool(DirectFunctionCall2(interval_le, IntervalPGetDatum(a), IntervalPGetDatum(b)));
00061 }
00062 
00063 static bool
00064 gbt_intvlt(const void *a, const void *b)
00065 {
00066     return DatumGetBool(DirectFunctionCall2(interval_lt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
00067 }
00068 
00069 static int
00070 gbt_intvkey_cmp(const void *a, const void *b)
00071 {
00072     intvKEY    *ia = (intvKEY *) (((const Nsrt *) a)->t);
00073     intvKEY    *ib = (intvKEY *) (((const Nsrt *) b)->t);
00074     int         res;
00075 
00076     res = DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->lower), IntervalPGetDatum(&ib->lower)));
00077     if (res == 0)
00078         return DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->upper), IntervalPGetDatum(&ib->upper)));
00079 
00080     return res;
00081 }
00082 
00083 
00084 static double
00085 intr2num(const Interval *i)
00086 {
00087     return INTERVAL_TO_SEC(i);
00088 }
00089 
00090 static float8
00091 gbt_intv_dist(const void *a, const void *b)
00092 {
00093     return (float8) Abs(intr2num((const Interval *) a) - intr2num((const Interval *) b));
00094 }
00095 
00096 /*
00097  * INTERVALSIZE should be the actual size-on-disk of an Interval, as shown
00098  * in pg_type.  This might be less than sizeof(Interval) if the compiler
00099  * insists on adding alignment padding at the end of the struct.
00100  */
00101 #define INTERVALSIZE 16
00102 
00103 static const gbtree_ninfo tinfo =
00104 {
00105     gbt_t_intv,
00106     sizeof(Interval),
00107     gbt_intvgt,
00108     gbt_intvge,
00109     gbt_intveq,
00110     gbt_intvle,
00111     gbt_intvlt,
00112     gbt_intvkey_cmp,
00113     gbt_intv_dist
00114 };
00115 
00116 
00117 Interval *
00118 abs_interval(Interval *a)
00119 {
00120     static Interval zero = {0, 0, 0};
00121 
00122     if (DatumGetBool(DirectFunctionCall2(interval_lt,
00123                                          IntervalPGetDatum(a),
00124                                          IntervalPGetDatum(&zero))))
00125         a = DatumGetIntervalP(DirectFunctionCall1(interval_um,
00126                                                   IntervalPGetDatum(a)));
00127 
00128     return a;
00129 }
00130 
00131 PG_FUNCTION_INFO_V1(interval_dist);
00132 Datum       interval_dist(PG_FUNCTION_ARGS);
00133 Datum
00134 interval_dist(PG_FUNCTION_ARGS)
00135 {
00136     Datum       diff = DirectFunctionCall2(interval_mi,
00137                                            PG_GETARG_DATUM(0),
00138                                            PG_GETARG_DATUM(1));
00139 
00140     PG_RETURN_INTERVAL_P(abs_interval(DatumGetIntervalP(diff)));
00141 }
00142 
00143 
00144 /**************************************************
00145  * interval ops
00146  **************************************************/
00147 
00148 
00149 Datum
00150 gbt_intv_compress(PG_FUNCTION_ARGS)
00151 {
00152     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00153     GISTENTRY  *retval = entry;
00154 
00155     if (entry->leafkey || INTERVALSIZE != sizeof(Interval))
00156     {
00157         char       *r = (char *) palloc(2 * INTERVALSIZE);
00158 
00159         retval = palloc(sizeof(GISTENTRY));
00160 
00161         if (entry->leafkey)
00162         {
00163             Interval   *key = DatumGetIntervalP(entry->key);
00164 
00165             memcpy((void *) r, (void *) key, INTERVALSIZE);
00166             memcpy((void *) (r + INTERVALSIZE), (void *) key, INTERVALSIZE);
00167         }
00168         else
00169         {
00170             intvKEY    *key = (intvKEY *) DatumGetPointer(entry->key);
00171 
00172             memcpy(r, &key->lower, INTERVALSIZE);
00173             memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
00174         }
00175         gistentryinit(*retval, PointerGetDatum(r),
00176                       entry->rel, entry->page,
00177                       entry->offset, FALSE);
00178     }
00179 
00180     PG_RETURN_POINTER(retval);
00181 
00182 }
00183 
00184 Datum
00185 gbt_intv_decompress(PG_FUNCTION_ARGS)
00186 {
00187     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00188     GISTENTRY  *retval = entry;
00189 
00190     if (INTERVALSIZE != sizeof(Interval))
00191     {
00192         intvKEY    *r = palloc(sizeof(intvKEY));
00193         char       *key = DatumGetPointer(entry->key);
00194 
00195         retval = palloc(sizeof(GISTENTRY));
00196         memcpy(&r->lower, key, INTERVALSIZE);
00197         memcpy(&r->upper, key + INTERVALSIZE, INTERVALSIZE);
00198 
00199         gistentryinit(*retval, PointerGetDatum(r),
00200                       entry->rel, entry->page,
00201                       entry->offset, FALSE);
00202     }
00203     PG_RETURN_POINTER(retval);
00204 }
00205 
00206 
00207 Datum
00208 gbt_intv_consistent(PG_FUNCTION_ARGS)
00209 {
00210     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00211     Interval   *query = PG_GETARG_INTERVAL_P(1);
00212     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
00213 
00214     /* Oid      subtype = PG_GETARG_OID(3); */
00215     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
00216     intvKEY    *kkk = (intvKEY *) DatumGetPointer(entry->key);
00217     GBT_NUMKEY_R key;
00218 
00219     /* All cases served by this function are exact */
00220     *recheck = false;
00221 
00222     key.lower = (GBT_NUMKEY *) &kkk->lower;
00223     key.upper = (GBT_NUMKEY *) &kkk->upper;
00224 
00225     PG_RETURN_BOOL(
00226                    gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
00227         );
00228 }
00229 
00230 
00231 Datum
00232 gbt_intv_distance(PG_FUNCTION_ARGS)
00233 {
00234     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00235     Interval   *query = PG_GETARG_INTERVAL_P(1);
00236 
00237     /* Oid      subtype = PG_GETARG_OID(3); */
00238     intvKEY    *kkk = (intvKEY *) DatumGetPointer(entry->key);
00239     GBT_NUMKEY_R key;
00240 
00241     key.lower = (GBT_NUMKEY *) &kkk->lower;
00242     key.upper = (GBT_NUMKEY *) &kkk->upper;
00243 
00244     PG_RETURN_FLOAT8(
00245              gbt_num_distance(&key, (void *) query, GIST_LEAF(entry), &tinfo)
00246         );
00247 }
00248 
00249 
00250 Datum
00251 gbt_intv_union(PG_FUNCTION_ARGS)
00252 {
00253     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
00254     void       *out = palloc(sizeof(intvKEY));
00255 
00256     *(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
00257     PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
00258 }
00259 
00260 
00261 Datum
00262 gbt_intv_penalty(PG_FUNCTION_ARGS)
00263 {
00264     intvKEY    *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
00265     intvKEY    *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
00266     float      *result = (float *) PG_GETARG_POINTER(2);
00267     double      iorg[2],
00268                 inew[2];
00269 
00270     iorg[0] = intr2num(&origentry->lower);
00271     iorg[1] = intr2num(&origentry->upper);
00272     inew[0] = intr2num(&newentry->lower);
00273     inew[1] = intr2num(&newentry->upper);
00274 
00275     penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
00276 
00277     PG_RETURN_POINTER(result);
00278 
00279 }
00280 
00281 Datum
00282 gbt_intv_picksplit(PG_FUNCTION_ARGS)
00283 {
00284     PG_RETURN_POINTER(gbt_num_picksplit(
00285                                     (GistEntryVector *) PG_GETARG_POINTER(0),
00286                                       (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
00287                                         &tinfo
00288                                         ));
00289 }
00290 
00291 Datum
00292 gbt_intv_same(PG_FUNCTION_ARGS)
00293 {
00294     intvKEY    *b1 = (intvKEY *) PG_GETARG_POINTER(0);
00295     intvKEY    *b2 = (intvKEY *) PG_GETARG_POINTER(1);
00296     bool       *result = (bool *) PG_GETARG_POINTER(2);
00297 
00298     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
00299     PG_RETURN_POINTER(result);
00300 }