Header And Logo

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

Data Structures | Functions

tsginidx.c File Reference

#include "postgres.h"
#include "access/gin.h"
#include "access/skey.h"
#include "tsearch/ts_type.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
Include dependency graph for tsginidx.c:

Go to the source code of this file.

Data Structures

struct  GinChkVal

Functions

Datum gin_cmp_tslexeme (PG_FUNCTION_ARGS)
Datum gin_cmp_prefix (PG_FUNCTION_ARGS)
Datum gin_extract_tsvector (PG_FUNCTION_ARGS)
Datum gin_extract_tsquery (PG_FUNCTION_ARGS)
static bool checkcondition_gin (void *checkval, QueryOperand *val)
Datum gin_tsquery_consistent (PG_FUNCTION_ARGS)
Datum gin_extract_tsvector_2args (PG_FUNCTION_ARGS)
Datum gin_extract_tsquery_5args (PG_FUNCTION_ARGS)
Datum gin_tsquery_consistent_6args (PG_FUNCTION_ARGS)

Function Documentation

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

Definition at line 181 of file tsginidx.c.

References GinChkVal::check, GinChkVal::first_item, GinChkVal::map_item_operand, GinChkVal::need_recheck, and QueryOperand::weight.

Referenced by gin_tsquery_consistent().

{
    GinChkVal  *gcv = (GinChkVal *) checkval;
    int         j;

    /* if any val requiring a weight is used, set recheck flag */
    if (val->weight != 0)
        *(gcv->need_recheck) = true;

    /* convert item's number to corresponding entry's (operand's) number */
    j = gcv->map_item_operand[((QueryItem *) val) - gcv->first_item];

    /* return presence of current entry in indexed value */
    return gcv->check[j];
}

Datum gin_cmp_prefix ( PG_FUNCTION_ARGS   ) 

Definition at line 40 of file tsginidx.c.

References PG_FREE_IF_COPY, PG_GETARG_POINTER, PG_GETARG_TEXT_PP, PG_GETARG_UINT16, PG_RETURN_INT32, tsCompareString(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    text       *a = PG_GETARG_TEXT_PP(0);
    text       *b = PG_GETARG_TEXT_PP(1);

#ifdef NOT_USED
    StrategyNumber strategy = PG_GETARG_UINT16(2);
    Pointer     extra_data = PG_GETARG_POINTER(3);
#endif
    int         cmp;

    cmp = tsCompareString(VARDATA_ANY(a), VARSIZE_ANY_EXHDR(a),
                          VARDATA_ANY(b), VARSIZE_ANY_EXHDR(b),
                          true);

    if (cmp < 0)
        cmp = 1;                /* prevent continue scan */

    PG_FREE_IF_COPY(a, 0);
    PG_FREE_IF_COPY(b, 1);
    PG_RETURN_INT32(cmp);
}

Datum gin_cmp_tslexeme ( PG_FUNCTION_ARGS   ) 
Datum gin_extract_tsquery ( PG_FUNCTION_ARGS   ) 

Definition at line 94 of file tsginidx.c.

References cstring_to_text_with_len(), QueryOperand::distance, GETOPERAND, GETQUERY, i, QueryOperand::length, palloc(), palloc0(), PG_FREE_IF_COPY, PG_GETARG_POINTER, PG_GETARG_TSQUERY, PG_RETURN_POINTER, PointerGetDatum, QueryOperand::prefix, QI_VAL, QueryItem::qoperand, TSQueryData::size, tsquery_requires_match(), and val.

Referenced by gin_extract_tsquery_5args().

{
    TSQuery     query = PG_GETARG_TSQUERY(0);
    int32      *nentries = (int32 *) PG_GETARG_POINTER(1);

    /* StrategyNumber strategy = PG_GETARG_UINT16(2); */
    bool      **ptr_partialmatch = (bool **) PG_GETARG_POINTER(3);
    Pointer   **extra_data = (Pointer **) PG_GETARG_POINTER(4);

    /* bool   **nullFlags = (bool **) PG_GETARG_POINTER(5); */
    int32      *searchMode = (int32 *) PG_GETARG_POINTER(6);
    Datum      *entries = NULL;

    *nentries = 0;

    if (query->size > 0)
    {
        QueryItem  *item = GETQUERY(query);
        int32       i,
                    j;
        bool       *partialmatch;
        int        *map_item_operand;

        /*
         * If the query doesn't have any required positive matches (for
         * instance, it's something like '! foo'), we have to do a full index
         * scan.
         */
        if (tsquery_requires_match(item))
            *searchMode = GIN_SEARCH_MODE_DEFAULT;
        else
            *searchMode = GIN_SEARCH_MODE_ALL;

        /* count number of VAL items */
        j = 0;
        for (i = 0; i < query->size; i++)
        {
            if (item[i].type == QI_VAL)
                j++;
        }
        *nentries = j;

        entries = (Datum *) palloc(sizeof(Datum) * j);
        partialmatch = *ptr_partialmatch = (bool *) palloc(sizeof(bool) * j);

        /*
         * Make map to convert item's number to corresponding operand's (the
         * same, entry's) number. Entry's number is used in check array in
         * consistent method. We use the same map for each entry.
         */
        *extra_data = (Pointer *) palloc(sizeof(Pointer) * j);
        map_item_operand = (int *) palloc0(sizeof(int) * query->size);

        /* Now rescan the VAL items and fill in the arrays */
        j = 0;
        for (i = 0; i < query->size; i++)
        {
            if (item[i].type == QI_VAL)
            {
                QueryOperand *val = &item[i].qoperand;
                text       *txt;

                txt = cstring_to_text_with_len(GETOPERAND(query) + val->distance,
                                               val->length);
                entries[j] = PointerGetDatum(txt);
                partialmatch[j] = val->prefix;
                (*extra_data)[j] = (Pointer) map_item_operand;
                map_item_operand[i] = j;
                j++;
            }
        }
    }

    PG_FREE_IF_COPY(query, 0);

    PG_RETURN_POINTER(entries);
}

