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