10 |
* NOTES: |
* NOTES: |
11 |
* - clear structures with memset to support hash indexes (who whould like |
* - clear structures with memset to support hash indexes (who whould like |
12 |
* to create hash index on table returned from function?) |
* to create hash index on table returned from function?) |
13 |
|
* - number of returned rows is set by PostgreSQL evaluator, see: |
14 |
|
* http://archives.postgresql.org/pgsql-hackers/2005-02/msg00546.php |
15 |
* |
* |
16 |
* Based on C example from PostgreSQL documentation and swish-e |
* Based on: |
17 |
* src/libtest.c, so licence is GPL |
* - C example from PostgreSQL documentation (BSD licence) |
18 |
|
* - swish-e example src/libtest.c (GPL) |
19 |
|
* - _textin/_textout from pgcurl.c (LGPL) |
20 |
|
* |
21 |
|
* This code is licenced under GPL |
22 |
*/ |
*/ |
23 |
|
|
24 |
#include "postgres.h" |
#include "postgres.h" |
25 |
#include "fmgr.h" |
#include "fmgr.h" |
26 |
#include "funcapi.h" |
#include "funcapi.h" |
27 |
|
#include "utils/builtins.h" |
28 |
|
#include "utils/array.h" |
29 |
|
#include "miscadmin.h" |
30 |
#include <swish-e.h> |
#include <swish-e.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 |
|
|
38 |
SW_HANDLE swish_handle = NULL;/* Database handle */ |
SW_HANDLE swish_handle = NULL;/* Database handle */ |
39 |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
40 |
SW_RESULTS results = NULL; /* results handle -- holds list of results */ |
SW_RESULTS swish_results = NULL; /* results handle -- holds list of results */ |
41 |
|
SW_RESULT *sw_res = NULL; /* one row from swish-e results */ |
42 |
|
|
43 |
/* define PostgreSQL v1 function */ |
/* define PostgreSQL v1 function */ |
44 |
PG_FUNCTION_INFO_V1(pgswish); |
PG_FUNCTION_INFO_V1(pgswish); |
50 |
TupleDesc tupdesc; |
TupleDesc tupdesc; |
51 |
TupleTableSlot *slot; |
TupleTableSlot *slot; |
52 |
AttInMetadata *attinmeta; |
AttInMetadata *attinmeta; |
53 |
SW_HANDLE swish_handle = NULL; /* Database handle */ |
char *index_path; |
54 |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
char *query; |
|
SW_RESULTS results = NULL; /* results handle -- holds list of results */ |
|
55 |
|
|
56 |
/* stuff done only on the first call of the function */ |
/* stuff done only on the first call of the function */ |
57 |
if (SRF_IS_FIRSTCALL()) { |
if (SRF_IS_FIRSTCALL()) { |
58 |
MemoryContext oldcontext; |
MemoryContext oldcontext; |
59 |
|
|
60 |
|
/* take arguments from function */ |
61 |
|
//index_path = _textout(PG_GETARG_TEXT_P(0)); |
62 |
|
index_path = _textout(PG_GETARG_TEXT_P(0)); |
63 |
|
query = _textout(PG_GETARG_TEXT_P(1)); |
64 |
|
|
65 |
/* create a function context for cross-call persistence */ |
/* create a function context for cross-call persistence */ |
66 |
funcctx = SRF_FIRSTCALL_INIT(); |
funcctx = SRF_FIRSTCALL_INIT(); |
67 |
|
|
68 |
/* switch to memory context appropriate for multiple function calls */ |
/* switch to memory context appropriate for multiple function calls */ |
69 |
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
70 |
|
|
71 |
|
|
72 |
/* Send any errors or warnings to stderr (default is stdout) */ |
/* Send any errors or warnings to stderr (default is stdout) */ |
73 |
SwishErrorsToStderr(); |
SwishErrorsToStderr(); |
74 |
|
|
75 |
elog(INFO, "pgswish opening %s, query %s", |
elog(INFO, "pgswish: SwishInit(%s)", index_path); |
|
PG_GETARG_TEXT_P(0), |
|
|
PG_GETARG_TEXT_P(1) |
|
|
); |
|
76 |
|
|
77 |
swish_handle = SwishInit( (char *)PG_GETARG_TEXT_P(0) ); |
swish_handle = SwishInit( index_path ); |
78 |
|
|
79 |
if (! swish_handle) { |
if (! swish_handle) { |
80 |
elog(ERROR, "pgswish: can't open %s", PG_GETARG_TEXT_P(0)); |
elog(ERROR, "pgswish: can't open %s", index_path); |
81 |
SRF_RETURN_DONE(funcctx); |
SRF_RETURN_DONE(funcctx); |
82 |
} |
} |
83 |
|
|
84 |
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
error_or_abort( swish_handle ); |
85 |
/* set ranking scheme. default is 0 */ |
/* set ranking scheme. default is 0 */ |
86 |
SwishRankScheme( swish_handle, 1 ); |
SwishRankScheme( swish_handle, 0 ); |
87 |
|
error_or_abort( swish_handle ); |
|
/* Check for errors after every call */ |
|
|
if ( SwishError( swish_handle ) ) |
|
|
error_or_abort( swish_handle ); /* print an error or abort -- see below */ |
|
88 |
|
|
89 |
|
elog(INFO, "pgswish: SwishQuery(%s)", query); |
90 |
/* Here's a short-cut to searching that creates a search object and searches at the same time */ |
/* Here's a short-cut to searching that creates a search object and searches at the same time */ |
91 |
results = SwishQuery( swish_handle, (char *)PG_GETARG_TEXT_P(1) ); |
swish_results = SwishQuery( swish_handle, query); |
92 |
|
error_or_abort( swish_handle ); |
|
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
|
93 |
|
|
94 |
/* total number of tuples to be returned */ |
/* total number of tuples to be returned */ |
95 |
funcctx->max_calls = SwishHits( results ); |
funcctx->max_calls = SwishHits( swish_results ); |
96 |
|
|
97 |
/* check if results exists */ |
/* check if results exists */ |
98 |
if ( 0 == funcctx->max_calls ) |
if ( 0 == funcctx->max_calls ) |
99 |
elog(INFO, "no results for: %s", PG_GETARG_TEXT_P(1) ); |
elog(INFO, "no results for: %s", query ); |
100 |
|
|
101 |
|
elog(INFO, "pgswish: SwishHits = %d", funcctx->max_calls); |
102 |
|
|
103 |
/* Build a tuple description for a __pgswish tuple */ |
/* Build a tuple description for a __pgswish tuple */ |
104 |
tupdesc = RelationNameGetTupleDesc("__pgswish"); |
tupdesc = RelationNameGetTupleDesc("__pgswish"); |
117 |
funcctx->attinmeta = attinmeta; |
funcctx->attinmeta = attinmeta; |
118 |
|
|
119 |
MemoryContextSwitchTo(oldcontext); |
MemoryContextSwitchTo(oldcontext); |
120 |
|
|
121 |
|
elog(INFO, "SRF_IS_FIRSTCALL done"); |
122 |
} |
} |
123 |
|
|
124 |
/* stuff done on every call of the function */ |
/* stuff done on every call of the function */ |
134 |
HeapTuple tuple; |
HeapTuple tuple; |
135 |
Datum result; |
Datum result; |
136 |
|
|
137 |
if (0) { |
elog(INFO, "pgswish: loop count %d", call_cntr); |
138 |
|
|
139 |
|
if (! swish_results) { |
140 |
|
elog(ERROR, "pgswish: no swish-e results"); |
141 |
|
SRF_RETURN_DONE(funcctx); |
142 |
|
} |
143 |
|
|
144 |
|
elog(DEBUG1, "pgswish: check for swish-e error"); |
145 |
|
error_or_abort( swish_handle ); |
146 |
|
|
147 |
/* |
/* |
148 |
* Prepare a values array for storage in our slot. |
* Prepare a values array for storage in our slot. |
149 |
* This should be an array of C strings which will |
* This should be an array of C strings which will |
150 |
* be processed later by the type input functions. |
* be processed later by the type input functions. |
151 |
*/ |
*/ |
152 |
values = (void **) palloc(5 * sizeof(void *)); |
|
153 |
values[0] = (long *) SwishResultPropertyULong ( result, "swishrank" ), |
sw_res = SwishNextResult( swish_results ); |
154 |
values[1] = (char *) SwishResultPropertyStr ( result, "swishdocpath" ), |
if (! sw_res) { |
155 |
values[2] = (char *) SwishResultPropertyStr ( result, "swishtitle" ), |
elog(ERROR, "pgswish: swish-e sort result list: %d rows expected %d", call_cntr, max_calls - 1); |
156 |
values[3] = (long *) SwishResultPropertyStr ( result, "swishdocsize" ), |
SRF_RETURN_DONE(funcctx); |
157 |
values[4] = (char *) SwishResultPropertyStr ( result, "swishdbfile" ), |
} |
158 |
|
|
159 |
|
elog(INFO, "Path: %s\n Rank: %lu\n Size: %lu\n Title: %s\n Index: %s\n Modified: %s\n Record #: %lu\n File #: %lu\n\n", |
160 |
|
SwishResultPropertyStr ( sw_res, "swishdocpath" ), |
161 |
|
SwishResultPropertyULong ( sw_res, "swishrank" ), |
162 |
|
SwishResultPropertyULong ( sw_res, "swishdocsize" ), |
163 |
|
SwishResultPropertyStr ( sw_res, "swishtitle"), |
164 |
|
SwishResultPropertyStr ( sw_res, "swishdbfile" ), |
165 |
|
SwishResultPropertyStr ( sw_res, "swishlastmodified" ), |
166 |
|
SwishResultPropertyULong ( sw_res, "swishreccount" ), /* can figure this out in loop, of course */ |
167 |
|
SwishResultPropertyULong ( sw_res, "swishfilenum" ) |
168 |
|
); |
169 |
|
|
170 |
|
values = (char **) palloc(4 * sizeof(char *)); |
171 |
|
|
172 |
|
values[0] = prop2int( sw_res, "swishrank" ); |
173 |
|
values[1] = prop2text( sw_res, "swishdocpath" ); |
174 |
|
values[2] = prop2text( sw_res, "swishtitle" ); |
175 |
|
values[3] = prop2int( sw_res, "swishdocsize" ); |
176 |
|
|
177 |
|
/* |
178 |
|
values[0] = (char *) palloc(16 * sizeof(char)); |
179 |
|
snprintf(values[0], 16, "%d", 1); |
180 |
|
values[1] = (char *) palloc(16 * sizeof(char)); |
181 |
|
snprintf(values[1], 16, "%d", 2); |
182 |
|
values[2] = (char *) palloc(16 * sizeof(char)); |
183 |
|
snprintf(values[2], 16, "%d", 3); |
184 |
|
values[3] = (char *) palloc(16 * sizeof(char)); |
185 |
|
snprintf(values[3], 16, "%d", 4); |
186 |
|
*/ |
187 |
|
|
188 |
/* build a tuple */ |
/* build a tuple */ |
189 |
tuple = BuildTupleFromCStrings(attinmeta, values); |
tuple = BuildTupleFromCStrings(attinmeta, values); |
191 |
/* make the tuple into a datum */ |
/* make the tuple into a datum */ |
192 |
result = TupleGetDatum(slot, tuple); |
result = TupleGetDatum(slot, tuple); |
193 |
|
|
194 |
} |
/* clean up ? */ |
195 |
/* clean up (this is not really necessary) */ |
pfree(values[0]); |
196 |
|
pfree(values[1]); |
197 |
|
pfree(values[2]); |
198 |
|
pfree(values[3]); |
199 |
|
pfree(values); |
200 |
|
|
201 |
|
elog(DEBUG1, "row: %s|%s|%s|%s",values[0],values[1],values[2],values[3]); |
202 |
|
|
203 |
SRF_RETURN_NEXT(funcctx, result); |
SRF_RETURN_NEXT(funcctx, result); |
204 |
} else { |
} else { |
205 |
|
elog(INFO, "loop over"); |
206 |
|
|
207 |
/* free swish object and close */ |
/* free swish object and close */ |
208 |
Free_Search_Object( search ); |
Free_Search_Object( search ); |
209 |
SwishClose( swish_handle ); |
SwishClose( swish_handle ); |
213 |
} |
} |
214 |
} |
} |
215 |
|
|
216 |
|
/* work in progress */ |
217 |
|
PG_FUNCTION_INFO_V1(pgswish2); |
218 |
|
Datum pgswish2(PG_FUNCTION_ARGS) |
219 |
|
{ |
220 |
|
int ncols = 2; |
221 |
|
int nrows = 3; |
222 |
|
int16 typlen; |
223 |
|
bool typbyval; |
224 |
|
char typalign; |
225 |
|
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
226 |
|
AttInMetadata *attinmeta; |
227 |
|
TupleDesc tupdesc; |
228 |
|
Tuplestorestate *tupstore = NULL; |
229 |
|
HeapTuple tuple; |
230 |
|
MemoryContext per_query_ctx; |
231 |
|
MemoryContext oldcontext; |
232 |
|
Datum dvalue; |
233 |
|
char **values; |
234 |
|
int rsinfo_ncols; |
235 |
|
int i, j; |
236 |
|
|
237 |
|
/* check to see if caller supports us returning a tuplestore */ |
238 |
|
if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize)) |
239 |
|
ereport(ERROR, |
240 |
|
(errcode(ERRCODE_SYNTAX_ERROR), |
241 |
|
errmsg("materialize mode required, but it is not " \ |
242 |
|
"allowed in this context"))); |
243 |
|
|
244 |
|
/* get the requested return tuple description */ |
245 |
|
tupdesc = rsinfo->expectedDesc; |
246 |
|
rsinfo_ncols = tupdesc->natts; |
247 |
|
|
248 |
|
/* |
249 |
|
* The requested tuple description better match up with the array |
250 |
|
* we were given. |
251 |
|
*/ |
252 |
|
elog(INFO, "rsinfo_ncols = %d, ncols = %d", rsinfo_ncols, ncols); |
253 |
|
|
254 |
|
/* OK, use it */ |
255 |
|
attinmeta = TupleDescGetAttInMetadata(tupdesc); |
256 |
|
|
257 |
|
/* Now go to work */ |
258 |
|
rsinfo->returnMode = SFRM_Materialize; |
259 |
|
|
260 |
|
per_query_ctx = fcinfo->flinfo->fn_mcxt; |
261 |
|
oldcontext = MemoryContextSwitchTo(per_query_ctx); |
262 |
|
|
263 |
|
/* initialize our tuplestore */ |
264 |
|
tupstore = tuplestore_begin_heap(true, false, SortMem); |
265 |
|
|
266 |
|
values = (char **) palloc(ncols * sizeof(char *)); |
267 |
|
|
268 |
|
for (i = 0; i < nrows; i++) |
269 |
|
{ |
270 |
|
for (j = 0; j < ncols; j++) |
271 |
|
{ |
272 |
|
values[j] = DatumGetCString( "foo" ); |
273 |
|
} |
274 |
|
/* construct the tuple */ |
275 |
|
tuple = BuildTupleFromCStrings(attinmeta, values); |
276 |
|
|
277 |
|
/* now store it */ |
278 |
|
tuplestore_puttuple(tupstore, tuple); |
279 |
|
} |
280 |
|
|
281 |
|
tuplestore_donestoring(tupstore); |
282 |
|
rsinfo->setResult = tupstore; |
283 |
|
|
284 |
|
/* |
285 |
|
* SFRM_Materialize mode expects us to return a NULL Datum. The actual |
286 |
|
* tuples are in our tuplestore and passed back through |
287 |
|
* rsinfo->setResult. rsinfo->setDesc is set to the tuple description |
288 |
|
* that we actually used to build our tuples with, so the caller can |
289 |
|
* verify we did what it was expecting. |
290 |
|
*/ |
291 |
|
rsinfo->setDesc = tupdesc; |
292 |
|
MemoryContextSwitchTo(oldcontext); |
293 |
|
|
294 |
|
return (Datum) 0; |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
/* make text var prom property */ |
299 |
|
char *prop2text(SW_RESULT sw_res, char *propname) { |
300 |
|
char *val; |
301 |
|
char *prop; |
302 |
|
int len; |
303 |
|
|
304 |
|
elog(DEBUG2, "prop2text(%s)", propname); |
305 |
|
|
306 |
|
prop = SwishResultPropertyStr( sw_res, propname ); |
307 |
|
error_or_abort( swish_handle ); |
308 |
|
|
309 |
|
len = strlen(prop); |
310 |
|
elog(DEBUG1, "prop2text(%s) = '%s' %d bytes", propname, prop, len); |
311 |
|
|
312 |
|
len++; |
313 |
|
len *= sizeof(char); |
314 |
|
|
315 |
|
elog(DEBUG2, "palloc(%d)", len); |
316 |
|
|
317 |
|
val = palloc(len); |
318 |
|
|
319 |
|
memset(val, 0, len); |
320 |
|
strncpy(val, prop, len); |
321 |
|
|
322 |
|
elog(DEBUG2, "val=%s", val); |
323 |
|
|
324 |
|
return val; |
325 |
|
} |
326 |
|
|
327 |
|
/* make integer variable from property */ |
328 |
|
char *prop2int(SW_RESULT sw_res, char *propname) { |
329 |
|
char *val; |
330 |
|
unsigned long prop; |
331 |
|
int len; |
332 |
|
|
333 |
|
elog(DEBUG2, "prop2int(%s)", propname); |
334 |
|
|
335 |
|
prop = SwishResultPropertyULong( sw_res, propname ); |
336 |
|
error_or_abort( swish_handle ); |
337 |
|
|
338 |
|
elog(DEBUG1, "prop2int(%s) = %lu", propname, prop); |
339 |
|
|
340 |
|
len = 128 * sizeof(char); |
341 |
|
elog(DEBUG2, "palloc(%d)", len); |
342 |
|
|
343 |
|
val = palloc(len); |
344 |
|
memset(val, 0, len); |
345 |
|
|
346 |
|
snprintf(val, len, "%lu", prop); |
347 |
|
|
348 |
|
elog(DEBUG2, "val=%s", val); |
349 |
|
|
350 |
|
return val; |
351 |
|
} |
352 |
|
|
353 |
|
|
354 |
/* |
/* |
355 |
* elog errors |
* check if swish has returned error, and elog it. |
|
* |
|
356 |
*/ |
*/ |
|
|
|
357 |
static void error_or_abort( SW_HANDLE swish_handle ) { |
static void error_or_abort( SW_HANDLE swish_handle ) { |
358 |
if ( !SwishError( swish_handle ) ) |
if ( !SwishError( swish_handle ) ) |
359 |
return; |
return; |
367 |
); |
); |
368 |
if ( search ) Free_Search_Object( search ); |
if ( search ) Free_Search_Object( search ); |
369 |
SwishClose( swish_handle ); |
SwishClose( swish_handle ); |
|
|
|
|
/* do when there is no more left */ |
|
370 |
} |
} |
371 |
|
|