~ubuntu-branches/ubuntu/natty/postgresql-8.4/natty-updates

« back to all changes in this revision

Viewing changes to contrib/btree_gist/btree_macaddr.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 "btree_gist.h"
 
5
#include "btree_utils_num.h"
 
6
#include "utils/builtins.h"
 
7
#include "utils/inet.h"
 
8
 
 
9
typedef struct
 
10
{
 
11
        macaddr         lower;
 
12
        macaddr         upper;
 
13
}       macKEY;
 
14
 
 
15
/*
 
16
** OID ops
 
17
*/
 
18
PG_FUNCTION_INFO_V1(gbt_macad_compress);
 
19
PG_FUNCTION_INFO_V1(gbt_macad_union);
 
20
PG_FUNCTION_INFO_V1(gbt_macad_picksplit);
 
21
PG_FUNCTION_INFO_V1(gbt_macad_consistent);
 
22
PG_FUNCTION_INFO_V1(gbt_macad_penalty);
 
23
PG_FUNCTION_INFO_V1(gbt_macad_same);
 
24
 
 
25
Datum           gbt_macad_compress(PG_FUNCTION_ARGS);
 
26
Datum           gbt_macad_union(PG_FUNCTION_ARGS);
 
27
Datum           gbt_macad_picksplit(PG_FUNCTION_ARGS);
 
28
Datum           gbt_macad_consistent(PG_FUNCTION_ARGS);
 
29
Datum           gbt_macad_penalty(PG_FUNCTION_ARGS);
 
30
Datum           gbt_macad_same(PG_FUNCTION_ARGS);
 
31
 
 
32
 
 
33
static bool
 
34
gbt_macadgt(const void *a, const void *b)
 
35
{
 
36
        return DatumGetBool(DirectFunctionCall2(macaddr_gt, PointerGetDatum(a), PointerGetDatum(b)));
 
37
}
 
38
static bool
 
39
gbt_macadge(const void *a, const void *b)
 
40
{
 
41
        return DatumGetBool(DirectFunctionCall2(macaddr_ge, PointerGetDatum(a), PointerGetDatum(b)));
 
42
}
 
43
 
 
44
static bool
 
45
gbt_macadeq(const void *a, const void *b)
 
46
{
 
47
        return DatumGetBool(DirectFunctionCall2(macaddr_eq, PointerGetDatum(a), PointerGetDatum(b)));
 
48
}
 
49
 
 
50
static bool
 
51
gbt_macadle(const void *a, const void *b)
 
52
{
 
53
        return DatumGetBool(DirectFunctionCall2(macaddr_le, PointerGetDatum(a), PointerGetDatum(b)));
 
54
}
 
55
 
 
56
static bool
 
57
gbt_macadlt(const void *a, const void *b)
 
58
{
 
59
        return DatumGetBool(DirectFunctionCall2(macaddr_lt, PointerGetDatum(a), PointerGetDatum(b)));
 
60
}
 
61
 
 
62
 
 
63
static int
 
64
gbt_macadkey_cmp(const void *a, const void *b)
 
65
{
 
66
        return DatumGetInt32(
 
67
                                                 DirectFunctionCall2(
 
68
                                                                                         macaddr_cmp,
 
69
                                                                                PointerGetDatum(&((Nsrt *) a)->t[0]),
 
70
                                                                                 PointerGetDatum(&((Nsrt *) b)->t[0])
 
71
                                                                                         )
 
72
                );
 
73
}
 
74
 
 
75
 
 
76
static const gbtree_ninfo tinfo =
 
77
{
 
78
        gbt_t_macad,
 
79
        sizeof(macaddr),
 
80
        gbt_macadgt,
 
81
        gbt_macadge,
 
82
        gbt_macadeq,
 
83
        gbt_macadle,
 
84
        gbt_macadlt,
 
85
        gbt_macadkey_cmp
 
86
};
 