Datum gin_extract_tsquery_5args ( PG_FUNCTION_ARGS   ) 

Definition at line 257 of file tsginidx.c.

References elog, ERROR, gin_extract_tsquery(), and PG_NARGS.

{
    if (PG_NARGS() < 7)         /* should not happen */
        elog(ERROR, "gin_extract_tsquery requires seven arguments");
    return gin_extract_tsquery(fcinfo);
}

Datum gin_extract_tsvector ( PG_FUNCTION_ARGS   ) 

Definition at line 64 of file tsginidx.c.

References ARRPTR, cstring_to_text_with_len(), i, WordEntry::len, palloc(), PG_FREE_IF_COPY, PG_GETARG_POINTER, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, PointerGetDatum, WordEntry::pos, TSVectorData::size, and STRPTR.

Referenced by gin_extract_tsvector_2args().

{
    TSVector    vector = PG_GETARG_TSVECTOR(0);
    int32      *nentries = (int32 *) PG_GETARG_POINTER(1);
    Datum      *entries = NULL;

    *nentries = vector->size;
    if (vector->size > 0)
    {
        int         i;
        WordEntry  *we = ARRPTR(vector);

        entries = (Datum *) palloc(sizeof(Datum) * vector->size);

        for (i = 0; i < vector->size; i++)
        {
            text       *txt;

            txt = cstring_to_text_with_len(STRPTR(vector) + we->pos, we->len);
            entries[i] = PointerGetDatum(txt);

            we++;
        }
    }

    PG_FREE_IF_COPY(vector, 0);
    PG_RETURN_POINTER(entries);
}

Datum gin_extract_tsvector_2args ( PG_FUNCTION_ARGS   ) 

Definition at line 245 of file tsginidx.c.

References elog, ERROR, gin_extract_tsvector(), and PG_NARGS.

{
    if (PG_NARGS() < 3)         /* should not happen */
        elog(ERROR, "gin_extract_tsvector requires three arguments");
    return gin_extract_tsvector(fcinfo);
}

Datum gin_tsquery_consistent ( PG_FUNCTION_ARGS   ) 

Definition at line 198 of file tsginidx.c.

References GinChkVal::check, checkcondition_gin(), GinChkVal::first_item, GETQUERY, GinChkVal::map_item_operand, GinChkVal::need_recheck, PG_GETARG_POINTER, PG_GETARG_TSQUERY, PG_RETURN_BOOL, TSQueryData::size, and TS_execute().

Referenced by gin_tsquery_consistent_6args().

{
    bool       *check = (bool *) PG_GETARG_POINTER(0);

    /* StrategyNumber strategy = PG_GETARG_UINT16(1); */
    TSQuery     query = PG_GETARG_TSQUERY(2);

    /* int32    nkeys = PG_GETARG_INT32(3); */
    Pointer    *extra_data = (Pointer *) PG_GETARG_POINTER(4);
    bool       *recheck = (bool *) PG_GETARG_POINTER(5);
    bool        res = FALSE;

    /* The query requires recheck only if it involves weights */
    *recheck = false;

    if (query->size > 0)
    {
        QueryItem  *item;
        GinChkVal   gcv;

        /*
         * check-parameter array has one entry for each value (operand) in the
         * query.
         */
        gcv.first_item = item = GETQUERY(query);
        gcv.check = check;
        gcv.map_item_operand = (int *) (extra_data[0]);
        gcv.need_recheck = recheck;

        res = TS_execute(GETQUERY(query),
                         &gcv,
                         true,
                         checkcondition_gin);
    }

    PG_RETURN_BOOL(res);
}

Datum gin_tsquery_consistent_6args ( PG_FUNCTION_ARGS   ) 

Definition at line 269 of file tsginidx.c.

References elog, ERROR, gin_tsquery_consistent(), and PG_NARGS.

{
    if (PG_NARGS() < 8)         /* should not happen */
        elog(ERROR, "gin_tsquery_consistent requires eight arguments");
    return gin_tsquery_consistent(fcinfo);
}