PostgreSQL Source Code git master
btree_uuid.c
Go to the documentation of this file.
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/sortsupport.h"
10#include "utils/uuid.h"
11
12typedef struct
13{
16} uuidKEY;
17
18
19/* GiST support functions */
28
29
30static int
31uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
32{
33 return memcmp(arg1->data, arg2->data, UUID_LEN);
34}
35
36static bool
37gbt_uuidgt(const void *a, const void *b, FmgrInfo *flinfo)
38{
39 return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) > 0;
40}
41
42static bool
43gbt_uuidge(const void *a, const void *b, FmgrInfo *flinfo)
44{
45 return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) >= 0;
46}
47
48static bool
49gbt_uuideq(const void *a, const void *b, FmgrInfo *flinfo)
50{
51 return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) == 0;
52}
53
54static bool
55gbt_uuidle(const void *a, const void *b, FmgrInfo *flinfo)
56{
57 return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) <= 0;
58}
59
60static bool
61gbt_uuidlt(const void *a, const void *b, FmgrInfo *flinfo)
62{
63 return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) < 0;
64}
65
66static int
67gbt_uuidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
68{
69 uuidKEY *ia = (uuidKEY *) (((const Nsrt *) a)->t);
70 uuidKEY *ib = (uuidKEY *) (((const Nsrt *) b)->t);
71 int res;
72
73 res = uuid_internal_cmp(&ia->lower, &ib->lower);
74 if (res == 0)
75 res = uuid_internal_cmp(&ia->upper, &ib->upper);
76 return res;
77}
78
79
80static const gbtree_ninfo tinfo =
81{
84 32, /* sizeof(gbtreekey32) */
91 NULL
92};
93
94
95/**************************************************
96 * GiST support functions
97 **************************************************/
98
101{
102 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
103 GISTENTRY *retval;
104
105 if (entry->leafkey)
106 {
107 char *r = (char *) palloc(2 * UUID_LEN);
108 pg_uuid_t *key = DatumGetUUIDP(entry->key);
109
110 retval = palloc(sizeof(GISTENTRY));
111
112 memcpy(r, key, UUID_LEN);
113 memcpy(r + UUID_LEN, key, UUID_LEN);
114 gistentryinit(*retval, PointerGetDatum(r),
115 entry->rel, entry->page,
116 entry->offset, false);
117 }
118 else
119 retval = entry;
120
121 PG_RETURN_POINTER(retval);
122}
123
124Datum
126{
127 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
128
130}
131
132Datum
134{
135 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
136 pg_uuid_t *query = PG_GETARG_UUID_P(1);
138
139 /* Oid subtype = PG_GETARG_OID(3); */
140 bool *recheck = (bool *) PG_GETARG_POINTER(4);
141 uuidKEY *kkk = (uuidKEY *) DatumGetPointer(entry->key);
143
144 /* All cases served by this function are exact */
145 *recheck = false;
146
147 key.lower = (GBT_NUMKEY *) &kkk->lower;
148 key.upper = (GBT_NUMKEY *) &kkk->upper;
149
150 PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
151 GIST_LEAF(entry), &tinfo,
152 fcinfo->flinfo));
153}
154
155Datum
157{
159 void *out = palloc(sizeof(uuidKEY));
160
161 *(int *) PG_GETARG_POINTER(1) = sizeof(uuidKEY);
162 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
163}
164
165/*
166 * Convert a uuid to a "double" value for estimating sizes of ranges.
167 */
168static double
170{
171 uint64 uu[2];
172 const double two64 = 18446744073709551616.0; /* 2^64 */
173
174 /* Source data may not be suitably aligned, so copy */
175 memcpy(uu, u->data, UUID_LEN);
176
177 /*
178 * uuid values should be considered as big-endian numbers, since that
179 * corresponds to how memcmp will compare them. On a little-endian
180 * machine, byte-swap each half so we can use native uint64 arithmetic.
181 */
182#ifndef WORDS_BIGENDIAN
183 uu[0] = pg_bswap64(uu[0]);
184 uu[1] = pg_bswap64(uu[1]);
185#endif
186
187 /*
188 * 2^128 is about 3.4e38, which in theory could exceed the range of
189 * "double" (POSIX only requires 1e37). To avoid any risk of overflow,
190 * put the decimal point between the two halves rather than treating the
191 * uuid value as a 128-bit integer.
192 */
193 return (double) uu[0] + (double) uu[1] / two64;
194}
195
196Datum
198{
199 uuidKEY *origentry = (uuidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
201 float *result = (float *) PG_GETARG_POINTER(2);
202 double olower,
203 oupper,
204 nlower,
205 nupper;
206
207 olower = uuid_2_double(&origentry->lower);
208 oupper = uuid_2_double(&origentry->upper);
209 nlower = uuid_2_double(&newentry->lower);
210 nupper = uuid_2_double(&newentry->upper);
211
212 penalty_num(result, olower, oupper, nlower, nupper);
213
214 PG_RETURN_POINTER(result);
215}
216
217Datum
219{
222 &tinfo, fcinfo->flinfo));
223}
224
225Datum
227{
228 uuidKEY *b1 = (uuidKEY *) PG_GETARG_POINTER(0);
229 uuidKEY *b2 = (uuidKEY *) PG_GETARG_POINTER(1);
230 bool *result = (bool *) PG_GETARG_POINTER(2);
231
232 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
233 PG_RETURN_POINTER(result);
234}
235
236static int
238{
239 uuidKEY *arg1 = (uuidKEY *) DatumGetPointer(x);
240 uuidKEY *arg2 = (uuidKEY *) DatumGetPointer(y);
241
242 /* for leaf items we expect lower == upper, so only compare lower */
243 return uuid_internal_cmp(&arg1->lower, &arg2->lower);
244}
245
246Datum
248{
250
252 ssup->ssup_extra = NULL;
253
255}
@ gbt_t_uuid
Definition: btree_gist.h:37
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define penalty_num(result, olower, oupper, nlower, nupper)
char GBT_NUMKEY
Datum gbt_uuid_consistent(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:133
Datum gbt_uuid_same(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:226
static int gbt_uuid_ssup_cmp(Datum x, Datum y, SortSupport ssup)
Definition: btree_uuid.c:237
static bool gbt_uuidle(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:55
static bool gbt_uuideq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:49
static bool gbt_uuidgt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:37
Datum gbt_uuid_union(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:156
Datum gbt_uuid_penalty(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:197
static double uuid_2_double(const pg_uuid_t *u)
Definition: btree_uuid.c:169
PG_FUNCTION_INFO_V1(gbt_uuid_compress)
Datum gbt_uuid_picksplit(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:218
static const gbtree_ninfo tinfo
Definition: btree_uuid.c:80
Datum gbt_uuid_fetch(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:125
static int uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
Definition: btree_uuid.c:31
static bool gbt_uuidge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:43
static bool gbt_uuidlt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:61
Datum gbt_uuid_compress(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:100
static int gbt_uuidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:67
Datum gbt_uuid_sortsupport(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:247
uint64_t uint64
Definition: c.h:503
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:171
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:245
int y
Definition: isn.c:76
int b
Definition: isn.c:74
int x
Definition: isn.c:75
int a
Definition: isn.c:73
void * palloc(Size size)
Definition: mcxt.c:1945
static uint64 pg_bswap64(uint64 x)
Definition: pg_bswap.h:89
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
OffsetNumber offset
Definition: gist.h:164
Datum key
Definition: gist.h:161
Page page
Definition: gist.h:163
Relation rel
Definition: gist.h:162
bool leafkey
Definition: gist.h:165
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
void * ssup_extra
Definition: sortsupport.h:87
Definition: uuid.h:21
unsigned char data[UUID_LEN]
Definition: uuid.h:22
pg_uuid_t upper
Definition: btree_uuid.c:15
pg_uuid_t lower
Definition: btree_uuid.c:14
static pg_uuid_t * DatumGetUUIDP(Datum X)
Definition: uuid.h:35
#define UUID_LEN
Definition: uuid.h:18
#define PG_GETARG_UUID_P(X)
Definition: uuid.h:40