~ubuntu-branches/ubuntu/lucid/postgresql-8.4/lucid-proposed

« back to all changes in this revision

Viewing changes to contrib/pg_trgm/trgm_gin.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $PostgreSQL$ 
 
3
 */
 
4
#include "trgm.h"
 
5
 
 
6
#include "access/gin.h"
 
7
#include "access/itup.h"
 
8
#include "access/tuptoaster.h"
 
9
#include "storage/bufpage.h"
 
10
#include "utils/array.h"
 
11
#include "utils/builtins.h"
 
12
 
 
13
PG_FUNCTION_INFO_V1(gin_extract_trgm);
 
14
Datum           gin_extract_trgm(PG_FUNCTION_ARGS);
 
15
 
 
16
PG_FUNCTION_INFO_V1(gin_trgm_consistent);
 
17
Datum           gin_trgm_consistent(PG_FUNCTION_ARGS);
 
18
 
 
19
Datum
 
20
gin_extract_trgm(PG_FUNCTION_ARGS)
 
21
{
 
22
        text       *val = (text *) PG_GETARG_TEXT_P(0);
 
23
        int32      *nentries = (int32 *) PG_GETARG_POINTER(1);
 
24
        Datum      *entries = NULL;
 
25
        TRGM       *trg;
 
26
        int4            trglen;
 
27
 
 
28
        *nentries = 0;
 
29
 
 
30
        trg = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
 
31
        trglen = ARRNELEM(trg);
 
32
 
 
33
        if (trglen > 0)
 
34
        {
 
35
                trgm       *ptr;
 
36
                int4            i = 0,
 
37
                                        item;
 
38
 
 
39
                *nentries = (int32) trglen;
 
40
                entries = (Datum *) palloc(sizeof(Datum) * trglen);
 
41
 
 
42
                ptr = GETARR(trg);
 
43
                while (ptr - GETARR(trg) < ARRNELEM(trg))
 
44
                {
 
45
                        item = trgm2int(ptr);
 
46
                        entries[i++] = Int32GetDatum(item);
 
47
 
 
48
                        ptr++;
 
49
                }
 
50
                if (PG_NARGS() > 4)
 
51
                {
 
52
                        /*
 
53
                         * Function called from query extracting
 
54
                         */
 
55
                        Pointer      **extra_data = (Pointer **) PG_GETARG_POINTER(4);
 
56
 
 
57
                        *extra_data = (Pointer*) palloc0(sizeof(Pointer)*(*nentries));
 
58
 
 
59
                        *(int32*)(*extra_data) = trglen;
 
60
                }
 
61
        }
 
62
 
 
63
        PG_RETURN_POINTER(entries);
 
64
}
 
65
 
 
66
Datum
 
67
gin_trgm_consistent(PG_FUNCTION_ARGS)
 
68
{
 
69
        bool       *check = (bool *) PG_GETARG_POINTER(0);
 
70
        /* StrategyNumber strategy = PG_GETARG_UINT16(1); */
 
71
        /* text    *query = PG_GETARG_TEXT_P(2); */
 
72
        /* int32        nkeys = PG_GETARG_INT32(3); */
 
73
        Pointer    *extra_data = (Pointer *) PG_GETARG_POINTER(4);
 
74
        bool       *recheck = (bool *) PG_GETARG_POINTER(5);
 
75
        bool            res = FALSE;
 
76
        int4            i,
 
77
                                trglen,
 
78
                                ntrue = 0;
 
79
 
 
80
        /* All cases served by this function are inexact */
 
81
        *recheck = true;
 
82
 
 
83
        trglen = *(int32*)extra_data;
 
84
 
 
85
        for (i = 0; i < trglen; i++)
 
86
                if (check[i])
 
87
                        ntrue++;
 
88
 
 
89
#ifdef DIVUNION
 
90
        res = (trglen == ntrue) ? true : ((((((float4) ntrue) / ((float4) (trglen - ntrue)))) >= trgm_limit) ? true : false);
 
91
#else
 
92
        res = (trglen == 0) ? false : ((((((float4) ntrue) / ((float4) trglen))) >= trgm_limit) ? true : false);
 
93
#endif
 
94
 
 
95
        PG_RETURN_BOOL(res);
 
96
}