Age Owner Branch data 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/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 */
7771 teodor@sigaev.ru 20 :CBC 5 : PG_FUNCTION_INFO_V1(gbt_inet_compress);
21 : 5 : PG_FUNCTION_INFO_V1(gbt_inet_union);
22 : 5 : PG_FUNCTION_INFO_V1(gbt_inet_picksplit);
23 : 5 : PG_FUNCTION_INFO_V1(gbt_inet_consistent);
24 : 5 : PG_FUNCTION_INFO_V1(gbt_inet_penalty);
25 : 5 : PG_FUNCTION_INFO_V1(gbt_inet_same);
156 heikki.linnakangas@i 26 : 5 : PG_FUNCTION_INFO_V1(gbt_inet_sortsupport);
27 : :
28 : :
29 : : static bool
3091 andrew@dunslane.net 30 : 5997 : gbt_inetgt(const void *a, const void *b, FmgrInfo *flinfo)
31 : : {
5109 peter_e@gmx.net 32 : 5997 : return (*((const double *) a) > *((const double *) b));
33 : : }
34 : : static bool
3091 andrew@dunslane.net 35 : 616 : gbt_inetge(const void *a, const void *b, FmgrInfo *flinfo)
36 : : {
5109 peter_e@gmx.net 37 : 616 : return (*((const double *) a) >= *((const double *) b));
38 : : }
39 : : static bool
3091 andrew@dunslane.net 40 : 1422 : gbt_ineteq(const void *a, const void *b, FmgrInfo *flinfo)
41 : : {
5109 peter_e@gmx.net 42 : 1422 : return (*((const double *) a) == *((const double *) b));
43 : : }
44 : : static bool
3091 andrew@dunslane.net 45 : 940 : gbt_inetle(const void *a, const void *b, FmgrInfo *flinfo)
46 : : {
5109 peter_e@gmx.net 47 : 940 : return (*((const double *) a) <= *((const double *) b));
48 : : }
49 : : static bool
3091 andrew@dunslane.net 50 : 6297 : gbt_inetlt(const void *a, const void *b, FmgrInfo *flinfo)
51 : : {
5109 peter_e@gmx.net 52 : 6297 : return (*((const double *) a) < *((const double *) b));
53 : : }
54 : :
55 : : static int
3091 andrew@dunslane.net 56 : 7408 : gbt_inetkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57 : : {
5109 peter_e@gmx.net 58 : 7408 : inetKEY *ia = (inetKEY *) (((const Nsrt *) a)->t);
59 : 7408 : inetKEY *ib = (inetKEY *) (((const Nsrt *) b)->t);
60 : :
5757 teodor@sigaev.ru 61 [ - + ]: 7408 : if (ia->lower == ib->lower)
62 : : {
5757 teodor@sigaev.ru 63 [ # # ]:UBC 0 : if (ia->upper == ib->upper)
64 : 0 : return 0;
65 : :
66 [ # # ]: 0 : return (ia->upper > ib->upper) ? 1 : -1;
67 : : }
68 : :
5757 teodor@sigaev.ru 69 [ + + ]:CBC 7408 : 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
7163 tgl@sss.pgh.pa.us 93 : 1819 : gbt_inet_compress(PG_FUNCTION_ARGS)
94 : : {
95 : 1819 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
96 : : GISTENTRY *retval;
97 : :
7678 bruce@momjian.us 98 [ + + ]: 1819 : if (entry->leafkey)
99 : : {
100 : 1800 : inetKEY *r = (inetKEY *) palloc(sizeof(inetKEY));
2744 tgl@sss.pgh.pa.us 101 : 1800 : bool failure = false;
102 : :
7678 bruce@momjian.us 103 : 1800 : retval = palloc(sizeof(GISTENTRY));
2744 tgl@sss.pgh.pa.us 104 : 1800 : r->lower = convert_network_to_scalar(entry->key, INETOID, &failure);
105 [ - + ]: 1800 : Assert(!failure);
7678 bruce@momjian.us 106 : 1800 : r->upper = r->lower;
107 : 1800 : gistentryinit(*retval, PointerGetDatum(r),
108 : : entry->rel, entry->page,
109 : : entry->offset, false);
110 : : }
111 : : else
112 : 19 : retval = entry;
113 : :
7163 tgl@sss.pgh.pa.us 114 : 1819 : PG_RETURN_POINTER(retval);
115 : : }
116 : :
117 : : Datum
118 : 3654 : gbt_inet_consistent(PG_FUNCTION_ARGS)
119 : : {
7678 bruce@momjian.us 120 : 3654 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
2744 tgl@sss.pgh.pa.us 121 : 3654 : Datum dquery = PG_GETARG_DATUM(1);
7163 122 : 3654 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
123 : :
124 : : /* Oid subtype = PG_GETARG_OID(3); */
6354 125 : 3654 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
7678 bruce@momjian.us 126 : 3654 : inetKEY *kkk = (inetKEY *) DatumGetPointer(entry->key);
127 : : GBT_NUMKEY_R key;
128 : : double query;
2744 tgl@sss.pgh.pa.us 129 : 3654 : bool failure = false;
130 : :
131 : 3654 : query = convert_network_to_scalar(dquery, INETOID, &failure);
132 [ - + ]: 3654 : Assert(!failure);
133 : :
134 : : /* All cases served by this function are inexact */
6354 135 : 3654 : *recheck = true;
136 : :
5931 bruce@momjian.us 137 : 3654 : key.lower = (GBT_NUMKEY *) &kkk->lower;
138 : 3654 : key.upper = (GBT_NUMKEY *) &kkk->upper;
139 : :
282 peter@eisentraut.org 140 : 3654 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query,
141 : : &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
142 : : }
143 : :
144 : : Datum
7771 teodor@sigaev.ru 145 : 417 : gbt_inet_union(PG_FUNCTION_ARGS)
146 : : {
7678 bruce@momjian.us 147 : 417 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
148 : 417 : void *out = palloc(sizeof(inetKEY));
149 : :
150 : 417 : *(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY);
282 peter@eisentraut.org 151 : 417 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
152 : : }
153 : :
154 : : Datum
7771 teodor@sigaev.ru 155 : 1774 : gbt_inet_penalty(PG_FUNCTION_ARGS)
156 : : {
7678 bruce@momjian.us 157 : 1774 : inetKEY *origentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
158 : 1774 : inetKEY *newentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
159 : 1774 : float *result = (float *) PG_GETARG_POINTER(2);
160 : :
7266 161 [ + + + + : 1774 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
+ + ]
162 : :
7678 163 : 1774 : PG_RETURN_POINTER(result);
164 : : }
165 : :
166 : : Datum
7771 teodor@sigaev.ru 167 : 9 : gbt_inet_picksplit(PG_FUNCTION_ARGS)
168 : : {
2046 alvherre@alvh.no-ip. 169 : 9 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
170 : : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
171 : : &tinfo, fcinfo->flinfo));
172 : : }
173 : :
174 : : Datum
7771 teodor@sigaev.ru 175 : 411 : gbt_inet_same(PG_FUNCTION_ARGS)
176 : : {
7678 bruce@momjian.us 177 : 411 : inetKEY *b1 = (inetKEY *) PG_GETARG_POINTER(0);
178 : 411 : inetKEY *b2 = (inetKEY *) PG_GETARG_POINTER(1);
179 : 411 : bool *result = (bool *) PG_GETARG_POINTER(2);
180 : :
3091 andrew@dunslane.net 181 : 411 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
7678 bruce@momjian.us 182 : 411 : PG_RETURN_POINTER(result);
183 : : }
184 : :
185 : : static int
156 heikki.linnakangas@i 186 : 12054 : gbt_inet_ssup_cmp(Datum x, Datum y, SortSupport ssup)
187 : : {
188 : 12054 : inetKEY *arg1 = (inetKEY *) DatumGetPointer(x);
189 : 12054 : inetKEY *arg2 = (inetKEY *) DatumGetPointer(y);
190 : :
191 : : /* for leaf items we expect lower == upper, so only compare lower */
192 [ + + ]: 12054 : if (arg1->lower < arg2->lower)
193 : 6584 : return -1;
194 [ + - ]: 5470 : else if (arg1->lower > arg2->lower)
195 : 5470 : return 1;
196 : : else
156 heikki.linnakangas@i 197 :UBC 0 : return 0;
198 : : }
199 : :
200 : : Datum
156 heikki.linnakangas@i 201 :CBC 2 : gbt_inet_sortsupport(PG_FUNCTION_ARGS)
202 : : {
203 : 2 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
204 : :
205 : 2 : ssup->comparator = gbt_inet_ssup_cmp;
206 : 2 : ssup->ssup_extra = NULL;
207 : :
208 : 2 : PG_RETURN_VOID();
209 : : }
|