PolarDB-for-PostgreSQL
238 строк · 5.6 Кб
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/uuid.h"10
11typedef struct12{
13pg_uuid_t lower,14upper;15} uuidKEY;16
17
18/*
19* UUID ops
20*/
21PG_FUNCTION_INFO_V1(gbt_uuid_compress);22PG_FUNCTION_INFO_V1(gbt_uuid_fetch);23PG_FUNCTION_INFO_V1(gbt_uuid_union);24PG_FUNCTION_INFO_V1(gbt_uuid_picksplit);25PG_FUNCTION_INFO_V1(gbt_uuid_consistent);26PG_FUNCTION_INFO_V1(gbt_uuid_penalty);27PG_FUNCTION_INFO_V1(gbt_uuid_same);28
29
30static int31uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)32{
33return memcmp(arg1->data, arg2->data, UUID_LEN);34}
35
36static bool37gbt_uuidgt(const void *a, const void *b, FmgrInfo *flinfo)38{
39return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) > 0;40}
41
42static bool43gbt_uuidge(const void *a, const void *b, FmgrInfo *flinfo)44{
45return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) >= 0;46}
47
48static bool49gbt_uuideq(const void *a, const void *b, FmgrInfo *flinfo)50{
51return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) == 0;52}
53
54static bool55gbt_uuidle(const void *a, const void *b, FmgrInfo *flinfo)56{
57return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) <= 0;58}
59
60static bool61gbt_uuidlt(const void *a, const void *b, FmgrInfo *flinfo)62{
63return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) < 0;64}
65
66static int67gbt_uuidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)68{
69uuidKEY *ia = (uuidKEY *) (((const Nsrt *) a)->t);70uuidKEY *ib = (uuidKEY *) (((const Nsrt *) b)->t);71int res;72
73res = uuid_internal_cmp(&ia->lower, &ib->lower);74if (res == 0)75res = uuid_internal_cmp(&ia->upper, &ib->upper);76return res;77}
78
79
80static const gbtree_ninfo tinfo =81{
82gbt_t_uuid,83UUID_LEN,8432, /* sizeof(gbtreekey32) */85gbt_uuidgt,86gbt_uuidge,87gbt_uuideq,88gbt_uuidle,89gbt_uuidlt,90gbt_uuidkey_cmp,91NULL92};93
94
95/**************************************************
96* uuid ops
97**************************************************/
98
99
100Datum
101gbt_uuid_compress(PG_FUNCTION_ARGS)102{
103GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);104GISTENTRY *retval;105
106if (entry->leafkey)107{108char *r = (char *) palloc(2 * UUID_LEN);109pg_uuid_t *key = DatumGetUUIDP(entry->key);110
111retval = palloc(sizeof(GISTENTRY));112
113memcpy((void *) r, (void *) key, UUID_LEN);114memcpy((void *) (r + UUID_LEN), (void *) key, UUID_LEN);115gistentryinit(*retval, PointerGetDatum(r),116entry->rel, entry->page,117entry->offset, false);118}119else120retval = entry;121
122PG_RETURN_POINTER(retval);123}
124
125Datum
126gbt_uuid_fetch(PG_FUNCTION_ARGS)127{
128GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);129
130PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));131}
132
133Datum
134gbt_uuid_consistent(PG_FUNCTION_ARGS)135{
136GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);137pg_uuid_t *query = PG_GETARG_UUID_P(1);138StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);139
140/* Oid subtype = PG_GETARG_OID(3); */141bool *recheck = (bool *) PG_GETARG_POINTER(4);142uuidKEY *kkk = (uuidKEY *) DatumGetPointer(entry->key);143GBT_NUMKEY_R key;144
145/* All cases served by this function are exact */146*recheck = false;147
148key.lower = (GBT_NUMKEY *) &kkk->lower;149key.upper = (GBT_NUMKEY *) &kkk->upper;150
151PG_RETURN_BOOL(152gbt_num_consistent(&key, (void *) query, &strategy,153GIST_LEAF(entry), &tinfo, fcinfo->flinfo)154);155}
156
157Datum
158gbt_uuid_union(PG_FUNCTION_ARGS)159{
160GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);161void *out = palloc(sizeof(uuidKEY));162
163*(int *) PG_GETARG_POINTER(1) = sizeof(uuidKEY);164PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));165}
166
167/*
168* Convert a uuid to a "double" value for estimating sizes of ranges.
169*/
170static double171uuid_2_double(const pg_uuid_t *u)172{
173uint64 uu[2];174const double two64 = 18446744073709551616.0; /* 2^64 */175
176/* Source data may not be suitably aligned, so copy */177memcpy(uu, u->data, UUID_LEN);178
179/*180* uuid values should be considered as big-endian numbers, since that
181* corresponds to how memcmp will compare them. On a little-endian
182* machine, byte-swap each half so we can use native uint64 arithmetic.
183*/
184#ifndef WORDS_BIGENDIAN185uu[0] = pg_bswap64(uu[0]);186uu[1] = pg_bswap64(uu[1]);187#endif188
189/*190* 2^128 is about 3.4e38, which in theory could exceed the range of
191* "double" (POSIX only requires 1e37). To avoid any risk of overflow,
192* put the decimal point between the two halves rather than treating the
193* uuid value as a 128-bit integer.
194*/
195return (double) uu[0] + (double) uu[1] / two64;196}
197
198Datum
199gbt_uuid_penalty(PG_FUNCTION_ARGS)200{
201uuidKEY *origentry = (uuidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);202uuidKEY *newentry = (uuidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);203float *result = (float *) PG_GETARG_POINTER(2);204double olower,205oupper,206nlower,207nupper;208
209olower = uuid_2_double(&origentry->lower);210oupper = uuid_2_double(&origentry->upper);211nlower = uuid_2_double(&newentry->lower);212nupper = uuid_2_double(&newentry->upper);213
214penalty_num(result, olower, oupper, nlower, nupper);215
216PG_RETURN_POINTER(result);217}
218
219Datum
220gbt_uuid_picksplit(PG_FUNCTION_ARGS)221{
222PG_RETURN_POINTER(gbt_num_picksplit(223(GistEntryVector *) PG_GETARG_POINTER(0),224(GIST_SPLITVEC *) PG_GETARG_POINTER(1),225&tinfo, fcinfo->flinfo226));227}
228
229Datum
230gbt_uuid_same(PG_FUNCTION_ARGS)231{
232uuidKEY *b1 = (uuidKEY *) PG_GETARG_POINTER(0);233uuidKEY *b2 = (uuidKEY *) PG_GETARG_POINTER(1);234bool *result = (bool *) PG_GETARG_POINTER(2);235
236*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);237PG_RETURN_POINTER(result);238}
239