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