Header And Logo

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

Data Structures | Defines | Typedefs | Functions | Variables

tsgistidx.c File Reference

#include "postgres.h"
#include "access/gist.h"
#include "access/tuptoaster.h"
#include "tsearch/ts_utils.h"
Include dependency graph for tsgistidx.c:

Go to the source code of this file.

Data Structures

struct  SignTSVector
struct  CHKVAL
struct  CACHESIGN
struct  SPLITCOST

Defines

#define SIGLENINT   31
#define SIGLEN   ( sizeof(int32) * SIGLENINT )
#define SIGLENBIT   (SIGLEN * BITS_PER_BYTE)
#define LOOPBYTE   for(i=0;i<SIGLEN;i++)
#define GETBYTE(x, i)   ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
#define GETBITBYTE(x, i)   ( ((char)(x)) >> (i) & 0x01 )
#define CLRBIT(x, i)   GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
#define SETBIT(x, i)   GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) )
#define GETBIT(x, i)   ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
#define HASHVAL(val)   (((unsigned int)(val)) % SIGLENBIT)
#define HASH(sign, val)   SETBIT((sign), HASHVAL(val))
#define GETENTRY(vec, pos)   ((SignTSVector *) DatumGetPointer((vec)->vector[(pos)].key))
#define ARRKEY   0x01
#define SIGNKEY   0x02
#define ALLISTRUE   0x04
#define ISARRKEY(x)   ( ((SignTSVector*)(x))->flag & ARRKEY )
#define ISSIGNKEY(x)   ( ((SignTSVector*)(x))->flag & SIGNKEY )
#define ISALLTRUE(x)   ( ((SignTSVector*)(x))->flag & ALLISTRUE )
#define GTHDRSIZE   ( VARHDRSZ + sizeof(int32) )
#define CALCGTSIZE(flag, len)   ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int32)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )
#define GETSIGN(x)   ( (BITVECP)( (char*)(x)+GTHDRSIZE ) )
#define GETARR(x)   ( (int32*)( (char*)(x)+GTHDRSIZE ) )
#define ARRNELEM(x)   ( ( VARSIZE(x) - GTHDRSIZE )/sizeof(int32) )
#define SINGOUTSTR   "%d true bits, %d false bits"
#define ARROUTSTR   "%d unique words"
#define EXTRALEN   ( 2*13 )
#define WISH_F(a, b, c)   (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )

Typedefs

typedef char BITVEC [SIGLEN]
typedef char * BITVECP

Functions

static int32 sizebitvec (BITVECP sign)
Datum gtsvectorin (PG_FUNCTION_ARGS)
Datum gtsvectorout (PG_FUNCTION_ARGS)
static int compareint (const void *va, const void *vb)
static int uniqueint (int32 *a, int32 l)
static void makesign (BITVECP sign, SignTSVector *a)
Datum gtsvector_compress (PG_FUNCTION_ARGS)
Datum gtsvector_decompress (PG_FUNCTION_ARGS)
static bool checkcondition_arr (void *checkval, QueryOperand *val)
static bool checkcondition_bit (void *checkval, QueryOperand *val)
Datum gtsvector_consistent (PG_FUNCTION_ARGS)
static int32 unionkey (BITVECP sbase, SignTSVector *add)
Datum gtsvector_union (PG_FUNCTION_ARGS)
Datum gtsvector_same (PG_FUNCTION_ARGS)
static int hemdistsign (BITVECP a, BITVECP b)
static int hemdist (SignTSVector *a, SignTSVector *b)
Datum gtsvector_penalty (PG_FUNCTION_ARGS)
static void fillcache (CACHESIGN *item, SignTSVector *key)
static int comparecost (const void *va, const void *vb)
static int hemdistcache (CACHESIGN *a, CACHESIGN *b)
Datum gtsvector_picksplit (PG_FUNCTION_ARGS)

Variables

static const uint8 number_of_ones [256]
static int outbuf_maxlen = 0

Define Documentation

