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 8 : PG_FUNCTION_INFO_V1(gbt_oid_compress);
19 8 : PG_FUNCTION_INFO_V1(gbt_oid_fetch);
20 8 : PG_FUNCTION_INFO_V1(gbt_oid_union);
21 8 : PG_FUNCTION_INFO_V1(gbt_oid_picksplit);
22 8 : PG_FUNCTION_INFO_V1(gbt_oid_consistent);
23 8 : PG_FUNCTION_INFO_V1(gbt_oid_distance);
24 8 : PG_FUNCTION_INFO_V1(gbt_oid_penalty);
25 8 : PG_FUNCTION_INFO_V1(gbt_oid_same);
26 8 : PG_FUNCTION_INFO_V1(gbt_oid_sortsupport);
27 :
28 :
29 : static bool
30 4488 : gbt_oidgt(const void *a, const void *b, FmgrInfo *flinfo)
31 : {
32 4488 : return (*((const Oid *) a) > *((const Oid *) b));
33 : }
34 : static bool
35 516 : gbt_oidge(const void *a, const void *b, FmgrInfo *flinfo)
36 : {
37 516 : return (*((const Oid *) a) >= *((const Oid *) b));
38 : }
39 : static bool
40 500 : gbt_oideq(const void *a, const void *b, FmgrInfo *flinfo)
41 : {
42 500 : return (*((const Oid *) a) == *((const Oid *) b));
43 : }
44 : static bool
45 2026 : gbt_oidle(const void *a, const void *b, FmgrInfo *flinfo)
46 : {
47 2026 : return (*((const Oid *) a) <= *((const Oid *) b));
48 : }
49 : static bool
50 5988 : gbt_oidlt(const void *a, const void *b, FmgrInfo *flinfo)
51 : {
52 5988 : return (*((const Oid *) a) < *((const Oid *) b));
53 : }
54 :
55 : static int
56 3994 : gbt_oidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57 : {
58 3994 : oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
59 3994 : oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
60 :
61 3994 : 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 3994 : return (ia->lower > ib->lower) ? 1 : -1;
70 : }
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 : }
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 6 : 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 : 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 : }
114 :
115 :
116 : /**************************************************
117 : * GiST support functions
118 : **************************************************/
119 :
120 : Datum
121 2008 : gbt_oid_compress(PG_FUNCTION_ARGS)
122 : {
123 2008 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124 :
125 2008 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
126 : }
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 : }
135 :
136 : Datum
137 5540 : gbt_oid_consistent(PG_FUNCTION_ARGS)
138 : {
139 5540 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140 5540 : Oid query = PG_GETARG_OID(1);
141 5540 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142 :
143 : /* Oid subtype = PG_GETARG_OID(3); */
144 5540 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
145 5540 : oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
146 : GBT_NUMKEY_R key;
147 :
148 : /* All cases served by this function are exact */
149 5540 : *recheck = false;
150 :
151 5540 : key.lower = (GBT_NUMKEY *) &kkk->lower;
152 5540 : key.upper = (GBT_NUMKEY *) &kkk->upper;
153 :
154 5540 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
155 : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
156 : }
157 :
158 : Datum
159 0 : gbt_oid_distance(PG_FUNCTION_ARGS)
160 : {
161 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
162 0 : Oid query = PG_GETARG_OID(1);
163 :
164 : /* Oid subtype = PG_GETARG_OID(3); */
165 0 : oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
166 : GBT_NUMKEY_R key;
167 :
168 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
169 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
170 :
171 0 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
172 : &tinfo, fcinfo->flinfo));
173 : }
174 :
175 : Datum
176 0 : gbt_oid_union(PG_FUNCTION_ARGS)
177 : {
178 0 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
179 0 : void *out = palloc(sizeof(oidKEY));
180 :
181 0 : *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
182 0 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
183 : }
184 :
185 : Datum
186 0 : gbt_oid_penalty(PG_FUNCTION_ARGS)
187 : {
188 0 : oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
189 0 : oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
190 0 : float *result = (float *) PG_GETARG_POINTER(2);
191 :
192 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
193 :
194 0 : PG_RETURN_POINTER(result);
195 : }
196 :
197 : Datum
198 6 : gbt_oid_picksplit(PG_FUNCTION_ARGS)
199 : {
200 6 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
201 : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
202 : &tinfo, fcinfo->flinfo));
203 : }
204 :
205 : Datum
206 0 : gbt_oid_same(PG_FUNCTION_ARGS)
207 : {
208 0 : oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
209 0 : oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
210 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
211 :
212 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
213 0 : PG_RETURN_POINTER(result);
214 : }
215 :
216 : static int
217 1998 : gbt_oid_ssup_cmp(Datum x, Datum y, SortSupport ssup)
218 : {
219 1998 : oidKEY *arg1 = (oidKEY *) DatumGetPointer(x);
220 1998 : oidKEY *arg2 = (oidKEY *) DatumGetPointer(y);
221 :
222 : /* for leaf items we expect lower == upper, so only compare lower */
223 1998 : if (arg1->lower > arg2->lower)
224 0 : return 1;
225 1998 : else if (arg1->lower < arg2->lower)
226 1998 : return -1;
227 : else
228 0 : return 0;
229 : }
230 :
231 : Datum
232 2 : gbt_oid_sortsupport(PG_FUNCTION_ARGS)
233 : {
234 2 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
235 :
236 2 : ssup->comparator = gbt_oid_ssup_cmp;
237 2 : ssup->ssup_extra = NULL;
238 :
239 2 : PG_RETURN_VOID();
240 : }
|