Age Owner TLA 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/inet.h"
11 :
12 : typedef struct inetkey
13 : {
14 : double lower;
15 : double upper;
16 : } inetKEY;
17 :
18 : /*
19 : ** inet ops
20 : */
6890 teodor 21 CBC 3 : PG_FUNCTION_INFO_V1(gbt_inet_compress);
22 3 : PG_FUNCTION_INFO_V1(gbt_inet_union);
23 3 : PG_FUNCTION_INFO_V1(gbt_inet_picksplit);
24 3 : PG_FUNCTION_INFO_V1(gbt_inet_consistent);
25 3 : PG_FUNCTION_INFO_V1(gbt_inet_penalty);
26 3 : PG_FUNCTION_INFO_V1(gbt_inet_same);
27 :
28 :
29 : static bool
2210 andrew 30 5195 : gbt_inetgt(const void *a, const void *b, FmgrInfo *flinfo)
31 : {
4228 peter_e 32 5195 : return (*((const double *) a) > *((const double *) b));
33 : }
34 : static bool
2210 andrew 35 642 : gbt_inetge(const void *a, const void *b, FmgrInfo *flinfo)
36 : {
4228 peter_e 37 642 : return (*((const double *) a) >= *((const double *) b));
38 : }
39 : static bool
2210 andrew 40 2836 : gbt_ineteq(const void *a, const void *b, FmgrInfo *flinfo)
41 : {
4228 peter_e 42 2836 : return (*((const double *) a) == *((const double *) b));
43 : }
44 : static bool
2210 andrew 45 910 : gbt_inetle(const void *a, const void *b, FmgrInfo *flinfo)
46 : {
4228 peter_e 47 910 : return (*((const double *) a) <= *((const double *) b));
48 : }
49 : static bool
2210 andrew 50 5439 : gbt_inetlt(const void *a, const void *b, FmgrInfo *flinfo)
51 : {
4228 peter_e 52 5439 : return (*((const double *) a) < *((const double *) b));
53 : }
54 :
55 : static int
2210 andrew 56 18958 : gbt_inetkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57 : {
4228 peter_e 58 18958 : inetKEY *ia = (inetKEY *) (((const Nsrt *) a)->t);
59 18958 : inetKEY *ib = (inetKEY *) (((const Nsrt *) b)->t);
60 :
4876 teodor 61 18958 : if (ia->lower == ib->lower)
62 : {
4876 teodor 63 UBC 0 : if (ia->upper == ib->upper)
64 0 : return 0;
65 :
66 0 : return (ia->upper > ib->upper) ? 1 : -1;
67 : }
68 :
4876 teodor 69 CBC 18958 : 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 : * inet ops
90 : **************************************************/
91 :
92 :
93 : Datum
6282 tgl 94 1833 : gbt_inet_compress(PG_FUNCTION_ARGS)
95 : {
96 1833 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
97 : GISTENTRY *retval;
98 :
6797 bruce 99 1833 : if (entry->leafkey)
100 : {
101 1800 : inetKEY *r = (inetKEY *) palloc(sizeof(inetKEY));
1863 tgl 102 1800 : bool failure = false;
103 :
6797 bruce 104 1800 : retval = palloc(sizeof(GISTENTRY));
1863 tgl 105 1800 : r->lower = convert_network_to_scalar(entry->key, INETOID, &failure);
106 1800 : Assert(!failure);
6797 bruce 107 1800 : r->upper = r->lower;
108 1800 : gistentryinit(*retval, PointerGetDatum(r),
109 : entry->rel, entry->page,
110 : entry->offset, false);
111 : }
112 : else
113 33 : retval = entry;
114 :
6282 tgl 115 1833 : PG_RETURN_POINTER(retval);
116 : }
117 :
118 :
119 : Datum
120 3640 : gbt_inet_consistent(PG_FUNCTION_ARGS)
121 : {
6797 bruce 122 3640 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
1863 tgl 123 3640 : Datum dquery = PG_GETARG_DATUM(1);
6282 124 3640 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
125 :
126 : /* Oid subtype = PG_GETARG_OID(3); */
5473 127 3640 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
6797 bruce 128 3640 : inetKEY *kkk = (inetKEY *) DatumGetPointer(entry->key);
129 : GBT_NUMKEY_R key;
130 : double query;
1863 tgl 131 3640 : bool failure = false;
132 :
133 3640 : query = convert_network_to_scalar(dquery, INETOID, &failure);
134 3640 : Assert(!failure);
135 :
136 : /* All cases served by this function are inexact */
5473 137 3640 : *recheck = true;
138 :
5050 bruce 139 3640 : key.lower = (GBT_NUMKEY *) &kkk->lower;
140 3640 : key.upper = (GBT_NUMKEY *) &kkk->upper;
141 :
6282 tgl 142 3640 : PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query,
143 : &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
144 : }
145 :
146 :
147 : Datum
6890 teodor 148 1129 : gbt_inet_union(PG_FUNCTION_ARGS)
149 : {
6797 bruce 150 1129 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
151 1129 : void *out = palloc(sizeof(inetKEY));
152 :
153 1129 : *(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY);
2210 andrew 154 1129 : PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
155 : }
156 :
157 :
158 : Datum
6890 teodor 159 3181 : gbt_inet_penalty(PG_FUNCTION_ARGS)
160 : {
6797 bruce 161 3181 : inetKEY *origentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
162 3181 : inetKEY *newentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
163 3181 : float *result = (float *) PG_GETARG_POINTER(2);
164 :
6385 165 3181 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
166 :
6797 167 3181 : PG_RETURN_POINTER(result);
168 : }
169 :
170 : Datum
6890 teodor 171 9 : gbt_inet_picksplit(PG_FUNCTION_ARGS)
172 : {
1165 alvherre 173 9 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
174 : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
175 : &tinfo, fcinfo->flinfo));
176 : }
177 :
178 : Datum
6890 teodor 179 1123 : gbt_inet_same(PG_FUNCTION_ARGS)
180 : {
6797 bruce 181 1123 : inetKEY *b1 = (inetKEY *) PG_GETARG_POINTER(0);
182 1123 : inetKEY *b2 = (inetKEY *) PG_GETARG_POINTER(1);
183 1123 : bool *result = (bool *) PG_GETARG_POINTER(2);
184 :
2210 andrew 185 1123 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
6797 bruce 186 1123 : PG_RETURN_POINTER(result);
187 : }
|