]> granicus.if.org Git - postgresql/blob - contrib/btree_gist/btree_oid.c
Push index operator lossiness determination down to GIST/GIN opclass
[postgresql] / contrib / btree_gist / btree_oid.c
1 #include "btree_gist.h"
2 #include "btree_utils_num.h"
3
4 typedef struct
5 {
6         Oid                     lower;
7         Oid                     upper;
8 }       oidKEY;
9
10 /*
11 ** OID ops
12 */
13 PG_FUNCTION_INFO_V1(gbt_oid_compress);
14 PG_FUNCTION_INFO_V1(gbt_oid_union);
15 PG_FUNCTION_INFO_V1(gbt_oid_picksplit);
16 PG_FUNCTION_INFO_V1(gbt_oid_consistent);
17 PG_FUNCTION_INFO_V1(gbt_oid_penalty);
18 PG_FUNCTION_INFO_V1(gbt_oid_same);
19
20 Datum           gbt_oid_compress(PG_FUNCTION_ARGS);
21 Datum           gbt_oid_union(PG_FUNCTION_ARGS);
22 Datum           gbt_oid_picksplit(PG_FUNCTION_ARGS);
23 Datum           gbt_oid_consistent(PG_FUNCTION_ARGS);
24 Datum           gbt_oid_penalty(PG_FUNCTION_ARGS);
25 Datum           gbt_oid_same(PG_FUNCTION_ARGS);
26
27
28 static bool
29 gbt_oidgt(const void *a, const void *b)
30 {
31         return (*((Oid *) a) > *((Oid *) b));
32 }
33 static bool
34 gbt_oidge(const void *a, const void *b)
35 {
36         return (*((Oid *) a) >= *((Oid *) b));
37 }
38 static bool
39 gbt_oideq(const void *a, const void *b)
40 {
41         return (*((Oid *) a) == *((Oid *) b));
42 }
43 static bool
44 gbt_oidle(const void *a, const void *b)
45 {
46         return (*((Oid *) a) <= *((Oid *) b));
47 }
48 static bool
49 gbt_oidlt(const void *a, const void *b)
50 {
51         return (*((Oid *) a) < *((Oid *) b));
52 }
53
54 static int
55 gbt_oidkey_cmp(const void *a, const void *b)
56 {
57
58         if (*(Oid *) &(((Nsrt *) a)->t[0]) > *(Oid *) &(((Nsrt *) b)->t[0]))
59                 return 1;
60         else if (*(Oid *) &(((Nsrt *) a)->t[0]) < *(Oid *) &(((Nsrt *) b)->t[0]))
61                 return -1;
62         return 0;
63
64 }
65
66
67 static const gbtree_ninfo tinfo =
68 {
69         gbt_t_oid,
70         sizeof(Oid),
71         gbt_oidgt,
72         gbt_oidge,
73         gbt_oideq,
74         gbt_oidle,
75         gbt_oidlt,
76         gbt_oidkey_cmp
77 };
78
79
80 /**************************************************
81  * Oid ops
82  **************************************************/
83
84
85 Datum
86 gbt_oid_compress(PG_FUNCTION_ARGS)
87 {
88         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
89         GISTENTRY  *retval = NULL;
90
91         PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
92 }
93
94
95 Datum
96 gbt_oid_consistent(PG_FUNCTION_ARGS)
97 {
98         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
99         Oid                     query = PG_GETARG_OID(1);
100         StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
101         /* Oid          subtype = PG_GETARG_OID(3); */
102         bool       *recheck = (bool *) PG_GETARG_POINTER(4);
103         oidKEY     *kkk = (oidKEY *) DatumGetPointer(entry->key);
104         GBT_NUMKEY_R key;
105
106         /* All cases served by this function are exact */
107         *recheck = false;
108
109         key.lower = (GBT_NUMKEY *) & kkk->lower;
110         key.upper = (GBT_NUMKEY *) & kkk->upper;
111
112         PG_RETURN_BOOL(
113                                    gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
114                 );
115 }
116
117
118 Datum
119 gbt_oid_union(PG_FUNCTION_ARGS)
120 {
121         GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
122         void       *out = palloc(sizeof(oidKEY));
123
124         *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
125         PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
126 }
127
128
129 Datum
130 gbt_oid_penalty(PG_FUNCTION_ARGS)
131 {
132         oidKEY     *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
133         oidKEY     *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
134         float      *result = (float *) PG_GETARG_POINTER(2);
135
136         penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
137
138         PG_RETURN_POINTER(result);
139 }
140
141 Datum
142 gbt_oid_picksplit(PG_FUNCTION_ARGS)
143 {
144         PG_RETURN_POINTER(gbt_num_picksplit(
145                                                                         (GistEntryVector *) PG_GETARG_POINTER(0),
146                                                                           (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
147                                                                                 &tinfo
148                                                                                 ));
149 }
150
151 Datum
152 gbt_oid_same(PG_FUNCTION_ARGS)
153 {
154         oidKEY     *b1 = (oidKEY *) PG_GETARG_POINTER(0);
155         oidKEY     *b2 = (oidKEY *) PG_GETARG_POINTER(1);
156         bool       *result = (bool *) PG_GETARG_POINTER(2);
157
158         *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
159         PG_RETURN_POINTER(result);
160 }