~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/btree_gist/btree_bit.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "btree_gist.h"
 
2
#include "btree_utils_var.h"
 
3
#include "utils/builtins.h"
 
4
#include "utils/varbit.h"
 
5
 
 
6
 
 
7
/*
 
8
** Bit ops
 
9
*/
 
10
PG_FUNCTION_INFO_V1(gbt_bit_compress);
 
11
PG_FUNCTION_INFO_V1(gbt_bit_union);
 
12
PG_FUNCTION_INFO_V1(gbt_bit_picksplit);
 
13
PG_FUNCTION_INFO_V1(gbt_bit_consistent);
 
14
PG_FUNCTION_INFO_V1(gbt_bit_penalty);
 
15
PG_FUNCTION_INFO_V1(gbt_bit_same);
 
16
 
 
17
Datum           gbt_bit_compress(PG_FUNCTION_ARGS);
 
18
Datum           gbt_bit_union(PG_FUNCTION_ARGS);
 
19
Datum           gbt_bit_picksplit(PG_FUNCTION_ARGS);
 
20
Datum           gbt_bit_consistent(PG_FUNCTION_ARGS);
 
21
Datum           gbt_bit_penalty(PG_FUNCTION_ARGS);
 
22
Datum           gbt_bit_same(PG_FUNCTION_ARGS);
 
23
 
 
24
 
 
25
 
 
26
/* define for comparison */
 
27
 
 
28
static bool
 
29
gbt_bitgt(const void *a, const void *b)
 
30
{
 
31
        return (DatumGetBool(DirectFunctionCall2(bitgt, PointerGetDatum(a), PointerGetDatum(b))));
 
32
}
 
33
 
 
34
static bool
 
35
gbt_bitge(const void *a, const void *b)
 
36
{
 
37
        return (DatumGetBool(DirectFunctionCall2(bitge, PointerGetDatum(a), PointerGetDatum(b))));
 
38
}
 
39
 
 
40
static bool
 
41
gbt_biteq(const void *a, const void *b)
 
42
{
 
43
        return (DatumGetBool(DirectFunctionCall2(biteq, PointerGetDatum(a), PointerGetDatum(b))));
 
44
}
 
45
 
 
46
static bool
 
47
gbt_bitle(const void *a, const void *b)
 
48
{
 
49
        return (DatumGetBool(DirectFunctionCall2(bitle, PointerGetDatum(a), PointerGetDatum(b))));
 
50
}
 
51
 
 
52
static bool
 
53
gbt_bitlt(const void *a, const void *b)
 
54
{
 
55
        return (DatumGetBool(DirectFunctionCall2(bitlt, PointerGetDatum(a), PointerGetDatum(b))));
 
56
}
 
57
 
 
58
static int32
 
59
gbt_bitcmp(const bytea *a, const bytea *b)
 
60
{
 
61
        return
 
62
                (DatumGetInt32(DirectFunctionCall2(byteacmp, PointerGetDatum(a), PointerGetDatum(b))));
 
63
}
 
64
 
 
65
 
 
66
static bytea *
 
67
gbt_bit_xfrm(bytea *leaf)
 
68
{
 
69
        bytea      *out = leaf;
 
70
        int                     s = VARBITBYTES(leaf) + VARHDRSZ;
 
71
 
 
72
        out = palloc(s);
 
73
        VARATT_SIZEP(out) = s;
 
74
        memcpy((void *) VARDATA(out), (void *) VARBITS(leaf), VARBITBYTES(leaf));
 
75
        return out;
 
76
}
 
77
 
 
78
 
 
79
 
 
80
 
 
81
static GBT_VARKEY *
 
82
gbt_bit_l2n(GBT_VARKEY * leaf)
 
83
{
 
84
 
 
85
        GBT_VARKEY *out = leaf;
 
86
        GBT_VARKEY_R r = gbt_var_key_readable(leaf);
 
87
        bytea      *o;
 
88
 
 
89
        o = gbt_bit_xfrm(r.lower);
 
90
        r.upper = r.lower = o;
 
91
        out = gbt_var_key_copy(&r, TRUE);
 
92
        pfree(o);
 
93
 
 
94
        return out;
 
95
 
 
96
}
 
97
 
 
98
static const gbtree_vinfo tinfo =
 
99
{
 
100
        gbt_t_bit,
 
101
        FALSE,
 
102
        TRUE,
 
103
        gbt_bitgt,
 
104
        gbt_bitge,
 
105
        gbt_biteq,
 
106
        gbt_bitle,
 
107
        gbt_bitlt,
 
108
        gbt_bitcmp,
 
109
        gbt_bit_l2n
 
110
};
 
111
 
 
112
 
 
113
/**************************************************
 
114
 * Bit ops
 
115
 **************************************************/
 
116
 
 
117
Datum
 
118
gbt_bit_compress(PG_FUNCTION_ARGS)
 
119
{
 
120
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
121
 
 
122
        PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
 
123
}
 
124
 
 
125
Datum
 
126
gbt_bit_consistent(PG_FUNCTION_ARGS)
 
127
{
 
128
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
129
        GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer(entry->key);
 
130
        GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
 
131
        void       *qtst = (void *) DatumGetPointer(PG_GETARG_DATUM(1));
 
132
        void       *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
 
133
        StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
 
134
        bool            retval = FALSE;
 
135
        GBT_VARKEY_R r = gbt_var_key_readable(key);
 
136
 
 
137
        if (GIST_LEAF(entry))
 
138
                retval = gbt_var_consistent(&r, query, &strategy, TRUE, &tinfo);
 
139
        else
 
140
        {
 
141
                bytea      *q = gbt_bit_xfrm((bytea *) query);
 
142
 
 
143
                retval = gbt_var_consistent(&r, (void *) q, &strategy, FALSE, &tinfo);
 
144
                pfree(q);
 
145
        }
 
146
 
 
147
        if (ktst != key)
 
148
                pfree(key);
 
149
        if (qtst != query)
 
150
                pfree(query);
 
151
        PG_RETURN_BOOL(retval);
 
152
}
 
153
 
 
154
 
 
155
 
 
156
Datum
 
157
gbt_bit_union(PG_FUNCTION_ARGS)
 
158
{
 
159
        GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
 
160
        int32      *size = (int *) PG_GETARG_POINTER(1);
 
161
 
 
162
        PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
 
163
}
 
164
 
 
165
 
 
166
Datum
 
167
gbt_bit_picksplit(PG_FUNCTION_ARGS)
 
168
{
 
169
        GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
 
170
        GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
 
171
 
 
172
        gbt_var_picksplit(entryvec, v, &tinfo);
 
173
        PG_RETURN_POINTER(v);
 
174
}
 
175
 
 
176
Datum
 
177
gbt_bit_same(PG_FUNCTION_ARGS)
 
178
{
 
179
        Datum           d1 = PG_GETARG_DATUM(0);
 
180
        Datum           d2 = PG_GETARG_DATUM(1);
 
181
        bool       *result = (bool *) PG_GETARG_POINTER(2);
 
182
 
 
183
        PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
 
184
}
 
185
 
 
186
 
 
187
Datum
 
188
gbt_bit_penalty(PG_FUNCTION_ARGS)
 
189
{
 
190
        float      *result = (float *) PG_GETARG_POINTER(2);
 
191
        GISTENTRY  *o = (GISTENTRY *) PG_GETARG_POINTER(0);
 
192
        GISTENTRY  *n = (GISTENTRY *) PG_GETARG_POINTER(1);
 
193
 
 
194
        PG_RETURN_POINTER(gbt_var_penalty(result, o, n, &tinfo));
 
195
}