Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

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/rel.h"
10#include "utils/sortsupport.h"
11#include "utils/uuid.h"
12
13typedef struct
14{
17} uuidKEY;
18
19
20/* GiST support functions */
29
30
31static int
32uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
33{
34 return memcmp(arg1->data, arg2->data, UUID_LEN);
35}
36
37static bool
38gbt_uuidgt(const void *a, const void *b, FmgrInfo *flinfo)
39{
40 return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) > 0;
41}
42
43static bool
44gbt_uuidge(const void *a, const void *b, FmgrInfo *flinfo)
45{
46 return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) >= 0;
47}
48
49static bool
50gbt_uuideq(const void *a, const void *b, FmgrInfo *flinfo)
51{
52 return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) == 0;
53}
54
55static bool
56gbt_uuidle(const void *a, const void *b, FmgrInfo *flinfo)
57{
58 return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) <= 0;
59}
60
61static bool
62gbt_uuidlt(const void *a, const void *b, FmgrInfo *flinfo)
63{
64 return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) < 0;
65}
66
67static int
68gbt_uuidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
69{
70 uuidKEY *ia = (uuidKEY *) (((const Nsrt *) a)->t);
71 uuidKEY *ib = (uuidKEY *) (((const Nsrt *) b)->t);
72 int res;
73
74 res = uuid_internal_cmp(&ia->lower, &ib->lower);
75 if (res == 0)
76 res = uuid_internal_cmp(&ia->upper, &ib->upper);
77 return res;
78}
79
80
81static const gbtree_ninfo tinfo =
82{
85 32, /* sizeof(gbtreekey32) */
92 NULL
93};
94
95
96/**************************************************
97 * GiST support functions
98 **************************************************/
99
100Datum
102{
103 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
104 GISTENTRY *retval;
105
106 if (entry->leafkey)
107 {
108 char *r = (char *) palloc(2 * UUID_LEN);
109 pg_uuid_t *key = DatumGetUUIDP(entry->key);
110
111 retval = palloc(sizeof(GISTENTRY));
112
113 memcpy(r, key, UUID_LEN);
114 memcpy(r + UUID_LEN, key, UUID_LEN);
115 gistentryinit(*retval, PointerGetDatum(r),
116 entry->rel, entry->page,
117 entry->offset, false);
118 }
119 else
120 retval = entry;
121
122 PG_RETURN_POINTER(retval);
123}
124
125Datum
127{
128 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
129
131}
132
133Datum
135{
136 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
137 pg_uuid_t *query = PG_GETARG_UUID_P(1);
139
140 /* Oid subtype = PG_GETARG_OID(3); */
141 bool *recheck = (bool *) PG_GETARG_POINTER(4);
142 uuidKEY *kkk = (uuidKEY *) DatumGetPointer(entry->key);
144
145 /* All cases served by this function are exact */
146 *recheck = false;
147
148 key.lower = (GBT_NUMKEY *) &kkk->lower;
149 key.upper = (GBT_NUMKEY *) &kkk->upper;
150
151 PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
152 GIST_LEAF(entry), &tinfo,
153 fcinfo->flinfo));
154}
155
156Datum
158{
160 void *out = palloc(sizeof(uuidKEY));
161
162 *(int *) PG_GETARG_POINTER(1) = sizeof(uuidKEY);
163 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 */
169static double
171{
172 uint64 uu[2];
173 const double two64 = 18446744073709551616.0; /* 2^64 */
174
175 /* Source data may not be suitably aligned, so copy */
176 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
184 uu[0] = pg_bswap64(uu[0]);
185 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 */
194 return (double) uu[0] + (double) uu[1] / two64;
195}
196
197Datum
199{
200 uuidKEY *origentry = (uuidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
202 float *result = (float *) PG_GETARG_POINTER(2);
203 double olower,
204 oupper,
205 nlower,
206 nupper;
207
208 olower = uuid_2_double(&origentry->lower);
209 oupper = uuid_2_double(&origentry->upper);
210 nlower = uuid_2_double(&newentry->lower);
211 nupper = uuid_2_double(&newentry->upper);
212
213 penalty_num(result, olower, oupper, nlower, nupper);
214
215 PG_RETURN_POINTER(result);
216}
217
218Datum
220{
223 &tinfo, fcinfo->flinfo));
224}
225
226Datum
228{
229 uuidKEY *b1 = (uuidKEY *) PG_GETARG_POINTER(0);
230 uuidKEY *b2 = (uuidKEY *) PG_GETARG_POINTER(1);
231 bool *result = (bool *) PG_GETARG_POINTER(2);
232
233 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
234 PG_RETURN_POINTER(result);
235}
236
237static int
239{
240 uuidKEY *arg1 = (uuidKEY *) DatumGetPointer(x);
241 uuidKEY *arg2 = (uuidKEY *) DatumGetPointer(y);
242
243 /* for leaf items we expect lower == upper, so only compare lower */
244 return uuid_internal_cmp(&arg1->lower, &arg2->lower);
245}
246
247Datum
249{
251
253 ssup->ssup_extra = NULL;
254
256}
@ 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:134
Datum gbt_uuid_same(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:227
static int gbt_uuid_ssup_cmp(Datum x, Datum y, SortSupport ssup)
Definition: btree_uuid.c:238
static bool gbt_uuidle(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:56
static bool gbt_uuideq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:50
static bool gbt_uuidgt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:38
Datum gbt_uuid_union(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:157
Datum gbt_uuid_penalty(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:198
static double uuid_2_double(const pg_uuid_t *u)
Definition: btree_uuid.c:170
PG_FUNCTION_INFO_V1(gbt_uuid_compress)
Datum gbt_uuid_picksplit(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:219
static const gbtree_ninfo tinfo
Definition: btree_uuid.c:81
Datum gbt_uuid_fetch(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:126
static int uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
Definition: btree_uuid.c:32
static bool gbt_uuidge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:44
static bool gbt_uuidlt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:62
Datum gbt_uuid_compress(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:101
static int gbt_uuidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_uuid.c:68
Datum gbt_uuid_sortsupport(PG_FUNCTION_ARGS)
Definition: btree_uuid.c:248
uint64_t uint64
Definition: c.h:540
#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:1365
static uint64 pg_bswap64(uint64 x)
Definition: pg_bswap.h:89
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
uint64_t Datum
Definition: postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
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:16
pg_uuid_t lower
Definition: btree_uuid.c:15
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