#define ALLISTRUE   0x04

Definition at line 58 of file tsgistidx.c.

Referenced by gtsvector_compress(), and gtsvector_picksplit().

#define ARRKEY   0x01

Definition at line 56 of file tsgistidx.c.

Referenced by gtsvector_compress().

#define ARRNELEM (   x  )     ( ( VARSIZE(x) - GTHDRSIZE )/sizeof(int32) )

Definition at line 69 of file tsgistidx.c.

Referenced by gtsvector_consistent(), gtsvector_same(), gtsvectorout(), makesign(), and unionkey().

#define ARROUTSTR   "%d unique words"

Definition at line 103 of file tsgistidx.c.

Referenced by gtsvectorout().

#define CALCGTSIZE (   flag,
  len 
)    ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int32)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )

Definition at line 65 of file tsgistidx.c.

Referenced by gtsvector_compress(), gtsvector_picksplit(), and gtsvector_union().

#define CLRBIT (   x,
  i 
)    GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )

Definition at line 36 of file tsgistidx.c.

#define EXTRALEN   ( 2*13 )

Definition at line 104 of file tsgistidx.c.

Referenced by gtsvectorout().

#define GETARR (   x  )     ( (int32*)( (char*)(x)+GTHDRSIZE ) )
#define GETBIT (   x,
  i 
)    ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )

Definition at line 38 of file tsgistidx.c.

Referenced by checkcondition_bit().

#define GETBITBYTE (   x,
  i 
)    ( ((char)(x)) >> (i) & 0x01 )

Definition at line 35 of file tsgistidx.c.

#define GETBYTE (   x,
  i 
)    ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )

Definition at line 34 of file tsgistidx.c.

#define GETENTRY (   vec,
  pos 
)    ((SignTSVector *) DatumGetPointer((vec)->vector[(pos)].key))

Definition at line 43 of file tsgistidx.c.

Referenced by gtsvector_picksplit(), and gtsvector_union().

#define GETSIGN (   x  )     ( (BITVECP)( (char*)(x)+GTHDRSIZE ) )
#define GTHDRSIZE   ( VARHDRSZ + sizeof(int32) )

Definition at line 64 of file tsgistidx.c.

#define HASH (   sign,
  val 
)    SETBIT((sign), HASHVAL(val))

Definition at line 41 of file tsgistidx.c.

Referenced by makesign(), and unionkey().

#define HASHVAL (   val  )     (((unsigned int)(val)) % SIGLENBIT)

Definition at line 40 of file tsgistidx.c.

Referenced by checkcondition_bit().

#define ISALLTRUE (   x  )     ( ((SignTSVector*)(x))->flag & ALLISTRUE )
#define ISARRKEY (   x  )     ( ((SignTSVector*)(x))->flag & ARRKEY )

Definition at line 60 of file tsgistidx.c.

Referenced by fillcache(), gtsvector_penalty(), and gtsvectorout().

#define ISSIGNKEY (   x  )     ( ((SignTSVector*)(x))->flag & SIGNKEY )

Definition at line 61 of file tsgistidx.c.

Referenced by gtsvector_compress(), gtsvector_consistent(), gtsvector_same(), and unionkey().

#define LOOPBYTE   for(i=0;i<SIGLEN;i++)
#define SETBIT (   x,
  i 
)    GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) )

Definition at line 37 of file tsgistidx.c.

#define SIGLEN   ( sizeof(int32) * SIGLENINT )

Definition at line 25 of file tsgistidx.c.

#define SIGLENBIT   (SIGLEN * BITS_PER_BYTE)
#define SIGLENINT   31

Definition at line 22 of file tsgistidx.c.

#define SIGNKEY   0x02

Definition at line 57 of file tsgistidx.c.

Referenced by gtsvector_compress(), and gtsvector_picksplit().

#define SINGOUTSTR   "%d true bits, %d false bits"

Definition at line 102 of file tsgistidx.c.

Referenced by gtsvectorout().

