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