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