#define WISH_F (   a,
  b,
  c 
)    (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )

Definition at line 586 of file tsgistidx.c.

Referenced by gtsvector_picksplit().


Typedef Documentation

typedef char BITVEC[SIGLEN]

Definition at line 28 of file tsgistidx.c.

typedef char* BITVECP

Definition at line 29 of file tsgistidx.c.


Function Documentation

static bool checkcondition_arr ( void *  checkval,
QueryOperand val 
) [static]

Definition at line 300 of file tsgistidx.c.

References QueryOperand::prefix, and QueryOperand::valcrc.

Referenced by gtsvector_consistent().

{
    int32      *StopLow = ((CHKVAL *) checkval)->arrb;
    int32      *StopHigh = ((CHKVAL *) checkval)->arre;
    int32      *StopMiddle;

    /* Loop invariant: StopLow <= val < StopHigh */

    /*
     * we are not able to find a a prefix by hash value
     */
    if (val->prefix)
        return true;

    while (StopLow < StopHigh)
    {
        StopMiddle = StopLow + (StopHigh - StopLow) / 2;
        if (*StopMiddle == val->valcrc)
            return (true);
        else if (*StopMiddle < val->valcrc)
            StopLow = StopMiddle + 1;
        else
            StopHigh = StopMiddle;
    }

    return (false);
}

static bool checkcondition_bit ( void *  checkval,
QueryOperand val 
) [static]

Definition at line 329 of file tsgistidx.c.

References GETBIT, HASHVAL, QueryOperand::prefix, and QueryOperand::valcrc.

Referenced by gtsvector_consistent().

{
    /*
     * we are not able to find a a prefix in signature tree
     */
    if (val->prefix)
        return true;
    return GETBIT(checkval, HASHVAL(val->valcrc));
}

static int comparecost ( const void *  va,
const void *  vb 
) [static]

Definition at line 594 of file tsgistidx.c.

References SPLITCOST::cost.

Referenced by gtsvector_picksplit().

{
    const SPLITCOST *a = (const SPLITCOST *) va;
    const SPLITCOST *b = (const SPLITCOST *) vb;

    if (a->cost == b->cost)
        return 0;
    else
        return (a->cost > b->cost) ? 1 : -1;
}

static int compareint ( const void *  va,
const void *  vb 
) [static]

Definition at line 132 of file tsgistidx.c.

Referenced by uniqueint().

{
    int32       a = *((const int32 *) va);
    int32       b = *((const int32 *) vb);

    if (a == b)
        return 0;
    return (a > b) ? 1 : -1;
}

static void fillcache ( CACHESIGN item,
SignTSVector key 
) [static]

Definition at line 575 of file tsgistidx.c.

References CACHESIGN::allistrue, GETSIGN, ISALLTRUE, ISARRKEY, makesign(), and CACHESIGN::sign.

Referenced by gtsvector_picksplit().

{
    item->allistrue = false;
    if (ISARRKEY(key))
        makesign(item->sign, key);
    else if (ISALLTRUE(key))
        item->allistrue = true;
    else
        memcpy((void *) item->sign, (void *) GETSIGN(key), sizeof(BITVEC));
}

Datum gtsvector_compress ( PG_FUNCTION_ARGS   ) 

Definition at line 180 of file tsgistidx.c.

References ALLISTRUE, ARRKEY, ARRPTR, CALCGTSIZE, COMP_CRC32, DatumGetPointer, DatumGetTSVector, FALSE, FIN_CRC32, SignTSVector::flag, GETARR, GETSIGN, gistentryinit, i, INIT_CRC32, ISALLTRUE, ISSIGNKEY, GISTENTRY::key, GISTENTRY::leafkey, WordEntry::len, LOOPBYTE, makesign(), GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, WordEntry::pos, GISTENTRY::rel, repalloc(), SET_VARSIZE, sign, SIGNKEY, TSVectorData::size, STRPTR, TOAST_INDEX_TARGET, uniqueint(), val, and VARSIZE.

