1 |
dpavlin |
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 |
dpavlin |
5 |
/* prototype */ |
38 |
|
|
char *attr2text(ESTDOC *doc, char *attr); |
39 |
dpavlin |
1 |
|
40 |
|
|
ESTDB *db; |
41 |
|
|
ESTCOND *cond; |
42 |
|
|
ESTDOC *doc; |
43 |
|
|
const CBLIST *texts; |
44 |
|
|
int ecode, *est_result, resnum, i, j; |
45 |
dpavlin |
3 |
int limit = 0; |
46 |
|
|
int offset = 0; |
47 |
dpavlin |
1 |
|
48 |
|
|
/* define PostgreSQL v1 function */ |
49 |
|
|
PG_FUNCTION_INFO_V1(pgest); |
50 |
|
|
Datum pgest(PG_FUNCTION_ARGS) { |
51 |
|
|
|
52 |
|
|
FuncCallContext *funcctx; |
53 |
|
|
int call_cntr; |
54 |
|
|
int max_calls; |
55 |
|
|
TupleDesc tupdesc; |
56 |
|
|
TupleTableSlot *slot; |
57 |
|
|
AttInMetadata *attinmeta; |
58 |
|
|
char *index_path; |
59 |
|
|
char *query; |
60 |
dpavlin |
5 |
char *attr; |
61 |
dpavlin |
1 |
|
62 |
|
|
/* stuff done only on the first call of the function */ |
63 |
|
|
if (SRF_IS_FIRSTCALL()) { |
64 |
|
|
MemoryContext oldcontext; |
65 |
|
|
|
66 |
|
|
/* create a function context for cross-call persistence */ |
67 |
|
|
funcctx = SRF_FIRSTCALL_INIT(); |
68 |
|
|
|
69 |
|
|
/* switch to memory context appropriate for multiple function calls */ |
70 |
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
71 |
dpavlin |
12 |
/* take arguments from function */ |
72 |
dpavlin |
1 |
|
73 |
dpavlin |
12 |
/* index path */ |
74 |
|
|
if (PG_ARGISNULL(0)) { |
75 |
|
|
elog(ERROR, "index path can't be null"); |
76 |
|
|
SRF_RETURN_DONE(funcctx); |
77 |
|
|
} |
78 |
|
|
index_path = _textout(PG_GETARG_TEXT_P(0)); |
79 |
|
|
|
80 |
|
|
/* query string */ |
81 |
|
|
if (PG_ARGISNULL(0)) { |
82 |
|
|
query = ""; |
83 |
|
|
} else { |
84 |
|
|
query = _textout(PG_GETARG_TEXT_P(1)); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
/* atribute filter */ |
88 |
|
|
if (PG_ARGISNULL(2)) { |
89 |
|
|
attr = ""; |
90 |
|
|
} else { |
91 |
|
|
attr = _textout(PG_GETARG_TEXT_P(2)); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
/* limit */ |
95 |
dpavlin |
14 |
if (PG_ARGISNULL(3)) { |
96 |
|
|
limit = 0; |
97 |
|
|
} else { |
98 |
|
|
limit = PG_GETARG_INT32(3); |
99 |
|
|
} |
100 |
dpavlin |
12 |
|
101 |
|
|
/* offset */ |
102 |
dpavlin |
14 |
if (PG_ARGISNULL(4)) { |
103 |
|
|
offset = 0; |
104 |
|
|
} else { |
105 |
|
|
offset = PG_GETARG_INT32(4); |
106 |
|
|
} |
107 |
dpavlin |
12 |
|
108 |
|
|
|
109 |
dpavlin |
1 |
/* open the database */ |
110 |
|
|
elog(DEBUG1, "pgest: est_db_open(%s)", index_path); |
111 |
|
|
|
112 |
|
|
if(!(db = est_db_open(index_path, ESTDBREADER, &ecode))){ |
113 |
|
|
elog(ERROR, "est_db_open: can't open %s [%d]: %s", index_path, ecode, est_err_msg(ecode)); |
114 |
|
|
SRF_RETURN_DONE(funcctx); |
115 |
|
|
} |
116 |
|
|
|
117 |
dpavlin |
12 |
elog(INFO, "pgest: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset); |
118 |
dpavlin |
1 |
|
119 |
|
|
/* create a search condition object */ |
120 |
|
|
if (!(cond = est_cond_new())) { |
121 |
|
|
elog(INFO, "pgest: est_cond_new failed"); |
122 |
|
|
SRF_RETURN_DONE(funcctx); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
/* set the search phrase to the search condition object */ |
126 |
dpavlin |
12 |
if (! PG_ARGISNULL(1) && strlen(query) > 0) |
127 |
dpavlin |
9 |
est_cond_set_phrase(cond, query); |
128 |
dpavlin |
1 |
|
129 |
dpavlin |
5 |
/* minimum valid attribute length is 10: @a STREQ a */ |
130 |
dpavlin |
12 |
if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { |
131 |
dpavlin |
5 |
elog(INFO,"est_cond_add_attr(%s)", attr); |
132 |
|
|
est_cond_add_attr(cond, attr); |
133 |
|
|
} |
134 |
|
|
|
135 |
dpavlin |
1 |
/* get the result of search */ |
136 |
|
|
est_result = est_db_search(db, cond, &resnum, NULL); |
137 |
|
|
|
138 |
|
|
/* total number of tuples to be returned */ |
139 |
dpavlin |
5 |
if (limit && limit < resnum) { |
140 |
|
|
funcctx->max_calls = limit - offset; |
141 |
|
|
} else { |
142 |
|
|
funcctx->max_calls = resnum - offset; |
143 |
|
|
} |
144 |
dpavlin |
1 |
|
145 |
|
|
/* check if results exists */ |
146 |
|
|
if ( 0 == funcctx->max_calls ) |
147 |
|
|
elog(INFO, "pgest: no results for: %s", query ); |
148 |
|
|
|
149 |
dpavlin |
5 |
elog(DEBUG1, "pgest: found %d hits for %s", resnum, query); |
150 |
dpavlin |
1 |
|
151 |
|
|
/* Build a tuple description for a __pgest tuple */ |
152 |
|
|
tupdesc = RelationNameGetTupleDesc("__pgest"); |
153 |
|
|
|
154 |
|
|
/* allocate a slot for a tuple with this tupdesc */ |
155 |
|
|
slot = TupleDescGetSlot(tupdesc); |
156 |
|
|
|
157 |
|
|
/* assign slot to function context */ |
158 |
|
|
funcctx->slot = slot; |
159 |
|
|
|
160 |
|
|
/* |
161 |
|
|
* generate attribute metadata needed later to produce tuples from raw |
162 |
|
|
* C strings |
163 |
|
|
*/ |
164 |
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc); |
165 |
|
|
funcctx->attinmeta = attinmeta; |
166 |
|
|
|
167 |
|
|
MemoryContextSwitchTo(oldcontext); |
168 |
|
|
|
169 |
dpavlin |
2 |
elog(DEBUG1, "SRF_IS_FIRSTCALL done"); |
170 |
dpavlin |
1 |
} |
171 |
|
|
|
172 |
|
|
/* stuff done on every call of the function */ |
173 |
|
|
funcctx = SRF_PERCALL_SETUP(); |
174 |
|
|
|
175 |
|
|
call_cntr = funcctx->call_cntr; |
176 |
|
|
max_calls = funcctx->max_calls; |
177 |
|
|
slot = funcctx->slot; |
178 |
|
|
attinmeta = funcctx->attinmeta; |
179 |
dpavlin |
3 |
|
180 |
|
|
if (limit && call_cntr > limit - 1) { |
181 |
|
|
elog(INFO, "call_cntr: %d limit: %d", call_cntr, limit); |
182 |
|
|
SRF_RETURN_DONE(funcctx); |
183 |
|
|
} |
184 |
|
|
|
185 |
dpavlin |
1 |
if (call_cntr < max_calls) { |
186 |
|
|
char **values; |
187 |
|
|
HeapTuple tuple; |
188 |
|
|
Datum result; |
189 |
|
|
|
190 |
dpavlin |
2 |
elog(DEBUG1, "pgest: loop count %d", call_cntr); |
191 |
dpavlin |
1 |
|
192 |
|
|
if (! est_result) { |
193 |
|
|
elog(ERROR, "pgest: no estraier results"); |
194 |
|
|
SRF_RETURN_DONE(funcctx); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
/* |
198 |
|
|
* Prepare a values array for storage in our slot. |
199 |
|
|
* This should be an array of C strings which will |
200 |
|
|
* be processed later by the type input functions. |
201 |
|
|
*/ |
202 |
|
|
|
203 |
dpavlin |
3 |
if (doc = est_db_get_doc(db, est_result[call_cntr + offset], 0)) { |
204 |
dpavlin |
1 |
|
205 |
dpavlin |
2 |
elog(DEBUG1, "URI: %s\n Title: %s\n", |
206 |
dpavlin |
1 |
est_doc_attr(doc, "@uri"), |
207 |
|
|
est_doc_attr(doc, "@title") |
208 |
|
|
); |
209 |
|
|
|
210 |
|
|
values = (char **) palloc(4 * sizeof(char *)); |
211 |
|
|
|
212 |
|
|
// values[0] = (char *) palloc(strlen(_estval) * sizeof(char)); |
213 |
|
|
|
214 |
dpavlin |
5 |
values[0] = (char *) attr2text(doc,"@id"); |
215 |
|
|
values[1] = (char *) attr2text(doc,"@uri"); |
216 |
|
|
values[2] = (char *) attr2text(doc,"@title"); |
217 |
|
|
values[3] = (char *) attr2text(doc,"@type"); |
218 |
dpavlin |
1 |
|
219 |
|
|
/* destloy the document object */ |
220 |
|
|
elog(DEBUG2, "est_doc_delete"); |
221 |
|
|
est_doc_delete(doc); |
222 |
|
|
} else { |
223 |
|
|
elog(INFO, "no result from estraier"); |
224 |
dpavlin |
7 |
values[0] = DatumGetCString( "" ); |
225 |
|
|
values[1] = DatumGetCString( "" ); |
226 |
|
|
values[2] = DatumGetCString( "" ); |
227 |
|
|
values[3] = DatumGetCString( "" ); |
228 |
dpavlin |
1 |
} |
229 |
|
|
|
230 |
|
|
|
231 |
|
|
elog(DEBUG2, "build tuple"); |
232 |
|
|
/* build a tuple */ |
233 |
|
|
tuple = BuildTupleFromCStrings(attinmeta, values); |
234 |
|
|
|
235 |
|
|
elog(DEBUG2, "make tuple into datum"); |
236 |
|
|
/* make the tuple into a datum */ |
237 |
|
|
result = TupleGetDatum(slot, tuple); |
238 |
|
|
|
239 |
|
|
elog(DEBUG2, "cleanup"); |
240 |
|
|
/* clean up ? */ |
241 |
|
|
/* |
242 |
|
|
pfree(values[0]); |
243 |
|
|
pfree(values[1]); |
244 |
|
|
pfree(values[2]); |
245 |
|
|
pfree(values[3]); |
246 |
|
|
pfree(values); |
247 |
|
|
*/ |
248 |
|
|
|
249 |
|
|
elog(DEBUG2, "cleanup over"); |
250 |
|
|
|
251 |
|
|
SRF_RETURN_NEXT(funcctx, result); |
252 |
|
|
} else { |
253 |
dpavlin |
2 |
elog(DEBUG1, "loop over"); |
254 |
dpavlin |
1 |
|
255 |
|
|
if(!est_db_close(db, &ecode)){ |
256 |
|
|
elog(INFO, "est_db_close error: %s", est_err_msg(ecode)); |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
/* do when there is no more left */ |
260 |
|
|
SRF_RETURN_DONE(funcctx); |
261 |
|
|
} |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
/* work in progress */ |
265 |
|
|
PG_FUNCTION_INFO_V1(pgest2); |
266 |
|
|
Datum pgest2(PG_FUNCTION_ARGS) |
267 |
|
|
{ |
268 |
|
|
int nrows = 3; |
269 |
|
|
int16 typlen; |
270 |
|
|
bool typbyval; |
271 |
|
|
char typalign; |
272 |
|
|
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
273 |
|
|
AttInMetadata *attinmeta; |
274 |
|
|
TupleDesc tupdesc; |
275 |
|
|
Tuplestorestate *tupstore = NULL; |
276 |
|
|
HeapTuple tuple; |
277 |
|
|
MemoryContext per_query_ctx; |
278 |
|
|
MemoryContext oldcontext; |
279 |
|
|
Datum dvalue; |
280 |
|
|
char **values; |
281 |
|
|
int ncols; |
282 |
|
|
int i, j; |
283 |
|
|
|
284 |
|
|
/* check to see if caller supports us returning a tuplestore */ |
285 |
|
|
if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize)) |
286 |
|
|
ereport(ERROR, |
287 |
|
|
(errcode(ERRCODE_SYNTAX_ERROR), |
288 |
|
|
errmsg("materialize mode required, but it is not " \ |
289 |
|
|
"allowed in this context"))); |
290 |
|
|
|
291 |
|
|
/* get the requested return tuple description */ |
292 |
|
|
tupdesc = rsinfo->expectedDesc; |
293 |
|
|
ncols = tupdesc->natts; |
294 |
|
|
|
295 |
|
|
/* |
296 |
|
|
* The requested tuple description better match up with the array |
297 |
|
|
* we were given. |
298 |
|
|
*/ |
299 |
|
|
/* OK, use it */ |
300 |
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc); |
301 |
|
|
|
302 |
|
|
/* Now go to work */ |
303 |
|
|
rsinfo->returnMode = SFRM_Materialize; |
304 |
|
|
|
305 |
|
|
per_query_ctx = fcinfo->flinfo->fn_mcxt; |
306 |
|
|
oldcontext = MemoryContextSwitchTo(per_query_ctx); |
307 |
|
|
|
308 |
|
|
/* initialize our tuplestore */ |
309 |
|
|
tupstore = tuplestore_begin_heap(true, false, SortMem); |
310 |
|
|
|
311 |
|
|
values = (char **) palloc(ncols * sizeof(char *)); |
312 |
|
|
|
313 |
|
|
for (i = 0; i < nrows; i++) |
314 |
|
|
{ |
315 |
|
|
for (j = 0; j < ncols; j++) |
316 |
|
|
{ |
317 |
|
|
values[j] = DatumGetCString( "foo" ); |
318 |
|
|
} |
319 |
|
|
/* construct the tuple */ |
320 |
|
|
tuple = BuildTupleFromCStrings(attinmeta, values); |
321 |
|
|
|
322 |
|
|
/* now store it */ |
323 |
|
|
tuplestore_puttuple(tupstore, tuple); |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
tuplestore_donestoring(tupstore); |
327 |
|
|
rsinfo->setResult = tupstore; |
328 |
|
|
|
329 |
|
|
/* |
330 |
|
|
* SFRM_Materialize mode expects us to return a NULL Datum. The actual |
331 |
|
|
* tuples are in our tuplestore and passed back through |
332 |
|
|
* rsinfo->setResult. rsinfo->setDesc is set to the tuple description |
333 |
|
|
* that we actually used to build our tuples with, so the caller can |
334 |
|
|
* verify we did what it was expecting. |
335 |
|
|
*/ |
336 |
|
|
rsinfo->setDesc = tupdesc; |
337 |
|
|
MemoryContextSwitchTo(oldcontext); |
338 |
|
|
|
339 |
|
|
return (Datum) 0; |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
|
343 |
|
|
/* make text var from attr */ |
344 |
|
|
char *attr2text(ESTDOC *doc, char *attr) { |
345 |
|
|
char *val; |
346 |
|
|
const char *attrval; |
347 |
|
|
int len; |
348 |
dpavlin |
4 |
int attrlen; |
349 |
dpavlin |
1 |
|
350 |
dpavlin |
2 |
elog(DEBUG1, "doc: %08x, attr: %s", doc, attr); |
351 |
dpavlin |
1 |
|
352 |
dpavlin |
4 |
if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) { |
353 |
|
|
val = (char *) palloc(attrlen * sizeof(char)); |
354 |
dpavlin |
1 |
} else { |
355 |
|
|
return (Datum) NULL; |
356 |
|
|
} |
357 |
|
|
|
358 |
|
|
len = strlen(attrval); |
359 |
dpavlin |
2 |
elog(DEBUG1, "attr2text(%s) = '%s' %d bytes", attr, attrval, len); |
360 |
dpavlin |
1 |
|
361 |
|
|
len++; |
362 |
|
|
len *= sizeof(char); |
363 |
|
|
|
364 |
|
|
elog(DEBUG2, "palloc(%d)", len); |
365 |
|
|
|
366 |
|
|
val = palloc(len); |
367 |
|
|
|
368 |
|
|
memset(val, 0, len); |
369 |
|
|
strncpy(val, attrval, len); |
370 |
|
|
|
371 |
|
|
elog(DEBUG2, "val=%s", val); |
372 |
|
|
|
373 |
|
|
return val; |
374 |
|
|
} |
375 |
|
|
|
376 |
|
|
/* make integer variable from property */ |
377 |
|
|
/* |
378 |
|
|
char *prop2int(SW_RESULT sw_res, char *propname) { |
379 |
|
|
char *val; |
380 |
|
|
unsigned long prop; |
381 |
|
|
int len; |
382 |
|
|
|
383 |
|
|
elog(DEBUG2, "prop2int(%s)", propname); |
384 |
|
|
|
385 |
|
|
prop = estResultPropertyULong( sw_res, propname ); |
386 |
|
|
if (error_or_abort( est_handle )) return NULL; |
387 |
|
|
|
388 |
|
|
elog(DEBUG1, "prop2int(%s) = %lu", propname, prop); |
389 |
|
|
|
390 |
|
|
len = 128 * sizeof(char); |
391 |
|
|
elog(DEBUG2, "palloc(%d)", len); |
392 |
|
|
|
393 |
|
|
val = palloc(len); |
394 |
|
|
memset(val, 0, len); |
395 |
|
|
|
396 |
|
|
snprintf(val, len, "%lu", prop); |
397 |
|
|
|
398 |
|
|
elog(DEBUG2, "val=%s", val); |
399 |
|
|
|
400 |
|
|
return val; |
401 |
|
|
} |
402 |
|
|
*/ |