LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_macaddr8.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12devel Lines: 69 69 100.0 %
Date: 2018-11-16 21:21:19 Functions: 21 21 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * contrib/btree_gist/btree_macaddr8.c
       3             :  */
       4             : #include "postgres.h"
       5             : 
       6             : #include "btree_gist.h"
       7             : #include "btree_utils_num.h"
       8             : #include "utils/builtins.h"
       9             : #include "utils/inet.h"
      10             : 
      11             : typedef struct
      12             : {
      13             :     macaddr8    lower;
      14             :     macaddr8    upper;
      15             :     /* make struct size = sizeof(gbtreekey16) */
      16             : } mac8KEY;
      17             : 
      18             : /*
      19             : ** OID ops
      20             : */
      21           4 : PG_FUNCTION_INFO_V1(gbt_macad8_compress);
      22           4 : PG_FUNCTION_INFO_V1(gbt_macad8_fetch);
      23           4 : PG_FUNCTION_INFO_V1(gbt_macad8_union);
      24           4 : PG_FUNCTION_INFO_V1(gbt_macad8_picksplit);
      25           4 : PG_FUNCTION_INFO_V1(gbt_macad8_consistent);
      26           4 : PG_FUNCTION_INFO_V1(gbt_macad8_penalty);
      27           4 : PG_FUNCTION_INFO_V1(gbt_macad8_same);
      28             : 
      29             : 
      30             : static bool
      31        3358 : gbt_macad8gt(const void *a, const void *b, FmgrInfo *flinfo)
      32             : {
      33        3358 :     return DatumGetBool(DirectFunctionCall2(macaddr8_gt, PointerGetDatum(a), PointerGetDatum(b)));
      34             : }
      35             : static bool
      36         324 : gbt_macad8ge(const void *a, const void *b, FmgrInfo *flinfo)
      37             : {
      38         324 :     return DatumGetBool(DirectFunctionCall2(macaddr8_ge, PointerGetDatum(a), PointerGetDatum(b)));
      39             : }
      40             : 
      41             : static bool
      42        1704 : gbt_macad8eq(const void *a, const void *b, FmgrInfo *flinfo)
      43             : {
      44        1704 :     return DatumGetBool(DirectFunctionCall2(macaddr8_eq, PointerGetDatum(a), PointerGetDatum(b)));
      45             : }
      46             : 
      47             : static bool
      48        1226 : gbt_macad8le(const void *a, const void *b, FmgrInfo *flinfo)
      49             : {
      50        1226 :     return DatumGetBool(DirectFunctionCall2(macaddr8_le, PointerGetDatum(a), PointerGetDatum(b)));
      51             : }
      52             : 
      53             : static bool
      54        3958 : gbt_macad8lt(const void *a, const void *b, FmgrInfo *flinfo)
      55             : {
      56        3958 :     return DatumGetBool(DirectFunctionCall2(macaddr8_lt, PointerGetDatum(a), PointerGetDatum(b)));
      57             : }
      58             : 
      59             : 
      60             : static int
      61       11730 : gbt_macad8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      62             : {
      63       11730 :     mac8KEY    *ia = (mac8KEY *) (((const Nsrt *) a)->t);
      64       11730 :     mac8KEY    *ib = (mac8KEY *) (((const Nsrt *) b)->t);
      65             :     int         res;
      66             : 
      67       11730 :     res = DatumGetInt32(DirectFunctionCall2(macaddr8_cmp, Macaddr8PGetDatum(&ia->lower), Macaddr8PGetDatum(&ib->lower)));
      68       11730 :     if (res == 0)
      69        1462 :         return DatumGetInt32(DirectFunctionCall2(macaddr8_cmp, Macaddr8PGetDatum(&ia->upper), Macaddr8PGetDatum(&ib->upper)));
      70             : 
      71       10268 :     return res;
      72             : }
      73             : 
      74             : 
      75             : static const gbtree_ninfo tinfo =
      76             : {
      77             :     gbt_t_macad8,
      78             :     sizeof(macaddr8),
      79             :     16,                         /* sizeof(gbtreekey16) */
      80             :     gbt_macad8gt,
      81             :     gbt_macad8ge,
      82             :     gbt_macad8eq,
      83             :     gbt_macad8le,
      84             :     gbt_macad8lt,
      85             :     gbt_macad8key_cmp,
      86             :     NULL
      87             : };
      88             : 
      89             : 
      90             : /**************************************************
      91             :  * macaddr ops
      92             :  **************************************************/
      93             : 
      94             : 
      95             : 
      96             : static uint64
      97        5808 : mac8_2_uint64(macaddr8 *m)
      98             : {
      99        5808 :     unsigned char *mi = (unsigned char *) m;
     100        5808 :     uint64      res = 0;
     101             :     int         i;
     102             : 
     103       52272 :     for (i = 0; i < 8; i++)
     104       46464 :         res += (((uint64) mi[i]) << ((uint64) ((7 - i) * 8)));
     105        5808 :     return res;
     106             : }
     107             : 
     108             : 
     109             : 
     110             : Datum
     111        1214 : gbt_macad8_compress(PG_FUNCTION_ARGS)
     112             : {
     113        1214 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     114             : 
     115        1214 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     116             : }
     117             : 
     118             : Datum
     119          16 : gbt_macad8_fetch(PG_FUNCTION_ARGS)
     120             : {
     121          16 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     122             : 
     123          16 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     124             : }
     125             : 
     126             : Datum
     127        3648 : gbt_macad8_consistent(PG_FUNCTION_ARGS)
     128             : {
     129        3648 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     130        3648 :     macaddr8   *query = (macaddr8 *) PG_GETARG_POINTER(1);
     131        3648 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     132             : 
     133             :     /* Oid      subtype = PG_GETARG_OID(3); */
     134        3648 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     135        3648 :     mac8KEY    *kkk = (mac8KEY *) DatumGetPointer(entry->key);
     136             :     GBT_NUMKEY_R key;
     137             : 
     138             :     /* All cases served by this function are exact */
     139        3648 :     *recheck = false;
     140             : 
     141        3648 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     142        3648 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     143             : 
     144        3648 :     PG_RETURN_BOOL(
     145             :                    gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
     146             :         );
     147             : }
     148             : 
     149             : 
     150             : Datum
     151         704 : gbt_macad8_union(PG_FUNCTION_ARGS)
     152             : {
     153         704 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     154         704 :     void       *out = palloc0(sizeof(mac8KEY));
     155             : 
     156         704 :     *(int *) PG_GETARG_POINTER(1) = sizeof(mac8KEY);
     157         704 :     PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
     158             : }
     159             : 
     160             : 
     161             : Datum
     162        1452 : gbt_macad8_penalty(PG_FUNCTION_ARGS)
     163             : {
     164        1452 :     mac8KEY    *origentry = (mac8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     165        1452 :     mac8KEY    *newentry = (mac8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     166        1452 :     float      *result = (float *) PG_GETARG_POINTER(2);
     167             :     uint64      iorg[2],
     168             :                 inew[2];
     169             : 
     170        1452 :     iorg[0] = mac8_2_uint64(&origentry->lower);
     171        1452 :     iorg[1] = mac8_2_uint64(&origentry->upper);
     172        1452 :     inew[0] = mac8_2_uint64(&newentry->lower);
     173        1452 :     inew[1] = mac8_2_uint64(&newentry->upper);
     174             : 
     175        1452 :     penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
     176             : 
     177        1452 :     PG_RETURN_POINTER(result);
     178             : 
     179             : }
     180             : 
     181             : Datum
     182           6 : gbt_macad8_picksplit(PG_FUNCTION_ARGS)
     183             : {
     184           6 :     PG_RETURN_POINTER(gbt_num_picksplit(
     185             :                                         (GistEntryVector *) PG_GETARG_POINTER(0),
     186             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     187             :                                         &tinfo, fcinfo->flinfo
     188             :                                         ));
     189             : }
     190             : 
     191             : Datum
     192         702 : gbt_macad8_same(PG_FUNCTION_ARGS)
     193             : {
     194         702 :     mac8KEY    *b1 = (mac8KEY *) PG_GETARG_POINTER(0);
     195         702 :     mac8KEY    *b2 = (mac8KEY *) PG_GETARG_POINTER(1);
     196         702 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     197             : 
     198         702 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     199         702 :     PG_RETURN_POINTER(result);
     200             : }

Generated by: LCOV version 1.13