{
    GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
    GISTENTRY  *retval = entry;

    if (entry->leafkey)
    {                           /* tsvector */
        SignTSVector *res;
        TSVector    val = DatumGetTSVector(entry->key);
        int32       len;
        int32      *arr;
        WordEntry  *ptr = ARRPTR(val);
        char       *words = STRPTR(val);

        len = CALCGTSIZE(ARRKEY, val->size);
        res = (SignTSVector *) palloc(len);
        SET_VARSIZE(res, len);
        res->flag = ARRKEY;
        arr = GETARR(res);
        len = val->size;
        while (len--)
        {
            pg_crc32    c;

            INIT_CRC32(c);
            COMP_CRC32(c, words + ptr->pos, ptr->len);
            FIN_CRC32(c);

            *arr = *(int32 *) &c;
            arr++;
            ptr++;
        }

        len = uniqueint(GETARR(res), val->size);
        if (len != val->size)
        {
            /*
             * there is a collision of hash-function; len is always less than
             * val->size
             */
            len = CALCGTSIZE(ARRKEY, len);
            res = (SignTSVector *) repalloc((void *) res, len);
            SET_VARSIZE(res, len);
        }

        /* make signature, if array is too long */
        if (VARSIZE(res) > TOAST_INDEX_TARGET)
        {
            SignTSVector *ressign;

            len = CALCGTSIZE(SIGNKEY, 0);
            ressign = (SignTSVector *) palloc(len);
            SET_VARSIZE(ressign, len);
            ressign->flag = SIGNKEY;
            makesign(GETSIGN(ressign), res);
            res = ressign;
        }

        retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
        gistentryinit(*retval, PointerGetDatum(res),
                      entry->rel, entry->page,
                      entry->offset, FALSE);
    }
    else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
             !ISALLTRUE(DatumGetPointer(entry->key)))
    {
        int32       i,
                    len;
        SignTSVector *res;
        BITVECP     sign = GETSIGN(DatumGetPointer(entry->key));

        LOOPBYTE
        {
            if ((sign[i] & 0xff) != 0xff)
                PG_RETURN_POINTER(retval);
        }

        len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
        res = (SignTSVector *) palloc(len);
        SET_VARSIZE(res, len);
        res->flag = SIGNKEY | ALLISTRUE;

        retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
        gistentryinit(*retval, PointerGetDatum(res),
                      entry->rel, entry->page,
                      entry->offset, FALSE);
    }
    PG_RETURN_POINTER(retval);
}

Datum gtsvector_consistent ( PG_FUNCTION_ARGS   ) 

Definition at line 340 of file tsgistidx.c.

References CHKVAL::arrb, CHKVAL::arre, ARRNELEM, checkcondition_arr(), checkcondition_bit(), DatumGetPointer, GETARR, GETQUERY, GETSIGN, ISALLTRUE, ISSIGNKEY, GISTENTRY::key, PG_GETARG_POINTER, PG_GETARG_TSQUERY, PG_RETURN_BOOL, TSQueryData::size, and TS_execute().

{
    GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
    TSQuery     query = PG_GETARG_TSQUERY(1);

    /* StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); */
    /* Oid      subtype = PG_GETARG_OID(3); */
    bool       *recheck = (bool *) PG_GETARG_POINTER(4);
    SignTSVector *key = (SignTSVector *) DatumGetPointer(entry->key);

    /* All cases served by this function are inexact */
    *recheck = true;

    if (!query->size)
        PG_RETURN_BOOL(false);

    if (ISSIGNKEY(key))
    {
        if (ISALLTRUE(key))
            PG_RETURN_BOOL(true);

        PG_RETURN_BOOL(TS_execute(
                                  GETQUERY(query),
                                  (void *) GETSIGN(key), false,
                                  checkcondition_bit
                                  ));
    }
    else
    {                           /* only leaf pages */
        CHKVAL      chkval;

        chkval.arrb = GETARR(key);
        chkval.arre = chkval.arrb + ARRNELEM(key);
        PG_RETURN_BOOL(TS_execute(
                                  GETQUERY(query),
                                  (void *) &chkval, true,
                                  checkcondition_arr
                                  ));
    }
}

