~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/btree_gist/btree_inet.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_num.h"
 
3
#include "utils/builtins.h"
 
4
#include "utils/inet.h"
 
5
#include "catalog/pg_type.h"
 
6
 
 
7
typedef struct inetkey
 
8
{
 
9
        double          lower;
 
10
        double          upper;
 
11
}       inetKEY;
 
12
 
 
13
/*
 
14
** inet ops
 
15
*/
 
16
PG_FUNCTION_INFO_V1(gbt_inet_compress);
 
17
PG_FUNCTION_INFO_V1(gbt_cidr_compress);
 
18
PG_FUNCTION_INFO_V1(gbt_inet_union);
 
19
PG_FUNCTION_INFO_V1(gbt_inet_picksplit);
 
20
PG_FUNCTION_INFO_V1(gbt_inet_consistent);
 
21
PG_FUNCTION_INFO_V1(gbt_cidr_consistent);
 
22
PG_FUNCTION_INFO_V1(gbt_inet_penalty);
 
23
PG_FUNCTION_INFO_V1(gbt_inet_same);
 
24
 
 
25
Datum           gbt_inet_compress(PG_FUNCTION_ARGS);
 
26
Datum           gbt_cidr_compress(PG_FUNCTION_ARGS);
 
27
Datum           gbt_inet_union(PG_FUNCTION_ARGS);
 
28
Datum           gbt_inet_picksplit(PG_FUNCTION_ARGS);
 
29
Datum           gbt_inet_consistent(PG_FUNCTION_ARGS);
 
30
Datum           gbt_cidr_consistent(PG_FUNCTION_ARGS);
 
31
Datum           gbt_inet_penalty(PG_FUNCTION_ARGS);
 
32
Datum           gbt_inet_same(PG_FUNCTION_ARGS);
 
33
 
 
34
 
 
35
static bool
 
36
gbt_inetgt(const void *a, const void *b)
 
37
{
 
38
        return (*((double *) a) > *((double *) b));
 
39
}
 
40
static bool
 
41
gbt_inetge(const void *a, const void *b)
 
42
{
 
43
        return (*((double *) a) >= *((double *) b));
 
44
}
 
45
static bool
 
46
gbt_ineteq(const void *a, const void *b)
 
47
{
 
48
        return (*((double *) a) == *((double *) b));
 
49
}
 
50
static bool
 
51
gbt_inetle(const void *a, const void *b)
 
52
{
 
53
        return (*((double *) a) <= *((double *) b));
 
54
}
 
55
static bool
 
56
gbt_inetlt(const void *a, const void *b)
 
57
{
 
58
        return (*((double *) a) < *((double *) b));
 
59
}
 
60
 
 
61
static int
 
62
gbt_inetkey_cmp(const void *a, const void *b)
 
63
{
 
64
 
 
65
        if (*(double *) (&((Nsrt *) a)->t[0]) > *(double *) (&((Nsrt *) b)->t[0]))
 
66
                return 1;
 
67
        else if (*(double *) (&((Nsrt *) a)->t[0]) < *(double *) (&((Nsrt *) b)->t[0]))
 
68
                return -1;
 
69
        return 0;
 
70
 
 
71
}
 
72
 
 
73
 
 
74
static const gbtree_ninfo tinfo =
 
75
{
 
76
        gbt_t_inet,
 
77
        sizeof(double),
 
78
        gbt_inetgt,
 
79
        gbt_inetge,
 
80
        gbt_ineteq,
 
81
        gbt_inetle,
 
82
        gbt_inetlt,
 
83
        gbt_inetkey_cmp
 
84
};
 
85
 
 
86
 
 
87
/**************************************************
 
88
 * inet ops
 
89
 **************************************************/
 
90
 
 
91
 
 
92
 
 
93
static GISTENTRY *
 
94
gbt_inet_compress_inetrnal(GISTENTRY *retval, GISTENTRY *entry, Oid typid)
 
95
{
 
96
 
 
97
        if (entry->leafkey)
 
98
        {
 
99
                inetKEY    *r = (inetKEY *) palloc(sizeof(inetKEY));
 
100
 
 
101
                retval = palloc(sizeof(GISTENTRY));
 
102
                r->lower = convert_network_to_scalar(entry->key, typid);
 
103
                r->upper = r->lower;
 
104
                gistentryinit(*retval, PointerGetDatum(r),
 
105
                                          entry->rel, entry->page,
 
106
                                          entry->offset, sizeof(inetKEY), FALSE);
 
107
        }
 
108
        else
 
109
                retval = entry;
 
110
 
 
111
        return (retval);
 
112
}
 
