Age Owner Branch data TLA 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 */
7771 teodor@sigaev.ru 21 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_macad_compress);
3816 heikki.linnakangas@i 22 : 4 : PG_FUNCTION_INFO_V1(gbt_macad_fetch);
7771 teodor@sigaev.ru 23 : 4 : PG_FUNCTION_INFO_V1(gbt_macad_union);
24 : 4 : PG_FUNCTION_INFO_V1(gbt_macad_picksplit);
25 : 4 : PG_FUNCTION_INFO_V1(gbt_macad_consistent);
26 : 4 : PG_FUNCTION_INFO_V1(gbt_macad_penalty);
27 : 4 : PG_FUNCTION_INFO_V1(gbt_macad_same);
156 heikki.linnakangas@i 28 : 4 : PG_FUNCTION_INFO_V1(gbt_macaddr_sortsupport);
29 : :
30 : :
31 : : static bool
3091 andrew@dunslane.net 32 : 2093 : gbt_macadgt(const void *a, const void *b, FmgrInfo *flinfo)
33 : : {
7678 bruce@momjian.us 34 : 2093 : return DatumGetBool(DirectFunctionCall2(macaddr_gt, PointerGetDatum(a), PointerGetDatum(b)));
35 : : }
36 : : static bool
3091 andrew@dunslane.net 37 : 162 : gbt_macadge(const void *a, const void *b, FmgrInfo *flinfo)
38 : : {
7678 bruce@momjian.us 39 : 162 : return DatumGetBool(DirectFunctionCall2(macaddr_ge, PointerGetDatum(a), PointerGetDatum(b)));
40 : : }
41 : :
42 : : static bool
3091 andrew@dunslane.net 43 : 150 : gbt_macadeq(const void *a, const void *b, FmgrInfo *flinfo)
44 : : {
7678 bruce@momjian.us 45 : 150 : return DatumGetBool(DirectFunctionCall2(macaddr_eq, PointerGetDatum(a), PointerGetDatum(b)));
46 : : }
47 : :
48 : : static bool
3091 andrew@dunslane.net 49 : 613 : gbt_macadle(const void *a, const void *b, FmgrInfo *flinfo)
50 : : {
7678 bruce@momjian.us 51 : 613 : return DatumGetBool(DirectFunctionCall2(macaddr_le, PointerGetDatum(a), PointerGetDatum(b)));
52 : : }
53 : :
54 : : static bool
3091 andrew@dunslane.net 55 : 2393 : gbt_macadlt(const void *a, const void *b, FmgrInfo *flinfo)
56 : : {
7678 bruce@momjian.us 57 : 2393 : return DatumGetBool(DirectFunctionCall2(macaddr_lt, PointerGetDatum(a), PointerGetDatum(b)));
58 : : }
59 : :
60 : :
61 : : static int
3091 andrew@dunslane.net 62 : 1197 : gbt_macadkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
63 : : {
5109 peter_e@gmx.net 64 : 1197 : macKEY *ia = (macKEY *) (((const Nsrt *) a)->t);
65 : 1197 : macKEY *ib = (macKEY *) (((const Nsrt *) b)->t);
66 : : int res;
67 : :
5757 teodor@sigaev.ru 68 : 1197 : res = DatumGetInt32(DirectFunctionCall2(macaddr_cmp, MacaddrPGetDatum(&ia->lower), MacaddrPGetDatum(&ib->lower)));
69 [ + + ]: 1197 : if (res == 0)
70 : 900 : return DatumGetInt32(DirectFunctionCall2(macaddr_cmp, MacaddrPGetDatum(&ia->upper), MacaddrPGetDatum(&ib->upper)));
71 : :
72 : 297 : return res;
73 : : }
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
7678 bruce@momjian.us 96 :UBC 0 : mac_2_uint64(macaddr *m)
97 : : {
98 : 0 : unsigned char *mi = (unsigned char *) m;
99 : 0 : uint64 res = 0;
100 : : 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 : : }
106 : :
107 : : Datum
7771 teodor@sigaev.ru 108 :CBC 604 : gbt_macad_compress(PG_FUNCTION_ARGS)
109 : : {
7678 bruce@momjian.us 110 : 604 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
111 : :
3817 heikki.linnakangas@i 112 : 604 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
113 : : }
114 : :
115 : : Datum
3816 116 : 8 : gbt_macad_fetch(PG_FUNCTION_ARGS)
117 : : {
118 : 8 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
119 : :
120 : 8 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
121 : : }
122 : :
123 : : Datum
7771 teodor@sigaev.ru 124 : 1824 : gbt_macad_consistent(PG_FUNCTION_ARGS)
125 : : {
7678 bruce@momjian.us 126 : 1824 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
127 : 1824 : macaddr *query = (macaddr *) PG_GETARG_POINTER(1);
6354 tgl@sss.pgh.pa.us 128 : 1824 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
129 : :
130 : : /* Oid subtype = PG_GETARG_OID(3); */
131 : 1824 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
7678 bruce@momjian.us 132 : 1824 : macKEY *kkk = (macKEY *) DatumGetPointer(entry->key);
133 : : GBT_NUMKEY_R key;
134 : :
135 : : /* All cases served by this function are exact */
6354 tgl@sss.pgh.pa.us 136 : 1824 : *recheck = false;
137 : :
5931 bruce@momjian.us 138 : 1824 : key.lower = (GBT_NUMKEY *) &kkk->lower;
139 : 1824 : key.upper = (GBT_NUMKEY *) &kkk->upper;
140 : :
282 peter@eisentraut.org 141 : 1824 : PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
142 : : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
143 : : }
144 : :
145 : :
146 : : Datum
7771 teodor@sigaev.ru 147 : 1 : gbt_macad_union(PG_FUNCTION_ARGS)
148 : : {
7678 bruce@momjian.us 149 : 1 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
4131 tgl@sss.pgh.pa.us 150 : 1 : void *out = palloc0(sizeof(macKEY));
151 : :
7678 bruce@momjian.us 152 : 1 : *(int *) PG_GETARG_POINTER(1) = sizeof(macKEY);
282 peter@eisentraut.org 153 : 1 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
154 : : }
155 : :
156 : :
157 : : Datum
7771 teodor@sigaev.ru 158 :UBC 0 : gbt_macad_penalty(PG_FUNCTION_ARGS)
159 : : {
7678 bruce@momjian.us 160 : 0 : macKEY *origentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
161 : 0 : macKEY *newentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
162 : 0 : float *result = (float *) PG_GETARG_POINTER(2);
163 : : uint64 iorg[2],
164 : : inew[2];
165 : :
166 : 0 : iorg[0] = mac_2_uint64(&origentry->lower);
167 : 0 : iorg[1] = mac_2_uint64(&origentry->upper);
168 : 0 : inew[0] = mac_2_uint64(&newentry->lower);
169 : 0 : inew[1] = mac_2_uint64(&newentry->upper);
170 : :
7266 171 [ # # # # : 0 : penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
# # ]
172 : :
7678 173 : 0 : PG_RETURN_POINTER(result);
174 : : }
175 : :
176 : : Datum
7771 teodor@sigaev.ru 177 :CBC 3 : gbt_macad_picksplit(PG_FUNCTION_ARGS)
178 : : {
2046 alvherre@alvh.no-ip. 179 : 3 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
180 : : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
181 : : &tinfo, fcinfo->flinfo));
182 : : }
183 : :
184 : : Datum
7771 teodor@sigaev.ru 185 :UBC 0 : gbt_macad_same(PG_FUNCTION_ARGS)
186 : : {
7678 bruce@momjian.us 187 : 0 : macKEY *b1 = (macKEY *) PG_GETARG_POINTER(0);
188 : 0 : macKEY *b2 = (macKEY *) PG_GETARG_POINTER(1);
189 : 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
190 : :
3091 andrew@dunslane.net 191 : 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
7678 bruce@momjian.us 192 : 0 : PG_RETURN_POINTER(result);
193 : : }
194 : :
195 : : static int
156 heikki.linnakangas@i 196 :CBC 5789 : gbt_macaddr_ssup_cmp(Datum x, Datum y, SortSupport ssup)
197 : : {
198 : 5789 : macKEY *arg1 = (macKEY *) DatumGetPointer(x);
199 : 5789 : macKEY *arg2 = (macKEY *) DatumGetPointer(y);
200 : :
201 : : /* for leaf items we expect lower == upper, so only compare lower */
202 : 5789 : return DatumGetInt32(DirectFunctionCall2(macaddr_cmp,
203 : : MacaddrPGetDatum(&arg1->lower),
204 : : MacaddrPGetDatum(&arg2->lower)));
205 : : }
206 : :
207 : : Datum
208 : 1 : gbt_macaddr_sortsupport(PG_FUNCTION_ARGS)
209 : : {
210 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
211 : :
212 : 1 : ssup->comparator = gbt_macaddr_ssup_cmp;
213 : 1 : ssup->ssup_extra = NULL;
214 : :
215 : 1 : PG_RETURN_VOID();
216 : : }
|