LCOV - differential code coverage report
Current view: top level - contrib/btree_gist - btree_uuid.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 94.9 % 79 75 4 2 73 2
Current Date: 2023-04-08 15:15:32 Functions: 95.5 % 22 21 1 1 20
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*
       2                 :  * contrib/btree_gist/btree_uuid.c
       3                 :  */
       4                 : #include "postgres.h"
       5                 : 
       6                 : #include "btree_gist.h"
       7                 : #include "btree_utils_num.h"
       8                 : #include "port/pg_bswap.h"
       9                 : #include "utils/uuid.h"
      10                 : 
      11                 : typedef struct
      12                 : {
      13                 :     pg_uuid_t   lower,
      14                 :                 upper;
      15                 : } uuidKEY;
      16                 : 
      17                 : 
      18                 : /*
      19                 :  * UUID ops
      20                 :  */
      21 CBC           2 : PG_FUNCTION_INFO_V1(gbt_uuid_compress);
      22               2 : PG_FUNCTION_INFO_V1(gbt_uuid_fetch);
      23               2 : PG_FUNCTION_INFO_V1(gbt_uuid_union);
      24               2 : PG_FUNCTION_INFO_V1(gbt_uuid_picksplit);
      25               2 : PG_FUNCTION_INFO_V1(gbt_uuid_consistent);
      26               2 : PG_FUNCTION_INFO_V1(gbt_uuid_penalty);
      27               2 : PG_FUNCTION_INFO_V1(gbt_uuid_same);
      28                 : 
      29                 : 
      30                 : static int
      31            8803 : uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
      32                 : {
      33            8803 :     return memcmp(arg1->data, arg2->data, UUID_LEN);
      34                 : }
      35                 : 
      36                 : static bool
      37            1397 : gbt_uuidgt(const void *a, const void *b, FmgrInfo *flinfo)
      38                 : {
      39            1397 :     return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) > 0;
      40                 : }
      41                 : 
      42                 : static bool
      43             308 : gbt_uuidge(const void *a, const void *b, FmgrInfo *flinfo)
      44                 : {
      45             308 :     return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) >= 0;
      46                 : }
      47                 : 
      48                 : static bool
      49            1026 : gbt_uuideq(const void *a, const void *b, FmgrInfo *flinfo)
      50                 : {
      51            1026 :     return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) == 0;
      52                 : }
      53                 : 
      54                 : static bool
      55             454 : gbt_uuidle(const void *a, const void *b, FmgrInfo *flinfo)
      56                 : {
      57             454 :     return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) <= 0;
      58                 : }
      59                 : 
      60                 : static bool
      61            1537 : gbt_uuidlt(const void *a, const void *b, FmgrInfo *flinfo)
      62                 : {
      63            1537 :     return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) < 0;
      64                 : }
      65                 : 
      66                 : static int
      67            4081 : gbt_uuidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      68                 : {
      69            4081 :     uuidKEY    *ia = (uuidKEY *) (((const Nsrt *) a)->t);
      70            4081 :     uuidKEY    *ib = (uuidKEY *) (((const Nsrt *) b)->t);
      71                 :     int         res;
      72                 : 
      73            4081 :     res = uuid_internal_cmp(&ia->lower, &ib->lower);
      74            4081 :     if (res == 0)
      75 UBC           0 :         res = uuid_internal_cmp(&ia->upper, &ib->upper);
      76 CBC        4081 :     return res;
      77                 : }
      78                 : 
      79                 : 
      80                 : static const gbtree_ninfo tinfo =
      81                 : {
      82                 :     gbt_t_uuid,
      83                 :     UUID_LEN,
      84                 :     32,                         /* sizeof(gbtreekey32) */
      85                 :     gbt_uuidgt,
      86                 :     gbt_uuidge,
      87                 :     gbt_uuideq,
      88                 :     gbt_uuidle,
      89                 :     gbt_uuidlt,
      90                 :     gbt_uuidkey_cmp,
      91                 :     NULL
      92                 : };
      93                 : 
      94                 : 
      95                 : /**************************************************
      96                 :  * uuid ops
      97                 :  **************************************************/
      98                 : 
      99                 : 
     100                 : Datum
     101             616 : gbt_uuid_compress(PG_FUNCTION_ARGS)
     102                 : {
     103             616 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     104                 :     GISTENTRY  *retval;
     105                 : 
     106             616 :     if (entry->leafkey)
     107                 :     {
     108             603 :         char       *r = (char *) palloc(2 * UUID_LEN);
     109             603 :         pg_uuid_t  *key = DatumGetUUIDP(entry->key);
     110                 : 
     111             603 :         retval = palloc(sizeof(GISTENTRY));
     112                 : 
     113 GNC         603 :         memcpy(r, key, UUID_LEN);
     114             603 :         memcpy(r + UUID_LEN, key, UUID_LEN);
     115 CBC         603 :         gistentryinit(*retval, PointerGetDatum(r),
     116                 :                       entry->rel, entry->page,
     117                 :                       entry->offset, false);
     118                 :     }
     119                 :     else
     120              13 :         retval = entry;
     121                 : 
     122             616 :     PG_RETURN_POINTER(retval);
     123                 : }
     124                 : 
     125                 : Datum
     126 UBC           0 : gbt_uuid_fetch(PG_FUNCTION_ARGS)
     127                 : {
     128               0 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     129                 : 
     130               0 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     131                 : }
     132                 : 
     133                 : Datum
     134 CBC        1637 : gbt_uuid_consistent(PG_FUNCTION_ARGS)
     135                 : {
     136            1637 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     137            1637 :     pg_uuid_t  *query = PG_GETARG_UUID_P(1);
     138            1637 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     139                 : 
     140                 :     /* Oid      subtype = PG_GETARG_OID(3); */
     141            1637 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     142            1637 :     uuidKEY    *kkk = (uuidKEY *) DatumGetPointer(entry->key);
     143                 :     GBT_NUMKEY_R key;
     144                 : 
     145                 :     /* All cases served by this function are exact */
     146            1637 :     *recheck = false;
     147                 : 
     148            1637 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     149            1637 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     150                 : 
     151            1637 :     PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) query, &strategy,
     152                 :                                       GIST_LEAF(entry), &tinfo,
     153                 :                                       fcinfo->flinfo));
     154                 : }
     155                 : 
     156                 : Datum
     157             447 : gbt_uuid_union(PG_FUNCTION_ARGS)
     158                 : {
     159             447 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     160             447 :     void       *out = palloc(sizeof(uuidKEY));
     161                 : 
     162             447 :     *(int *) PG_GETARG_POINTER(1) = sizeof(uuidKEY);
     163             447 :     PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
     164                 : }
     165                 : 
     166                 : /*
     167                 :  * Convert a uuid to a "double" value for estimating sizes of ranges.
     168                 :  */
     169                 : static double
     170            4668 : uuid_2_double(const pg_uuid_t *u)
     171                 : {
     172                 :     uint64      uu[2];
     173            4668 :     const double two64 = 18446744073709551616.0;    /* 2^64 */
     174                 : 
     175                 :     /* Source data may not be suitably aligned, so copy */
     176            4668 :     memcpy(uu, u->data, UUID_LEN);
     177                 : 
     178                 :     /*
     179                 :      * uuid values should be considered as big-endian numbers, since that
     180                 :      * corresponds to how memcmp will compare them.  On a little-endian
     181                 :      * machine, byte-swap each half so we can use native uint64 arithmetic.
     182                 :      */
     183                 : #ifndef WORDS_BIGENDIAN
     184            4668 :     uu[0] = pg_bswap64(uu[0]);
     185            4668 :     uu[1] = pg_bswap64(uu[1]);
     186                 : #endif
     187                 : 
     188                 :     /*
     189                 :      * 2^128 is about 3.4e38, which in theory could exceed the range of
     190                 :      * "double" (POSIX only requires 1e37).  To avoid any risk of overflow,
     191                 :      * put the decimal point between the two halves rather than treating the
     192                 :      * uuid value as a 128-bit integer.
     193                 :      */
     194            4668 :     return (double) uu[0] + (double) uu[1] / two64;
     195                 : }
     196                 : 
     197                 : Datum
     198            1167 : gbt_uuid_penalty(PG_FUNCTION_ARGS)
     199                 : {
     200            1167 :     uuidKEY    *origentry = (uuidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     201            1167 :     uuidKEY    *newentry = (uuidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     202            1167 :     float      *result = (float *) PG_GETARG_POINTER(2);
     203                 :     double      olower,
     204                 :                 oupper,
     205                 :                 nlower,
     206                 :                 nupper;
     207                 : 
     208            1167 :     olower = uuid_2_double(&origentry->lower);
     209            1167 :     oupper = uuid_2_double(&origentry->upper);
     210            1167 :     nlower = uuid_2_double(&newentry->lower);
     211            1167 :     nupper = uuid_2_double(&newentry->upper);
     212                 : 
     213            1167 :     penalty_num(result, olower, oupper, nlower, nupper);
     214                 : 
     215            1167 :     PG_RETURN_POINTER(result);
     216                 : }
     217                 : 
     218                 : Datum
     219               3 : gbt_uuid_picksplit(PG_FUNCTION_ARGS)
     220                 : {
     221               3 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     222                 :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     223                 :                                         &tinfo, fcinfo->flinfo));
     224                 : }
     225                 : 
     226                 : Datum
     227             446 : gbt_uuid_same(PG_FUNCTION_ARGS)
     228                 : {
     229             446 :     uuidKEY    *b1 = (uuidKEY *) PG_GETARG_POINTER(0);
     230             446 :     uuidKEY    *b2 = (uuidKEY *) PG_GETARG_POINTER(1);
     231             446 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     232                 : 
     233             446 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     234             446 :     PG_RETURN_POINTER(result);
     235                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a