113
 
 
114
 
 
115
 
 
116
Datum
 
117
gbt_inet_compress(PG_FUNCTION_ARGS)
 
118
{
 
119
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
120
        GISTENTRY  *retval = NULL;
 
121
 
 
122
        PG_RETURN_POINTER(gbt_inet_compress_inetrnal(retval, entry, INETOID));
 
123
}
 
124
 
 
125
Datum
 
126
gbt_cidr_compress(PG_FUNCTION_ARGS)
 
127
{
 
128
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
129
        GISTENTRY  *retval = NULL;
 
130
 
 
131
        PG_RETURN_POINTER(gbt_inet_compress_inetrnal(retval, entry, CIDROID));
 
132
}
 
133
 
 
134
 
 
135
static bool
 
136
gbt_inet_consistent_internal(
 
137
                                                         const GISTENTRY *entry,
 
138
                                                         const double *query,
 
139
                                                         const StrategyNumber *strategy
 
140
)
 
141
{
 
142
        inetKEY    *kkk = (inetKEY *) DatumGetPointer(entry->key);
 
143
        GBT_NUMKEY_R key;
 
144
 
 
145
        key.lower = (GBT_NUMKEY *) & kkk->lower;
 
146
        key.upper = (GBT_NUMKEY *) & kkk->upper;
 
147
 
 
148
        return (
 
149
                        gbt_num_consistent(&key, (void *) query, strategy, GIST_LEAF(entry), &tinfo)
 
150
                );
 
151
}
 
152
 
 
153
 
 
154
Datum
 
155
gbt_inet_consistent(PG_FUNCTION_ARGS)
 
156
{
 
157
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
158
        double          query = convert_network_to_scalar(PG_GETARG_DATUM(1), INETOID);
 
159
        StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
 
160
 
 
161
        PG_RETURN_BOOL(
 
162
                                   gbt_inet_consistent_internal(entry, &query, &strategy)
 
163
                );
 
164
}
 
165
 
 
166
Datum
 
167
gbt_cidr_consistent(PG_FUNCTION_ARGS)
 
168
{
 
169
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
170
        double          query = convert_network_to_scalar(PG_GETARG_DATUM(1), CIDROID);
 
171
        StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
 
172
 
 
173
        PG_RETURN_BOOL(
 
174
                                   gbt_inet_consistent_internal(entry, &query, &strategy)
 
175
                );
 
176
}
 
177
 
 
178
 
 
179
Datum
 
180
gbt_inet_union(PG_FUNCTION_ARGS)
 
181
{
 
182
        GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
 
183
        void       *out = palloc(sizeof(inetKEY));
 
184
 
 
185
        *(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY);
 
186
        PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
 
187
}
 
188
 
 
189
 
 
190
Datum
 
191
gbt_inet_penalty(PG_FUNCTION_ARGS)
 
192
{
 
193
        inetKEY    *origentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
 
194
        inetKEY    *newentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
 
195
        float      *result = (float *) PG_GETARG_POINTER(2);
 
196
 
 
197
        double          res;
 
198
 
 
199
        *result = 0.0;
 
200
 
 
201
        penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
 
202
 
 
203
        if (res > 0)
 
204
        {
 
205
                *result += FLT_MIN;
 
206
                *result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
 
207
                *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
 
208
        }
 
209
 
 
210
        PG_RETURN_POINTER(result);
 
211
 
 
212
}
 
213
 
 
214
Datum
 
215
gbt_inet_picksplit(PG_FUNCTION_ARGS)
 
216
{
 
217
        PG_RETURN_POINTER(gbt_num_picksplit(
 
218
                                                                (GistEntryVector *) PG_GETARG_POINTER(0),
 
219
                                                                  (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
 
220
                                                                                &tinfo
 
221
                                                                                ));
 
222
}
 
223
 
 
224
Datum
 
225
gbt_inet_same(PG_FUNCTION_ARGS)
 
226
{
 
227
        inetKEY    *b1 = (inetKEY *) PG_GETARG_POINTER(0);
 
228
        inetKEY    *b2 = (inetKEY *) PG_GETARG_POINTER(1);
 
229
        bool       *result = (bool *) PG_GETARG_POINTER(2);
 
230
 
 
231
        *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
 
232
        PG_RETURN_POINTER(result);
 
233
}