1 |
/* |
2 |
* integrate Hyper Estraier into PostgreSQL |
3 |
* |
4 |
* Dobrica Pavlinusic <dpavlin@rot13.org> 2005-05-19 |
5 |
* |
6 |
* TODO: |
7 |
* - all |
8 |
* |
9 |
* NOTES: |
10 |
* - clear structures with memset to support hash indexes (who whould like |
11 |
* to create hash index on table returned from function?) |
12 |
* - number of returned rows is set by PostgreSQL evaluator, see: |
13 |
* http://archives.postgresql.org/pgsql-hackers/2005-02/msg00546.php |
14 |
* |
15 |
* Based on: |
16 |
* - C example from PostgreSQL documentation (BSD licence) |
17 |
* - example002.c from Hyper Estraier (GPL) |
18 |
* - _textin/_textout from pgcurl.c (LGPL) |
19 |
* |
20 |
* This code is licenced under GPL |
21 |
*/ |
22 |
|
23 |
#include "postgres.h" |
24 |
#include "fmgr.h" |
25 |
#include "funcapi.h" |
26 |
#include "utils/builtins.h" |
27 |
#include "utils/array.h" |
28 |
#include "miscadmin.h" |
29 |
#include <estraier.h> |
30 |
#include <cabin.h> |
31 |
|
32 |
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) |
33 |
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) |
34 |
#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) |
35 |
#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) |
36 |
|
37 |
/* SortMem got renamed in PostgreSQL 8.0 */ |
38 |
#ifndef SortMem |
39 |
#define SortMem 16 * 1024 |
40 |
#endif |
41 |
|
42 |
/* prototype */ |
43 |
char *attr2text(ESTDOC *doc, char *attr); |
44 |
|
45 |
|
46 |
/* work in progress */ |
47 |
PG_FUNCTION_INFO_V1(pgest_attr); |
48 |
Datum pgest_attr(PG_FUNCTION_ARGS) |
49 |
{ |
50 |
ArrayType *attr_arr = PG_GETARG_ARRAYTYPE_P(6); |
51 |
Oid attr_element_type = ARR_ELEMTYPE(attr_arr); |
52 |
int attr_ndims = ARR_NDIM(attr_arr); |
53 |
int *attr_dim_counts = ARR_DIMS(attr_arr); |
54 |
int *attr_dim_lower_bounds = ARR_LBOUND(attr_arr); |
55 |
int ncols = 0; |
56 |
int nrows = 0; |
57 |
int indx[MAXDIM]; |
58 |
int16 attr_len; |
59 |
bool attr_byval; |
60 |
char attr_align; |
61 |
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
62 |
AttInMetadata *attinmeta; |
63 |
TupleDesc tupdesc; |
64 |
Tuplestorestate *tupstore = NULL; |
65 |
HeapTuple tuple; |
66 |
MemoryContext per_query_ctx; |
67 |
MemoryContext oldcontext; |
68 |
Datum dvalue; |
69 |
char **values; |
70 |
int rsinfo_ncols; |
71 |
int i, j; |
72 |
/* estvars */ |
73 |
ESTDB *db; |
74 |
ESTCOND *cond; |
75 |
ESTDOC *doc; |
76 |
const CBLIST *texts; |
77 |
int ecode, *est_result, resnum; |
78 |
int limit = 0; |
79 |
int offset = 0; |
80 |
|
81 |
char *index_path; |
82 |
char *query; |
83 |
char *attr; |
84 |
char *order; |
85 |
|
86 |
|
87 |
/* only allow 1D input array */ |
88 |
if (attr_ndims == 1) |
89 |
{ |
90 |
ncols = attr_dim_counts[0]; |
91 |
} |
92 |
else |
93 |
ereport(ERROR, |
94 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
95 |
errmsg("invalid input array"), |
96 |
errdetail("Input array must have 1 dimension"))); |
97 |
|
98 |
/* check to see if caller supports us returning a tuplestore */ |
99 |
if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize)) |
100 |
ereport(ERROR, |
101 |
(errcode(ERRCODE_SYNTAX_ERROR), |
102 |
errmsg("materialize mode required, but it is not " \ |
103 |
"allowed in this context"))); |
104 |
|
105 |
/* get info about element type needed to construct the array */ |
106 |
get_typlenbyvalalign(attr_element_type, &attr_len, &attr_byval, &attr_align); |
107 |
|
108 |
/* get the requested return tuple description */ |
109 |
tupdesc = rsinfo->expectedDesc; |
110 |
rsinfo_ncols = tupdesc->natts; |
111 |
|
112 |
/* |
113 |
* The requested tuple description better match up with the array |
114 |
* we were given. |
115 |
*/ |
116 |
if (rsinfo_ncols != ncols) |
117 |
ereport(ERROR, |
118 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
119 |
errmsg("invalid input array"), |
120 |
errdetail("Number of elements in array must match number of query specified columns."))); |
121 |
|
122 |
/* OK, use it */ |
123 |
attinmeta = TupleDescGetAttInMetadata(tupdesc); |
124 |
|
125 |
/* Now go to work */ |
126 |
rsinfo->returnMode = SFRM_Materialize; |
127 |
|
128 |
per_query_ctx = fcinfo->flinfo->fn_mcxt; |
129 |
oldcontext = MemoryContextSwitchTo(per_query_ctx); |
130 |
|
131 |
/* initialize our tuplestore */ |
132 |
tupstore = tuplestore_begin_heap(true, false, SortMem); |
133 |
|
134 |
|
135 |
/* take rest of arguments from function */ |
136 |
|
137 |
/* index path */ |
138 |
if (PG_ARGISNULL(0)) { |
139 |
ereport(ERROR, |
140 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
141 |
errmsg("index path can't be null"), |
142 |
errdetail("Index path must be valid full path to HyperEstraier index"))); |
143 |
} |
144 |
index_path = _textout(PG_GETARG_TEXT_P(0)); |
145 |
|
146 |
/* query string */ |
147 |
if (PG_ARGISNULL(1)) { |
148 |
query = ""; |
149 |
} else { |
150 |
query = _textout(PG_GETARG_TEXT_P(1)); |
151 |
} |
152 |
|
153 |
/* atribute filter */ |
154 |
if (PG_ARGISNULL(2)) { |
155 |
attr = ""; |
156 |
} else { |
157 |
attr = _textout(PG_GETARG_TEXT_P(2)); |
158 |
} |
159 |
|
160 |
/* sort order */ |
161 |
if (PG_ARGISNULL(3)) { |
162 |
order = ""; |
163 |
} else { |
164 |
order = _textout(PG_GETARG_TEXT_P(3)); |
165 |
} |
166 |
|
167 |
|
168 |
/* limit */ |
169 |
if (PG_ARGISNULL(4)) { |
170 |
limit = 0; |
171 |
} else { |
172 |
limit = PG_GETARG_INT32(4); |
173 |
} |
174 |
|
175 |
/* offset */ |
176 |
if (PG_ARGISNULL(5)) { |
177 |
offset = 0; |
178 |
} else { |
179 |
offset = PG_GETARG_INT32(5); |
180 |
} |
181 |
|
182 |
|
183 |
/* open the database */ |
184 |
elog(DEBUG1, "pgest_attr: est_db_open(%s)", index_path); |
185 |
|
186 |
if(!(db = est_db_open(index_path, ESTDBREADER, &ecode))){ |
187 |
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
188 |
errmsg("est_db_open: can't open %s: %d", index_path, ecode), |
189 |
errdetail(est_err_msg(ecode)))); |
190 |
} |
191 |
|
192 |
elog(DEBUG1, "pgest_attr: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset); |
193 |
|
194 |
/* create a search condition object */ |
195 |
if (!(cond = est_cond_new())) { |
196 |
ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED), |
197 |
errmsg("pgest_attr: est_cond_new failed"))); |
198 |
} |
199 |
|
200 |
/* set the search phrase to the search condition object */ |
201 |
if (! PG_ARGISNULL(1) && strlen(query) > 0) |
202 |
est_cond_set_phrase(cond, query); |
203 |
|
204 |
/* minimum valid attribute length is 10: @a STREQ a */ |
205 |
if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { |
206 |
elog(DEBUG1,"est_cond_add_attr(%s)", attr); |
207 |
est_cond_add_attr(cond, attr); |
208 |
} |
209 |
|
210 |
/* set the search phrase to the search condition object */ |
211 |
if (! PG_ARGISNULL(3) && strlen(order) > 0) { |
212 |
elog(DEBUG1,"est_cond_set_order(%s)", order); |
213 |
est_cond_set_order(cond, order); |
214 |
} |
215 |
|
216 |
/* get the result of search */ |
217 |
est_result = est_db_search(db, cond, &resnum, NULL); |
218 |
|
219 |
/* check if results exists */ |
220 |
if ( 0 == resnum ) { |
221 |
elog(INFO, "pgest_attr: no results for: %s", query ); |
222 |
} |
223 |
|
224 |
/* total number of tuples to be returned */ |
225 |
if (limit && limit < resnum) { |
226 |
nrows = limit; |
227 |
} else { |
228 |
nrows = resnum - offset; |
229 |
} |
230 |
|
231 |
|
232 |
elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query); |
233 |
|
234 |
|
235 |
values = (char **) palloc(ncols * sizeof(char *)); |
236 |
|
237 |
for (i = 0; i < nrows; i++) |
238 |
{ |
239 |
|
240 |
/* get result from estraier */ |
241 |
if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) { |
242 |
elog(INFO, "can't find result %d", i + offset); |
243 |
} else { |
244 |
elog(DEBUG1, "URI: %s\n Title: %s\n", |
245 |
est_doc_attr(doc, "@uri"), |
246 |
est_doc_attr(doc, "@title") |
247 |
); |
248 |
} |
249 |
|
250 |
/* iterate over results */ |
251 |
for (j = 0; j < ncols; j++) |
252 |
{ |
253 |
bool isnull; |
254 |
|
255 |
/* array value of this position */ |
256 |
indx[0] = j + attr_dim_lower_bounds[0]; |
257 |
|
258 |
dvalue = array_ref(attr_arr, attr_ndims, indx, -1, attr_len, attr_byval, attr_align, &isnull); |
259 |
|
260 |
if (!isnull && doc) |
261 |
values[j] = DatumGetCString( |
262 |
attr2text(doc, |
263 |
(char *)DirectFunctionCall1(textout, dvalue) |
264 |
)); |
265 |
else |
266 |
values[j] = NULL; |
267 |
} |
268 |
/* construct the tuple */ |
269 |
tuple = BuildTupleFromCStrings(attinmeta, values); |
270 |
|
271 |
/* now store it */ |
272 |
tuplestore_puttuple(tupstore, tuple); |
273 |
|
274 |
|
275 |
/* delete estraier document object */ |
276 |
est_doc_delete(doc); |
277 |
} |
278 |
|
279 |
tuplestore_donestoring(tupstore); |
280 |
rsinfo->setResult = tupstore; |
281 |
|
282 |
/* |
283 |
* SFRM_Materialize mode expects us to return a NULL Datum. The actual |
284 |
* tuples are in our tuplestore and passed back through |
285 |
* rsinfo->setResult. rsinfo->setDesc is set to the tuple description |
286 |
* that we actually used to build our tuples with, so the caller can |
287 |
* verify we did what it was expecting. |
288 |
*/ |
289 |
rsinfo->setDesc = tupdesc; |
290 |
MemoryContextSwitchTo(oldcontext); |
291 |
|
292 |
est_cond_delete(cond); |
293 |
|
294 |
if(!est_db_close(db, &ecode)){ |
295 |
ereport(ERROR, (errcode(ERRCODE_IO_ERROR), |
296 |
errmsg("est_db_close: %d", ecode), |
297 |
errdetail(est_err_msg(ecode)))); |
298 |
} |
299 |
|
300 |
return (Datum) 0; |
301 |
} |
302 |
|
303 |
|
304 |
/* make text var from attr */ |
305 |
char *attr2text(ESTDOC *doc, char *attr) { |
306 |
char *val; |
307 |
const char *attrval; |
308 |
int len; |
309 |
int attrlen; |
310 |
|
311 |
elog(DEBUG1, "doc: %08x, attr: %s", doc, attr); |
312 |
|
313 |
if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) { |
314 |
val = (char *) palloc(attrlen * sizeof(char)); |
315 |
} else { |
316 |
return (Datum) NULL; |
317 |
} |
318 |
|
319 |
len = strlen(attrval); |
320 |
elog(DEBUG1, "attr2text(%s) = '%s' %d bytes", attr, attrval, len); |
321 |
|
322 |
len++; |
323 |
len *= sizeof(char); |
324 |
|
325 |
elog(DEBUG2, "palloc(%d)", len); |
326 |
|
327 |
val = palloc(len); |
328 |
|
329 |
memset(val, 0, len); |
330 |
strncpy(val, attrval, len); |
331 |
|
332 |
elog(DEBUG2, "val=%s", val); |
333 |
|
334 |
return val; |
335 |
} |
336 |
|