Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Typedefs | Functions

btree_utils_var.h File Reference

#include "btree_gist.h"
#include "access/gist.h"
#include "mb/pg_wchar.h"
Include dependency graph for btree_utils_var.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  GBT_VARKEY_R
struct  gbtree_vinfo

Typedefs

typedef bytea GBT_VARKEY

Functions

GBT_VARKEY_R gbt_var_key_readable (const GBT_VARKEY *k)
GBT_VARKEYgbt_var_key_copy (const GBT_VARKEY_R *u, bool force_node)
GISTENTRYgbt_var_compress (GISTENTRY *entry, const gbtree_vinfo *tinfo)
GBT_VARKEYgbt_var_union (const GistEntryVector *entryvec, int32 *size, Oid collation, const gbtree_vinfo *tinfo)
bool gbt_var_same (Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo)
float * gbt_var_penalty (float *res, const GISTENTRY *o, const GISTENTRY *n, Oid collation, const gbtree_vinfo *tinfo)
bool gbt_var_consistent (GBT_VARKEY_R *key, const void *query, StrategyNumber strategy, Oid collation, bool is_leaf, const gbtree_vinfo *tinfo)
GIST_SPLITVECgbt_var_picksplit (const GistEntryVector *entryvec, GIST_SPLITVEC *v, Oid collation, const gbtree_vinfo *tinfo)
void gbt_var_bin_union (Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo)

Typedef Documentation

typedef bytea GBT_VARKEY

Definition at line 13 of file btree_utils_var.h.


Function Documentation

void gbt_var_bin_union ( Datum u,
GBT_VARKEY e,
Oid  collation,
const gbtree_vinfo tinfo 
)

Definition at line 222 of file btree_utils_var.c.

References DatumGetPointer, gbtree_vinfo::f_cmp, gbt_var_key_copy(), gbt_var_key_readable(), gbt_var_leaf2node(), GBT_VARKEY_R::lower, PointerGetDatum, TRUE, and GBT_VARKEY_R::upper.

Referenced by gbt_numeric_penalty(), gbt_var_penalty(), gbt_var_picksplit(), and gbt_var_union().

{
    GBT_VARKEY_R eo = gbt_var_key_readable(e);
    GBT_VARKEY_R nr;

    if (eo.lower == eo.upper)   /* leaf */
    {
        GBT_VARKEY *tmp;

        tmp = gbt_var_leaf2node(e, tinfo);
        if (tmp != e)
            eo = gbt_var_key_readable(tmp);
    }

    if (DatumGetPointer(*u))
    {
        GBT_VARKEY_R ro = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(*u));
        bool        update = false;

        nr.lower = ro.lower;
        nr.upper = ro.upper;

        if ((*tinfo->f_cmp) (ro.lower, eo.lower, collation) > 0)
        {
            nr.lower = eo.lower;
            update = true;
        }

        if ((*tinfo->f_cmp) (ro.upper, eo.upper, collation) < 0)
        {
            nr.upper = eo.upper;
            update = true;
        }

        if (update)
            *u = PointerGetDatum(gbt_var_key_copy(&nr, TRUE));
    }
    else
    {
        nr.lower = eo.lower;
        nr.upper = eo.upper;
        *u = PointerGetDatum(gbt_var_key_copy(&nr, TRUE));
    }
}

GISTENTRY* gbt_var_compress ( GISTENTRY entry,
const gbtree_vinfo tinfo 
)
bool gbt_var_consistent ( GBT_VARKEY_R key,
const void *  query,
StrategyNumber  strategy,
Oid  collation,
bool  is_leaf,
const gbtree_vinfo tinfo 
)

Definition at line 537 of file btree_utils_var.c.

References BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, BtreeGistNotEqualStrategyNumber, gbtree_vinfo::f_cmp, gbtree_vinfo::f_eq, gbtree_vinfo::f_ge, gbtree_vinfo::f_gt, gbtree_vinfo::f_le, gbtree_vinfo::f_lt, gbt_var_node_pf_match(), GBT_VARKEY_R::lower, and GBT_VARKEY_R::upper.

Referenced by gbt_bit_consistent(), gbt_bpchar_consistent(), gbt_bytea_consistent(), gbt_numeric_consistent(), and gbt_text_consistent().