87
 
 
88
 
 
89
/**************************************************
 
90
 * macaddr ops
 
91
 **************************************************/
 
92
 
 
93
 
 
94
 
 
95
static uint64
 
96
mac_2_uint64(macaddr *m)
 
97
{
 
98
        unsigned char *mi = (unsigned char *) m;
 
99
        uint64          res = 0;
 
100
        int                     i;
 
101
 
 
102
        for (i = 0; i < 6; i++)
 
103
                res += (((uint64) mi[i]) << ((uint64) ((5 - i) * 8)));
 
104
        return res;
 
105
}
 
106
 
 
107
 
 
108
 
 
109
Datum
 
110
gbt_macad_compress(PG_FUNCTION_ARGS)
 
111
{
 
112
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
113
        GISTENTRY  *retval = NULL;
 
114
 
 
115
        PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
 
116
}
 
117
 
 
118
 
 
119
Datum
 
120
gbt_macad_consistent(PG_FUNCTION_ARGS)
 
121
{
 
122
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
123
        macaddr    *query = (macaddr *) PG_GETARG_POINTER(1);
 
124
        StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
 
125
        /* Oid          subtype = PG_GETARG_OID(3); */
 
126
        bool       *recheck = (bool *) PG_GETARG_POINTER(4);
 
127
        macKEY     *kkk = (macKEY *) DatumGetPointer(entry->key);
 
128
        GBT_NUMKEY_R key;
 
129
 
 
130
        /* All cases served by this function are exact */
 
131
        *recheck = false;
 
132
 
 
133
        key.lower = (GBT_NUMKEY *) & kkk->lower;
 
134
        key.upper = (GBT_NUMKEY *) & kkk->upper;
 
135
 
 
136
        PG_RETURN_BOOL(
 
137
                                   gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
 
138
                );
 
139
}
 
140
 
 
141
 
 
142
Datum
 
143
gbt_macad_union(PG_FUNCTION_ARGS)
 
144
{
 
145
        GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
 
146
        void       *out = palloc(sizeof(macKEY));
 
147
 
 
148
        *(int *) PG_GETARG_POINTER(1) = sizeof(macKEY);
 
149
        PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
 
150
}
 
151
 
 
152
 
 
153
Datum
 
154
gbt_macad_penalty(PG_FUNCTION_ARGS)
 
155
{
 
156
        macKEY     *origentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
 
157
        macKEY     *newentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
 
158
        float      *result = (float *) PG_GETARG_POINTER(2);
 
159
        uint64          iorg[2],
 
160
                                inew[2];
 
161
 
 
162
        iorg[0] = mac_2_uint64(&origentry->lower);
 
163
        iorg[1] = mac_2_uint64(&origentry->upper);
 
164
        inew[0] = mac_2_uint64(&newentry->lower);
 
165
        inew[1] = mac_2_uint64(&newentry->upper);
 
166
 
 
167
        penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
 
168
 
 
169
        PG_RETURN_POINTER(result);
 
170
 
 
171
}
 
172
 
 
173
Datum
 
174
gbt_macad_picksplit(PG_FUNCTION_ARGS)
 
175
{
 
176
        PG_RETURN_POINTER(gbt_num_picksplit(
 
177
                                                                        (GistEntryVector *) PG_GETARG_POINTER(0),
 
178
                                                                          (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
 
179
                                                                                &tinfo
 
180
                                                                                ));
 
181
}
 
182
 
 
183
Datum
 
184
gbt_macad_same(PG_FUNCTION_ARGS)
 
185
{
 
186
        macKEY     *b1 = (macKEY *) PG_GETARG_POINTER(0);
 
187
        macKEY     *b2 = (macKEY *) PG_GETARG_POINTER(1);
 
188
        bool       *result = (bool *) PG_GETARG_POINTER(2);
 
189
 
 
190
        *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
 
191
        PG_RETURN_POINTER(result);
 
192
}