LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_macaddr.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 72.2 % 79 57
Test Date: 2026-07-03 19:57:34 Functions: 87.5 % 24 21
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 50.0 % 4 2

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * contrib/btree_gist/btree_macaddr.c
       3                 :             :  */
       4                 :             : #include "postgres.h"
       5                 :             : 
       6                 :             : #include "btree_gist.h"
       7                 :             : #include "btree_utils_num.h"
       8                 :             : #include "utils/fmgrprotos.h"
       9                 :             : #include "utils/inet.h"
      10                 :             : #include "utils/rel.h"
      11                 :             : #include "utils/sortsupport.h"
      12                 :             : 
      13                 :             : typedef struct
      14                 :             : {
      15                 :             :     macaddr     lower;
      16                 :             :     macaddr     upper;
      17                 :             :     char        pad[4];         /* make struct size = sizeof(gbtreekey16) */
      18                 :             : } macKEY;
      19                 :             : 
      20                 :             : /* GiST support functions */
      21                 :           4 : PG_FUNCTION_INFO_V1(gbt_macad_compress);
      22                 :           4 : PG_FUNCTION_INFO_V1(gbt_macad_fetch);
      23                 :           4 : PG_FUNCTION_INFO_V1(gbt_macad_union);
      24                 :           4 : PG_FUNCTION_INFO_V1(gbt_macad_picksplit);
      25                 :           4 : PG_FUNCTION_INFO_V1(gbt_macad_consistent);
      26                 :           4 : PG_FUNCTION_INFO_V1(gbt_macad_penalty);
      27                 :           4 : PG_FUNCTION_INFO_V1(gbt_macad_same);
      28                 :           4 : PG_FUNCTION_INFO_V1(gbt_macaddr_sortsupport);
      29                 :             : 
      30                 :             : 
      31                 :             : static bool
      32                 :        2393 : gbt_macadgt(const void *a, const void *b, FmgrInfo *flinfo)
      33                 :             : {
      34                 :        2393 :     return DatumGetBool(DirectFunctionCall2(macaddr_gt, PointerGetDatum(a), PointerGetDatum(b)));
      35                 :             : }
      36                 :             : static bool
      37                 :         609 : gbt_macadge(const void *a, const void *b, FmgrInfo *flinfo)
      38                 :             : {
      39                 :         609 :     return DatumGetBool(DirectFunctionCall2(macaddr_ge, PointerGetDatum(a), PointerGetDatum(b)));
      40                 :             : }
      41                 :             : 
      42                 :             : static bool
      43                 :         150 : gbt_macadeq(const void *a, const void *b, FmgrInfo *flinfo)
      44                 :             : {
      45                 :         150 :     return DatumGetBool(DirectFunctionCall2(macaddr_eq, PointerGetDatum(a), PointerGetDatum(b)));
      46                 :             : }
      47                 :             : 
      48                 :             : static bool
      49                 :         166 : gbt_macadle(const void *a, const void *b, FmgrInfo *flinfo)
      50                 :             : {
      51                 :         166 :     return DatumGetBool(DirectFunctionCall2(macaddr_le, PointerGetDatum(a), PointerGetDatum(b)));
      52                 :             : }
      53                 :             : 
      54                 :             : static bool
      55                 :        2093 : gbt_macadlt(const void *a, const void *b, FmgrInfo *flinfo)
      56                 :             : {
      57                 :        2093 :     return DatumGetBool(DirectFunctionCall2(macaddr_lt, PointerGetDatum(a), PointerGetDatum(b)));
      58                 :             : }
      59                 :             : 
      60                 :             : 
      61                 :             : static int
      62                 :        1197 : gbt_macadkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      63                 :             : {
      64                 :        1197 :     macKEY     *ia = (macKEY *) (((const Nsrt *) a)->t);
      65                 :        1197 :     macKEY     *ib = (macKEY *) (((const Nsrt *) b)->t);
      66                 :             :     int         res;
      67                 :             : 
      68                 :        1197 :     res = DatumGetInt32(DirectFunctionCall2(macaddr_cmp, MacaddrPGetDatum(&ia->lower), MacaddrPGetDatum(&ib->lower)));
      69         [ +  + ]:        1197 :     if (res == 0)
      70                 :         900 :         return DatumGetInt32(DirectFunctionCall2(macaddr_cmp, MacaddrPGetDatum(&ia->upper), MacaddrPGetDatum(&ib->upper)));
      71                 :             : 
      72                 :         297 :     return res;
      73                 :             : }
      74                 :             : 
      75                 :             : 
      76                 :             : static const gbtree_ninfo tinfo =
      77                 :             : {
      78                 :             :     gbt_t_macad,
      79                 :             :     sizeof(macaddr),
      80                 :             :     16,                         /* sizeof(gbtreekey16) */
      81                 :             :     gbt_macadgt,
      82                 :             :     gbt_macadge,
      83                 :             :     gbt_macadeq,
      84                 :             :     gbt_macadle,
      85                 :             :     gbt_macadlt,
      86                 :             :     gbt_macadkey_cmp,
      87                 :             :     NULL
      88                 :             : };
      89                 :             : 
      90                 :             : 
      91                 :             : /**************************************************
      92                 :             :  * GiST support functions
      93                 :             :  **************************************************/
      94                 :             : 
      95                 :             : static uint64
      96                 :           0 : mac_2_uint64(macaddr *m)
      97                 :             : {
      98                 :           0 :     unsigned char *mi = (unsigned char *) m;
      99                 :           0 :     uint64      res = 0;
     100                 :             :     int         i;
     101                 :             : 
     102         [ #  # ]:           0 :     for (i = 0; i < 6; i++)
     103                 :           0 :         res += (((uint64) mi[i]) << ((uint64) ((5 - i) * 8)));
     104                 :           0 :     return res;
     105                 :             : }
     106                 :             : 
     107                 :             : Datum
     108                 :         604 : gbt_macad_compress(PG_FUNCTION_ARGS)
     109                 :             : {
     110                 :         604 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     111                 :             : 
     112                 :         604 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     113                 :             : }
     114                 :             : 
     115                 :             : Datum
     116                 :           8 : gbt_macad_fetch(PG_FUNCTION_ARGS)
     117                 :             : {
     118                 :           8 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     119                 :             : 
     120                 :           8 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     121                 :             : }
     122                 :             : 
     123                 :             : Datum
     124                 :        1824 : gbt_macad_consistent(PG_FUNCTION_ARGS)
     125                 :             : {
     126                 :        1824 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     127                 :        1824 :     macaddr    *query = (macaddr *) PG_GETARG_POINTER(1);
     128                 :        1824 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     129                 :             : #ifdef NOT_USED
     130                 :             :     Oid         subtype = PG_GETARG_OID(3);
     131                 :             : #endif
     132                 :        1824 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     133                 :        1824 :     macKEY     *kkk = (macKEY *) DatumGetPointer(entry->key);
     134                 :             :     GBT_NUMKEY_R key;
     135                 :             : 
     136                 :             :     /* All cases served by this function are exact */
     137                 :        1824 :     *recheck = false;
     138                 :             : 
     139                 :        1824 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     140                 :        1824 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     141                 :             : 
     142                 :        1824 :     PG_RETURN_BOOL(gbt_num_consistent(&key, query, strategy,
     143                 :             :                                       GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
     144                 :             : }
     145                 :             : 
     146                 :             : 
     147                 :             : Datum
     148                 :           1 : gbt_macad_union(PG_FUNCTION_ARGS)
     149                 :             : {
     150                 :           1 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     151                 :           1 :     void       *out = palloc0(sizeof(macKEY));
     152                 :             : 
     153                 :           1 :     *(int *) PG_GETARG_POINTER(1) = sizeof(macKEY);
     154                 :           1 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     155                 :             : }
     156                 :             : 
     157                 :             : 
     158                 :             : Datum
     159                 :           0 : gbt_macad_penalty(PG_FUNCTION_ARGS)
     160                 :             : {
     161                 :           0 :     macKEY     *origentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     162                 :           0 :     macKEY     *newentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     163                 :           0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     164                 :             :     uint64      iorg[2],
     165                 :             :                 inew[2];
     166                 :             : 
     167                 :           0 :     iorg[0] = mac_2_uint64(&origentry->lower);
     168                 :           0 :     iorg[1] = mac_2_uint64(&origentry->upper);
     169                 :           0 :     inew[0] = mac_2_uint64(&newentry->lower);
     170                 :           0 :     inew[1] = mac_2_uint64(&newentry->upper);
     171                 :             : 
     172                 :           0 :     penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
     173                 :             : 
     174                 :           0 :     PG_RETURN_POINTER(result);
     175                 :             : }
     176                 :             : 
     177                 :             : Datum
     178                 :           3 : gbt_macad_picksplit(PG_FUNCTION_ARGS)
     179                 :             : {
     180                 :           3 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     181                 :             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     182                 :             :                                         &tinfo, fcinfo->flinfo));
     183                 :             : }
     184                 :             : 
     185                 :             : Datum
     186                 :           0 : gbt_macad_same(PG_FUNCTION_ARGS)
     187                 :             : {
     188                 :           0 :     macKEY     *b1 = (macKEY *) PG_GETARG_POINTER(0);
     189                 :           0 :     macKEY     *b2 = (macKEY *) PG_GETARG_POINTER(1);
     190                 :           0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     191                 :             : 
     192                 :           0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     193                 :           0 :     PG_RETURN_POINTER(result);
     194                 :             : }
     195                 :             : 
     196                 :             : static int
     197                 :        5789 : gbt_macaddr_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     198                 :             : {
     199                 :        5789 :     macKEY     *arg1 = (macKEY *) DatumGetPointer(x);
     200                 :        5789 :     macKEY     *arg2 = (macKEY *) DatumGetPointer(y);
     201                 :             : 
     202                 :             :     /* for leaf items we expect lower == upper, so only compare lower */
     203                 :        5789 :     return DatumGetInt32(DirectFunctionCall2(macaddr_cmp,
     204                 :             :                                              MacaddrPGetDatum(&arg1->lower),
     205                 :             :                                              MacaddrPGetDatum(&arg2->lower)));
     206                 :             : }
     207                 :             : 
     208                 :             : Datum
     209                 :           1 : gbt_macaddr_sortsupport(PG_FUNCTION_ARGS)
     210                 :             : {
     211                 :           1 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     212                 :             : 
     213                 :           1 :     ssup->comparator = gbt_macaddr_ssup_cmp;
     214                 :           1 :     ssup->ssup_extra = NULL;
     215                 :             : 
     216                 :           1 :     PG_RETURN_VOID();
     217                 :             : }
        

Generated by: LCOV version 2.0-1