PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
btree_inet.c
Go to the documentation of this file.
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 
12 typedef struct inetkey
13 {
14  double lower;
15  double upper;
16 } inetKEY;
17 
18 /*
19 ** inet ops
20 */
27 
28 
29 static bool
30 gbt_inetgt(const void *a, const void *b, FmgrInfo *flinfo)
31 {
32  return (*((const double *) a) > *((const double *) b));
33 }
34 static bool
35 gbt_inetge(const void *a, const void *b, FmgrInfo *flinfo)
36 {
37  return (*((const double *) a) >= *((const double *) b));
38 }
39 static bool
40 gbt_ineteq(const void *a, const void *b, FmgrInfo *flinfo)
41 {
42  return (*((const double *) a) == *((const double *) b));
43 }
44 static bool
45 gbt_inetle(const void *a, const void *b, FmgrInfo *flinfo)
46 {
47  return (*((const double *) a) <= *((const double *) b));
48 }
49 static bool
50 gbt_inetlt(const void *a, const void *b, FmgrInfo *flinfo)
51 {
52  return (*((const double *) a) < *((const double *) b));
53 }
54 
55 static int
56 gbt_inetkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57 {
58  inetKEY *ia = (inetKEY *) (((const Nsrt *) a)->t);
59  inetKEY *ib = (inetKEY *) (((const Nsrt *) b)->t);
60 
61  if (ia->lower == ib->lower)
62  {
63  if (ia->upper == ib->upper)
64  return 0;
65 
66  return (ia->upper > ib->upper) ? 1 : -1;
67  }
68 
69  return (ia->lower > ib->lower) ? 1 : -1;
70 }
71 
72 
73 static const gbtree_ninfo tinfo =
74 {
75  gbt_t_inet,
76  sizeof(double),
77  16, /* sizeof(gbtreekey16) */
78  gbt_inetgt,
79  gbt_inetge,
80  gbt_ineteq,
81  gbt_inetle,
82  gbt_inetlt,
84  NULL
85 };
86 
87 
88 /**************************************************
89  * inet ops
90  **************************************************/
91 
92 
93 Datum
95 {
96  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
97  GISTENTRY *retval;
98 
99  if (entry->leafkey)
100  {
101  inetKEY *r = (inetKEY *) palloc(sizeof(inetKEY));
102 
103  retval = palloc(sizeof(GISTENTRY));
105  r->upper = r->lower;
106  gistentryinit(*retval, PointerGetDatum(r),
107  entry->rel, entry->page,
108  entry->offset, FALSE);
109  }
110  else
111  retval = entry;
112 
113  PG_RETURN_POINTER(retval);
114 }
115 
116 
117 Datum
119 {
120  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
123 
124  /* Oid subtype = PG_GETARG_OID(3); */
125  bool *recheck = (bool *) PG_GETARG_POINTER(4);
126  inetKEY *kkk = (inetKEY *) DatumGetPointer(entry->key);
127  GBT_NUMKEY_R key;
128 
129  /* All cases served by this function are inexact */
130  *recheck = true;
131 
132  key.lower = (GBT_NUMKEY *) &kkk->lower;
133  key.upper = (GBT_NUMKEY *) &kkk->upper;
134 
135  PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query,
136  &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
137 }
138 
139 
140 Datum
142 {
144  void *out = palloc(sizeof(inetKEY));
145 
146  *(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY);
147  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
148 }
149 
150 
151 Datum
153 {
154  inetKEY *origentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
155  inetKEY *newentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
156  float *result = (float *) PG_GETARG_POINTER(2);
157 
158  penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
159 
160  PG_RETURN_POINTER(result);
161 
162 }
163 
164 Datum
166 {
170  &tinfo, fcinfo->flinfo
171  ));
172 }
173 
174 Datum
176 {
177  inetKEY *b1 = (inetKEY *) PG_GETARG_POINTER(0);
178  inetKEY *b2 = (inetKEY *) PG_GETARG_POINTER(1);
179  bool *result = (bool *) PG_GETARG_POINTER(2);
180 
181  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
182  PG_RETURN_POINTER(result);
183 }
#define GIST_LEAF(entry)
Definition: gist.h:133
Relation rel
Definition: gist.h:124
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Definition: fmgr.h:56
const GBT_NUMKEY * lower
struct inetkey inetKEY
PG_FUNCTION_INFO_V1(gbt_inet_compress)
#define PointerGetDatum(X)
Definition: postgres.h:562
#define INETOID
Definition: pg_type.h:448
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
static bool gbt_inetle(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_inet.c:45
uint16 StrategyNumber
Definition: stratnum.h:22
return result
Definition: formatting.c:1618
const GBT_NUMKEY * upper
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
Datum gbt_inet_penalty(PG_FUNCTION_ARGS)
Definition: btree_inet.c:152
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
static bool gbt_inetgt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_inet.c:30
Page page
Definition: gist.h:125
#define FALSE
Definition: c.h:221
#define penalty_num(result, olower, oupper, nlower, nupper)
double convert_network_to_scalar(Datum value, Oid typid)
Definition: network.c:897
Datum key
Definition: gist.h:123
char GBT_NUMKEY
Datum gbt_inet_compress(PG_FUNCTION_ARGS)
Definition: btree_inet.c:94
bool leafkey
Definition: gist.h:127
Datum gbt_inet_picksplit(PG_FUNCTION_ARGS)
Definition: btree_inet.c:165
Datum gbt_inet_union(PG_FUNCTION_ARGS)
Definition: btree_inet.c:141
double upper
Definition: btree_inet.c:15
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
static int gbt_inetkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_inet.c:56
static const gbtree_ninfo tinfo
Definition: btree_inet.c:73
#define NULL
Definition: c.h:229
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:169
static bool gbt_ineteq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_inet.c:40
Datum gbt_inet_consistent(PG_FUNCTION_ARGS)
Definition: btree_inet.c:118
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:237
#define DatumGetPointer(X)
Definition: postgres.h:555
double lower
Definition: btree_inet.c:14
void * palloc(Size size)
Definition: mcxt.c:849
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
static bool gbt_inetge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_inet.c:35
OffsetNumber offset
Definition: gist.h:126
static bool gbt_inetlt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_inet.c:50
Datum gbt_inet_same(PG_FUNCTION_ARGS)
Definition: btree_inet.c:175
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)