Age Owner Branch data TLA 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 */
7961 teodor@sigaev.ru 18 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_oid_compress);
4006 heikki.linnakangas@i 19 : 4 : PG_FUNCTION_INFO_V1(gbt_oid_fetch);
7961 teodor@sigaev.ru 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);
5492 tgl@sss.pgh.pa.us 23 : 4 : PG_FUNCTION_INFO_V1(gbt_oid_distance);
7961 teodor@sigaev.ru 24 : 4 : PG_FUNCTION_INFO_V1(gbt_oid_penalty);
25 : 4 : PG_FUNCTION_INFO_V1(gbt_oid_same);
346 heikki.linnakangas@i 26 : 4 : PG_FUNCTION_INFO_V1(gbt_oid_sortsupport);
27 : :
28 : :
29 : : static bool
3281 andrew@dunslane.net 30 : 2244 : gbt_oidgt(const void *a, const void *b, FmgrInfo *flinfo)
31 : : {
5299 peter_e@gmx.net 32 : 2244 : return (*((const Oid *) a) > *((const Oid *) b));
33 : : }
34 : : static bool
3281 andrew@dunslane.net 35 : 258 : gbt_oidge(const void *a, const void *b, FmgrInfo *flinfo)
36 : : {
5299 peter_e@gmx.net 37 : 258 : return (*((const Oid *) a) >= *((const Oid *) b));
38 : : }
39 : : static bool
3281 andrew@dunslane.net 40 : 250 : gbt_oideq(const void *a, const void *b, FmgrInfo *flinfo)
41 : : {
5299 peter_e@gmx.net 42 : 250 : return (*((const Oid *) a) == *((const Oid *) b));
43 : : }
44 : : static bool
3281 andrew@dunslane.net 45 : 1013 : gbt_oidle(const void *a, const void *b, FmgrInfo *flinfo)
46 : : {
5299 peter_e@gmx.net 47 : 1013 : return (*((const Oid *) a) <= *((const Oid *) b));
48 : : }
49 : : static bool
3281 andrew@dunslane.net 50 : 2994 : gbt_oidlt(const void *a, const void *b, FmgrInfo *flinfo)
51 : : {
5299 peter_e@gmx.net 52 : 2994 : return (*((const Oid *) a) < *((const Oid *) b));
53 : : }
54 : :
55 : : static int
3281 andrew@dunslane.net 56 : 1997 : gbt_oidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57 : : {
5299 peter_e@gmx.net 58 : 1997 : oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
59 : 1997 : oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
60 : :
5947 teodor@sigaev.ru 61 [ - + ]: 1997 : if (ia->lower == ib->lower)
62 : : {
5947 teodor@sigaev.ru 63 [ # # ]:UBC 0 : if (ia->upper == ib->upper)
64 : 0 : return 0;
65 : :
66 [ # # ]: 0 : return (ia->upper > ib->upper) ? 1 : -1;
67 : : }
68 : :
5947 teodor@sigaev.ru 69 [ - + ]:CBC 1997 : return (ia->lower > ib->lower) ? 1 : -1;
70 : : }
71 : :
72 : : static float8
3281 andrew@dunslane.net 73 :UBC 0 : gbt_oid_dist(const void *a, const void *b, FmgrInfo *flinfo)
74 : : {
5492 tgl@sss.pgh.pa.us 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 : :
5492 tgl@sss.pgh.pa.us 100 :CBC 3 : PG_FUNCTION_INFO_V1(oid_dist);
101 : : Datum
5492 tgl@sss.pgh.pa.us 102 :UBC 0 : oid_dist(PG_FUNCTION_ARGS)
103 : : {
5453 bruce@momjian.us 104 : 0 : Oid a = PG_GETARG_OID(0);
105 : 0 : Oid b = PG_GETARG_OID(1);
106 : : Oid res;
107 : :
5492 tgl@sss.pgh.pa.us 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
7961 teodor@sigaev.ru 121 :CBC 1004 : gbt_oid_compress(PG_FUNCTION_ARGS)
122 : : {
7868 bruce@momjian.us 123 : 1004 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124 : :
4007 heikki.linnakangas@i 125 : 1004 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
126 : : }
127 : :
128 : : Datum
4006 heikki.linnakangas@i 129 :UBC 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
7961 teodor@sigaev.ru 137 :CBC 2770 : gbt_oid_consistent(PG_FUNCTION_ARGS)
138 : : {
7868 bruce@momjian.us 139 : 2770 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140 : 2770 : Oid query = PG_GETARG_OID(1);
6544 tgl@sss.pgh.pa.us 141 : 2770 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142 : : #ifdef NOT_USED
143 : : Oid subtype = PG_GETARG_OID(3);
144 : : #endif
145 : 2770 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
7868 bruce@momjian.us 146 : 2770 : oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
147 : : GBT_NUMKEY_R key;
148 : :
149 : : /* All cases served by this function are exact */
6544 tgl@sss.pgh.pa.us 150 : 2770 : *recheck = false;
151 : :
6121 bruce@momjian.us 152 : 2770 : key.lower = (GBT_NUMKEY *) &kkk->lower;
153 : 2770 : key.upper = (GBT_NUMKEY *) &kkk->upper;
154 : :
472 peter@eisentraut.org 155 : 2770 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
156 : : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
157 : : }
158 : :
159 : : Datum
5492 tgl@sss.pgh.pa.us 160 :UBC 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 : : GBT_NUMKEY_R key;
169 : :
170 : 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
171 : 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
172 : :
472 peter@eisentraut.org 173 : 0 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
174 : : &tinfo, fcinfo->flinfo));
175 : : }
176 : :
177 : : Datum
7961 teodor@sigaev.ru 178 : 0 : gbt_oid_union(PG_FUNCTION_ARGS)
179 : : {
7868 bruce@momjian.us 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);
472 peter@eisentraut.org 184 : 0 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
185 : : }
186 : :
187 : : Datum
7961 teodor@sigaev.ru 188 : 0 : gbt_oid_penalty(PG_FUNCTION_ARGS)
189 : : {
7868 bruce@momjian.us 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 : :
7456 194 [ # # # # : 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
# # ]
195 : :
7868 196 : 0 : PG_RETURN_POINTER(result);
197 : : }
198 : :
199 : : Datum
7961 teodor@sigaev.ru 200 :CBC 3 : gbt_oid_picksplit(PG_FUNCTION_ARGS)
201 : : {
2236 alvherre@alvh.no-ip. 202 : 3 : 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
7961 teodor@sigaev.ru 208 :UBC 0 : gbt_oid_same(PG_FUNCTION_ARGS)
209 : : {
7868 bruce@momjian.us 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 : :
3281 andrew@dunslane.net 214 : 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
7868 bruce@momjian.us 215 : 0 : PG_RETURN_POINTER(result);
216 : : }
217 : :
218 : : static int
346 heikki.linnakangas@i 219 :CBC 999 : gbt_oid_ssup_cmp(Datum x, Datum y, SortSupport ssup)
220 : : {
221 : 999 : oidKEY *arg1 = (oidKEY *) DatumGetPointer(x);
222 : 999 : oidKEY *arg2 = (oidKEY *) DatumGetPointer(y);
223 : :
224 : : /* for leaf items we expect lower == upper, so only compare lower */
225 [ - + ]: 999 : if (arg1->lower > arg2->lower)
346 heikki.linnakangas@i 226 :UBC 0 : return 1;
346 heikki.linnakangas@i 227 [ + - ]:CBC 999 : else if (arg1->lower < arg2->lower)
228 : 999 : return -1;
229 : : else
346 heikki.linnakangas@i 230 :UBC 0 : return 0;
231 : : }
232 : :
233 : : Datum
346 heikki.linnakangas@i 234 :CBC 1 : gbt_oid_sortsupport(PG_FUNCTION_ARGS)
235 : : {
236 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
237 : :
238 : 1 : ssup->comparator = gbt_oid_ssup_cmp;
239 : 1 : ssup->ssup_extra = NULL;
240 : :
241 : 1 : PG_RETURN_VOID();
242 : : }
|