PolarDB-for-PostgreSQL
190 строк · 4.3 Кб
1/*
2* contrib/btree_gist/btree_inet.c
3*/
4#include "postgres.h"5
6#include "btree_gist.h"7#include "btree_utils_num.h"8#include "utils/builtins.h"9#include "utils/inet.h"10#include "catalog/pg_type.h"11
12typedef struct inetkey13{
14double lower;15double upper;16} inetKEY;17
18/*
19** inet ops
20*/
21PG_FUNCTION_INFO_V1(gbt_inet_compress);22PG_FUNCTION_INFO_V1(gbt_inet_union);23PG_FUNCTION_INFO_V1(gbt_inet_picksplit);24PG_FUNCTION_INFO_V1(gbt_inet_consistent);25PG_FUNCTION_INFO_V1(gbt_inet_penalty);26PG_FUNCTION_INFO_V1(gbt_inet_same);27
28
29static bool30gbt_inetgt(const void *a, const void *b, FmgrInfo *flinfo)31{
32return (*((const double *) a) > *((const double *) b));33}
34static bool35gbt_inetge(const void *a, const void *b, FmgrInfo *flinfo)36{
37return (*((const double *) a) >= *((const double *) b));38}
39static bool40gbt_ineteq(const void *a, const void *b, FmgrInfo *flinfo)41{
42return (*((const double *) a) == *((const double *) b));43}
44static bool45gbt_inetle(const void *a, const void *b, FmgrInfo *flinfo)46{
47return (*((const double *) a) <= *((const double *) b));48}
49static bool50gbt_inetlt(const void *a, const void *b, FmgrInfo *flinfo)51{
52return (*((const double *) a) < *((const double *) b));53}
54
55static int56gbt_inetkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)57{
58inetKEY *ia = (inetKEY *) (((const Nsrt *) a)->t);59inetKEY *ib = (inetKEY *) (((const Nsrt *) b)->t);60
61if (ia->lower == ib->lower)62{63if (ia->upper == ib->upper)64return 0;65
66return (ia->upper > ib->upper) ? 1 : -1;67}68
69return (ia->lower > ib->lower) ? 1 : -1;70}
71
72
73static const gbtree_ninfo tinfo =74{
75gbt_t_inet,76sizeof(double),7716, /* sizeof(gbtreekey16) */78gbt_inetgt,79gbt_inetge,80gbt_ineteq,81gbt_inetle,82gbt_inetlt,83gbt_inetkey_cmp,84NULL85};86
87
88/**************************************************
89* inet ops
90**************************************************/
91
92
93Datum
94gbt_inet_compress(PG_FUNCTION_ARGS)95{
96GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);97GISTENTRY *retval;98
99if (entry->leafkey)100{101inetKEY *r = (inetKEY *) palloc(sizeof(inetKEY));102bool failure = false;103
104retval = palloc(sizeof(GISTENTRY));105r->lower = convert_network_to_scalar(entry->key, INETOID, &failure);106Assert(!failure);107r->upper = r->lower;108gistentryinit(*retval, PointerGetDatum(r),109entry->rel, entry->page,110entry->offset, false);111}112else113retval = entry;114
115PG_RETURN_POINTER(retval);116}
117
118
119Datum
120gbt_inet_consistent(PG_FUNCTION_ARGS)121{
122GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);123Datum dquery = PG_GETARG_DATUM(1);124StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);125
126/* Oid subtype = PG_GETARG_OID(3); */127bool *recheck = (bool *) PG_GETARG_POINTER(4);128inetKEY *kkk = (inetKEY *) DatumGetPointer(entry->key);129GBT_NUMKEY_R key;130double query;131bool failure = false;132
133query = convert_network_to_scalar(dquery, INETOID, &failure);134Assert(!failure);135
136/* All cases served by this function are inexact */137*recheck = true;138
139key.lower = (GBT_NUMKEY *) &kkk->lower;140key.upper = (GBT_NUMKEY *) &kkk->upper;141
142PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query,143&strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo));144}
145
146
147Datum
148gbt_inet_union(PG_FUNCTION_ARGS)149{
150GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);151void *out = palloc(sizeof(inetKEY));152
153*(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY);154PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));155}
156
157
158Datum
159gbt_inet_penalty(PG_FUNCTION_ARGS)160{
161inetKEY *origentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);162inetKEY *newentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);163float *result = (float *) PG_GETARG_POINTER(2);164
165penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);166
167PG_RETURN_POINTER(result);168
169}
170
171Datum
172gbt_inet_picksplit(PG_FUNCTION_ARGS)173{
174PG_RETURN_POINTER(gbt_num_picksplit(175(GistEntryVector *) PG_GETARG_POINTER(0),176(GIST_SPLITVEC *) PG_GETARG_POINTER(1),177&tinfo, fcinfo->flinfo178));179}
180
181Datum
182gbt_inet_same(PG_FUNCTION_ARGS)183{
184inetKEY *b1 = (inetKEY *) PG_GETARG_POINTER(0);185inetKEY *b2 = (inetKEY *) PG_GETARG_POINTER(1);186bool *result = (bool *) PG_GETARG_POINTER(2);187
188*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);189PG_RETURN_POINTER(result);190}
191