Datum gtsvector_decompress ( PG_FUNCTION_ARGS   ) 
Datum gtsvector_penalty ( PG_FUNCTION_ARGS   ) 

Definition at line 541 of file tsgistidx.c.

References DatumGetPointer, GETSIGN, hemdist(), hemdistsign(), ISALLTRUE, ISARRKEY, GISTENTRY::key, makesign(), PG_GETARG_POINTER, PG_RETURN_POINTER, SIGLENBIT, sign, and sizebitvec().

{
    GISTENTRY  *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
    GISTENTRY  *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
    float      *penalty = (float *) PG_GETARG_POINTER(2);
    SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
    SignTSVector *newval = (SignTSVector *) DatumGetPointer(newentry->key);
    BITVECP     orig = GETSIGN(origval);

    *penalty = 0.0;

    if (ISARRKEY(newval))
    {
        BITVEC      sign;

        makesign(sign, newval);

        if (ISALLTRUE(origval))
            *penalty = ((float) (SIGLENBIT - sizebitvec(sign))) / (float) (SIGLENBIT + 1);
        else
            *penalty = hemdistsign(sign, orig);
    }
    else
        *penalty = hemdist(origval, newval);
    PG_RETURN_POINTER(penalty);
}

Datum gtsvector_picksplit ( PG_FUNCTION_ARGS   ) 

Definition at line 623 of file tsgistidx.c.

References Abs, ALLISTRUE, CALCGTSIZE, comparecost(), SPLITCOST::cost, fillcache(), FirstOffsetNumber, SignTSVector::flag, GETENTRY, GETSIGN, hemdistcache(), hemdistsign(), i, ISALLTRUE, LOOPBYTE, MemSet, GistEntryVector::n, OffsetNumberNext, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, SPLITCOST::pos, qsort, SET_VARSIZE, SIGLENBIT, CACHESIGN::sign, sign, SIGNKEY, sizebitvec(), GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, and WISH_F.

