Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / contrib / btree_gist / btree_macaddr.c
blob40dd41b530df8bd3397ade35b4cbe183a809ae6a
1 /*
2 * $PostgreSQL$
3 */
4 #include "btree_gist.h"
5 #include "btree_utils_num.h"
6 #include "utils/builtins.h"
7 #include "utils/inet.h"
9 typedef struct
11 macaddr lower;
12 macaddr upper;
13 } macKEY;
16 ** OID ops
18 PG_FUNCTION_INFO_V1(gbt_macad_compress);
19 PG_FUNCTION_INFO_V1(gbt_macad_union);
20 PG_FUNCTION_INFO_V1(gbt_macad_picksplit);
21 PG_FUNCTION_INFO_V1(gbt_macad_consistent);
22 PG_FUNCTION_INFO_V1(gbt_macad_penalty);
23 PG_FUNCTION_INFO_V1(gbt_macad_same);
25 Datum gbt_macad_compress(PG_FUNCTION_ARGS);
26 Datum gbt_macad_union(PG_FUNCTION_ARGS);
27 Datum gbt_macad_picksplit(PG_FUNCTION_ARGS);
28 Datum gbt_macad_consistent(PG_FUNCTION_ARGS);
29 Datum gbt_macad_penalty(PG_FUNCTION_ARGS);
30 Datum gbt_macad_same(PG_FUNCTION_ARGS);
33 static bool
34 gbt_macadgt(const void *a, const void *b)
36 return DatumGetBool(DirectFunctionCall2(macaddr_gt, PointerGetDatum(a), PointerGetDatum(b)));
38 static bool
39 gbt_macadge(const void *a, const void *b)
41 return DatumGetBool(DirectFunctionCall2(macaddr_ge, PointerGetDatum(a), PointerGetDatum(b)));
44 static bool
45 gbt_macadeq(const void *a, const void *b)
47 return DatumGetBool(DirectFunctionCall2(macaddr_eq, PointerGetDatum(a), PointerGetDatum(b)));
50 static bool
51 gbt_macadle(const void *a, const void *b)
53 return DatumGetBool(DirectFunctionCall2(macaddr_le, PointerGetDatum(a), PointerGetDatum(b)));
56 static bool
57 gbt_macadlt(const void *a, const void *b)
59 return DatumGetBool(DirectFunctionCall2(macaddr_lt, PointerGetDatum(a), PointerGetDatum(b)));
63 static int
64 gbt_macadkey_cmp(const void *a, const void *b)
66 return DatumGetInt32(
67 DirectFunctionCall2(
68 macaddr_cmp,
69 PointerGetDatum(&((Nsrt *) a)->t[0]),
70 PointerGetDatum(&((Nsrt *) b)->t[0])
76 static const gbtree_ninfo tinfo =
78 gbt_t_macad,
79 sizeof(macaddr),
80 gbt_macadgt,
81 gbt_macadge,
82 gbt_macadeq,
83 gbt_macadle,
84 gbt_macadlt,
85 gbt_macadkey_cmp
89 /**************************************************
90 * macaddr ops
91 **************************************************/
95 static uint64
96 mac_2_uint64(macaddr *m)
98 unsigned char *mi = (unsigned char *) m;
99 uint64 res = 0;
100 int i;
102 for (i = 0; i < 6; i++)
103 res += (((uint64) mi[i]) << ((uint64) ((5 - i) * 8)));
104 return res;
109 Datum
110 gbt_macad_compress(PG_FUNCTION_ARGS)
112 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
113 GISTENTRY *retval = NULL;
115 PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
119 Datum
120 gbt_macad_consistent(PG_FUNCTION_ARGS)
122 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
123 macaddr *query = (macaddr *) PG_GETARG_POINTER(1);
124 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
126 /* Oid subtype = PG_GETARG_OID(3); */
127 bool *recheck = (bool *) PG_GETARG_POINTER(4);
128 macKEY *kkk = (macKEY *) DatumGetPointer(entry->key);
129 GBT_NUMKEY_R key;
131 /* All cases served by this function are exact */
132 *recheck = false;
134 key.lower = (GBT_NUMKEY *) &kkk->lower;
135 key.upper = (GBT_NUMKEY *) &kkk->upper;
137 PG_RETURN_BOOL(
138 gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
143 Datum
144 gbt_macad_union(PG_FUNCTION_ARGS)
146 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
147 void *out = palloc(sizeof(macKEY));
149 *(int *) PG_GETARG_POINTER(1) = sizeof(macKEY);
150 PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
154 Datum
155 gbt_macad_penalty(PG_FUNCTION_ARGS)
157 macKEY *origentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
158 macKEY *newentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
159 float *result = (float *) PG_GETARG_POINTER(2);
160 uint64 iorg[2],
161 inew[2];
163 iorg[0] = mac_2_uint64(&origentry->lower);
164 iorg[1] = mac_2_uint64(&origentry->upper);
165 inew[0] = mac_2_uint64(&newentry->lower);
166 inew[1] = mac_2_uint64(&newentry->upper);
168 penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
170 PG_RETURN_POINTER(result);
174 Datum
175 gbt_macad_picksplit(PG_FUNCTION_ARGS)
177 PG_RETURN_POINTER(gbt_num_picksplit(
178 (GistEntryVector *) PG_GETARG_POINTER(0),
179 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
180 &tinfo
184 Datum
185 gbt_macad_same(PG_FUNCTION_ARGS)
187 macKEY *b1 = (macKEY *) PG_GETARG_POINTER(0);
188 macKEY *b2 = (macKEY *) PG_GETARG_POINTER(1);
189 bool *result = (bool *) PG_GETARG_POINTER(2);
191 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
192 PG_RETURN_POINTER(result);