Fix minor nbtree page deletion buffer lock issue.
[pgsql.git] / contrib / btree_gist / btree_float4.c
blob3604c73313a958c3031ca51f31d2c1c86700a8a7
1 /*
2 * contrib/btree_gist/btree_float4.c
3 */
4 #include "postgres.h"
6 #include "btree_gist.h"
7 #include "btree_utils_num.h"
9 typedef struct float4key
11 float4 lower;
12 float4 upper;
13 } float4KEY;
16 ** float4 ops
18 PG_FUNCTION_INFO_V1(gbt_float4_compress);
19 PG_FUNCTION_INFO_V1(gbt_float4_fetch);
20 PG_FUNCTION_INFO_V1(gbt_float4_union);
21 PG_FUNCTION_INFO_V1(gbt_float4_picksplit);
22 PG_FUNCTION_INFO_V1(gbt_float4_consistent);
23 PG_FUNCTION_INFO_V1(gbt_float4_distance);
24 PG_FUNCTION_INFO_V1(gbt_float4_penalty);
25 PG_FUNCTION_INFO_V1(gbt_float4_same);
27 static bool
28 gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
30 return (*((const float4 *) a) > *((const float4 *) b));
32 static bool
33 gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
35 return (*((const float4 *) a) >= *((const float4 *) b));
37 static bool
38 gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
40 return (*((const float4 *) a) == *((const float4 *) b));
42 static bool
43 gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
45 return (*((const float4 *) a) <= *((const float4 *) b));
47 static bool
48 gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
50 return (*((const float4 *) a) < *((const float4 *) b));
53 static int
54 gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56 float4KEY *ia = (float4KEY *) (((const Nsrt *) a)->t);
57 float4KEY *ib = (float4KEY *) (((const Nsrt *) b)->t);
59 if (ia->lower == ib->lower)
61 if (ia->upper == ib->upper)
62 return 0;
64 return (ia->upper > ib->upper) ? 1 : -1;
67 return (ia->lower > ib->lower) ? 1 : -1;
70 static float8
71 gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
73 return GET_FLOAT_DISTANCE(float4, a, b);
77 static const gbtree_ninfo tinfo =
79 gbt_t_float4,
80 sizeof(float4),
81 8, /* sizeof(gbtreekey8) */
82 gbt_float4gt,
83 gbt_float4ge,
84 gbt_float4eq,
85 gbt_float4le,
86 gbt_float4lt,
87 gbt_float4key_cmp,
88 gbt_float4_dist
92 PG_FUNCTION_INFO_V1(float4_dist);
93 Datum
94 float4_dist(PG_FUNCTION_ARGS)
96 float4 a = PG_GETARG_FLOAT4(0);
97 float4 b = PG_GETARG_FLOAT4(1);
98 float4 r;
100 r = a - b;
101 CHECKFLOATVAL(r, isinf(a) || isinf(b), true);
103 PG_RETURN_FLOAT4(Abs(r));
107 /**************************************************
108 * float4 ops
109 **************************************************/
112 Datum
113 gbt_float4_compress(PG_FUNCTION_ARGS)
115 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
117 PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
120 Datum
121 gbt_float4_fetch(PG_FUNCTION_ARGS)
123 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
125 PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
128 Datum
129 gbt_float4_consistent(PG_FUNCTION_ARGS)
131 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132 float4 query = PG_GETARG_FLOAT4(1);
133 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
135 /* Oid subtype = PG_GETARG_OID(3); */
136 bool *recheck = (bool *) PG_GETARG_POINTER(4);
137 float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
138 GBT_NUMKEY_R key;
140 /* All cases served by this function are exact */
141 *recheck = false;
143 key.lower = (GBT_NUMKEY *) &kkk->lower;
144 key.upper = (GBT_NUMKEY *) &kkk->upper;
146 PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query, &strategy,
147 GIST_LEAF(entry), &tinfo,
148 fcinfo->flinfo));
152 Datum
153 gbt_float4_distance(PG_FUNCTION_ARGS)
155 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
156 float4 query = PG_GETARG_FLOAT4(1);
158 /* Oid subtype = PG_GETARG_OID(3); */
159 float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
160 GBT_NUMKEY_R key;
162 key.lower = (GBT_NUMKEY *) &kkk->lower;
163 key.upper = (GBT_NUMKEY *) &kkk->upper;
165 PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry),
166 &tinfo, fcinfo->flinfo));
170 Datum
171 gbt_float4_union(PG_FUNCTION_ARGS)
173 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
174 void *out = palloc(sizeof(float4KEY));
176 *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
177 PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
181 Datum
182 gbt_float4_penalty(PG_FUNCTION_ARGS)
184 float4KEY *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
185 float4KEY *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
186 float *result = (float *) PG_GETARG_POINTER(2);
188 penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
190 PG_RETURN_POINTER(result);
194 Datum
195 gbt_float4_picksplit(PG_FUNCTION_ARGS)
197 PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
198 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
199 &tinfo, fcinfo->flinfo));
202 Datum
203 gbt_float4_same(PG_FUNCTION_ARGS)
205 float4KEY *b1 = (float4KEY *) PG_GETARG_POINTER(0);
206 float4KEY *b2 = (float4KEY *) PG_GETARG_POINTER(1);
207 bool *result = (bool *) PG_GETARG_POINTER(2);
209 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
210 PG_RETURN_POINTER(result);