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