Debian Codegraph @

contrib/btree_gist/btree_inet.c

raw
1 /*
2 * contrib/btree_gist/btree_inet.c
3 */
4 #include "postgres.h"
5
6 #include "btree_gist.h"
7 #include "btree_utils_num.h"
8 #include "catalog/pg_type.h"
9 #include "utils/builtins.h"
10 #include "utils/sortsupport.h"
11
12 typedef struct inetkey
13 {
14 double lower;
15 double upper;
16 } inetKEY;
17
18 /* GiST support functions */
19 PG_FUNCTION_INFO_V1(gbt_inet_compress);
20 PG_FUNCTION_INFO_V1(gbt_inet_union);
21 PG_FUNCTION_INFO_V1(gbt_inet_picksplit);
22 PG_FUNCTION_INFO_V1(gbt_inet_consistent);
23 PG_FUNCTION_INFO_V1(gbt_inet_penalty);
24 PG_FUNCTION_INFO_V1(gbt_inet_same);
25 PG_FUNCTION_INFO_V1(gbt_inet_sortsupport);
26
27
28 static bool
29 gbt_inetgt(const void *a, const void *b, FmgrInfo *flinfo)
30 {
31 return (*((const double *) a) > *((const double *) b));
32 }
33 static bool
34 gbt_inetge(const void *a, const void *b, FmgrInfo *flinfo)
35 {
36 return (*((const double *) a) >= *((const double *) b));
37 }
38 static bool
39 gbt_ineteq(const void *a, const void *b, FmgrInfo *flinfo)
40 {
41 return (*((const double *) a) == *((const double *) b));
42 }
43 static bool
44 gbt_inetle(const void *a, const void *b, FmgrInfo *flinfo)
45 {
46 return (*((const double *) a) <= *((const double *) b));
47 }
48 static bool
49 gbt_inetlt(const void *a, const void *b, FmgrInfo *flinfo)
50 {
51 return (*((const double *) a) < *((const double *) b));
52 }
53
54 static int
55 gbt_inetkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56 {
57 inetKEY *ia = (inetKEY *) (((const Nsrt *) a)->t);
58 inetKEY *ib = (inetKEY *) (((const Nsrt *) b)->t);
59
60 if (ia->lower == ib->lower)
61 {
62 if (ia->upper == ib->upper)
63 return 0;
64
65 return (ia->upper > ib->upper) ? 1 : -1;
66 }
67
68 return (ia->lower > ib->lower) ? 1 : -1;
69 }
70
71
72 static const gbtree_ninfo tinfo =
73 {
74 gbt_t_inet,
75 sizeof(double),
76 16, /* sizeof(gbtreekey16) */
77 gbt_inetgt,
78 gbt_inetge,
79 gbt_ineteq,
80 gbt_inetle,
81 gbt_inetlt,
82 gbt_inetkey_cmp,
83 NULL
84 };
85
86
87 /**************************************************
88 * GiST support functions
89 **************************************************/
90
91 Datum
92 gbt_inet_compress(PG_FUNCTION_ARGS)
93 {
94 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
95 GISTENTRY *retval;
96
97 if (entry->leafkey)
98 {
99 inetKEY *r = (inetKEY *) palloc(sizeof(inetKEY));
100 bool failure = false;
101
102 retval = palloc(sizeof(GISTENTRY));
103 r->lower = convert_network_to_scalar(entry->key, INETOID, &failure);
104 Assert(!failure);
105 r->upper = r->lower;
106 gistentryinit(*retval, PointerGetDatum(r),
107 entry->rel, entry->page,
108 entry->offset, false);
109 }
110 else
111 retval = entry;
112
113 PG_RETURN_POINTER(retval);
114 }
115
116 Datum
117 gbt_inet_consistent(PG_FUNCTION_ARGS)
118 {
119 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
120 Datum dquery = PG_GETARG_DATUM(1);
121 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
122
123 /* Oid subtype = PG_GETARG_OID(3); */
124 bool *recheck = (bool *) PG_GETARG_POINTER(4);
125 inetKEY *kkk = (inetKEY *) DatumGetPointer(entry->key);
126 GBT_NUMKEY_R key;
127 double query;
128 bool failure = false;
129
130 query = convert_network_to_scalar(dquery, INETOID, &failure);
131 Assert(!failure);
132
133 /* All cases served by this function are inexact */
134 *recheck = true;
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,
140 &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
141 }
142
143 Datum
144 gbt_inet_union(PG_FUNCTION_ARGS)
145 {
146 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
147 void *out = palloc(sizeof(inetKEY));
148
149 *(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY);
150 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
151 }
152
153 Datum
154 gbt_inet_penalty(PG_FUNCTION_ARGS)
155 {
156 inetKEY *origentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
157 inetKEY *newentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
158 float *result = (float *) PG_GETARG_POINTER(2);
159
160 penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
161
162 PG_RETURN_POINTER(result);
163 }
164
165 Datum
166 gbt_inet_picksplit(PG_FUNCTION_ARGS)
167 {
168 PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
169 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
170 &tinfo, fcinfo->flinfo));
171 }
172
173 Datum
174 gbt_inet_same(PG_FUNCTION_ARGS)
175 {
176 inetKEY *b1 = (inetKEY *) PG_GETARG_POINTER(0);
177 inetKEY *b2 = (inetKEY *) PG_GETARG_POINTER(1);
178 bool *result = (bool *) PG_GETARG_POINTER(2);
179
180 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
181 PG_RETURN_POINTER(result);
182 }
183
184 static int
185 gbt_inet_ssup_cmp(Datum x, Datum y, SortSupport ssup)
186 {
187 inetKEY *arg1 = (inetKEY *) DatumGetPointer(x);
188 inetKEY *arg2 = (inetKEY *) DatumGetPointer(y);
189
190 /* for leaf items we expect lower == upper, so only compare lower */
191 if (arg1->lower < arg2->lower)
192 return -1;
193 else if (arg1->lower > arg2->lower)
194 return 1;
195 else
196 return 0;
197 }
198
199 Datum
200 gbt_inet_sortsupport(PG_FUNCTION_ARGS)
201 {
202 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
203
204 ssup->comparator = gbt_inet_ssup_cmp;
205 ssup->ssup_extra = NULL;
206
207 PG_RETURN_VOID();
208 }