LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_macaddr.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 57 79 72.2 %
Date: 2025-08-31 01:17:28 Functions: 21 24 87.5 %
Legend: Lines: hit not hit

          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           8 : PG_FUNCTION_INFO_V1(gbt_macad_compress);
      22           8 : PG_FUNCTION_INFO_V1(gbt_macad_fetch);
      23           8 : PG_FUNCTION_INFO_V1(gbt_macad_union);
      24           8 : PG_FUNCTION_INFO_V1(gbt_macad_picksplit);
      25           8 : PG_FUNCTION_INFO_V1(gbt_macad_consistent);
      26           8 : PG_FUNCTION_INFO_V1(gbt_macad_penalty);
      27           8 : PG_FUNCTION_INFO_V1(gbt_macad_same);
      28           8 : PG_FUNCTION_INFO_V1(gbt_macaddr_sortsupport);
      29             : 
      30             : 
      31             : static bool
      32        4186 : gbt_macadgt(const void *a, const void *b, FmgrInfo *flinfo)
      33             : {
      34        4186 :     return DatumGetBool(DirectFunctionCall2(macaddr_gt, PointerGetDatum(a), PointerGetDatum(b)));
      35             : }
      36             : static bool
      37         324 : gbt_macadge(const void *a, const void *b, FmgrInfo *flinfo)
      38             : {
      39         324 :     return DatumGetBool(DirectFunctionCall2(macaddr_ge, PointerGetDatum(a), PointerGetDatum(b)));
      40             : }
      41             : 
      42             : static bool
      43         300 : gbt_macadeq(const void *a, const void *b, FmgrInfo *flinfo)
      44             : {
      45         300 :     return DatumGetBool(DirectFunctionCall2(macaddr_eq, PointerGetDatum(a), PointerGetDatum(b)));
      46             : }
      47             : 
      48             : static bool
      49        1226 : gbt_macadle(const void *a, const void *b, FmgrInfo *flinfo)
      50             : {
      51        1226 :     return DatumGetBool(DirectFunctionCall2(macaddr_le, PointerGetDatum(a), PointerGetDatum(b)));
      52             : }
      53             : 
      54             : static bool
      55        4786 : gbt_macadlt(const void *a, const void *b, FmgrInfo *flinfo)
      56             : {
      57        4786 :     return DatumGetBool(DirectFunctionCall2(macaddr_lt, PointerGetDatum(a), PointerGetDatum(b)));
      58             : }
      59             : 
      60             : 
      61             : static int
      62        2394 : gbt_macadkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      63             : {
      64        2394 :     macKEY     *ia = (macKEY *) (((const Nsrt *) a)->t);
      65        2394 :     macKEY     *ib = (macKEY *) (((const Nsrt *) b)->t);
      66             :     int         res;
      67             : 
      68        2394 :     res = DatumGetInt32(DirectFunctionCall2(macaddr_cmp, MacaddrPGetDatum(&ia->lower), MacaddrPGetDatum(&ib->lower)));
      69        2394 :     if (res == 0)
      70        1800 :         return DatumGetInt32(DirectFunctionCall2(macaddr_cmp, MacaddrPGetDatum(&ia->upper), MacaddrPGetDatum(&ib->upper)));
      71             : 
      72         594 :     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        1208 : gbt_macad_compress(PG_FUNCTION_ARGS)
     109             : {
     110        1208 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     111             : 
     112        1208 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     113             : }
     114             : 
     115             : Datum
     116          16 : gbt_macad_fetch(PG_FUNCTION_ARGS)
     117             : {
     118          16 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     119             : 
     120          16 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     121             : }
     122             : 
     123             : Datum
     124        3648 : gbt_macad_consistent(PG_FUNCTION_ARGS)
     125             : {
     126        3648 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     127        3648 :     macaddr    *query = (macaddr *) PG_GETARG_POINTER(1);
     128        3648 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     129             : 
     130             :     /* Oid      subtype = PG_GETARG_OID(3); */
     131        3648 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     132        3648 :     macKEY     *kkk = (macKEY *) DatumGetPointer(entry->key);
     133             :     GBT_NUMKEY_R key;
     134             : 
     135             :     /* All cases served by this function are exact */
     136        3648 :     *recheck = false;
     137             : 
     138        3648 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     139        3648 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     140             : 
     141        3648 :     PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
     142             :                                       GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
     143             : }
     144             : 
     145             : 
     146             : Datum
     147           2 : gbt_macad_union(PG_FUNCTION_ARGS)
     148             : {
     149           2 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     150           2 :     void       *out = palloc0(sizeof(macKEY));
     151             : 
     152           2 :     *(int *) PG_GETARG_POINTER(1) = sizeof(macKEY);
     153           2 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     154             : }
     155             : 
     156             : 
     157             : Datum
     158           0 : gbt_macad_penalty(PG_FUNCTION_ARGS)
     159             : {
     160           0 :     macKEY     *origentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     161           0 :     macKEY     *newentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     162           0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     163             :     uint64      iorg[2],
     164             :                 inew[2];
     165             : 
     166           0 :     iorg[0] = mac_2_uint64(&origentry->lower);
     167           0 :     iorg[1] = mac_2_uint64(&origentry->upper);
     168           0 :     inew[0] = mac_2_uint64(&newentry->lower);
     169           0 :     inew[1] = mac_2_uint64(&newentry->upper);
     170             : 
     171           0 :     penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
     172             : 
     173           0 :     PG_RETURN_POINTER(result);
     174             : }
     175             : 
     176             : Datum
     177           6 : gbt_macad_picksplit(PG_FUNCTION_ARGS)
     178             : {
     179           6 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     180             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     181             :                                         &tinfo, fcinfo->flinfo));
     182             : }
     183             : 
     184             : Datum
     185           0 : gbt_macad_same(PG_FUNCTION_ARGS)
     186             : {
     187           0 :     macKEY     *b1 = (macKEY *) PG_GETARG_POINTER(0);
     188           0 :     macKEY     *b2 = (macKEY *) PG_GETARG_POINTER(1);
     189           0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     190             : 
     191           0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     192           0 :     PG_RETURN_POINTER(result);
     193             : }
     194             : 
     195             : static int
     196       11578 : gbt_macaddr_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     197             : {
     198       11578 :     macKEY     *arg1 = (macKEY *) DatumGetPointer(x);
     199       11578 :     macKEY     *arg2 = (macKEY *) DatumGetPointer(y);
     200             : 
     201             :     /* for leaf items we expect lower == upper, so only compare lower */
     202       11578 :     return DatumGetInt32(DirectFunctionCall2(macaddr_cmp,
     203             :                                              MacaddrPGetDatum(&arg1->lower),
     204             :                                              MacaddrPGetDatum(&arg2->lower)));
     205             : }
     206             : 
     207             : Datum
     208           2 : gbt_macaddr_sortsupport(PG_FUNCTION_ARGS)
     209             : {
     210           2 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     211             : 
     212           2 :     ssup->comparator = gbt_macaddr_ssup_cmp;
     213           2 :     ssup->ssup_extra = NULL;
     214             : 
     215           2 :     PG_RETURN_VOID();
     216             : }

Generated by: LCOV version 1.16