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