4 #include "btree_gist.h"
5 #include "btree_utils_num.h"
6 #include "utils/builtins.h"
7 #include "utils/inet.h"
8 #include "catalog/pg_type.h"
10 typedef struct inetkey
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
);
26 Datum
gbt_inet_compress(PG_FUNCTION_ARGS
);
27 Datum
gbt_inet_union(PG_FUNCTION_ARGS
);
28 Datum
gbt_inet_picksplit(PG_FUNCTION_ARGS
);
29 Datum
gbt_inet_consistent(PG_FUNCTION_ARGS
);
30 Datum
gbt_inet_penalty(PG_FUNCTION_ARGS
);
31 Datum
gbt_inet_same(PG_FUNCTION_ARGS
);
35 gbt_inetgt(const void *a
, const void *b
)
37 return (*((double *) a
) > *((double *) b
));
40 gbt_inetge(const void *a
, const void *b
)
42 return (*((double *) a
) >= *((double *) b
));
45 gbt_ineteq(const void *a
, const void *b
)
47 return (*((double *) a
) == *((double *) b
));
50 gbt_inetle(const void *a
, const void *b
)
52 return (*((double *) a
) <= *((double *) b
));
55 gbt_inetlt(const void *a
, const void *b
)
57 return (*((double *) a
) < *((double *) b
));
61 gbt_inetkey_cmp(const void *a
, const void *b
)
64 if (*(double *) (&((Nsrt
*) a
)->t
[0]) > *(double *) (&((Nsrt
*) b
)->t
[0]))
66 else if (*(double *) (&((Nsrt
*) a
)->t
[0]) < *(double *) (&((Nsrt
*) b
)->t
[0]))
73 static const gbtree_ninfo tinfo
=
86 /**************************************************
88 **************************************************/
92 gbt_inet_compress(PG_FUNCTION_ARGS
)
94 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
99 inetKEY
*r
= (inetKEY
*) palloc(sizeof(inetKEY
));
101 retval
= palloc(sizeof(GISTENTRY
));
102 r
->lower
= convert_network_to_scalar(entry
->key
, INETOID
);
104 gistentryinit(*retval
, PointerGetDatum(r
),
105 entry
->rel
, entry
->page
,
106 entry
->offset
, FALSE
);
111 PG_RETURN_POINTER(retval
);
116 gbt_inet_consistent(PG_FUNCTION_ARGS
)
118 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
119 double query
= convert_network_to_scalar(PG_GETARG_DATUM(1), INETOID
);
120 StrategyNumber strategy
= (StrategyNumber
) PG_GETARG_UINT16(2);
121 /* Oid subtype = PG_GETARG_OID(3); */
122 bool *recheck
= (bool *) PG_GETARG_POINTER(4);
123 inetKEY
*kkk
= (inetKEY
*) DatumGetPointer(entry
->key
);
126 /* All cases served by this function are inexact */
129 key
.lower
= (GBT_NUMKEY
*) & kkk
->lower
;
130 key
.upper
= (GBT_NUMKEY
*) & kkk
->upper
;
132 PG_RETURN_BOOL(gbt_num_consistent(&key
, (void *) &query
,
133 &strategy
, GIST_LEAF(entry
), &tinfo
));
138 gbt_inet_union(PG_FUNCTION_ARGS
)
140 GistEntryVector
*entryvec
= (GistEntryVector
*) PG_GETARG_POINTER(0);
141 void *out
= palloc(sizeof(inetKEY
));
143 *(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY
);
144 PG_RETURN_POINTER(gbt_num_union((void *) out
, entryvec
, &tinfo
));
149 gbt_inet_penalty(PG_FUNCTION_ARGS
)
151 inetKEY
*origentry
= (inetKEY
*) DatumGetPointer(((GISTENTRY
*) PG_GETARG_POINTER(0))->key
);
152 inetKEY
*newentry
= (inetKEY
*) DatumGetPointer(((GISTENTRY
*) PG_GETARG_POINTER(1))->key
);
153 float *result
= (float *) PG_GETARG_POINTER(2);
155 penalty_num(result
, origentry
->lower
, origentry
->upper
, newentry
->lower
, newentry
->upper
);
157 PG_RETURN_POINTER(result
);
162 gbt_inet_picksplit(PG_FUNCTION_ARGS
)
164 PG_RETURN_POINTER(gbt_num_picksplit(
165 (GistEntryVector
*) PG_GETARG_POINTER(0),
166 (GIST_SPLITVEC
*) PG_GETARG_POINTER(1),
172 gbt_inet_same(PG_FUNCTION_ARGS
)
174 inetKEY
*b1
= (inetKEY
*) PG_GETARG_POINTER(0);
175 inetKEY
*b2
= (inetKEY
*) PG_GETARG_POINTER(1);
176 bool *result
= (bool *) PG_GETARG_POINTER(2);
178 *result
= gbt_num_same((void *) b1
, (void *) b2
, &tinfo
);
179 PG_RETURN_POINTER(result
);