LCOV - differential code coverage report
Current view: top level - contrib/btree_gist - btree_utils_var.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 83.8 % 278 233 45 233
Current Date: 2025-09-06 07:49:51 +0900 Functions: 95.0 % 20 19 1 19
Baseline: lcov-20250906-005545-baseline Branches: 69.2 % 133 92 41 92
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(360..) days: 83.8 % 278 233 45 233
Function coverage date bins:
(360..) days: 95.0 % 20 19 1 19
Branch coverage date bins:
(360..) days: 69.2 % 133 92 41 92

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * contrib/btree_gist/btree_utils_var.c
                                  3                 :                :  */
                                  4                 :                : #include "postgres.h"
                                  5                 :                : 
                                  6                 :                : #include <math.h>
                                  7                 :                : #include <limits.h>
                                  8                 :                : #include <float.h>
                                  9                 :                : 
                                 10                 :                : #include "btree_gist.h"
                                 11                 :                : #include "btree_utils_var.h"
                                 12                 :                : #include "mb/pg_wchar.h"
                                 13                 :                : #include "utils/rel.h"
                                 14                 :                : #include "varatt.h"
                                 15                 :                : 
                                 16                 :                : /* used for key sorting */
                                 17                 :                : typedef struct
                                 18                 :                : {
                                 19                 :                :     int         i;
                                 20                 :                :     GBT_VARKEY *t;
                                 21                 :                : } Vsrt;
                                 22                 :                : 
                                 23                 :                : typedef struct
                                 24                 :                : {
                                 25                 :                :     const gbtree_vinfo *tinfo;
                                 26                 :                :     Oid         collation;
                                 27                 :                :     FmgrInfo   *flinfo;
                                 28                 :                : } gbt_vsrt_arg;
                                 29                 :                : 
                                 30                 :                : 
 7494 teodor@sigaev.ru           31                 :CBC          11 : PG_FUNCTION_INFO_V1(gbt_var_decompress);
 3816 heikki.linnakangas@i       32                 :             11 : PG_FUNCTION_INFO_V1(gbt_var_fetch);
                                 33                 :                : 
                                 34                 :                : 
                                 35                 :                : Datum
 7494 teodor@sigaev.ru           36                 :          64490 : gbt_var_decompress(PG_FUNCTION_ARGS)
                                 37                 :                : {
 7413 neilc@samurai.com          38                 :          64490 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 2910 tgl@sss.pgh.pa.us          39                 :          64490 :     GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
                                 40                 :                : 
 7413 neilc@samurai.com          41         [ +  + ]:          64490 :     if (key != (GBT_VARKEY *) DatumGetPointer(entry->key))
                                 42                 :                :     {
                                 43                 :          64460 :         GISTENTRY  *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
                                 44                 :                : 
                                 45                 :          64460 :         gistentryinit(*retval, PointerGetDatum(key),
                                 46                 :                :                       entry->rel, entry->page,
                                 47                 :                :                       entry->offset, false);
                                 48                 :                : 
                                 49                 :          64460 :         PG_RETURN_POINTER(retval);
                                 50                 :                :     }
                                 51                 :                : 
                                 52                 :             30 :     PG_RETURN_POINTER(entry);
                                 53                 :                : }
                                 54                 :                : 
                                 55                 :                : /* Returns a better readable representation of variable key ( sets pointer ) */
                                 56                 :                : GBT_VARKEY_R
 5931 bruce@momjian.us           57                 :         306578 : gbt_var_key_readable(const GBT_VARKEY *k)
                                 58                 :                : {
                                 59                 :                :     GBT_VARKEY_R r;
                                 60                 :                : 
 7678                            61                 :         306578 :     r.lower = (bytea *) &(((char *) k)[VARHDRSZ]);
                                 62         [ +  + ]:         306578 :     if (VARSIZE(k) > (VARHDRSZ + (VARSIZE(r.lower))))
                                 63                 :          66123 :         r.upper = (bytea *) &(((char *) k)[VARHDRSZ + INTALIGN(VARSIZE(r.lower))]);
                                 64                 :                :     else
                                 65                 :         240455 :         r.upper = r.lower;
                                 66                 :         306578 :     return r;
                                 67                 :                : }
                                 68                 :                : 
                                 69                 :                : 
                                 70                 :                : /*
                                 71                 :                :  * Create a leaf-entry to store in the index, from a single Datum.
                                 72                 :                :  */
                                 73                 :                : static GBT_VARKEY *
 2999 tgl@sss.pgh.pa.us          74                 :           8262 : gbt_var_key_from_datum(const struct varlena *u)
                                 75                 :                : {
 3817 heikki.linnakangas@i       76                 :           8262 :     int32       lowersize = VARSIZE(u);
                                 77                 :                :     GBT_VARKEY *r;
                                 78                 :                : 
                                 79                 :           8262 :     r = (GBT_VARKEY *) palloc(lowersize + VARHDRSZ);
                                 80                 :           8262 :     memcpy(VARDATA(r), u, lowersize);
                                 81                 :           8262 :     SET_VARSIZE(r, lowersize + VARHDRSZ);
                                 82                 :                : 
                                 83                 :           8262 :     return r;
                                 84                 :                : }
                                 85                 :                : 
                                 86                 :                : /*
                                 87                 :                :  * Create an entry to store in the index, from lower and upper bound.
                                 88                 :                :  */
                                 89                 :                : GBT_VARKEY *
                                 90                 :          21779 : gbt_var_key_copy(const GBT_VARKEY_R *u)
                                 91                 :                : {
 4131 tgl@sss.pgh.pa.us          92                 :          21779 :     int32       lowersize = VARSIZE(u->lower);
                                 93                 :          21779 :     int32       uppersize = VARSIZE(u->upper);
                                 94                 :                :     GBT_VARKEY *r;
                                 95                 :                : 
 3817 heikki.linnakangas@i       96                 :          21779 :     r = (GBT_VARKEY *) palloc0(INTALIGN(lowersize) + uppersize + VARHDRSZ);
                                 97                 :          21779 :     memcpy(VARDATA(r), u->lower, lowersize);
                                 98                 :          21779 :     memcpy(VARDATA(r) + INTALIGN(lowersize), u->upper, uppersize);
                                 99                 :          21779 :     SET_VARSIZE(r, INTALIGN(lowersize) + uppersize + VARHDRSZ);
                                100                 :                : 
 7678 bruce@momjian.us          101                 :          21779 :     return r;
                                102                 :                : }
                                103                 :                : 
                                104                 :                : 
                                105                 :                : static GBT_VARKEY *
 3091 andrew@dunslane.net       106                 :          47013 : gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
                                107                 :                : {
 7678 bruce@momjian.us          108                 :          47013 :     GBT_VARKEY *out = leaf;
                                109                 :                : 
                                110         [ +  + ]:          47013 :     if (tinfo->f_l2n)
 2921 peter_e@gmx.net           111                 :           3598 :         out = tinfo->f_l2n(leaf, flinfo);
                                112                 :                : 
 7678 bruce@momjian.us          113                 :          47013 :     return out;
                                114                 :                : }
                                115                 :                : 
                                116                 :                : 
                                117                 :                : /*
                                118                 :                :  * returns the common prefix length of a node key
                                119                 :                : */
                                120                 :                : static int32
 5931                           121                 :             25 : gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
                                122                 :                : {
 7266                           123                 :             25 :     GBT_VARKEY_R r = gbt_var_key_readable(node);
                                124                 :             25 :     int32       i = 0;
                                125                 :             25 :     int32       l = 0;
                                126                 :             25 :     int32       t1len = VARSIZE(r.lower) - VARHDRSZ;
                                127                 :             25 :     int32       t2len = VARSIZE(r.upper) - VARHDRSZ;
                                128                 :             25 :     int32       ml = Min(t1len, t2len);
                                129                 :             25 :     char       *p1 = VARDATA(r.lower);
                                130                 :             25 :     char       *p2 = VARDATA(r.upper);
                                131                 :                : 
                                132         [ +  + ]:             25 :     if (ml == 0)
                                133                 :              4 :         return 0;
                                134                 :                : 
                                135         [ +  - ]:             21 :     while (i < ml)
                                136                 :                :     {
                                137   [ -  +  -  - ]:             21 :         if (tinfo->eml > 1 && l == 0)
                                138                 :                :         {
 7266 bruce@momjian.us          139         [ #  # ]:UBC           0 :             if ((l = pg_mblen(p1)) != pg_mblen(p2))
                                140                 :                :             {
                                141                 :              0 :                 return i;
                                142                 :                :             }
                                143                 :                :         }
 7266 bruce@momjian.us          144         [ +  - ]:CBC          21 :         if (*p1 != *p2)
                                145                 :                :         {
                                146         [ -  + ]:             21 :             if (tinfo->eml > 1)
                                147                 :                :             {
 7266 bruce@momjian.us          148                 :UBC           0 :                 return (i - l + 1);
                                149                 :                :             }
                                150                 :                :             else
                                151                 :                :             {
 7266 bruce@momjian.us          152                 :CBC          21 :                 return i;
                                153                 :                :             }
                                154                 :                :         }
                                155                 :                : 
 7266 bruce@momjian.us          156                 :UBC           0 :         p1++;
                                157                 :              0 :         p2++;
                                158                 :              0 :         l--;
                                159                 :              0 :         i++;
                                160                 :                :     }
 2910 tgl@sss.pgh.pa.us         161                 :              0 :     return ml;                  /* lower == upper */
                                162                 :                : }
                                163                 :                : 
                                164                 :                : 
                                165                 :                : /*
                                166                 :                :  * returns true, if query matches prefix ( common prefix )
                                167                 :                :  */
                                168                 :                : static bool
 5931 bruce@momjian.us          169                 :CBC          78 : gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)
                                170                 :                : {
 2943 peter_e@gmx.net           171                 :             78 :     bool        out = false;
 7266 bruce@momjian.us          172                 :             78 :     int32       qlen = VARSIZE(query) - VARHDRSZ;
                                173                 :             78 :     int32       nlen = VARSIZE(pf) - VARHDRSZ;
                                174                 :                : 
 7678                           175         [ +  - ]:             78 :     if (nlen <= qlen)
                                176                 :                :     {
 7266                           177                 :             78 :         char       *q = VARDATA(query);
                                178                 :             78 :         char       *n = VARDATA(pf);
                                179                 :                : 
 5251 tgl@sss.pgh.pa.us         180                 :             78 :         out = (memcmp(q, n, nlen) == 0);
                                181                 :                :     }
                                182                 :                : 
 7678 bruce@momjian.us          183                 :             78 :     return out;
                                184                 :                : }
                                185                 :                : 
                                186                 :                : 
                                187                 :                : /*
                                188                 :                :  * returns true, if query matches node using common prefix
                                189                 :                :  */
                                190                 :                : static bool
 5931                           191                 :            172 : gbt_var_node_pf_match(const GBT_VARKEY_R *node, const bytea *query, const gbtree_vinfo *tinfo)
                                192                 :                : {
 2046 alvherre@alvh.no-ip.      193   [ +  +  +  + ]:            250 :     return (tinfo->trnc &&
                                194         [ +  + ]:             78 :             (gbt_bytea_pf_match(node->lower, query, tinfo) ||
                                195                 :             34 :              gbt_bytea_pf_match(node->upper, query, tinfo)));
                                196                 :                : }
                                197                 :                : 
                                198                 :                : 
                                199                 :                : /*
                                200                 :                : *  truncates / compresses the node key
                                201                 :                : *  cpf_length .. common prefix length
                                202                 :                : */
                                203                 :                : static GBT_VARKEY *
 5931 bruce@momjian.us          204                 :             25 : gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
                                205                 :                : {
 7678                           206                 :             25 :     GBT_VARKEY *out = NULL;
                                207                 :             25 :     GBT_VARKEY_R r = gbt_var_key_readable(node);
 7266                           208                 :             25 :     int32       len1 = VARSIZE(r.lower) - VARHDRSZ;
                                209                 :             25 :     int32       len2 = VARSIZE(r.upper) - VARHDRSZ;
                                210                 :                :     int32       si;
                                211                 :                :     char       *out2;
                                212                 :                : 
                                213                 :             25 :     len1 = Min(len1, (cpf_length + 1));
                                214                 :             25 :     len2 = Min(len2, (cpf_length + 1));
                                215                 :                : 
 6766 tgl@sss.pgh.pa.us         216                 :             25 :     si = 2 * VARHDRSZ + INTALIGN(len1 + VARHDRSZ) + len2;
 4131                           217                 :             25 :     out = (GBT_VARKEY *) palloc0(si);
 6766                           218                 :             25 :     SET_VARSIZE(out, si);
                                219                 :                : 
                                220                 :             25 :     memcpy(VARDATA(out), r.lower, len1 + VARHDRSZ);
                                221                 :             25 :     SET_VARSIZE(VARDATA(out), len1 + VARHDRSZ);
                                222                 :                : 
                                223                 :             25 :     out2 = VARDATA(out) + INTALIGN(len1 + VARHDRSZ);
                                224                 :             25 :     memcpy(out2, r.upper, len2 + VARHDRSZ);
                                225                 :             25 :     SET_VARSIZE(out2, len2 + VARHDRSZ);
                                226                 :                : 
 7678 bruce@momjian.us          227                 :             25 :     return out;
                                228                 :                : }
                                229                 :                : 
                                230                 :                : 
                                231                 :                : 
                                232                 :                : void
 5251 tgl@sss.pgh.pa.us         233                 :          31398 : gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation,
                                234                 :                :                   const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
                                235                 :                : {
 7678 bruce@momjian.us          236                 :          31398 :     GBT_VARKEY_R eo = gbt_var_key_readable(e);
                                237                 :                :     GBT_VARKEY_R nr;
                                238                 :                : 
                                239         [ +  + ]:          31398 :     if (eo.lower == eo.upper)   /* leaf */
                                240                 :                :     {
                                241                 :                :         GBT_VARKEY *tmp;
                                242                 :                : 
 3091 andrew@dunslane.net       243                 :          28992 :         tmp = gbt_var_leaf2node(e, tinfo, flinfo);
 7678 bruce@momjian.us          244         [ +  + ]:          28992 :         if (tmp != e)
                                245                 :           1198 :             eo = gbt_var_key_readable(tmp);
                                246                 :                :     }
                                247                 :                : 
                                248         [ +  + ]:          31398 :     if (DatumGetPointer(*u))
                                249                 :                :     {
                                250                 :          31296 :         GBT_VARKEY_R ro = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(*u));
 4594 tgl@sss.pgh.pa.us         251                 :          31296 :         bool        update = false;
                                252                 :                : 
                                253                 :          31296 :         nr.lower = ro.lower;
                                254                 :          31296 :         nr.upper = ro.upper;
                                255                 :                : 
 2921 peter_e@gmx.net           256         [ +  + ]:          31296 :         if (tinfo->f_cmp(ro.lower, eo.lower, collation, flinfo) > 0)
                                257                 :                :         {
 7678 bruce@momjian.us          258                 :             16 :             nr.lower = eo.lower;
 4594 tgl@sss.pgh.pa.us         259                 :             16 :             update = true;
                                260                 :                :         }
                                261                 :                : 
 2921 peter_e@gmx.net           262         [ +  + ]:          31296 :         if (tinfo->f_cmp(ro.upper, eo.upper, collation, flinfo) < 0)
                                263                 :                :         {
 7678 bruce@momjian.us          264                 :          12683 :             nr.upper = eo.upper;
 4594 tgl@sss.pgh.pa.us         265                 :          12683 :             update = true;
                                266                 :                :         }
                                267                 :                : 
                                268         [ +  + ]:          31296 :         if (update)
 3817 heikki.linnakangas@i      269                 :          12699 :             *u = PointerGetDatum(gbt_var_key_copy(&nr));
                                270                 :                :     }
                                271                 :                :     else
                                272                 :                :     {
 7678 bruce@momjian.us          273                 :            102 :         nr.lower = eo.lower;
                                274                 :            102 :         nr.upper = eo.upper;
 3817 heikki.linnakangas@i      275                 :            102 :         *u = PointerGetDatum(gbt_var_key_copy(&nr));
                                276                 :                :     }
 7771 teodor@sigaev.ru          277                 :          31398 : }
                                278                 :                : 
                                279                 :                : 
                                280                 :                : GISTENTRY *
 5931 bruce@momjian.us          281                 :           8343 : gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
                                282                 :                : {
                                283                 :                :     GISTENTRY  *retval;
                                284                 :                : 
 7678                           285         [ +  + ]:           8343 :     if (entry->leafkey)
                                286                 :                :     {
 3817 heikki.linnakangas@i      287                 :           8262 :         struct varlena *leaf = PG_DETOAST_DATUM(entry->key);
                                288                 :                :         GBT_VARKEY *r;
                                289                 :                : 
                                290                 :           8262 :         r = gbt_var_key_from_datum(leaf);
                                291                 :                : 
 7678 bruce@momjian.us          292                 :           8262 :         retval = palloc(sizeof(GISTENTRY));
                                293                 :           8262 :         gistentryinit(*retval, PointerGetDatum(r),
                                294                 :                :                       entry->rel, entry->page,
                                295                 :                :                       entry->offset, true);
                                296                 :                :     }
                                297                 :                :     else
                                298                 :             81 :         retval = entry;
                                299                 :                : 
 2942 peter_e@gmx.net           300                 :           8343 :     return retval;
                                301                 :                : }
                                302                 :                : 
                                303                 :                : 
                                304                 :                : Datum
 3816 heikki.linnakangas@i      305                 :              6 : gbt_var_fetch(PG_FUNCTION_ARGS)
                                306                 :                : {
                                307                 :              6 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 2910 tgl@sss.pgh.pa.us         308                 :              6 :     GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
 3816 heikki.linnakangas@i      309                 :              6 :     GBT_VARKEY_R r = gbt_var_key_readable(key);
                                310                 :                :     GISTENTRY  *retval;
                                311                 :                : 
                                312                 :              6 :     retval = palloc(sizeof(GISTENTRY));
                                313                 :              6 :     gistentryinit(*retval, PointerGetDatum(r.lower),
                                314                 :                :                   entry->rel, entry->page,
                                315                 :                :                   entry->offset, true);
                                316                 :                : 
                                317                 :              6 :     PG_RETURN_POINTER(retval);
                                318                 :                : }
                                319                 :                : 
                                320                 :                : 
                                321                 :                : GBT_VARKEY *
 5251 tgl@sss.pgh.pa.us         322                 :           1865 : gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation,
                                323                 :                :               const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
                                324                 :                : {
 7678 bruce@momjian.us          325                 :           1865 :     int         i = 0,
                                326                 :           1865 :                 numranges = entryvec->n;
                                327                 :                :     GBT_VARKEY *cur;
                                328                 :                :     Datum       out;
                                329                 :                :     GBT_VARKEY_R rk;
                                330                 :                : 
                                331                 :           1865 :     *size = sizeof(GBT_VARKEY);
                                332                 :                : 
 7494 teodor@sigaev.ru          333                 :           1865 :     cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[0].key);
 7678 bruce@momjian.us          334                 :           1865 :     rk = gbt_var_key_readable(cur);
 3817 heikki.linnakangas@i      335                 :           1865 :     out = PointerGetDatum(gbt_var_key_copy(&rk));
                                336                 :                : 
 7678 bruce@momjian.us          337         [ +  + ]:          11727 :     for (i = 1; i < numranges; i++)
                                338                 :                :     {
 7494 teodor@sigaev.ru          339                 :           9862 :         cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[i].key);
 3091 andrew@dunslane.net       340                 :           9862 :         gbt_var_bin_union(&out, cur, collation, tinfo, flinfo);
                                341                 :                :     }
                                342                 :                : 
                                343                 :                : 
                                344                 :                :     /* Truncate (=compress) key */
 7678 bruce@momjian.us          345         [ +  + ]:           1865 :     if (tinfo->trnc)
                                346                 :                :     {
                                347                 :                :         int32       plen;
                                348                 :              3 :         GBT_VARKEY *trc = NULL;
                                349                 :                : 
                                350                 :              3 :         plen = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(out), tinfo);
                                351                 :              3 :         trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo);
                                352                 :                : 
                                353                 :              3 :         out = PointerGetDatum(trc);
                                354                 :                :     }
                                355                 :                : 
                                356                 :           1865 :     return ((GBT_VARKEY *) DatumGetPointer(out));
                                357                 :                : }
                                358                 :                : 
                                359                 :                : 
                                360                 :                : bool
 5251 tgl@sss.pgh.pa.us         361                 :           1814 : gbt_var_same(Datum d1, Datum d2, Oid collation,
                                362                 :                :              const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
                                363                 :                : {
 7494 teodor@sigaev.ru          364                 :           1814 :     GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1);
                                365                 :           1814 :     GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2);
                                366                 :                :     GBT_VARKEY_R r1,
                                367                 :                :                 r2;
                                368                 :                : 
 7678 bruce@momjian.us          369                 :           1814 :     r1 = gbt_var_key_readable(t1);
                                370                 :           1814 :     r2 = gbt_var_key_readable(t2);
                                371                 :                : 
 2921 peter_e@gmx.net           372   [ +  -  +  - ]:           3628 :     return (tinfo->f_cmp(r1.lower, r2.lower, collation, flinfo) == 0 &&
                                373                 :           1814 :             tinfo->f_cmp(r1.upper, r2.upper, collation, flinfo) == 0);
                                374                 :                : }
                                375                 :                : 
                                376                 :                : 
                                377                 :                : float *
 5251 tgl@sss.pgh.pa.us         378                 :UBC           0 : gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n,
                                379                 :                :                 Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
                                380                 :                : {
 7494 teodor@sigaev.ru          381                 :              0 :     GBT_VARKEY *orge = (GBT_VARKEY *) DatumGetPointer(o->key);
                                382                 :              0 :     GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
                                383                 :                :     GBT_VARKEY_R ok,
                                384                 :                :                 nk;
                                385                 :                : 
 7678 bruce@momjian.us          386                 :              0 :     *res = 0.0;
                                387                 :                : 
                                388                 :              0 :     nk = gbt_var_key_readable(newe);
                                389         [ #  # ]:              0 :     if (nk.lower == nk.upper)   /* leaf */
                                390                 :                :     {
                                391                 :                :         GBT_VARKEY *tmp;
                                392                 :                : 
 3091 andrew@dunslane.net       393                 :              0 :         tmp = gbt_var_leaf2node(newe, tinfo, flinfo);
 7678 bruce@momjian.us          394         [ #  # ]:              0 :         if (tmp != newe)
                                395                 :              0 :             nk = gbt_var_key_readable(tmp);
                                396                 :                :     }
                                397                 :              0 :     ok = gbt_var_key_readable(orge);
                                398                 :                : 
 7372 teodor@sigaev.ru          399   [ #  #  #  # ]:              0 :     if ((VARSIZE(ok.lower) - VARHDRSZ) == 0 && (VARSIZE(ok.upper) - VARHDRSZ) == 0)
 7678 bruce@momjian.us          400                 :              0 :         *res = 0.0;
 2921 peter_e@gmx.net           401   [ #  #  #  #  :              0 :     else if (!((tinfo->f_cmp(nk.lower, ok.lower, collation, flinfo) >= 0 ||
                                              #  # ]
 5251 tgl@sss.pgh.pa.us         402                 :              0 :                 gbt_bytea_pf_match(ok.lower, nk.lower, tinfo)) &&
 2921 peter_e@gmx.net           403         [ #  # ]:              0 :                (tinfo->f_cmp(nk.upper, ok.upper, collation, flinfo) <= 0 ||
 2999 tgl@sss.pgh.pa.us         404                 :              0 :                 gbt_bytea_pf_match(ok.upper, nk.upper, tinfo))))
                                405                 :                :     {
 7678 bruce@momjian.us          406                 :              0 :         Datum       d = PointerGetDatum(0);
                                407                 :                :         double      dres;
                                408                 :                :         int32       ol,
                                409                 :                :                     ul;
                                410                 :                : 
 3091 andrew@dunslane.net       411                 :              0 :         gbt_var_bin_union(&d, orge, collation, tinfo, flinfo);
 7678 bruce@momjian.us          412                 :              0 :         ol = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
 3091 andrew@dunslane.net       413                 :              0 :         gbt_var_bin_union(&d, newe, collation, tinfo, flinfo);
 7678 bruce@momjian.us          414                 :              0 :         ul = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
                                415                 :                : 
                                416         [ #  # ]:              0 :         if (ul < ol)
                                417                 :                :         {
 4594 tgl@sss.pgh.pa.us         418                 :              0 :             dres = (ol - ul);   /* reduction of common prefix len */
                                419                 :                :         }
                                420                 :                :         else
                                421                 :                :         {
 7678 bruce@momjian.us          422                 :              0 :             GBT_VARKEY_R uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(d));
                                423                 :                :             unsigned char tmp[4];
                                424                 :                : 
 4594 tgl@sss.pgh.pa.us         425         [ #  # ]:              0 :             tmp[0] = (unsigned char) (((VARSIZE(ok.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.lower)[ul]));
                                426         [ #  # ]:              0 :             tmp[1] = (unsigned char) (((VARSIZE(uk.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.lower)[ul]));
                                427         [ #  # ]:              0 :             tmp[2] = (unsigned char) (((VARSIZE(ok.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.upper)[ul]));
                                428         [ #  # ]:              0 :             tmp[3] = (unsigned char) (((VARSIZE(uk.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.upper)[ul]));
 1065 peter@eisentraut.org      429                 :              0 :             dres = abs(tmp[0] - tmp[1]) + abs(tmp[3] - tmp[2]);
 7678 bruce@momjian.us          430                 :              0 :             dres /= 256.0;
                                431                 :                :         }
                                432                 :                : 
                                433                 :              0 :         *res += FLT_MIN;
                                434                 :              0 :         *res += (float) (dres / ((double) (ol + 1)));
                                435                 :              0 :         *res *= (FLT_MAX / (o->rel->rd_att->natts + 1));
                                436                 :                :     }
                                437                 :                : 
                                438                 :              0 :     return res;
                                439                 :                : }
                                440                 :                : 
                                441                 :                : 
                                442                 :                : static int
 6911 tgl@sss.pgh.pa.us         443                 :CBC       23016 : gbt_vsrt_cmp(const void *a, const void *b, void *arg)
                                444                 :                : {
 7678 bruce@momjian.us          445                 :          23016 :     GBT_VARKEY_R ar = gbt_var_key_readable(((const Vsrt *) a)->t);
                                446                 :          23016 :     GBT_VARKEY_R br = gbt_var_key_readable(((const Vsrt *) b)->t);
 5251 tgl@sss.pgh.pa.us         447                 :          23016 :     const gbt_vsrt_arg *varg = (const gbt_vsrt_arg *) arg;
                                448                 :                :     int         res;
                                449                 :                : 
 2921 peter_e@gmx.net           450                 :          23016 :     res = varg->tinfo->f_cmp(ar.lower, br.lower, varg->collation, varg->flinfo);
 5757 teodor@sigaev.ru          451         [ +  + ]:          23016 :     if (res == 0)
 2921 peter_e@gmx.net           452                 :           7633 :         return varg->tinfo->f_cmp(ar.upper, br.upper, varg->collation, varg->flinfo);
                                453                 :                : 
 5757 teodor@sigaev.ru          454                 :          15383 :     return res;
                                455                 :                : }
                                456                 :                : 
                                457                 :                : GIST_SPLITVEC *
 5251 tgl@sss.pgh.pa.us         458                 :             51 : gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v,
                                459                 :                :                   Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
                                460                 :                : {
                                461                 :                :     OffsetNumber i,
 7678 bruce@momjian.us          462                 :             51 :                 maxoff = entryvec->n - 1;
                                463                 :                :     Vsrt       *arr;
 7494 teodor@sigaev.ru          464                 :             51 :     int         svcntr = 0,
                                465                 :                :                 nbytes;
                                466                 :                :     char       *cur;
 7678 bruce@momjian.us          467                 :             51 :     GBT_VARKEY **sv = NULL;
                                468                 :                :     gbt_vsrt_arg varg;
                                469                 :                : 
                                470                 :             51 :     arr = (Vsrt *) palloc((maxoff + 1) * sizeof(Vsrt));
                                471                 :             51 :     nbytes = (maxoff + 2) * sizeof(OffsetNumber);
                                472                 :             51 :     v->spl_left = (OffsetNumber *) palloc(nbytes);
                                473                 :             51 :     v->spl_right = (OffsetNumber *) palloc(nbytes);
                                474                 :             51 :     v->spl_ldatum = PointerGetDatum(0);
                                475                 :             51 :     v->spl_rdatum = PointerGetDatum(0);
                                476                 :             51 :     v->spl_nleft = 0;
                                477                 :             51 :     v->spl_nright = 0;
                                478                 :                : 
                                479                 :             51 :     sv = palloc(sizeof(bytea *) * (maxoff + 1));
                                480                 :                : 
                                481                 :                :     /* Sort entries */
                                482                 :                : 
                                483         [ +  + ]:          18072 :     for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
                                484                 :                :     {
                                485                 :                :         GBT_VARKEY_R ro;
                                486                 :                : 
 7494 teodor@sigaev.ru          487                 :          18021 :         cur = (char *) DatumGetPointer(entryvec->vector[i].key);
 7678 bruce@momjian.us          488                 :          18021 :         ro = gbt_var_key_readable((GBT_VARKEY *) cur);
 2999 tgl@sss.pgh.pa.us         489         [ +  - ]:          18021 :         if (ro.lower == ro.upper)   /* leaf */
                                490                 :                :         {
 3091 andrew@dunslane.net       491                 :          18021 :             sv[svcntr] = gbt_var_leaf2node((GBT_VARKEY *) cur, tinfo, flinfo);
 7678 bruce@momjian.us          492                 :          18021 :             arr[i].t = sv[svcntr];
                                493         [ +  + ]:          18021 :             if (sv[svcntr] != (GBT_VARKEY *) cur)
                                494                 :           2400 :                 svcntr++;
                                495                 :                :         }
                                496                 :                :         else
 7678 bruce@momjian.us          497                 :UBC           0 :             arr[i].t = (GBT_VARKEY *) cur;
 7678 bruce@momjian.us          498                 :CBC       18021 :         arr[i].i = i;
                                499                 :                :     }
                                500                 :                : 
                                501                 :                :     /* sort */
 5251 tgl@sss.pgh.pa.us         502                 :             51 :     varg.tinfo = tinfo;
                                503                 :             51 :     varg.collation = collation;
 3091 andrew@dunslane.net       504                 :             51 :     varg.flinfo = flinfo;
  942 peter@eisentraut.org      505                 :             51 :     qsort_arg(&arr[FirstOffsetNumber],
                                506                 :                :               maxoff - FirstOffsetNumber + 1,
                                507                 :                :               sizeof(Vsrt),
                                508                 :                :               gbt_vsrt_cmp,
                                509                 :                :               &varg);
                                510                 :                : 
                                511                 :                :     /* We do simply create two parts */
                                512                 :                : 
 7678 bruce@momjian.us          513         [ +  + ]:          18072 :     for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
                                514                 :                :     {
                                515         [ +  + ]:          18021 :         if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
                                516                 :                :         {
 3091 andrew@dunslane.net       517                 :           9005 :             gbt_var_bin_union(&v->spl_ldatum, arr[i].t, collation, tinfo, flinfo);
 7678 bruce@momjian.us          518                 :           9005 :             v->spl_left[v->spl_nleft] = arr[i].i;
                                519                 :           9005 :             v->spl_nleft++;
                                520                 :                :         }
                                521                 :                :         else
                                522                 :                :         {
 3091 andrew@dunslane.net       523                 :           9016 :             gbt_var_bin_union(&v->spl_rdatum, arr[i].t, collation, tinfo, flinfo);
 7678 bruce@momjian.us          524                 :           9016 :             v->spl_right[v->spl_nright] = arr[i].i;
                                525                 :           9016 :             v->spl_nright++;
                                526                 :                :         }
                                527                 :                :     }
                                528                 :                : 
                                529                 :                :     /* Truncate (=compress) key */
                                530         [ +  + ]:             51 :     if (tinfo->trnc)
                                531                 :                :     {
                                532                 :             11 :         int32       ll = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), tinfo);
                                533                 :             11 :         int32       lr = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), tinfo);
                                534                 :                :         GBT_VARKEY *dl;
                                535                 :                :         GBT_VARKEY *dr;
                                536                 :                : 
                                537                 :             11 :         ll = Max(ll, lr);
                                538                 :             11 :         ll++;
                                539                 :                : 
                                540                 :             11 :         dl = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), ll, tinfo);
                                541                 :             11 :         dr = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), ll, tinfo);
                                542                 :             11 :         v->spl_ldatum = PointerGetDatum(dl);
                                543                 :             11 :         v->spl_rdatum = PointerGetDatum(dr);
                                544                 :                :     }
                                545                 :                : 
                                546                 :             51 :     return v;
                                547                 :                : }
                                548                 :                : 
                                549                 :                : 
                                550                 :                : /*
                                551                 :                :  * The GiST consistent method
                                552                 :                :  */
                                553                 :                : bool
 5251 tgl@sss.pgh.pa.us         554                 :          24891 : gbt_var_consistent(GBT_VARKEY_R *key,
                                555                 :                :                    const void *query,
                                556                 :                :                    StrategyNumber strategy,
                                557                 :                :                    Oid collation,
                                558                 :                :                    bool is_leaf,
                                559                 :                :                    const gbtree_vinfo *tinfo,
                                560                 :                :                    FmgrInfo *flinfo)
                                561                 :                : {
 2943 peter_e@gmx.net           562                 :          24891 :     bool        retval = false;
                                563                 :                : 
 5251 tgl@sss.pgh.pa.us         564   [ +  +  +  +  :          24891 :     switch (strategy)
                                           +  +  - ]
                                565                 :                :     {
 7678 bruce@momjian.us          566                 :           5703 :         case BTLessEqualStrategyNumber:
                                567         [ +  + ]:           5703 :             if (is_leaf)
 2921 peter_e@gmx.net           568                 :           5634 :                 retval = tinfo->f_ge(query, key->lower, collation, flinfo);
                                569                 :                :             else
                                570                 :            138 :                 retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
 7678 bruce@momjian.us          571   [ +  +  -  + ]:             69 :                     || gbt_var_node_pf_match(key, query, tinfo);
                                572                 :           5703 :             break;
                                573                 :           5610 :         case BTLessStrategyNumber:
                                574         [ +  + ]:           5610 :             if (is_leaf)
 2921 peter_e@gmx.net           575                 :           5555 :                 retval = tinfo->f_gt(query, key->lower, collation, flinfo);
                                576                 :                :             else
                                577                 :            110 :                 retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
 7678 bruce@momjian.us          578   [ +  +  -  + ]:             55 :                     || gbt_var_node_pf_match(key, query, tinfo);
                                579                 :           5610 :             break;
                                580                 :           2689 :         case BTEqualStrategyNumber:
                                581         [ +  + ]:           2689 :             if (is_leaf)
 2921 peter_e@gmx.net           582                 :           2622 :                 retval = tinfo->f_eq(query, key->lower, collation, flinfo);
                                583                 :                :             else
 5251 tgl@sss.pgh.pa.us         584                 :             67 :                 retval =
 2921 peter_e@gmx.net           585         [ +  + ]:            105 :                     (tinfo->f_cmp(key->lower, query, collation, flinfo) <= 0 &&
                                586   [ +  +  +  + ]:            134 :                      tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0) ||
 5251 tgl@sss.pgh.pa.us         587                 :             56 :                     gbt_var_node_pf_match(key, query, tinfo);
 7678 bruce@momjian.us          588                 :           2689 :             break;
                                589                 :           5466 :         case BTGreaterStrategyNumber:
                                590         [ +  + ]:           5466 :             if (is_leaf)
 2921 peter_e@gmx.net           591                 :           5405 :                 retval = tinfo->f_lt(query, key->upper, collation, flinfo);
                                592                 :                :             else
                                593                 :            122 :                 retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
 7678 bruce@momjian.us          594   [ +  +  +  + ]:             61 :                     || gbt_var_node_pf_match(key, query, tinfo);
                                595                 :           5466 :             break;
                                596                 :           5412 :         case BTGreaterEqualStrategyNumber:
                                597         [ +  + ]:           5412 :             if (is_leaf)
 2921 peter_e@gmx.net           598                 :           5336 :                 retval = tinfo->f_le(query, key->upper, collation, flinfo);
                                599                 :                :             else
                                600                 :            152 :                 retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
 7678 bruce@momjian.us          601   [ +  +  +  + ]:             76 :                     || gbt_var_node_pf_match(key, query, tinfo);
                                602                 :           5412 :             break;
 5514 rhaas@postgresql.org      603                 :             11 :         case BtreeGistNotEqualStrategyNumber:
 2921 peter_e@gmx.net           604   [ +  +  +  + ]:             17 :             retval = !(tinfo->f_eq(query, key->lower, collation, flinfo) &&
                                605                 :              6 :                        tinfo->f_eq(query, key->upper, collation, flinfo));
 5514 rhaas@postgresql.org      606                 :             11 :             break;
 7678 bruce@momjian.us          607                 :UBC           0 :         default:
 2943 peter_e@gmx.net           608                 :              0 :             retval = false;
                                609                 :                :     }
                                610                 :                : 
 5251 tgl@sss.pgh.pa.us         611                 :CBC       24891 :     return retval;
                                612                 :                : }
        

Generated by: LCOV version 2.4-beta