Start background writer during archive recovery. Background writer now performs
[PostgreSQL.git] / contrib / btree_gist / btree_macaddr.c
blobd3da94b916e0f80c9b5f3cd9d5bec8a78d1fd7fa
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);
125 /* Oid subtype = PG_GETARG_OID(3); */
126 bool *recheck = (bool *) PG_GETARG_POINTER(4);
127 macKEY *kkk = (macKEY *) DatumGetPointer(entry->key);
128 GBT_NUMKEY_R key;
130 /* All cases served by this function are exact */
131 *recheck = false;
133 key.lower = (GBT_NUMKEY *) & kkk->lower;
134 key.upper = (GBT_NUMKEY *) & kkk->upper;
136 PG_RETURN_BOOL(
137 gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
142 Datum
143 gbt_macad_union(PG_FUNCTION_ARGS)
145 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
146 void *out = palloc(sizeof(macKEY));
148 *(int *) PG_GETARG_POINTER(1) = sizeof(macKEY);
149 PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
153 Datum
154 gbt_macad_penalty(PG_FUNCTION_ARGS)
156 macKEY *origentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
157 macKEY *newentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
158 float *result = (float *) PG_GETARG_POINTER(2);
159 uint64 iorg[2],
160 inew[2];
162 iorg[0] = mac_2_uint64(&origentry->lower);
163 iorg[1] = mac_2_uint64(&origentry->upper);
164 inew[0] = mac_2_uint64(&newentry->lower);
165 inew[1] = mac_2_uint64(&newentry->upper);
167 penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
169 PG_RETURN_POINTER(result);
173 Datum
174 gbt_macad_picksplit(PG_FUNCTION_ARGS)
176 PG_RETURN_POINTER(gbt_num_picksplit(
177 (GistEntryVector *) PG_GETARG_POINTER(0),
178 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
179 &tinfo
183 Datum
184 gbt_macad_same(PG_FUNCTION_ARGS)
186 macKEY *b1 = (macKEY *) PG_GETARG_POINTER(0);
187 macKEY *b2 = (macKEY *) PG_GETARG_POINTER(1);
188 bool *result = (bool *) PG_GETARG_POINTER(2);
190 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
191 PG_RETURN_POINTER(result);