{
    bool        retval = FALSE;

    switch (strategy)
    {
        case BTLessEqualStrategyNumber:
            if (is_leaf)
                retval = (*tinfo->f_ge) (query, key->lower, collation);
            else
                retval = (*tinfo->f_cmp) (query, key->lower, collation) >= 0
                    || gbt_var_node_pf_match(key, query, tinfo);
            break;
        case BTLessStrategyNumber:
            if (is_leaf)
                retval = (*tinfo->f_gt) (query, key->lower, collation);
            else
                retval = (*tinfo->f_cmp) (query, key->lower, collation) >= 0
                    || gbt_var_node_pf_match(key, query, tinfo);
            break;
        case BTEqualStrategyNumber:
            if (is_leaf)
                retval = (*tinfo->f_eq) (query, key->lower, collation);
            else
                retval =
                    ((*tinfo->f_cmp) (key->lower, query, collation) <= 0 &&
                     (*tinfo->f_cmp) (query, key->upper, collation) <= 0) ||
                    gbt_var_node_pf_match(key, query, tinfo);
            break;
        case BTGreaterStrategyNumber:
            if (is_leaf)
                retval = (*tinfo->f_lt) (query, key->upper, collation);
            else
                retval = (*tinfo->f_cmp) (query, key->upper, collation) <= 0
                    || gbt_var_node_pf_match(key, query, tinfo);
            break;
        case BTGreaterEqualStrategyNumber:
            if (is_leaf)
                retval = (*tinfo->f_le) (query, key->upper, collation);
            else
                retval = (*tinfo->f_cmp) (query, key->upper, collation) <= 0
                    || gbt_var_node_pf_match(key, query, tinfo);
            break;
        case BtreeGistNotEqualStrategyNumber:
            retval = !((*tinfo->f_eq) (query, key->lower, collation) &&
                       (*tinfo->f_eq) (query, key->upper, collation));
            break;
        default:
            retval = FALSE;
    }

    return retval;
}

GBT_VARKEY* gbt_var_key_copy ( const GBT_VARKEY_R u,
bool  force_node 
)

Definition at line 72 of file btree_utils_var.c.

References INTALIGN, GBT_VARKEY_R::lower, palloc(), SET_VARSIZE, GBT_VARKEY_R::upper, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by gbt_bit_l2n(), gbt_numeric_penalty(), gbt_var_bin_union(), gbt_var_compress(), and gbt_var_union().

{
    GBT_VARKEY *r = NULL;

    if (u->lower == u->upper && !force_node)
    {                           /* leaf key mode */
        r = (GBT_VARKEY *) palloc(VARSIZE(u->lower) + VARHDRSZ);
        memcpy(VARDATA(r), u->lower, VARSIZE(u->lower));
        SET_VARSIZE(r, VARSIZE(u->lower) + VARHDRSZ);
    }
    else
    {                           /* node key mode  */
        r = (GBT_VARKEY *) palloc(INTALIGN(VARSIZE(u->lower)) + VARSIZE(u->upper) + VARHDRSZ);
        memcpy(VARDATA(r), u->lower, VARSIZE(u->lower));
        memcpy(VARDATA(r) + INTALIGN(VARSIZE(u->lower)), u->upper, VARSIZE(u->upper));
        SET_VARSIZE(r, INTALIGN(VARSIZE(u->lower)) + VARSIZE(u->upper) + VARHDRSZ);
    }
    return r;
}

GBT_VARKEY_R gbt_var_key_readable ( const GBT_VARKEY k  ) 
float* gbt_var_penalty ( float *  res,
const GISTENTRY o,
const GISTENTRY n,
Oid  collation,
const gbtree_vinfo tinfo 
)

Definition at line 362 of file btree_utils_var.c.

References Abs, DatumGetPointer, gbtree_vinfo::f_cmp, gbt_bytea_pf_match(), gbt_var_bin_union(), gbt_var_key_readable(), gbt_var_leaf2node(), gbt_var_node_cp_len(), GISTENTRY::key, GBT_VARKEY_R::lower, tupleDesc::natts, PointerGetDatum, RelationData::rd_att, GISTENTRY::rel, GBT_VARKEY_R::upper, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by gbt_bit_penalty(), gbt_bytea_penalty(), and gbt_text_penalty().

