Header And Logo

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

btree_macaddr.c

Go to the documentation of this file.
00001 /*
00002  * contrib/btree_gist/btree_macaddr.c
00003  */
00004 #include "postgres.h"
00005 
00006 #include "btree_gist.h"
00007 #include "btree_utils_num.h"
00008 #include "utils/builtins.h"
00009 #include "utils/inet.h"
00010 
00011 typedef struct
00012 {
00013     macaddr     lower;
00014     macaddr     upper;
00015 } macKEY;
00016 
00017 /*
00018 ** OID ops
00019 */
00020 PG_FUNCTION_INFO_V1(gbt_macad_compress);
00021 PG_FUNCTION_INFO_V1(gbt_macad_union);
00022 PG_FUNCTION_INFO_V1(gbt_macad_picksplit);
00023 PG_FUNCTION_INFO_V1(gbt_macad_consistent);
00024 PG_FUNCTION_INFO_V1(gbt_macad_penalty);
00025 PG_FUNCTION_INFO_V1(gbt_macad_same);
00026 
00027 Datum       gbt_macad_compress(PG_FUNCTION_ARGS);
00028 Datum       gbt_macad_union(PG_FUNCTION_ARGS);
00029 Datum       gbt_macad_picksplit(PG_FUNCTION_ARGS);
00030 Datum       gbt_macad_consistent(PG_FUNCTION_ARGS);
00031 Datum       gbt_macad_penalty(PG_FUNCTION_ARGS);
00032 Datum       gbt_macad_same(PG_FUNCTION_ARGS);
00033 
00034 
00035 static bool
00036 gbt_macadgt(const void *a, const void *b)
00037 {
00038     return DatumGetBool(DirectFunctionCall2(macaddr_gt, PointerGetDatum(a), PointerGetDatum(b)));
00039 }
00040 static bool
00041 gbt_macadge(const void *a, const void *b)
00042 {
00043     return DatumGetBool(DirectFunctionCall2(macaddr_ge, PointerGetDatum(a), PointerGetDatum(b)));
00044 }
00045 
00046 static bool
00047 gbt_macadeq(const void *a, const void *b)
00048 {
00049     return DatumGetBool(DirectFunctionCall2(macaddr_eq, PointerGetDatum(a), PointerGetDatum(b)));
00050 }
00051 
00052 static bool
00053 gbt_macadle(const void *a, const void *b)
00054 {
00055     return DatumGetBool(DirectFunctionCall2(macaddr_le, PointerGetDatum(a), PointerGetDatum(b)));
00056 }
00057 
00058 static bool
00059 gbt_macadlt(const void *a, const void *b)
00060 {
00061     return DatumGetBool(DirectFunctionCall2(macaddr_lt, PointerGetDatum(a), PointerGetDatum(b)));
00062 }
00063 
00064 
00065 static int
00066 gbt_macadkey_cmp(const void *a, const void *b)
00067 {
00068     macKEY     *ia = (macKEY *) (((const Nsrt *) a)->t);
00069     macKEY     *ib = (macKEY *) (((const Nsrt *) b)->t);
00070     int         res;
00071 
00072     res = DatumGetInt32(DirectFunctionCall2(macaddr_cmp, MacaddrPGetDatum(&ia->lower), MacaddrPGetDatum(&ib->lower)));
00073     if (res == 0)
00074         return DatumGetInt32(DirectFunctionCall2(macaddr_cmp, MacaddrPGetDatum(&ia->upper), MacaddrPGetDatum(&ib->upper)));
00075 
00076     return res;
00077 }
00078 
00079 
00080 static const gbtree_ninfo tinfo =
00081 {
00082     gbt_t_macad,
00083     sizeof(macaddr),
00084     gbt_macadgt,
00085     gbt_macadge,
00086     gbt_macadeq,
00087     gbt_macadle,
00088     gbt_macadlt,
00089     gbt_macadkey_cmp,
00090     NULL
00091 };
00092 
00093 
00094 /**************************************************
00095  * macaddr ops
00096  **************************************************/
00097 
00098 
00099 
00100 static uint64
00101 mac_2_uint64(macaddr *m)
00102 {
00103     unsigned char *mi = (unsigned char *) m;
00104     uint64      res = 0;
00105     int         i;
00106 
00107     for (i = 0; i < 6; i++)
00108         res += (((uint64) mi[i]) << ((uint64) ((5 - i) * 8)));
00109     return res;
00110 }
00111 
00112 
00113 
00114 Datum
00115 gbt_macad_compress(PG_FUNCTION_ARGS)
00116 {
00117     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00118     GISTENTRY  *retval = NULL;
00119 
00120     PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
00121 }
00122 
00123 
00124 Datum
00125 gbt_macad_consistent(PG_FUNCTION_ARGS)
00126 {
00127     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00128     macaddr    *query = (macaddr *) PG_GETARG_POINTER(1);
00129     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
00130 
00131     /* Oid      subtype = PG_GETARG_OID(3); */
00132     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
00133     macKEY     *kkk = (macKEY *) DatumGetPointer(entry->key);
00134     GBT_NUMKEY_R key;
00135 
00136     /* All cases served by this function are exact */
00137     *recheck = false;
00138 
00139     key.lower = (GBT_NUMKEY *) &kkk->lower;
00140     key.upper = (GBT_NUMKEY *) &kkk->upper;
00141 
00142     PG_RETURN_BOOL(
00143                    gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
00144         );
00145 }
00146 
00147 
00148 Datum
00149 gbt_macad_union(PG_FUNCTION_ARGS)
00150 {
00151     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
00152     void       *out = palloc(sizeof(macKEY));
00153 
00154     *(int *) PG_GETARG_POINTER(1) = sizeof(macKEY);
00155     PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
00156 }
00157 
00158 
00159 Datum
00160 gbt_macad_penalty(PG_FUNCTION_ARGS)
00161 {
00162     macKEY     *origentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
00163     macKEY     *newentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
00164     float      *result = (float *) PG_GETARG_POINTER(2);
00165     uint64      iorg[2],
00166                 inew[2];
00167 
00168     iorg[0] = mac_2_uint64(&origentry->lower);
00169     iorg[1] = mac_2_uint64(&origentry->upper);
00170     inew[0] = mac_2_uint64(&newentry->lower);
00171     inew[1] = mac_2_uint64(&newentry->upper);
00172 
00173     penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
00174 
00175     PG_RETURN_POINTER(result);
00176 
00177 }
00178 
00179 Datum
00180 gbt_macad_picksplit(PG_FUNCTION_ARGS)
00181 {
00182     PG_RETURN_POINTER(gbt_num_picksplit(
00183                                     (GistEntryVector *) PG_GETARG_POINTER(0),
00184                                       (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
00185                                         &tinfo
00186                                         ));
00187 }
00188 
00189 Datum
00190 gbt_macad_same(PG_FUNCTION_ARGS)
00191 {
00192     macKEY     *b1 = (macKEY *) PG_GETARG_POINTER(0);
00193     macKEY     *b2 = (macKEY *) PG_GETARG_POINTER(1);
00194     bool       *result = (bool *) PG_GETARG_POINTER(2);
00195 
00196     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
00197     PG_RETURN_POINTER(result);
00198 }