{
    GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
    GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
    OffsetNumber k,
                j;
    SignTSVector *datum_l,
               *datum_r;
    BITVECP     union_l,
                union_r;
    int32       size_alpha,
                size_beta;
    int32       size_waste,
                waste = -1;
    int32       nbytes;
    OffsetNumber seed_1 = 0,
                seed_2 = 0;
    OffsetNumber *left,
               *right;
    OffsetNumber maxoff;
    BITVECP     ptr;
    int         i;
    CACHESIGN  *cache;
    SPLITCOST  *costvector;

    maxoff = entryvec->n - 2;
    nbytes = (maxoff + 2) * sizeof(OffsetNumber);
    v->spl_left = (OffsetNumber *) palloc(nbytes);
    v->spl_right = (OffsetNumber *) palloc(nbytes);

    cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
    fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber));

    for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
    {
        for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
        {
            if (k == FirstOffsetNumber)
                fillcache(&cache[j], GETENTRY(entryvec, j));

            size_waste = hemdistcache(&(cache[j]), &(cache[k]));
            if (size_waste > waste)
            {
                waste = size_waste;
                seed_1 = k;
                seed_2 = j;
            }
        }
    }

    left = v->spl_left;
    v->spl_nleft = 0;
    right = v->spl_right;
    v->spl_nright = 0;

    if (seed_1 == 0 || seed_2 == 0)
    {
        seed_1 = 1;
        seed_2 = 2;
    }

    /* form initial .. */
    if (cache[seed_1].allistrue)
    {
        datum_l = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
        SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
        datum_l->flag = SIGNKEY | ALLISTRUE;
    }
    else
    {
        datum_l = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY, 0));
        SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY, 0));
        datum_l->flag = SIGNKEY;
        memcpy((void *) GETSIGN(datum_l), (void *) cache[seed_1].sign, sizeof(BITVEC));
    }
    if (cache[seed_2].allistrue)
    {
        datum_r = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
        SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
        datum_r->flag = SIGNKEY | ALLISTRUE;
    }
    else
    {
        datum_r = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY, 0));
        SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY, 0));
        datum_r->flag = SIGNKEY;
        memcpy((void *) GETSIGN(datum_r), (void *) cache[seed_2].sign, sizeof(BITVEC));
    }

    union_l = GETSIGN(datum_l);
    union_r = GETSIGN(datum_r);
    maxoff = OffsetNumberNext(maxoff);
    fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff));
    /* sort before ... */
    costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
    for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
    {
        costvector[j - 1].pos = j;
        size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]));
        size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
        costvector[j - 1].cost = Abs(size_alpha - size_beta);
    }
    qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);

    for (k = 0; k < maxoff; k++)
    {
        j = costvector[k].pos;
        if (j == seed_1)
        {
            *left++ = j;
            v->spl_nleft++;
            continue;
        }
        else if (j == seed_2)
        {
            *right++ = j;
            v->spl_nright++;
            continue;
        }

        if (ISALLTRUE(datum_l) || cache[j].allistrue)
        {
            if (ISALLTRUE(datum_l) && cache[j].allistrue)
                size_alpha = 0;
            else
                size_alpha = SIGLENBIT - sizebitvec(
                                                    (cache[j].allistrue) ? GETSIGN(datum_l) : GETSIGN(cache[j].sign)
                    );
        }
        else
            size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l));

        if (ISALLTRUE(datum_r) || cache[j].allistrue)
        {
            if (ISALLTRUE(datum_r) && cache[j].allistrue)
                size_beta = 0;
            else
                size_beta = SIGLENBIT - sizebitvec(
                                                   (cache[j].allistrue) ? GETSIGN(datum_r) : GETSIGN(cache[j].sign)
                    );
        }
        else
            size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r));

        if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
        {
            if (ISALLTRUE(datum_l) || cache[j].allistrue)
            {
                if (!ISALLTRUE(datum_l))
                    MemSet((void *) GETSIGN(datum_l), 0xff, sizeof(BITVEC));
            }
            else
            {
                ptr = cache[j].sign;
                LOOPBYTE
                    union_l[i] |= ptr[i];
            }
            *left++ = j;
            v->spl_nleft++;
        }
        else
        {
            if (ISALLTRUE(datum_r) || cache[j].allistrue)
            {
                if (!ISALLTRUE(datum_r))
                    MemSet((void *) GETSIGN(datum_r), 0xff, sizeof(BITVEC));
            }
            else
            {
                ptr = cache[j].sign;
                LOOPBYTE
                    union_r[i] |= ptr[i];
            }
            *right++ = j;
            v->spl_nright++;
        }
    }

    *right = *left = FirstOffsetNumber;
    v->spl_ldatum = PointerGetDatum(datum_l);
    v->spl_rdatum = PointerGetDatum(datum_r);

    PG_RETURN_POINTER(v);
}

Datum gtsvector_same ( PG_FUNCTION_ARGS   ) 

Definition at line 441 of file tsgistidx.c.

References ARRNELEM, GETARR, GETSIGN, i, ISALLTRUE, ISSIGNKEY, LOOPBYTE, PG_GETARG_POINTER, and PG_RETURN_POINTER.

