]> granicus.if.org Git - postgresql/blob - contrib/btree_gist/btree_oid.c
Post-PG 10 beta1 pgindent run
[postgresql] / contrib / btree_gist / btree_oid.c
1 /*
2  * contrib/btree_gist/btree_oid.c
3  */
4 #include "postgres.h"
5
6 #include "btree_gist.h"
7 #include "btree_utils_num.h"
8
9 typedef struct
10 {
11         Oid                     lower;
12         Oid                     upper;
13 } oidKEY;
14
15 /*
16 ** OID ops
17 */
18 PG_FUNCTION_INFO_V1(gbt_oid_compress);
19 PG_FUNCTION_INFO_V1(gbt_oid_fetch);
20 PG_FUNCTION_INFO_V1(gbt_oid_union);
21 PG_FUNCTION_INFO_V1(gbt_oid_picksplit);
22 PG_FUNCTION_INFO_V1(gbt_oid_consistent);
23 PG_FUNCTION_INFO_V1(gbt_oid_distance);
24 PG_FUNCTION_INFO_V1(gbt_oid_penalty);
25 PG_FUNCTION_INFO_V1(gbt_oid_same);
26
27
28 static bool
29 gbt_oidgt(const void *a, const void *b, FmgrInfo *flinfo)
30 {
31         return (*((const Oid *) a) > *((const Oid *) b));
32 }
33 static bool
34 gbt_oidge(const void *a, const void *b, FmgrInfo *flinfo)
35 {
36         return (*((const Oid *) a) >= *((const Oid *) b));
37 }
38 static bool
39 gbt_oideq(const void *a, const void *b, FmgrInfo *flinfo)
40 {
41         return (*((const Oid *) a) == *((const Oid *) b));
42 }
43 static bool
44 gbt_oidle(const void *a, const void *b, FmgrInfo *flinfo)
45 {
46         return (*((const Oid *) a) <= *((const Oid *) b));
47 }
48 static bool
49 gbt_oidlt(const void *a, const void *b, FmgrInfo *flinfo)
50 {
51         return (*((const Oid *) a) < *((const Oid *) b));
52 }
53
54 static int
55 gbt_oidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56 {
57         oidKEY     *ia = (oidKEY *) (((const Nsrt *) a)->t);
58         oidKEY     *ib = (oidKEY *) (((const Nsrt *) b)->t);
59
60         if (ia->lower == ib->lower)
61         {
62                 if (ia->upper == ib->upper)
63                         return 0;
64
65                 return (ia->upper > ib->upper) ? 1 : -1;
66         }
67
68         return (ia->lower > ib->lower) ? 1 : -1;
69 }
70
71 static float8
72 gbt_oid_dist(const void *a, const void *b, FmgrInfo *flinfo)
73 {
74         Oid                     aa = *(const Oid *) a;
75         Oid                     bb = *(const Oid *) b;
76
77         if (aa < bb)
78                 return (float8) (bb - aa);
79         else
80                 return (float8) (aa - bb);
81 }
82
83
84 static const gbtree_ninfo tinfo =
85 {
86         gbt_t_oid,
87         sizeof(Oid),
88         8,                                                      /* sizeof(gbtreekey8) */
89         gbt_oidgt,
90         gbt_oidge,
91         gbt_oideq,
92         gbt_oidle,
93         gbt_oidlt,
94         gbt_oidkey_cmp,
95         gbt_oid_dist
96 };
97
98
99 PG_FUNCTION_INFO_V1(oid_dist);
100 Datum
101 oid_dist(PG_FUNCTION_ARGS)
102 {
103         Oid                     a = PG_GETARG_OID(0);
104         Oid                     b = PG_GETARG_OID(1);
105         Oid                     res;
106
107         if (a < b)
108                 res = b - a;
109         else
110                 res = a - b;
111         PG_RETURN_OID(res);
112 }
113
114
115 /**************************************************
116  * Oid ops
117  **************************************************/
118
119
120 Datum
121 gbt_oid_compress(PG_FUNCTION_ARGS)
122 {
123         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124
125         PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
126 }
127
128 Datum
129 gbt_oid_fetch(PG_FUNCTION_ARGS)
130 {
131         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132
133         PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
134 }
135
136 Datum
137 gbt_oid_consistent(PG_FUNCTION_ARGS)
138 {
139         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140         Oid                     query = PG_GETARG_OID(1);
141         StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142
143         /* Oid          subtype = PG_GETARG_OID(3); */
144         bool       *recheck = (bool *) PG_GETARG_POINTER(4);
145         oidKEY     *kkk = (oidKEY *) DatumGetPointer(entry->key);
146         GBT_NUMKEY_R key;
147
148         /* All cases served by this function are exact */
149         *recheck = false;
150
151         key.lower = (GBT_NUMKEY *) &kkk->lower;
152         key.upper = (GBT_NUMKEY *) &kkk->upper;
153
154         PG_RETURN_BOOL(
155                                    gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
156                 );
157 }
158
159
160 Datum
161 gbt_oid_distance(PG_FUNCTION_ARGS)
162 {
163         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
164         Oid                     query = PG_GETARG_OID(1);
165
166         /* Oid          subtype = PG_GETARG_OID(3); */
167         oidKEY     *kkk = (oidKEY *) DatumGetPointer(entry->key);
168         GBT_NUMKEY_R key;
169
170         key.lower = (GBT_NUMKEY *) &kkk->lower;
171         key.upper = (GBT_NUMKEY *) &kkk->upper;
172
173         PG_RETURN_FLOAT8(
174                                          gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
175                 );
176 }
177
178
179 Datum
180 gbt_oid_union(PG_FUNCTION_ARGS)
181 {
182         GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
183         void       *out = palloc(sizeof(oidKEY));
184
185         *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
186         PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
187 }
188
189
190 Datum
191 gbt_oid_penalty(PG_FUNCTION_ARGS)
192 {
193         oidKEY     *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
194         oidKEY     *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
195         float      *result = (float *) PG_GETARG_POINTER(2);
196
197         penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
198
199         PG_RETURN_POINTER(result);
200 }
201
202 Datum
203 gbt_oid_picksplit(PG_FUNCTION_ARGS)
204 {
205         PG_RETURN_POINTER(gbt_num_picksplit(
206                                                                         (GistEntryVector *) PG_GETARG_POINTER(0),
207                                                                           (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
208                                                                                 &tinfo, fcinfo->flinfo
209                                                                                 ));
210 }
211
212 Datum
213 gbt_oid_same(PG_FUNCTION_ARGS)
214 {
215         oidKEY     *b1 = (oidKEY *) PG_GETARG_POINTER(0);
216         oidKEY     *b2 = (oidKEY *) PG_GETARG_POINTER(1);
217         bool       *result = (bool *) PG_GETARG_POINTER(2);
218
219         *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
220         PG_RETURN_POINTER(result);
221 }