PostgreSQL Source Code  git master
btree_oid.c
Go to the documentation of this file.
1 /*
2  * contrib/btree_gist/btree_oid.c
3  */
4 #include "postgres.h"
5 
6 #include "btree_gist.h"
7 #include "btree_utils_num.h"
8 
9 typedef struct
10 {
11  Oid lower;
12  Oid upper;
13 } oidKEY;
14 
15 /*
16 ** OID ops
17 */
26 
27 
28 static bool
29 gbt_oidgt(const void *a, const void *b, FmgrInfo *flinfo)
30 {
31  return (*((const Oid *) a) > *((const Oid *) b));
32 }
33 static bool
34 gbt_oidge(const void *a, const void *b, FmgrInfo *flinfo)
35 {
36  return (*((const Oid *) a) >= *((const Oid *) b));
37 }
38 static bool
39 gbt_oideq(const void *a, const void *b, FmgrInfo *flinfo)
40 {
41  return (*((const Oid *) a) == *((const Oid *) b));
42 }
43 static bool
44 gbt_oidle(const void *a, const void *b, FmgrInfo *flinfo)
45 {
46  return (*((const Oid *) a) <= *((const Oid *) b));
47 }
48 static bool
49 gbt_oidlt(const void *a, const void *b, FmgrInfo *flinfo)
50 {
51  return (*((const Oid *) a) < *((const Oid *) b));
52 }
53 
54 static int
55 gbt_oidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56 {
57  oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
58  oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
59 
60  if (ia->lower == ib->lower)
61  {
62  if (ia->upper == ib->upper)
63  return 0;
64 
65  return (ia->upper > ib->upper) ? 1 : -1;
66  }
67 
68  return (ia->lower > ib->lower) ? 1 : -1;
69 }
70 
71 static float8
72 gbt_oid_dist(const void *a, const void *b, FmgrInfo *flinfo)
73 {
74  Oid aa = *(const Oid *) a;
75  Oid bb = *(const Oid *) b;
76 
77  if (aa < bb)
78  return (float8) (bb - aa);
79  else
80  return (float8) (aa - bb);
81 }
82 
83 
84 static const gbtree_ninfo tinfo =
85 {
86  gbt_t_oid,
87  sizeof(Oid),
88  8, /* sizeof(gbtreekey8) */
89  gbt_oidgt,
90  gbt_oidge,
91  gbt_oideq,
92  gbt_oidle,
93  gbt_oidlt,
96 };
97 
98 
100 Datum
102 {
103  Oid a = PG_GETARG_OID(0);
104  Oid b = PG_GETARG_OID(1);
105  Oid res;
106 
107  if (a < b)
108  res = b - a;
109  else
110  res = a - b;
112 }
113 
114 
115 /**************************************************
116  * Oid ops
117  **************************************************/
118 
119 
120 Datum
122 {
123  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124 
126 }
127 
128 Datum
130 {
131  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132 
134 }
135 
136 Datum
138 {
139  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140  Oid query = PG_GETARG_OID(1);
142 
143  /* Oid subtype = PG_GETARG_OID(3); */
144  bool *recheck = (bool *) PG_GETARG_POINTER(4);
145  oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
147 
148  /* All cases served by this function are exact */
149  *recheck = false;
150 
151  key.lower = (GBT_NUMKEY *) &kkk->lower;
152  key.upper = (GBT_NUMKEY *) &kkk->upper;
153 
154  PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query, &strategy,
155  GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
156 }
157 
158 
159 Datum
161 {
162  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
163  Oid query = PG_GETARG_OID(1);
164 
165  /* Oid subtype = PG_GETARG_OID(3); */
166  oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
168 
169  key.lower = (GBT_NUMKEY *) &kkk->lower;
170  key.upper = (GBT_NUMKEY *) &kkk->upper;
171 
172  PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry),
173  &tinfo, fcinfo->flinfo));
174 }
175 
176 
177 Datum
179 {
181  void *out = palloc(sizeof(oidKEY));
182 
183  *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
184  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
185 }
186 
187 
188 Datum
190 {
191  oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
192  oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
193  float *result = (float *) PG_GETARG_POINTER(2);
194 
195  penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
196 
197  PG_RETURN_POINTER(result);
198 }
199 
200 Datum
202 {
205  &tinfo, fcinfo->flinfo));
206 }
207 
208 Datum
210 {
211  oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
212  oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
213  bool *result = (bool *) PG_GETARG_POINTER(2);
214 
215  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
216  PG_RETURN_POINTER(result);
217 }
@ gbt_t_oid
Definition: btree_gist.h:25
static bool gbt_oidlt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_oid.c:49
Datum gbt_oid_penalty(PG_FUNCTION_ARGS)
Definition: btree_oid.c:189
Datum oid_dist(PG_FUNCTION_ARGS)
Definition: btree_oid.c:101
Datum gbt_oid_picksplit(PG_FUNCTION_ARGS)
Definition: btree_oid.c:201
static bool gbt_oidle(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_oid.c:44
static bool gbt_oideq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_oid.c:39
static bool gbt_oidge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_oid.c:34
Datum gbt_oid_compress(PG_FUNCTION_ARGS)
Definition: btree_oid.c:121
static const gbtree_ninfo tinfo
Definition: btree_oid.c:84
Datum gbt_oid_same(PG_FUNCTION_ARGS)
Definition: btree_oid.c:209
Datum gbt_oid_consistent(PG_FUNCTION_ARGS)
Definition: btree_oid.c:137
Datum gbt_oid_union(PG_FUNCTION_ARGS)
Definition: btree_oid.c:178
static int gbt_oidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_oid.c:55
static bool gbt_oidgt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_oid.c:29
PG_FUNCTION_INFO_V1(gbt_oid_compress)
Datum gbt_oid_fetch(PG_FUNCTION_ARGS)
Definition: btree_oid.c:129
Datum gbt_oid_distance(PG_FUNCTION_ARGS)
Definition: btree_oid.c:160
static float8 gbt_oid_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_oid.c:72
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
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_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define penalty_num(result, olower, oupper, nlower, nupper)
char GBT_NUMKEY
double float8
Definition: c.h:630
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#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_RETURN_OID(x)
Definition: fmgr.h:360
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:170
int b
Definition: isn.c:70
int a
Definition: isn.c:69
void * palloc(Size size)
Definition: mcxt.c:1316
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:49
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:80
uintptr_t Datum
Definition: postgres.h:64
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
unsigned int Oid
Definition: postgres_ext.h:31
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
Datum key
Definition: gist.h:160
Oid upper
Definition: btree_enum.c:16
Oid lower
Definition: btree_enum.c:15