2 * contrib/btree_gist/btree_inet.c
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"
12 typedef struct inetkey
21 PG_FUNCTION_INFO_V1(gbt_inet_compress
);
22 PG_FUNCTION_INFO_V1(gbt_inet_union
);
23 PG_FUNCTION_INFO_V1(gbt_inet_picksplit
);
24 PG_FUNCTION_INFO_V1(gbt_inet_consistent
);
25 PG_FUNCTION_INFO_V1(gbt_inet_penalty
);
26 PG_FUNCTION_INFO_V1(gbt_inet_same
);
30 gbt_inetgt(const void *a
, const void *b
, FmgrInfo
*flinfo
)
32 return (*((const double *) a
) > *((const double *) b
));
35 gbt_inetge(const void *a
, const void *b
, FmgrInfo
*flinfo
)
37 return (*((const double *) a
) >= *((const double *) b
));
40 gbt_ineteq(const void *a
, const void *b
, FmgrInfo
*flinfo
)
42 return (*((const double *) a
) == *((const double *) b
));
45 gbt_inetle(const void *a
, const void *b
, FmgrInfo
*flinfo
)
47 return (*((const double *) a
) <= *((const double *) b
));
50 gbt_inetlt(const void *a
, const void *b
, FmgrInfo
*flinfo
)
52 return (*((const double *) a
) < *((const double *) b
));
56 gbt_inetkey_cmp(const void *a
, const void *b
, FmgrInfo
*flinfo
)
58 inetKEY
*ia
= (inetKEY
*) (((const Nsrt
*) a
)->t
);
59 inetKEY
*ib
= (inetKEY
*) (((const Nsrt
*) b
)->t
);
61 if (ia
->lower
== ib
->lower
)
63 if (ia
->upper
== ib
->upper
)
66 return (ia
->upper
> ib
->upper
) ? 1 : -1;
69 return (ia
->lower
> ib
->lower
) ? 1 : -1;
73 static const gbtree_ninfo tinfo
=
77 16, /* sizeof(gbtreekey16) */
88 /**************************************************
90 **************************************************/
94 gbt_inet_compress(PG_FUNCTION_ARGS
)
96 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
101 inetKEY
*r
= (inetKEY
*) palloc(sizeof(inetKEY
));
102 bool failure
= false;
104 retval
= palloc(sizeof(GISTENTRY
));
105 r
->lower
= convert_network_to_scalar(entry
->key
, INETOID
, &failure
);
108 gistentryinit(*retval
, PointerGetDatum(r
),
109 entry
->rel
, entry
->page
,
110 entry
->offset
, false);
115 PG_RETURN_POINTER(retval
);
120 gbt_inet_consistent(PG_FUNCTION_ARGS
)
122 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
123 Datum dquery
= PG_GETARG_DATUM(1);
124 StrategyNumber strategy
= (StrategyNumber
) PG_GETARG_UINT16(2);
126 /* Oid subtype = PG_GETARG_OID(3); */
127 bool *recheck
= (bool *) PG_GETARG_POINTER(4);
128 inetKEY
*kkk
= (inetKEY
*) DatumGetPointer(entry
->key
);
131 bool failure
= false;
133 query
= convert_network_to_scalar(dquery
, INETOID
, &failure
);
136 /* All cases served by this function are inexact */
139 key
.lower
= (GBT_NUMKEY
*) &kkk
->lower
;
140 key
.upper
= (GBT_NUMKEY
*) &kkk
->upper
;
142 PG_RETURN_BOOL(gbt_num_consistent(&key
, (void *) &query
,
143 &strategy
, GIST_LEAF(entry
), &tinfo
, fcinfo
->flinfo
));
148 gbt_inet_union(PG_FUNCTION_ARGS
)
150 GistEntryVector
*entryvec
= (GistEntryVector
*) PG_GETARG_POINTER(0);
151 void *out
= palloc(sizeof(inetKEY
));
153 *(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY
);
154 PG_RETURN_POINTER(gbt_num_union((void *) out
, entryvec
, &tinfo
, fcinfo
->flinfo
));
159 gbt_inet_penalty(PG_FUNCTION_ARGS
)
161 inetKEY
*origentry
= (inetKEY
*) DatumGetPointer(((GISTENTRY
*) PG_GETARG_POINTER(0))->key
);
162 inetKEY
*newentry
= (inetKEY
*) DatumGetPointer(((GISTENTRY
*) PG_GETARG_POINTER(1))->key
);
163 float *result
= (float *) PG_GETARG_POINTER(2);
165 penalty_num(result
, origentry
->lower
, origentry
->upper
, newentry
->lower
, newentry
->upper
);
167 PG_RETURN_POINTER(result
);
172 gbt_inet_picksplit(PG_FUNCTION_ARGS
)
174 PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector
*) PG_GETARG_POINTER(0),
175 (GIST_SPLITVEC
*) PG_GETARG_POINTER(1),
176 &tinfo
, fcinfo
->flinfo
));
180 gbt_inet_same(PG_FUNCTION_ARGS
)
182 inetKEY
*b1
= (inetKEY
*) PG_GETARG_POINTER(0);
183 inetKEY
*b2
= (inetKEY
*) PG_GETARG_POINTER(1);
184 bool *result
= (bool *) PG_GETARG_POINTER(2);
186 *result
= gbt_num_same((void *) b1
, (void *) b2
, &tinfo
, fcinfo
->flinfo
);
187 PG_RETURN_POINTER(result
);