Fix minor nbtree page deletion buffer lock issue.
[pgsql.git] / contrib / btree_gist / btree_macaddr8.c
blobab4bca5d50d84766caabefa9cb6a48059929fdd9
1 /*
2 * contrib/btree_gist/btree_macaddr8.c
3 */
4 #include "postgres.h"
6 #include "btree_gist.h"
7 #include "btree_utils_num.h"
8 #include "utils/builtins.h"
9 #include "utils/inet.h"
11 typedef struct
13 macaddr8 lower;
14 macaddr8 upper;
15 /* make struct size = sizeof(gbtreekey16) */
16 } mac8KEY;
19 ** OID ops
21 PG_FUNCTION_INFO_V1(gbt_macad8_compress);
22 PG_FUNCTION_INFO_V1(gbt_macad8_fetch);
23 PG_FUNCTION_INFO_V1(gbt_macad8_union);
24 PG_FUNCTION_INFO_V1(gbt_macad8_picksplit);
25 PG_FUNCTION_INFO_V1(gbt_macad8_consistent);
26 PG_FUNCTION_INFO_V1(gbt_macad8_penalty);
27 PG_FUNCTION_INFO_V1(gbt_macad8_same);
30 static bool
31 gbt_macad8gt(const void *a, const void *b, FmgrInfo *flinfo)
33 return DatumGetBool(DirectFunctionCall2(macaddr8_gt, PointerGetDatum(a), PointerGetDatum(b)));
35 static bool
36 gbt_macad8ge(const void *a, const void *b, FmgrInfo *flinfo)
38 return DatumGetBool(DirectFunctionCall2(macaddr8_ge, PointerGetDatum(a), PointerGetDatum(b)));
41 static bool
42 gbt_macad8eq(const void *a, const void *b, FmgrInfo *flinfo)
44 return DatumGetBool(DirectFunctionCall2(macaddr8_eq, PointerGetDatum(a), PointerGetDatum(b)));
47 static bool
48 gbt_macad8le(const void *a, const void *b, FmgrInfo *flinfo)
50 return DatumGetBool(DirectFunctionCall2(macaddr8_le, PointerGetDatum(a), PointerGetDatum(b)));
53 static bool
54 gbt_macad8lt(const void *a, const void *b, FmgrInfo *flinfo)
56 return DatumGetBool(DirectFunctionCall2(macaddr8_lt, PointerGetDatum(a), PointerGetDatum(b)));
60 static int
61 gbt_macad8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
63 mac8KEY *ia = (mac8KEY *) (((const Nsrt *) a)->t);
64 mac8KEY *ib = (mac8KEY *) (((const Nsrt *) b)->t);
65 int res;
67 res = DatumGetInt32(DirectFunctionCall2(macaddr8_cmp, Macaddr8PGetDatum(&ia->lower), Macaddr8PGetDatum(&ib->lower)));
68 if (res == 0)
69 return DatumGetInt32(DirectFunctionCall2(macaddr8_cmp, Macaddr8PGetDatum(&ia->upper), Macaddr8PGetDatum(&ib->upper)));
71 return res;
75 static const gbtree_ninfo tinfo =
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
90 /**************************************************
91 * macaddr ops
92 **************************************************/
96 static uint64
97 mac8_2_uint64(macaddr8 *m)
99 unsigned char *mi = (unsigned char *) m;
100 uint64 res = 0;
101 int i;
103 for (i = 0; i < 8; i++)
104 res += (((uint64) mi[i]) << ((uint64) ((7 - i) * 8)));
105 return res;
110 Datum
111 gbt_macad8_compress(PG_FUNCTION_ARGS)
113 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
115 PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
118 Datum
119 gbt_macad8_fetch(PG_FUNCTION_ARGS)
121 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
123 PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
126 Datum
127 gbt_macad8_consistent(PG_FUNCTION_ARGS)
129 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
130 macaddr8 *query = (macaddr8 *) PG_GETARG_POINTER(1);
131 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
133 /* Oid subtype = PG_GETARG_OID(3); */
134 bool *recheck = (bool *) PG_GETARG_POINTER(4);
135 mac8KEY *kkk = (mac8KEY *) DatumGetPointer(entry->key);
136 GBT_NUMKEY_R key;
138 /* All cases served by this function are exact */
139 *recheck = false;
141 key.lower = (GBT_NUMKEY *) &kkk->lower;
142 key.upper = (GBT_NUMKEY *) &kkk->upper;
144 PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) query, &strategy,
145 GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
149 Datum
150 gbt_macad8_union(PG_FUNCTION_ARGS)
152 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
153 void *out = palloc0(sizeof(mac8KEY));
155 *(int *) PG_GETARG_POINTER(1) = sizeof(mac8KEY);
156 PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
160 Datum
161 gbt_macad8_penalty(PG_FUNCTION_ARGS)
163 mac8KEY *origentry = (mac8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
164 mac8KEY *newentry = (mac8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
165 float *result = (float *) PG_GETARG_POINTER(2);
166 uint64 iorg[2],
167 inew[2];
169 iorg[0] = mac8_2_uint64(&origentry->lower);
170 iorg[1] = mac8_2_uint64(&origentry->upper);
171 inew[0] = mac8_2_uint64(&newentry->lower);
172 inew[1] = mac8_2_uint64(&newentry->upper);
174 penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
176 PG_RETURN_POINTER(result);
180 Datum
181 gbt_macad8_picksplit(PG_FUNCTION_ARGS)
183 PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
184 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
185 &tinfo, fcinfo->flinfo));
188 Datum
189 gbt_macad8_same(PG_FUNCTION_ARGS)
191 mac8KEY *b1 = (mac8KEY *) PG_GETARG_POINTER(0);
192 mac8KEY *b2 = (mac8KEY *) PG_GETARG_POINTER(1);
193 bool *result = (bool *) PG_GETARG_POINTER(2);
195 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
196 PG_RETURN_POINTER(result);