Start background writer during archive recovery. Background writer now performs
[PostgreSQL.git] / contrib / btree_gist / btree_inet.c
blob396c21321d0aece5b20176e30111711621d01196
1 /*
2 * $PostgreSQL:$
3 */
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
12 double lower;
13 double upper;
14 } inetKEY;
17 ** inet ops
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);
34 static bool
35 gbt_inetgt(const void *a, const void *b)
37 return (*((double *) a) > *((double *) b));
39 static bool
40 gbt_inetge(const void *a, const void *b)
42 return (*((double *) a) >= *((double *) b));
44 static bool
45 gbt_ineteq(const void *a, const void *b)
47 return (*((double *) a) == *((double *) b));
49 static bool
50 gbt_inetle(const void *a, const void *b)
52 return (*((double *) a) <= *((double *) b));
54 static bool
55 gbt_inetlt(const void *a, const void *b)
57 return (*((double *) a) < *((double *) b));
60 static int
61 gbt_inetkey_cmp(const void *a, const void *b)
64 if (*(double *) (&((Nsrt *) a)->t[0]) > *(double *) (&((Nsrt *) b)->t[0]))
65 return 1;
66 else if (*(double *) (&((Nsrt *) a)->t[0]) < *(double *) (&((Nsrt *) b)->t[0]))
67 return -1;
68 return 0;
73 static const gbtree_ninfo tinfo =
75 gbt_t_inet,
76 sizeof(double),
77 gbt_inetgt,
78 gbt_inetge,
79 gbt_ineteq,
80 gbt_inetle,
81 gbt_inetlt,
82 gbt_inetkey_cmp
86 /**************************************************
87 * inet ops
88 **************************************************/
91 Datum
92 gbt_inet_compress(PG_FUNCTION_ARGS)
94 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
95 GISTENTRY *retval;
97 if (entry->leafkey)
99 inetKEY *r = (inetKEY *) palloc(sizeof(inetKEY));
101 retval = palloc(sizeof(GISTENTRY));
102 r->lower = convert_network_to_scalar(entry->key, INETOID);
103 r->upper = r->lower;
104 gistentryinit(*retval, PointerGetDatum(r),
105 entry->rel, entry->page,
106 entry->offset, FALSE);
108 else
109 retval = entry;
111 PG_RETURN_POINTER(retval);
115 Datum
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);
124 GBT_NUMKEY_R key;
126 /* All cases served by this function are inexact */
127 *recheck = true;
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));
137 Datum
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));
148 Datum
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);
161 Datum
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),
167 &tinfo
171 Datum
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);