Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / contrib / btree_gist / btree_text.c
blob793a25aaf43aef7678a71b2a26b5a0254680b636
1 /*
2 * $PostgreSQL$
3 */
4 #include "btree_gist.h"
5 #include "btree_utils_var.h"
6 #include "utils/builtins.h"
8 /*
9 ** Text ops
11 PG_FUNCTION_INFO_V1(gbt_text_compress);
12 PG_FUNCTION_INFO_V1(gbt_bpchar_compress);
13 PG_FUNCTION_INFO_V1(gbt_text_union);
14 PG_FUNCTION_INFO_V1(gbt_text_picksplit);
15 PG_FUNCTION_INFO_V1(gbt_text_consistent);
16 PG_FUNCTION_INFO_V1(gbt_bpchar_consistent);
17 PG_FUNCTION_INFO_V1(gbt_text_penalty);
18 PG_FUNCTION_INFO_V1(gbt_text_same);
20 Datum gbt_text_compress(PG_FUNCTION_ARGS);
21 Datum gbt_bpchar_compress(PG_FUNCTION_ARGS);
22 Datum gbt_text_union(PG_FUNCTION_ARGS);
23 Datum gbt_text_picksplit(PG_FUNCTION_ARGS);
24 Datum gbt_text_consistent(PG_FUNCTION_ARGS);
25 Datum gbt_bpchar_consistent(PG_FUNCTION_ARGS);
26 Datum gbt_text_penalty(PG_FUNCTION_ARGS);
27 Datum gbt_text_same(PG_FUNCTION_ARGS);
30 /* define for comparison */
32 static bool
33 gbt_textgt(const void *a, const void *b)
35 return (DatumGetBool(DirectFunctionCall2(text_gt, PointerGetDatum(a), PointerGetDatum(b))));
38 static bool
39 gbt_textge(const void *a, const void *b)
41 return (DatumGetBool(DirectFunctionCall2(text_ge, PointerGetDatum(a), PointerGetDatum(b))));
44 static bool
45 gbt_texteq(const void *a, const void *b)
47 return (DatumGetBool(DirectFunctionCall2(texteq, PointerGetDatum(a), PointerGetDatum(b))));
50 static bool
51 gbt_textle(const void *a, const void *b)
53 return (DatumGetBool(DirectFunctionCall2(text_le, PointerGetDatum(a), PointerGetDatum(b))));
56 static bool
57 gbt_textlt(const void *a, const void *b)
59 return (DatumGetBool(DirectFunctionCall2(text_lt, PointerGetDatum(a), PointerGetDatum(b))));
62 static int32
63 gbt_textcmp(const bytea *a, const bytea *b)
65 return DatumGetInt32(DirectFunctionCall2(bttextcmp, PointerGetDatum(a), PointerGetDatum(b)));
68 static gbtree_vinfo tinfo =
70 gbt_t_text,
72 FALSE,
73 gbt_textgt,
74 gbt_textge,
75 gbt_texteq,
76 gbt_textle,
77 gbt_textlt,
78 gbt_textcmp,
79 NULL
83 /**************************************************
84 * Text ops
85 **************************************************/
88 Datum
89 gbt_text_compress(PG_FUNCTION_ARGS)
91 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
93 if (tinfo.eml == 0)
95 tinfo.eml = pg_database_encoding_max_length();
98 PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
101 Datum
102 gbt_bpchar_compress(PG_FUNCTION_ARGS)
105 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
106 GISTENTRY *retval;
108 if (tinfo.eml == 0)
110 tinfo.eml = pg_database_encoding_max_length();
113 if (entry->leafkey)
116 Datum d = DirectFunctionCall1(rtrim1, entry->key);
117 GISTENTRY trim;
119 gistentryinit(trim, d,
120 entry->rel, entry->page,
121 entry->offset, TRUE);
122 retval = gbt_var_compress(&trim, &tinfo);
124 else
125 retval = entry;
127 PG_RETURN_POINTER(retval);
132 Datum
133 gbt_text_consistent(PG_FUNCTION_ARGS)
135 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
136 void *query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
137 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
139 /* Oid subtype = PG_GETARG_OID(3); */
140 bool *recheck = (bool *) PG_GETARG_POINTER(4);
141 bool retval;
142 GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
143 GBT_VARKEY_R r = gbt_var_key_readable(key);
145 /* All cases served by this function are exact */
146 *recheck = false;
148 if (tinfo.eml == 0)
150 tinfo.eml = pg_database_encoding_max_length();
153 retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
155 PG_RETURN_BOOL(retval);
159 Datum
160 gbt_bpchar_consistent(PG_FUNCTION_ARGS)
162 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
163 void *query = (void *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
164 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
166 /* Oid subtype = PG_GETARG_OID(3); */
167 bool *recheck = (bool *) PG_GETARG_POINTER(4);
168 bool retval;
169 GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
170 GBT_VARKEY_R r = gbt_var_key_readable(key);
171 void *trim = (void *) DatumGetPointer(DirectFunctionCall1(rtrim1, PointerGetDatum(query)));
173 /* All cases served by this function are exact */
174 *recheck = false;
176 if (tinfo.eml == 0)
178 tinfo.eml = pg_database_encoding_max_length();
181 retval = gbt_var_consistent(&r, trim, &strategy, GIST_LEAF(entry), &tinfo);
182 PG_RETURN_BOOL(retval);
186 Datum
187 gbt_text_union(PG_FUNCTION_ARGS)
189 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
190 int32 *size = (int *) PG_GETARG_POINTER(1);
192 PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
196 Datum
197 gbt_text_picksplit(PG_FUNCTION_ARGS)
199 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
200 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
202 gbt_var_picksplit(entryvec, v, &tinfo);
203 PG_RETURN_POINTER(v);
206 Datum
207 gbt_text_same(PG_FUNCTION_ARGS)
209 Datum d1 = PG_GETARG_DATUM(0);
210 Datum d2 = PG_GETARG_DATUM(1);
211 bool *result = (bool *) PG_GETARG_POINTER(2);
213 PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
217 Datum
218 gbt_text_penalty(PG_FUNCTION_ARGS)
220 GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
221 GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
222 float *result = (float *) PG_GETARG_POINTER(2);
224 PG_RETURN_POINTER(gbt_var_penalty(result, o, n, &tinfo));