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