{
    SignTSVector *a = (SignTSVector *) PG_GETARG_POINTER(0);
    SignTSVector *b = (SignTSVector *) PG_GETARG_POINTER(1);
    bool       *result = (bool *) PG_GETARG_POINTER(2);

    if (ISSIGNKEY(a))
    {                           /* then b also ISSIGNKEY */
        if (ISALLTRUE(a) && ISALLTRUE(b))
            *result = true;
        else if (ISALLTRUE(a))
            *result = false;
        else if (ISALLTRUE(b))
            *result = false;
        else
        {
            int32       i;
            BITVECP     sa = GETSIGN(a),
                        sb = GETSIGN(b);

            *result = true;
            LOOPBYTE
            {
                if (sa[i] != sb[i])
                {
                    *result = false;
                    break;
                }
            }
        }
    }
    else
    {                           /* a and b ISARRKEY */
        int32       lena = ARRNELEM(a),
                    lenb = ARRNELEM(b);

        if (lena != lenb)
            *result = false;
        else
        {
            int32      *ptra = GETARR(a),
                       *ptrb = GETARR(b);
            int32       i;

            *result = true;
            for (i = 0; i < lena; i++)
                if (ptra[i] != ptrb[i])
                {
                    *result = false;
                    break;
                }
        }
    }

    PG_RETURN_POINTER(result);
}

Datum gtsvector_union ( PG_FUNCTION_ARGS   ) 

Definition at line 408 of file tsgistidx.c.

References CALCGTSIZE, SignTSVector::flag, flag(), GETENTRY, GETSIGN, i, ISALLTRUE, MemSet, GistEntryVector::n, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, SET_VARSIZE, and unionkey().

{
    GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
    int        *size = (int *) PG_GETARG_POINTER(1);
    BITVEC      base;
    int32       i,
                len;
    int32       flag = 0;
    SignTSVector *result;

    MemSet((void *) base, 0, sizeof(BITVEC));
    for (i = 0; i < entryvec->n; i++)
    {
        if (unionkey(base, GETENTRY(entryvec, i)))
        {
            flag = ALLISTRUE;
            break;
        }
    }

    flag |= SIGNKEY;
    len = CALCGTSIZE(flag, 0);
    result = (SignTSVector *) palloc(len);
    *size = len;
    SET_VARSIZE(result, len);
    result->flag = flag;
    if (!ISALLTRUE(result))
        memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));

    PG_RETURN_POINTER(result);
}

Datum gtsvectorin ( PG_FUNCTION_ARGS   ) 

Definition at line 94 of file tsgistidx.c.

References ereport, errcode(), errmsg(), ERROR, and PG_RETURN_DATUM.

{
    ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("gtsvector_in not implemented")));
    PG_RETURN_DATUM(0);
}

Datum gtsvectorout ( PG_FUNCTION_ARGS   ) 

Definition at line 109 of file tsgistidx.c.

References ARRNELEM, ARROUTSTR, DatumGetPointer, EXTRALEN, GETSIGN, ISALLTRUE, ISARRKEY, Max, outbuf_maxlen, palloc(), PG_DETOAST_DATUM, PG_FREE_IF_COPY, PG_GETARG_POINTER, PG_RETURN_POINTER, SIGLENBIT, SINGOUTSTR, and sizebitvec().

{
    SignTSVector *key = (SignTSVector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_POINTER(0)));
    char       *outbuf;

    if (outbuf_maxlen == 0)
        outbuf_maxlen = 2 * EXTRALEN + Max(strlen(SINGOUTSTR), strlen(ARROUTSTR)) + 1;
    outbuf = palloc(outbuf_maxlen);

    if (ISARRKEY(key))
        sprintf(outbuf, ARROUTSTR, (int) ARRNELEM(key));
    else
    {
        int         cnttrue = (ISALLTRUE(key)) ? SIGLENBIT : sizebitvec(GETSIGN(key));

        sprintf(outbuf, SINGOUTSTR, cnttrue, (int) SIGLENBIT - cnttrue);
    }

    PG_FREE_IF_COPY(key, 0);
    PG_RETURN_POINTER(outbuf);
}

static int hemdist ( SignTSVector a,
SignTSVector b 
) [static]

Definition at line 525 of file tsgistidx.c.

References GETSIGN, hemdistsign(), ISALLTRUE, SIGLENBIT, and sizebitvec().

Referenced by gtsvector_penalty().

