]> granicus.if.org Git - postgresql/blob - contrib/btree_gist/btree_text.c
Convert contrib modules to use the extension facility.
[postgresql] / contrib / btree_gist / btree_text.c
1 /*
2  * contrib/btree_gist/btree_text.c
3  */
4 #include "btree_gist.h"
5 #include "btree_utils_var.h"
6 #include "catalog/pg_collation.h"
7 #include "utils/builtins.h"
8
9 /*
10 ** Text ops
11 */
12 PG_FUNCTION_INFO_V1(gbt_text_compress);
13 PG_FUNCTION_INFO_V1(gbt_bpchar_compress);
14 PG_FUNCTION_INFO_V1(gbt_text_union);
15 PG_FUNCTION_INFO_V1(gbt_text_picksplit);
16 PG_FUNCTION_INFO_V1(gbt_text_consistent);
17 PG_FUNCTION_INFO_V1(gbt_bpchar_consistent);
18 PG_FUNCTION_INFO_V1(gbt_text_penalty);
19 PG_FUNCTION_INFO_V1(gbt_text_same);
20
21 Datum           gbt_text_compress(PG_FUNCTION_ARGS);
22 Datum           gbt_bpchar_compress(PG_FUNCTION_ARGS);
23 Datum           gbt_text_union(PG_FUNCTION_ARGS);
24 Datum           gbt_text_picksplit(PG_FUNCTION_ARGS);
25 Datum           gbt_text_consistent(PG_FUNCTION_ARGS);
26 Datum           gbt_bpchar_consistent(PG_FUNCTION_ARGS);
27 Datum           gbt_text_penalty(PG_FUNCTION_ARGS);
28 Datum           gbt_text_same(PG_FUNCTION_ARGS);
29
30
31 /* define for comparison */
32
33 static bool
34 gbt_textgt(const void *a, const void *b)
35 {
36         return (DatumGetBool(DirectFunctionCall2WithCollation(text_gt, DEFAULT_COLLATION_OID, PointerGetDatum(a), PointerGetDatum(b))));
37 }
38
39 static bool
40 gbt_textge(const void *a, const void *b)
41 {
42         return (DatumGetBool(DirectFunctionCall2WithCollation(text_ge, DEFAULT_COLLATION_OID, PointerGetDatum(a), PointerGetDatum(b))));
43 }
44
45 static bool
46 gbt_texteq(const void *a, const void *b)
47 {
48         return (DatumGetBool(DirectFunctionCall2WithCollation(texteq, DEFAULT_COLLATION_OID, PointerGetDatum(a), PointerGetDatum(b))));
49 }
50
51 static bool
52 gbt_textle(const void *a, const void *b)
53 {
54         return (DatumGetBool(DirectFunctionCall2WithCollation(text_le, DEFAULT_COLLATION_OID, PointerGetDatum(a), PointerGetDatum(b))));
55 }
56
57 static bool
58 gbt_textlt(const void *a, const void *b)
59 {
60         return (DatumGetBool(DirectFunctionCall2WithCollation(text_lt, DEFAULT_COLLATION_OID, PointerGetDatum(a), PointerGetDatum(b))));
61 }
62
63 static int32
64 gbt_textcmp(const bytea *a, const bytea *b)
65 {
66         return DatumGetInt32(DirectFunctionCall2WithCollation(bttextcmp, DEFAULT_COLLATION_OID, PointerGetDatum(a), PointerGetDatum(b)));
67 }
68
69 static gbtree_vinfo tinfo =
70 {
71         gbt_t_text,
72         0,
73         FALSE,
74         gbt_textgt,
75         gbt_textge,
76         gbt_texteq,
77         gbt_textle,
78         gbt_textlt,
79         gbt_textcmp,
80         NULL
81 };
82
83
84 /**************************************************
85  * Text ops
86  **************************************************/
87
88
89 Datum
90 gbt_text_compress(PG_FUNCTION_ARGS)
91 {
92         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
93
94         if (tinfo.eml == 0)
95         {
96                 tinfo.eml = pg_database_encoding_max_length();
97         }
98
99         PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
100 }
101
102 Datum
103 gbt_bpchar_compress(PG_FUNCTION_ARGS)
104 {
105
106         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
107         GISTENTRY  *retval;
108
109         if (tinfo.eml == 0)
110         {
111                 tinfo.eml = pg_database_encoding_max_length();
112         }
113
114         if (entry->leafkey)
115         {
116
117                 Datum           d = DirectFunctionCall1(rtrim1, entry->key);
118                 GISTENTRY       trim;
119
120                 gistentryinit(trim, d,
121                                           entry->rel, entry->page,
122                                           entry->offset, TRUE);
123                 retval = gbt_var_compress(&trim, &tinfo);
124         }
125         else
126                 retval = entry;
127
128         PG_RETURN_POINTER(retval);
129 }
130
131
132
133 Datum
134 gbt_text_consistent(PG_FUNCTION_ARGS)
135 {
136         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
137         void       *query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
138         StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
139
140         /* Oid          subtype = PG_GETARG_OID(3); */
141         bool       *recheck = (bool *) PG_GETARG_POINTER(4);
142         bool            retval;
143         GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
144         GBT_VARKEY_R r = gbt_var_key_readable(key);
145
146         /* All cases served by this function are exact */
147         *recheck = false;
148
149         if (tinfo.eml == 0)
150         {
151                 tinfo.eml = pg_database_encoding_max_length();
152         }
153
154         retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
155
156         PG_RETURN_BOOL(retval);
157 }
158
159
160 Datum
161 gbt_bpchar_consistent(PG_FUNCTION_ARGS)
162 {
163         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
164         void       *query = (void *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
165         StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
166
167         /* Oid          subtype = PG_GETARG_OID(3); */
168         bool       *recheck = (bool *) PG_GETARG_POINTER(4);
169         bool            retval;
170         GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
171         GBT_VARKEY_R r = gbt_var_key_readable(key);
172         void       *trim = (void *) DatumGetPointer(DirectFunctionCall1(rtrim1, PointerGetDatum(query)));
173
174         /* All cases served by this function are exact */
175         *recheck = false;
176
177         if (tinfo.eml == 0)
178         {
179                 tinfo.eml = pg_database_encoding_max_length();
180         }
181
182         retval = gbt_var_consistent(&r, trim, &strategy, GIST_LEAF(entry), &tinfo);
183         PG_RETURN_BOOL(retval);
184 }
185
186
187 Datum
188 gbt_text_union(PG_FUNCTION_ARGS)
189 {
190         GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
191         int32      *size = (int *) PG_GETARG_POINTER(1);
192
193         PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
194 }
195
196
197 Datum
198 gbt_text_picksplit(PG_FUNCTION_ARGS)
199 {
200         GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
201         GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
202
203         gbt_var_picksplit(entryvec, v, &tinfo);
204         PG_RETURN_POINTER(v);
205 }
206
207 Datum
208 gbt_text_same(PG_FUNCTION_ARGS)
209 {
210         Datum           d1 = PG_GETARG_DATUM(0);
211         Datum           d2 = PG_GETARG_DATUM(1);
212         bool       *result = (bool *) PG_GETARG_POINTER(2);
213
214         PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
215 }
216
217
218 Datum
219 gbt_text_penalty(PG_FUNCTION_ARGS)
220 {
221         GISTENTRY  *o = (GISTENTRY *) PG_GETARG_POINTER(0);
222         GISTENTRY  *n = (GISTENTRY *) PG_GETARG_POINTER(1);
223         float      *result = (float *) PG_GETARG_POINTER(2);
224
225         PG_RETURN_POINTER(gbt_var_penalty(result, o, n, &tinfo));
226 }