OSDN Git Service

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