Age Owner Branch data TLA Line data Source code
1 : : /*
2 : : * contrib/btree_gist/btree_uuid.c
3 : : */
4 : : #include "postgres.h"
5 : :
6 : : #include "btree_gist.h"
7 : : #include "btree_utils_num.h"
8 : : #include "port/pg_bswap.h"
9 : : #include "utils/rel.h"
10 : : #include "utils/sortsupport.h"
11 : : #include "utils/uuid.h"
12 : :
13 : : typedef struct
14 : : {
15 : : pg_uuid_t lower,
16 : : upper;
17 : : } uuidKEY;
18 : :
19 : :
20 : : /* GiST support functions */
3203 tgl@sss.pgh.pa.us 21 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_uuid_compress);
22 : 4 : PG_FUNCTION_INFO_V1(gbt_uuid_fetch);
23 : 4 : PG_FUNCTION_INFO_V1(gbt_uuid_union);
24 : 4 : PG_FUNCTION_INFO_V1(gbt_uuid_picksplit);
25 : 4 : PG_FUNCTION_INFO_V1(gbt_uuid_consistent);
26 : 4 : PG_FUNCTION_INFO_V1(gbt_uuid_penalty);
27 : 4 : PG_FUNCTION_INFO_V1(gbt_uuid_same);
156 heikki.linnakangas@i 28 : 4 : PG_FUNCTION_INFO_V1(gbt_uuid_sortsupport);
29 : :
30 : :
31 : : static int
3203 tgl@sss.pgh.pa.us 32 : 12334 : uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
33 : : {
34 : 12334 : return memcmp(arg1->data, arg2->data, UUID_LEN);
35 : : }
36 : :
37 : : static bool
3091 andrew@dunslane.net 38 : 2103 : gbt_uuidgt(const void *a, const void *b, FmgrInfo *flinfo)
39 : : {
3203 tgl@sss.pgh.pa.us 40 : 2103 : return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) > 0;
41 : : }
42 : :
43 : : static bool
3091 andrew@dunslane.net 44 : 309 : gbt_uuidge(const void *a, const void *b, FmgrInfo *flinfo)
45 : : {
3203 tgl@sss.pgh.pa.us 46 : 309 : return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) >= 0;
47 : : }
48 : :
49 : : static bool
3091 andrew@dunslane.net 50 : 151 : gbt_uuideq(const void *a, const void *b, FmgrInfo *flinfo)
51 : : {
3203 tgl@sss.pgh.pa.us 52 : 151 : return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) == 0;
53 : : }
54 : :
55 : : static bool
3091 andrew@dunslane.net 56 : 467 : gbt_uuidle(const void *a, const void *b, FmgrInfo *flinfo)
57 : : {
3203 tgl@sss.pgh.pa.us 58 : 467 : return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) <= 0;
59 : : }
60 : :
61 : : static bool
3091 andrew@dunslane.net 62 : 2255 : gbt_uuidlt(const void *a, const void *b, FmgrInfo *flinfo)
63 : : {
3203 tgl@sss.pgh.pa.us 64 : 2255 : return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) < 0;
65 : : }
66 : :
67 : : static int
3091 andrew@dunslane.net 68 : 1203 : gbt_uuidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
69 : : {
3203 tgl@sss.pgh.pa.us 70 : 1203 : uuidKEY *ia = (uuidKEY *) (((const Nsrt *) a)->t);
71 : 1203 : uuidKEY *ib = (uuidKEY *) (((const Nsrt *) b)->t);
72 : : int res;
73 : :
74 : 1203 : res = uuid_internal_cmp(&ia->lower, &ib->lower);
75 [ - + ]: 1203 : if (res == 0)
3203 tgl@sss.pgh.pa.us 76 :UBC 0 : res = uuid_internal_cmp(&ia->upper, &ib->upper);
3203 tgl@sss.pgh.pa.us 77 :CBC 1203 : return res;
78 : : }
79 : :
80 : :
81 : : static const gbtree_ninfo tinfo =
82 : : {
83 : : gbt_t_uuid,
84 : : UUID_LEN,
85 : : 32, /* sizeof(gbtreekey32) */
86 : : gbt_uuidgt,
87 : : gbt_uuidge,
88 : : gbt_uuideq,
89 : : gbt_uuidle,
90 : : gbt_uuidlt,
91 : : gbt_uuidkey_cmp,
92 : : NULL
93 : : };
94 : :
95 : :
96 : : /**************************************************
97 : : * GiST support functions
98 : : **************************************************/
99 : :
100 : : Datum
101 : 607 : gbt_uuid_compress(PG_FUNCTION_ARGS)
102 : : {
103 : 607 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
104 : : GISTENTRY *retval;
105 : :
106 [ + + ]: 607 : if (entry->leafkey)
107 : : {
108 : 603 : char *r = (char *) palloc(2 * UUID_LEN);
109 : 603 : pg_uuid_t *key = DatumGetUUIDP(entry->key);
110 : :
111 : 603 : retval = palloc(sizeof(GISTENTRY));
112 : :
942 peter@eisentraut.org 113 : 603 : memcpy(r, key, UUID_LEN);
114 : 603 : memcpy(r + UUID_LEN, key, UUID_LEN);
3203 tgl@sss.pgh.pa.us 115 : 603 : gistentryinit(*retval, PointerGetDatum(r),
116 : : entry->rel, entry->page,
117 : : entry->offset, false);
118 : : }
119 : : else
120 : 4 : retval = entry;
121 : :
122 : 607 : PG_RETURN_POINTER(retval);
123 : : }
124 : :
125 : : Datum
3203 tgl@sss.pgh.pa.us 126 :UBC 0 : gbt_uuid_fetch(PG_FUNCTION_ARGS)
127 : : {
128 : 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
129 : :
130 : 0 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
131 : : }
132 : :
133 : : Datum
3203 tgl@sss.pgh.pa.us 134 :CBC 1679 : gbt_uuid_consistent(PG_FUNCTION_ARGS)
135 : : {
136 : 1679 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
137 : 1679 : pg_uuid_t *query = PG_GETARG_UUID_P(1);
138 : 1679 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
139 : :
140 : : /* Oid subtype = PG_GETARG_OID(3); */
141 : 1679 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
142 : 1679 : uuidKEY *kkk = (uuidKEY *) DatumGetPointer(entry->key);
143 : : GBT_NUMKEY_R key;
144 : :
145 : : /* All cases served by this function are exact */
146 : 1679 : *recheck = false;
147 : :
148 : 1679 : key.lower = (GBT_NUMKEY *) &kkk->lower;
149 : 1679 : key.upper = (GBT_NUMKEY *) &kkk->upper;
150 : :
282 peter@eisentraut.org 151 : 1679 : PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
152 : : GIST_LEAF(entry), &tinfo,
153 : : fcinfo->flinfo));
154 : : }
155 : :
156 : : Datum
3203 tgl@sss.pgh.pa.us 157 : 1 : gbt_uuid_union(PG_FUNCTION_ARGS)
158 : : {
159 : 1 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
160 : 1 : void *out = palloc(sizeof(uuidKEY));
161 : :
162 : 1 : *(int *) PG_GETARG_POINTER(1) = sizeof(uuidKEY);
282 peter@eisentraut.org 163 : 1 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
164 : : }
165 : :
166 : : /*
167 : : * Convert a uuid to a "double" value for estimating sizes of ranges.
168 : : */
169 : : static double
3203 tgl@sss.pgh.pa.us 170 :UBC 0 : uuid_2_double(const pg_uuid_t *u)
171 : : {
172 : : uint64 uu[2];
2999 173 : 0 : const double two64 = 18446744073709551616.0; /* 2^64 */
174 : :
175 : : /* Source data may not be suitably aligned, so copy */
3203 176 : 0 : memcpy(uu, u->data, UUID_LEN);
177 : :
178 : : /*
179 : : * uuid values should be considered as big-endian numbers, since that
180 : : * corresponds to how memcmp will compare them. On a little-endian
181 : : * machine, byte-swap each half so we can use native uint64 arithmetic.
182 : : */
183 : : #ifndef WORDS_BIGENDIAN
2899 andres@anarazel.de 184 : 0 : uu[0] = pg_bswap64(uu[0]);
185 : 0 : uu[1] = pg_bswap64(uu[1]);
186 : : #endif
187 : :
188 : : /*
189 : : * 2^128 is about 3.4e38, which in theory could exceed the range of
190 : : * "double" (POSIX only requires 1e37). To avoid any risk of overflow,
191 : : * put the decimal point between the two halves rather than treating the
192 : : * uuid value as a 128-bit integer.
193 : : */
3203 tgl@sss.pgh.pa.us 194 : 0 : return (double) uu[0] + (double) uu[1] / two64;
195 : : }
196 : :
197 : : Datum
198 : 0 : gbt_uuid_penalty(PG_FUNCTION_ARGS)
199 : : {
200 : 0 : uuidKEY *origentry = (uuidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
201 : 0 : uuidKEY *newentry = (uuidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
202 : 0 : float *result = (float *) PG_GETARG_POINTER(2);
203 : : double olower,
204 : : oupper,
205 : : nlower,
206 : : nupper;
207 : :
208 : 0 : olower = uuid_2_double(&origentry->lower);
209 : 0 : oupper = uuid_2_double(&origentry->upper);
210 : 0 : nlower = uuid_2_double(&newentry->lower);
211 : 0 : nupper = uuid_2_double(&newentry->upper);
212 : :
213 [ # # # # : 0 : penalty_num(result, olower, oupper, nlower, nupper);
# # ]
214 : :
215 : 0 : PG_RETURN_POINTER(result);
216 : : }
217 : :
218 : : Datum
3203 tgl@sss.pgh.pa.us 219 :CBC 3 : gbt_uuid_picksplit(PG_FUNCTION_ARGS)
220 : : {
2046 alvherre@alvh.no-ip. 221 : 3 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
222 : : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
223 : : &tinfo, fcinfo->flinfo));
224 : : }
225 : :
226 : : Datum
3203 tgl@sss.pgh.pa.us 227 :UBC 0 : gbt_uuid_same(PG_FUNCTION_ARGS)
228 : : {
229 : 0 : uuidKEY *b1 = (uuidKEY *) PG_GETARG_POINTER(0);
230 : 0 : uuidKEY *b2 = (uuidKEY *) PG_GETARG_POINTER(1);
231 : 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
232 : :
3091 andrew@dunslane.net 233 : 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
3203 tgl@sss.pgh.pa.us 234 : 0 : PG_RETURN_POINTER(result);
235 : : }
236 : :
237 : : static int
156 heikki.linnakangas@i 238 :CBC 5846 : gbt_uuid_ssup_cmp(Datum x, Datum y, SortSupport ssup)
239 : : {
240 : 5846 : uuidKEY *arg1 = (uuidKEY *) DatumGetPointer(x);
241 : 5846 : uuidKEY *arg2 = (uuidKEY *) DatumGetPointer(y);
242 : :
243 : : /* for leaf items we expect lower == upper, so only compare lower */
244 : 5846 : return uuid_internal_cmp(&arg1->lower, &arg2->lower);
245 : : }
246 : :
247 : : Datum
248 : 1 : gbt_uuid_sortsupport(PG_FUNCTION_ARGS)
249 : : {
250 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
251 : :
252 : 1 : ssup->comparator = gbt_uuid_ssup_cmp;
253 : 1 : ssup->ssup_extra = NULL;
254 : :
255 : 1 : PG_RETURN_VOID();
256 : : }
|