{
    if (ISALLTRUE(a))
    {
        if (ISALLTRUE(b))
            return 0;
        else
            return SIGLENBIT - sizebitvec(GETSIGN(b));
    }
    else if (ISALLTRUE(b))
        return SIGLENBIT - sizebitvec(GETSIGN(a));

    return hemdistsign(GETSIGN(a), GETSIGN(b));
}

static int hemdistcache ( CACHESIGN a,
CACHESIGN b 
) [static]

Definition at line 607 of file tsgistidx.c.

References CACHESIGN::allistrue, hemdistsign(), SIGLENBIT, CACHESIGN::sign, and sizebitvec().

Referenced by gtsvector_picksplit().

{
    if (a->allistrue)
    {
        if (b->allistrue)
            return 0;
        else
            return SIGLENBIT - sizebitvec(b->sign);
    }
    else if (b->allistrue)
        return SIGLENBIT - sizebitvec(a->sign);

    return hemdistsign(a->sign, b->sign);
}

static int hemdistsign ( BITVECP  a,
BITVECP  b 
) [static]

Definition at line 510 of file tsgistidx.c.

References i, LOOPBYTE, and number_of_ones.

Referenced by gtsvector_penalty(), gtsvector_picksplit(), hemdist(), and hemdistcache().

{
    int         i,
                diff,
                dist = 0;

    LOOPBYTE
    {
        diff = (unsigned char) (a[i] ^ b[i]);
        dist += number_of_ones[diff];
    }
    return dist;
}

static void makesign ( BITVECP  sign,
SignTSVector a 
) [static]

Definition at line 168 of file tsgistidx.c.

References ARRNELEM, GETARR, HASH, and MemSet.

Referenced by fillcache(), gtsvector_compress(), and gtsvector_penalty().

{
    int32       k,
                len = ARRNELEM(a);
    int32      *ptr = GETARR(a);

    MemSet((void *) sign, 0, sizeof(BITVEC));
    for (k = 0; k < len; k++)
        HASH(sign, ptr[k]);
}

static int32 sizebitvec ( BITVECP  sign  )  [static]

Definition at line 499 of file tsgistidx.c.

References i, LOOPBYTE, and number_of_ones.

Referenced by gtsvector_penalty(), gtsvector_picksplit(), gtsvectorout(), hemdist(), and hemdistcache().

{
    int32       size = 0,
                i;

    LOOPBYTE
        size += number_of_ones[(unsigned char) sign[i]];
    return size;
}

static int32 unionkey ( BITVECP  sbase,
SignTSVector add 
) [static]

Definition at line 382 of file tsgistidx.c.

References ARRNELEM, GETARR, GETSIGN, HASH, i, ISALLTRUE, ISSIGNKEY, and LOOPBYTE.

Referenced by gtsvector_union().

{
    int32       i;

    if (ISSIGNKEY(add))
    {
        BITVECP     sadd = GETSIGN(add);

        if (ISALLTRUE(add))
            return 1;

        LOOPBYTE
            sbase[i] |= sadd[i];
    }
    else
    {
        int32      *ptr = GETARR(add);

        for (i = 0; i < ARRNELEM(add); i++)
            HASH(sbase, ptr[i]);
    }
    return 0;
}

static int uniqueint ( int32 a,
int32  l 
) [static]

Definition at line 147 of file tsgistidx.c.

References compareint(), and qsort.

Referenced by gtsvector_compress().

{
    int32      *ptr,
               *res;

    if (l <= 1)
        return l;

    ptr = res = a;

    qsort((void *) a, l, sizeof(int32), compareint);

    while (ptr - a < l)
        if (*ptr != *res)
            *(++res) = *ptr++;
        else
            ptr++;
    return res + 1 - a;
}


Variable Documentation

const uint8 number_of_ones[256] [static]
Initial value:
 {
    0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
    4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
}

Definition at line 72 of file tsgistidx.c.

Referenced by hemdistsign(), and sizebitvec().

int outbuf_maxlen = 0 [static]

Definition at line 106 of file tsgistidx.c.

Referenced by gtsvectorout().