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