{
    GBT_VARKEY *orge = (GBT_VARKEY *) DatumGetPointer(o->key);
    GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
    GBT_VARKEY_R ok,
                nk;

    *res = 0.0;

    nk = gbt_var_key_readable(newe);
    if (nk.lower == nk.upper)   /* leaf */
    {
        GBT_VARKEY *tmp;

        tmp = gbt_var_leaf2node(newe, tinfo);
        if (tmp != newe)
            nk = gbt_var_key_readable(tmp);
    }
    ok = gbt_var_key_readable(orge);

    if ((VARSIZE(ok.lower) - VARHDRSZ) == 0 && (VARSIZE(ok.upper) - VARHDRSZ) == 0)
        *res = 0.0;
    else if (!(((*tinfo->f_cmp) (nk.lower, ok.lower, collation) >= 0 ||
                gbt_bytea_pf_match(ok.lower, nk.lower, tinfo)) &&
               ((*tinfo->f_cmp) (nk.upper, ok.upper, collation) <= 0 ||
                gbt_bytea_pf_match(ok.upper, nk.upper, tinfo))))
    {
        Datum       d = PointerGetDatum(0);
        double      dres;
        int32       ol,
                    ul;

        gbt_var_bin_union(&d, orge, collation, tinfo);
        ol = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
        gbt_var_bin_union(&d, newe, collation, tinfo);
        ul = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);

        if (ul < ol)
        {
            dres = (ol - ul);   /* reduction of common prefix len */
        }
        else
        {
            GBT_VARKEY_R uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(d));
            unsigned char tmp[4];

            tmp[0] = (unsigned char) (((VARSIZE(ok.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.lower)[ul]));
            tmp[1] = (unsigned char) (((VARSIZE(uk.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.lower)[ul]));
            tmp[2] = (unsigned char) (((VARSIZE(ok.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.upper)[ul]));
            tmp[3] = (unsigned char) (((VARSIZE(uk.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.upper)[ul]));
            dres = Abs(tmp[0] - tmp[1]) + Abs(tmp[3] - tmp[2]);
            dres /= 256.0;
        }

        *res += FLT_MIN;
        *res += (float) (dres / ((double) (ol + 1)));
        *res *= (FLT_MAX / (o->rel->rd_att->natts + 1));
    }

    return res;
}

GIST_SPLITVEC* gbt_var_picksplit ( const GistEntryVector entryvec,
GIST_SPLITVEC v,
Oid  collation,
const gbtree_vinfo tinfo 
)

Definition at line 442 of file btree_utils_var.c.

References gbt_vsrt_arg::collation, cur, DatumGetPointer, FirstOffsetNumber, gbt_var_bin_union(), gbt_var_key_readable(), gbt_var_leaf2node(), gbt_var_node_cp_len(), gbt_var_node_truncate(), gbt_vsrt_cmp(), Vsrt::i, i, GISTENTRY::key, GBT_VARKEY_R::lower, Max, GistEntryVector::n, OffsetNumberNext, palloc(), PointerGetDatum, qsort_arg(), GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, Vsrt::t, gbt_vsrt_arg::tinfo, gbtree_vinfo::trnc, GBT_VARKEY_R::upper, and GistEntryVector::vector.

Referenced by gbt_bit_picksplit(), gbt_bytea_picksplit(), gbt_numeric_picksplit(), and gbt_text_picksplit().

{
    OffsetNumber i,
                maxoff = entryvec->n - 1;
    Vsrt       *arr;
    int         svcntr = 0,
                nbytes;
    char       *cur;
    GBT_VARKEY **sv = NULL;
    gbt_vsrt_arg varg;

    arr = (Vsrt *) palloc((maxoff + 1) * sizeof(Vsrt));
    nbytes = (maxoff + 2) * sizeof(OffsetNumber);
    v->spl_left = (OffsetNumber *) palloc(nbytes);
    v->spl_right = (OffsetNumber *) palloc(nbytes);
    v->spl_ldatum = PointerGetDatum(0);
    v->spl_rdatum = PointerGetDatum(0);
    v->spl_nleft = 0;
    v->spl_nright = 0;

    sv = palloc(sizeof(bytea *) * (maxoff + 1));

    /* Sort entries */

    for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
    {
        GBT_VARKEY_R ro;

        cur = (char *) DatumGetPointer(entryvec->vector[i].key);
        ro = gbt_var_key_readable((GBT_VARKEY *) cur);
        if (ro.lower == ro.upper)       /* leaf */
        {
            sv[svcntr] = gbt_var_leaf2node((GBT_VARKEY *) cur, tinfo);
            arr[i].t = sv[svcntr];
            if (sv[svcntr] != (GBT_VARKEY *) cur)
                svcntr++;
        }
        else
            arr[i].t = (GBT_VARKEY *) cur;
        arr[i].i = i;
    }

    /* sort */
    varg.tinfo = tinfo;
    varg.collation = collation;
    qsort_arg((void *) &arr[FirstOffsetNumber],
              maxoff - FirstOffsetNumber + 1,
              sizeof(Vsrt),
              gbt_vsrt_cmp,
              (void *) &varg);

    /* We do simply create two parts */

    for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
    {
        if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
        {
            gbt_var_bin_union(&v->spl_ldatum, arr[i].t, collation, tinfo);
            v->spl_left[v->spl_nleft] = arr[i].i;
            v->spl_nleft++;
        }
        else
        {
            gbt_var_bin_union(&v->spl_rdatum, arr[i].t, collation, tinfo);
            v->spl_right[v->spl_nright] = arr[i].i;
            v->spl_nright++;
        }
    }

    /* Truncate (=compress) key */
    if (tinfo->trnc)
    {
        int32       ll = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), tinfo);
        int32       lr = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), tinfo);
        GBT_VARKEY *dl;
        GBT_VARKEY *dr;

        ll = Max(ll, lr);
        ll++;

        dl = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), ll, tinfo);
        dr = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), ll, tinfo);
        v->spl_ldatum = PointerGetDatum(dl);
        v->spl_rdatum = PointerGetDatum(dr);
    }

    return v;
}

