LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_uuid.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 63 89 70.8 %
Date: 2025-04-24 12:15:10 Functions: 21 25 84.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14