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 :
9 : typedef struct
10 : {
11 : Oid lower;
12 : Oid upper;
13 : } oidKEY;
14 :
15 : /*
16 : ** OID ops
17 : */
18 4 : PG_FUNCTION_INFO_V1(gbt_oid_compress);
19 4 : PG_FUNCTION_INFO_V1(gbt_oid_fetch);
20 4 : PG_FUNCTION_INFO_V1(gbt_oid_union);
21 4 : PG_FUNCTION_INFO_V1(gbt_oid_picksplit);
22 4 : PG_FUNCTION_INFO_V1(gbt_oid_consistent);
23 4 : PG_FUNCTION_INFO_V1(gbt_oid_distance);
24 4 : PG_FUNCTION_INFO_V1(gbt_oid_penalty);
25 4 : PG_FUNCTION_INFO_V1(gbt_oid_same);
26 :
27 :
28 : static bool
29 4552 : gbt_oidgt(const void *a, const void *b, FmgrInfo *flinfo)
30 : {
31 4552 : return (*((const Oid *) a) > *((const Oid *) b));
32 : }
33 : static bool
34 386 : gbt_oidge(const void *a, const void *b, FmgrInfo *flinfo)
35 : {
36 386 : return (*((const Oid *) a) >= *((const Oid *) b));
37 : }
38 : static bool
39 2898 : gbt_oideq(const void *a, const void *b, FmgrInfo *flinfo)
40 : {
41 2898 : return (*((const Oid *) a) == *((const Oid *) b));
42 : }
43 : static bool
44 2032 : gbt_oidle(const void *a, const void *b, FmgrInfo *flinfo)
45 : {
46 2032 : return (*((const Oid *) a) <= *((const Oid *) b));
47 : }
48 : static bool
49 6186 : gbt_oidlt(const void *a, const void *b, FmgrInfo *flinfo)
50 : {
51 6186 : return (*((const Oid *) a) < *((const Oid *) b));
52 : }
53 :
54 : static int
55 2928 : gbt_oidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56 : {
57 2928 : oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
58 2928 : oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
59 :
60 2928 : if (ia->lower == ib->lower)
61 : {
62 0 : if (ia->upper == ib->upper)
63 0 : return 0;
64 :
65 0 : return (ia->upper > ib->upper) ? 1 : -1;
66 : }
67 :
68 2928 : return (ia->lower > ib->lower) ? 1 : -1;
69 : }
70 :
71 : static float8
72 0 : gbt_oid_dist(const void *a, const void *b, FmgrInfo *flinfo)
73 : {
74 0 : Oid aa = *(const Oid *) a;
75 0 : Oid bb = *(const Oid *) b;
76 :
77 0 : if (aa < bb)
78 0 : return (float8) (bb - aa);
79 : else
80 0 : 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 2 : PG_FUNCTION_INFO_V1(oid_dist);
100 : Datum
101 0 : oid_dist(PG_FUNCTION_ARGS)
102 : {
103 0 : Oid a = PG_GETARG_OID(0);
104 0 : Oid b = PG_GETARG_OID(1);
105 : Oid res;
106 :
107 0 : if (a < b)
108 0 : res = b - a;
109 : else
110 0 : res = a - b;
111 0 : PG_RETURN_OID(res);
112 : }
113 :
114 :
115 : /**************************************************
116 : * Oid ops
117 : **************************************************/
118 :
119 :
120 : Datum
121 3282 : gbt_oid_compress(PG_FUNCTION_ARGS)
122 : {
123 3282 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124 :
125 3282 : 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 5148 : gbt_oid_consistent(PG_FUNCTION_ARGS)
138 : {
139 5148 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140 5148 : Oid query = PG_GETARG_OID(1);
141 5148 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142 :
143 : /* Oid subtype = PG_GETARG_OID(3); */
144 5148 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
145 5148 : oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
146 : GBT_NUMKEY_R key;
147 :
148 : /* All cases served by this function are exact */
149 5148 : *recheck = false;
150 :
151 5148 : key.lower = (GBT_NUMKEY *) &kkk->lower;
152 5148 : key.upper = (GBT_NUMKEY *) &kkk->upper;
153 :
154 5148 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
155 : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
156 : }
157 :
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 :
165 : /* Oid subtype = PG_GETARG_OID(3); */
166 0 : oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
167 : GBT_NUMKEY_R key;
168 :
169 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
170 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
171 :
172 0 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
173 : &tinfo, fcinfo->flinfo));
174 : }
175 :
176 :
177 : Datum
178 1266 : gbt_oid_union(PG_FUNCTION_ARGS)
179 : {
180 1266 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
181 1266 : void *out = palloc(sizeof(oidKEY));
182 :
183 1266 : *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
184 1266 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
185 : }
186 :
187 :
188 : Datum
189 4134 : gbt_oid_penalty(PG_FUNCTION_ARGS)
190 : {
191 4134 : oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
192 4134 : oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
193 4134 : float *result = (float *) PG_GETARG_POINTER(2);
194 :
195 4134 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
196 :
197 4134 : PG_RETURN_POINTER(result);
198 : }
199 :
200 : Datum
201 8 : gbt_oid_picksplit(PG_FUNCTION_ARGS)
202 : {
203 8 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
204 : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
205 : &tinfo, fcinfo->flinfo));
206 : }
207 :
208 : Datum
209 1266 : gbt_oid_same(PG_FUNCTION_ARGS)
210 : {
211 1266 : oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
212 1266 : oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
213 1266 : bool *result = (bool *) PG_GETARG_POINTER(2);
214 :
215 1266 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
216 1266 : PG_RETURN_POINTER(result);
217 : }
|