PostgreSQL Source Code  git master
btree_macaddr.c
Go to the documentation of this file.
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 
11 typedef struct
12 {
15  char pad[4]; /* make struct size = sizeof(gbtreekey16) */
16 } macKEY;
17 
18 /*
19 ** OID ops
20 */
28 
29 
30 static bool
31 gbt_macadgt(const void *a, const void *b, FmgrInfo *flinfo)
32 {
34 }
35 static bool
36 gbt_macadge(const void *a, const void *b, FmgrInfo *flinfo)
37 {
39 }
40 
41 static bool
42 gbt_macadeq(const void *a, const void *b, FmgrInfo *flinfo)
43 {
45 }
46 
47 static bool
48 gbt_macadle(const void *a, const void *b, FmgrInfo *flinfo)
49 {
51 }
52 
53 static bool
54 gbt_macadlt(const void *a, const void *b, FmgrInfo *flinfo)
55 {
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 
68  if (res == 0)
70 
71  return res;
72 }
73 
74 
75 static const gbtree_ninfo tinfo =
76 {
78  sizeof(macaddr),
79  16, /* sizeof(gbtreekey16) */
86  NULL
87 };
88 
89 
90 /**************************************************
91  * macaddr ops
92  **************************************************/
93 
94 
95 
96 static uint64
98 {
99  unsigned char *mi = (unsigned char *) m;
100  uint64 res = 0;
101  int i;
102 
103  for (i = 0; i < 6; i++)
104  res += (((uint64) mi[i]) << ((uint64) ((5 - i) * 8)));
105  return res;
106 }
107 
108 
109 
110 Datum
112 {
113  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
114 
116 }
117 
118 Datum
120 {
121  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
122 
124 }
125 
126 Datum
128 {
129  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
130  macaddr *query = (macaddr *) PG_GETARG_POINTER(1);
132 
133  /* Oid subtype = PG_GETARG_OID(3); */
134  bool *recheck = (bool *) PG_GETARG_POINTER(4);
135  macKEY *kkk = (macKEY *) DatumGetPointer(entry->key);
137 
138  /* All cases served by this function are exact */
139  *recheck = false;
140 
141  key.lower = (GBT_NUMKEY *) &kkk->lower;
142  key.upper = (GBT_NUMKEY *) &kkk->upper;
143 
144  PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
145  GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
146 }
147 
148 
149 Datum
151 {
153  void *out = palloc0(sizeof(macKEY));
154 
155  *(int *) PG_GETARG_POINTER(1) = sizeof(macKEY);
156  PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
157 }
158 
159 
160 Datum
162 {
163  macKEY *origentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
164  macKEY *newentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
165  float *result = (float *) PG_GETARG_POINTER(2);
166  uint64 iorg[2],
167  inew[2];
168 
169  iorg[0] = mac_2_uint64(&origentry->lower);
170  iorg[1] = mac_2_uint64(&origentry->upper);
171  inew[0] = mac_2_uint64(&newentry->lower);
172  inew[1] = mac_2_uint64(&newentry->upper);
173 
174  penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
175 
176  PG_RETURN_POINTER(result);
177 }
178 
179 Datum
181 {
184  &tinfo, fcinfo->flinfo));
185 }
186 
187 Datum
189 {
190  macKEY *b1 = (macKEY *) PG_GETARG_POINTER(0);
191  macKEY *b2 = (macKEY *) PG_GETARG_POINTER(1);
192  bool *result = (bool *) PG_GETARG_POINTER(2);
193 
194  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
195  PG_RETURN_POINTER(result);
196 }
@ gbt_t_macad
Definition: btree_gist.h:29
static bool gbt_macadle(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_macaddr.c:48
Datum gbt_macad_fetch(PG_FUNCTION_ARGS)
Datum gbt_macad_consistent(PG_FUNCTION_ARGS)
static uint64 mac_2_uint64(macaddr *m)
Definition: btree_macaddr.c:97
static int gbt_macadkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_macaddr.c:61
static bool gbt_macadlt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_macaddr.c:54
Datum gbt_macad_union(PG_FUNCTION_ARGS)
Datum gbt_macad_picksplit(PG_FUNCTION_ARGS)
static bool gbt_macadeq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_macaddr.c:42
static const gbtree_ninfo tinfo
Definition: btree_macaddr.c:75
Datum gbt_macad_penalty(PG_FUNCTION_ARGS)
Datum gbt_macad_compress(PG_FUNCTION_ARGS)
Datum gbt_macad_same(PG_FUNCTION_ARGS)
static bool gbt_macadge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_macaddr.c:36
PG_FUNCTION_INFO_V1(gbt_macad_compress)
static bool gbt_macadgt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_macaddr.c:31
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define penalty_num(result, olower, oupper, nlower, nupper)
char GBT_NUMKEY
uint64_t uint64
Definition: c.h:486
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:170
int b
Definition: isn.c:69
int a
Definition: isn.c:68
int i
Definition: isn.c:72
Datum macaddr_lt(PG_FUNCTION_ARGS)
Definition: mac.c:210
Datum macaddr_cmp(PG_FUNCTION_ARGS)
Definition: mac.c:197
Datum macaddr_eq(PG_FUNCTION_ARGS)
Definition: mac.c:228
Datum macaddr_ge(PG_FUNCTION_ARGS)
Definition: mac.c:237
Datum macaddr_le(PG_FUNCTION_ARGS)
Definition: mac.c:219
Datum macaddr_gt(PG_FUNCTION_ARGS)
Definition: mac.c:246
void * palloc0(Size size)
Definition: mcxt.c:1347
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
Datum key
Definition: gist.h:160
macaddr upper
Definition: btree_macaddr.c:14
macaddr lower
Definition: btree_macaddr.c:13
Definition: inet.h:95
static Datum MacaddrPGetDatum(const macaddr *X)
Definition: inet.h:153
struct macaddr macaddr