Age Owner 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 :
9 : typedef struct
10 : {
11 : Oid lower;
12 : Oid upper;
13 : } oidKEY;
14 :
15 : /*
16 : ** OID ops
17 : */
6890 teodor 18 CBC 2 : PG_FUNCTION_INFO_V1(gbt_oid_compress);
2935 heikki.linnakangas 19 2 : PG_FUNCTION_INFO_V1(gbt_oid_fetch);
6890 teodor 20 2 : PG_FUNCTION_INFO_V1(gbt_oid_union);
21 2 : PG_FUNCTION_INFO_V1(gbt_oid_picksplit);
22 2 : PG_FUNCTION_INFO_V1(gbt_oid_consistent);
4421 tgl 23 2 : PG_FUNCTION_INFO_V1(gbt_oid_distance);
6890 teodor 24 2 : PG_FUNCTION_INFO_V1(gbt_oid_penalty);
25 2 : PG_FUNCTION_INFO_V1(gbt_oid_same);
26 :
27 :
28 : static bool
2210 andrew 29 2276 : gbt_oidgt(const void *a, const void *b, FmgrInfo *flinfo)
30 : {
4228 peter_e 31 2276 : return (*((const Oid *) a) > *((const Oid *) b));
32 : }
33 : static bool
2210 andrew 34 193 : gbt_oidge(const void *a, const void *b, FmgrInfo *flinfo)
35 : {
4228 peter_e 36 193 : return (*((const Oid *) a) >= *((const Oid *) b));
37 : }
38 : static bool
2210 andrew 39 1449 : gbt_oideq(const void *a, const void *b, FmgrInfo *flinfo)
40 : {
4228 peter_e 41 1449 : return (*((const Oid *) a) == *((const Oid *) b));
42 : }
43 : static bool
2210 andrew 44 1016 : gbt_oidle(const void *a, const void *b, FmgrInfo *flinfo)
45 : {
4228 peter_e 46 1016 : return (*((const Oid *) a) <= *((const Oid *) b));
47 : }
48 : static bool
2210 andrew 49 3093 : gbt_oidlt(const void *a, const void *b, FmgrInfo *flinfo)
50 : {
4228 peter_e 51 3093 : return (*((const Oid *) a) < *((const Oid *) b));
52 : }
53 :
54 : static int
2210 andrew 55 1464 : gbt_oidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56 : {
4228 peter_e 57 1464 : oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
58 1464 : oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
59 :
4876 teodor 60 1464 : if (ia->lower == ib->lower)
61 : {
4876 teodor 62 UBC 0 : if (ia->upper == ib->upper)
63 0 : return 0;
64 :
65 0 : return (ia->upper > ib->upper) ? 1 : -1;
66 : }
67 :
4876 teodor 68 CBC 1464 : return (ia->lower > ib->lower) ? 1 : -1;
69 : }
70 :
71 : static float8
2210 andrew 72 UBC 0 : gbt_oid_dist(const void *a, const void *b, FmgrInfo *flinfo)
73 : {
4421 tgl 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 :
4421 tgl 99 CBC 1 : PG_FUNCTION_INFO_V1(oid_dist);
100 : Datum
4421 tgl 101 UBC 0 : oid_dist(PG_FUNCTION_ARGS)
102 : {
4382 bruce 103 0 : Oid a = PG_GETARG_OID(0);
104 0 : Oid b = PG_GETARG_OID(1);
105 : Oid res;
106 :
4421 tgl 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
6890 teodor 121 CBC 1641 : gbt_oid_compress(PG_FUNCTION_ARGS)
122 : {
6797 bruce 123 1641 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124 :
2936 heikki.linnakangas 125 1641 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
126 : }
127 :
128 : Datum
2935 heikki.linnakangas 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
6890 teodor 137 CBC 2574 : gbt_oid_consistent(PG_FUNCTION_ARGS)
138 : {
6797 bruce 139 2574 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140 2574 : Oid query = PG_GETARG_OID(1);
5473 tgl 141 2574 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142 :
143 : /* Oid subtype = PG_GETARG_OID(3); */
144 2574 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
6797 bruce 145 2574 : oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
146 : GBT_NUMKEY_R key;
147 :
148 : /* All cases served by this function are exact */
5473 tgl 149 2574 : *recheck = false;
150 :
5050 bruce 151 2574 : key.lower = (GBT_NUMKEY *) &kkk->lower;
152 2574 : key.upper = (GBT_NUMKEY *) &kkk->upper;
153 :
1165 alvherre 154 2574 : PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query, &strategy,
155 : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
156 : }
157 :
158 :
159 : Datum
4421 tgl 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 :
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 :
1165 alvherre 172 0 : PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry),
173 : &tinfo, fcinfo->flinfo));
174 : }
175 :
176 :
177 : Datum
6890 teodor 178 CBC 633 : gbt_oid_union(PG_FUNCTION_ARGS)
179 : {
6797 bruce 180 633 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
181 633 : void *out = palloc(sizeof(oidKEY));
182 :
183 633 : *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
2210 andrew 184 633 : PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
185 : }
186 :
187 :
188 : Datum
6890 teodor 189 2067 : gbt_oid_penalty(PG_FUNCTION_ARGS)
190 : {
6797 bruce 191 2067 : oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
192 2067 : oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
193 2067 : float *result = (float *) PG_GETARG_POINTER(2);
194 :
6385 195 2067 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
196 :
6797 197 2067 : PG_RETURN_POINTER(result);
198 : }
199 :
200 : Datum
6890 teodor 201 4 : gbt_oid_picksplit(PG_FUNCTION_ARGS)
202 : {
1165 alvherre 203 4 : 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
6890 teodor 209 633 : gbt_oid_same(PG_FUNCTION_ARGS)
210 : {
6797 bruce 211 633 : oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
212 633 : oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
213 633 : bool *result = (bool *) PG_GETARG_POINTER(2);
214 :
2210 andrew 215 633 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
6797 bruce 216 633 : PG_RETURN_POINTER(result);
217 : }
|