bool gbt_var_same ( Datum  d1,
Datum  d2,
Oid  collation,
const gbtree_vinfo tinfo 
)

Definition at line 339 of file btree_utils_var.c.

References DatumGetPointer, gbtree_vinfo::f_cmp, gbt_var_key_readable(), GBT_VARKEY_R::lower, NULL, and GBT_VARKEY_R::upper.

Referenced by gbt_bit_same(), gbt_bytea_same(), gbt_numeric_same(), and gbt_text_same().

{
    bool        result;
    GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1);
    GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2);
    GBT_VARKEY_R r1,
                r2;

    r1 = gbt_var_key_readable(t1);
    r2 = gbt_var_key_readable(t2);

    if (t1 && t2)
        result = ((*tinfo->f_cmp) (r1.lower, r2.lower, collation) == 0 &&
                  (*tinfo->f_cmp) (r1.upper, r2.upper, collation) == 0);
    else
        result = (t1 == NULL && t2 == NULL);

    return result;
}

GBT_VARKEY* gbt_var_union ( const GistEntryVector entryvec,
int32 size,
Oid  collation,
const gbtree_vinfo tinfo 
)

Definition at line 299 of file btree_utils_var.c.

References cur, DatumGetPointer, gbt_var_bin_union(), gbt_var_key_copy(), gbt_var_key_readable(), gbt_var_node_cp_len(), gbt_var_node_truncate(), i, GISTENTRY::key, GistEntryVector::n, PointerGetDatum, gbtree_vinfo::trnc, TRUE, and GistEntryVector::vector.

Referenced by gbt_bit_union(), gbt_bytea_union(), gbt_numeric_union(), and gbt_text_union().

{

    int         i = 0,
                numranges = entryvec->n;
    GBT_VARKEY *cur;
    Datum       out;
    GBT_VARKEY_R rk;

    *size = sizeof(GBT_VARKEY);

    cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[0].key);
    rk = gbt_var_key_readable(cur);
    out = PointerGetDatum(gbt_var_key_copy(&rk, TRUE));

    for (i = 1; i < numranges; i++)
    {
        cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[i].key);
        gbt_var_bin_union(&out, cur, collation, tinfo);
    }


    /* Truncate (=compress) key */
    if (tinfo->trnc)
    {
        int32       plen;
        GBT_VARKEY *trc = NULL;

        plen = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(out), tinfo);
        trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo);

        out = PointerGetDatum(trc);
    }

    return ((GBT_VARKEY *) DatumGetPointer(out));
}