/[pgswish]/trunk/pgswish.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/pgswish.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 19 - (show annotations)
Sun Mar 6 21:13:39 2005 UTC (19 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 10375 byte(s)
better handling of swish-e errors

1 /*
2 * integrate swish-e into PostgreSQL
3 *
4 * Dobrica Pavlinusic <dpavlin@rot13.org> 2005-02-18
5 *
6 * TODO:
7 * - check null input using PG_ARGISNULL before using PG_GETARG_xxxx
8 * - support composite type arguments
9 * - split error_or_abort
10 * - use getResultPropValue not SwishResultPropertyStr
11 *
12 * NOTES:
13 * - clear structures with memset to support hash indexes (who whould like
14 * to create hash index on table returned from function?)
15 * - number of returned rows is set by PostgreSQL evaluator, see:
16 * http://archives.postgresql.org/pgsql-hackers/2005-02/msg00546.php
17 *
18 * Based on:
19 * - C example from PostgreSQL documentation (BSD licence)
20 * - swish-e example src/libtest.c (GPL)
21 * - _textin/_textout from pgcurl.c (LGPL)
22 *
23 * This code is licenced under GPL
24 */
25
26 #include "postgres.h"
27 #include "fmgr.h"
28 #include "funcapi.h"
29 #include "utils/builtins.h"
30 #include "utils/array.h"
31 #include "miscadmin.h"
32 #include <swish-e.h>
33
34 #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
35 #define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))
36 #define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
37 #define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
38
39 SW_HANDLE swish_handle = NULL;/* Database handle */
40 SW_SEARCH search = NULL; /* search handle -- holds search parameters */
41 SW_RESULTS swish_results = NULL; /* results handle -- holds list of results */
42 SW_RESULT *sw_res = NULL; /* one row from swish-e results */
43
44 /* define PostgreSQL v1 function */
45 PG_FUNCTION_INFO_V1(pgswish);
46 Datum pgswish(PG_FUNCTION_ARGS) {
47
48 FuncCallContext *funcctx;
49 int call_cntr;
50 int max_calls;
51 TupleDesc tupdesc;
52 TupleTableSlot *slot;
53 AttInMetadata *attinmeta;
54 char *index_path;
55 char *query;
56
57 /* stuff done only on the first call of the function */
58 if (SRF_IS_FIRSTCALL()) {
59 MemoryContext oldcontext;
60
61 /* take arguments from function */
62 //index_path = _textout(PG_GETARG_TEXT_P(0));
63 index_path = _textout(PG_GETARG_TEXT_P(0));
64 query = _textout(PG_GETARG_TEXT_P(1));
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
72
73 /* Send any errors or warnings to stderr (default is stdout) */
74 SwishErrorsToStderr();
75
76 elog(INFO, "pgswish: SwishInit(%s)", index_path);
77
78 swish_handle = SwishInit( index_path );
79
80 if (! swish_handle) {
81 elog(ERROR, "pgswish: can't open %s", index_path);
82 SRF_RETURN_DONE(funcctx);
83 }
84
85 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
86 /* set ranking scheme. default is 0 */
87 SwishRankScheme( swish_handle, 0 );
88 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
89
90 elog(INFO, "pgswish: SwishQuery(%s)", query);
91 /* Here's a short-cut to searching that creates a search object and searches at the same time */
92 swish_results = SwishQuery( swish_handle, query);
93 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
94
95 /* total number of tuples to be returned */
96 funcctx->max_calls = SwishHits( swish_results );
97
98 /* check if results exists */
99 if ( 0 == funcctx->max_calls )
100 elog(INFO, "no results for: %s", query );
101
102 elog(INFO, "pgswish: SwishHits = %d", funcctx->max_calls);
103
104 /* Build a tuple description for a __pgswish tuple */
105 tupdesc = RelationNameGetTupleDesc("__pgswish");
106
107 /* allocate a slot for a tuple with this tupdesc */
108 slot = TupleDescGetSlot(tupdesc);
109
110 /* assign slot to function context */
111 funcctx->slot = slot;
112
113 /*
114 * generate attribute metadata needed later to produce tuples from raw
115 * C strings
116 */
117 attinmeta = TupleDescGetAttInMetadata(tupdesc);
118 funcctx->attinmeta = attinmeta;
119
120 MemoryContextSwitchTo(oldcontext);
121
122 elog(INFO, "SRF_IS_FIRSTCALL done");
123 }
124
125 /* stuff done on every call of the function */
126 funcctx = SRF_PERCALL_SETUP();
127
128 call_cntr = funcctx->call_cntr;
129 max_calls = funcctx->max_calls;
130 slot = funcctx->slot;
131 attinmeta = funcctx->attinmeta;
132
133 if (call_cntr < max_calls) {
134 char **values;
135 HeapTuple tuple;
136 Datum result;
137
138 elog(INFO, "pgswish: loop count %d", call_cntr);
139
140 if (! swish_results) {
141 elog(ERROR, "pgswish: no swish-e results");
142 SRF_RETURN_DONE(funcctx);
143 }
144
145 elog(DEBUG1, "pgswish: check for swish-e error");
146 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
147
148 /*
149 * Prepare a values array for storage in our slot.
150 * This should be an array of C strings which will
151 * be processed later by the type input functions.
152 */
153
154 sw_res = SwishNextResult( swish_results );
155 if (! sw_res) {
156 elog(ERROR, "pgswish: swish-e sort result list: %d rows expected %d", call_cntr, max_calls - 1);
157 Free_Results_Object( swish_results );
158 Free_Search_Object( search );
159 SRF_RETURN_DONE(funcctx);
160 }
161
162 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",
163 SwishResultPropertyStr ( sw_res, "swishdocpath" ),
164 SwishResultPropertyULong ( sw_res, "swishrank" ),
165 SwishResultPropertyULong ( sw_res, "swishdocsize" ),
166 SwishResultPropertyStr ( sw_res, "swishtitle"),
167 SwishResultPropertyStr ( sw_res, "swishdbfile" ),
168 SwishResultPropertyStr ( sw_res, "swishlastmodified" ),
169 SwishResultPropertyULong ( sw_res, "swishreccount" ), /* can figure this out in loop, of course */
170 SwishResultPropertyULong ( sw_res, "swishfilenum" )
171 );
172
173 values = (char **) palloc(4 * sizeof(char *));
174
175 values[0] = prop2int( sw_res, "swishrank" );
176 values[1] = prop2text( sw_res, "swishdocpath" );
177 values[2] = prop2text( sw_res, "swishtitle" );
178 values[3] = prop2int( sw_res, "swishdocsize" );
179
180 /*
181 values[0] = (char *) palloc(16 * sizeof(char));
182 snprintf(values[0], 16, "%d", 1);
183 values[1] = (char *) palloc(16 * sizeof(char));
184 snprintf(values[1], 16, "%d", 2);
185 values[2] = (char *) palloc(16 * sizeof(char));
186 snprintf(values[2], 16, "%d", 3);
187 values[3] = (char *) palloc(16 * sizeof(char));
188 snprintf(values[3], 16, "%d", 4);
189 */
190
191 /* build a tuple */
192 tuple = BuildTupleFromCStrings(attinmeta, values);
193
194 /* make the tuple into a datum */
195 result = TupleGetDatum(slot, tuple);
196
197 /* clean up ? */
198 pfree(values[0]);
199 pfree(values[1]);
200 pfree(values[2]);
201 pfree(values[3]);
202 pfree(values);
203
204 elog(DEBUG1, "row: %s|%s|%s|%s",values[0],values[1],values[2],values[3]);
205
206 SRF_RETURN_NEXT(funcctx, result);
207 } else {
208 elog(INFO, "loop over");
209
210 /* free swish object and close */
211 Free_Search_Object( search );
212 SwishClose( swish_handle );
213
214 /* do when there is no more left */
215 SRF_RETURN_DONE(funcctx);
216 }
217 }
218
219 /* work in progress */
220 PG_FUNCTION_INFO_V1(pgswish2);
221 Datum pgswish2(PG_FUNCTION_ARGS)
222 {
223 int nrows = 3;
224 int16 typlen;
225 bool typbyval;
226 char typalign;
227 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
228 AttInMetadata *attinmeta;
229 TupleDesc tupdesc;
230 Tuplestorestate *tupstore = NULL;
231 HeapTuple tuple;
232 MemoryContext per_query_ctx;
233 MemoryContext oldcontext;
234 Datum dvalue;
235 char **values;
236 int ncols;
237 int i, j;
238
239 /* check to see if caller supports us returning a tuplestore */
240 if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
241 ereport(ERROR,
242 (errcode(ERRCODE_SYNTAX_ERROR),
243 errmsg("materialize mode required, but it is not " \
244 "allowed in this context")));
245
246 /* get the requested return tuple description */
247 tupdesc = rsinfo->expectedDesc;
248 ncols = tupdesc->natts;
249
250 /*
251 * The requested tuple description better match up with the array
252 * we were given.
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 from 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 if (error_or_abort( swish_handle )) return NULL;
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 if (error_or_abort( swish_handle )) return NULL;
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 * check if swish has returned error, and elog it.
356 */
357 static int error_or_abort( SW_HANDLE swish_handle ) {
358 if ( !SwishError( swish_handle ) )
359 return 0;
360
361 /* print a message */
362 elog(ERROR,
363 "pgswish error: Number [%d], Type [%s], Optional Message: [%s]\n",
364 SwishError( swish_handle ),
365 SwishErrorString( swish_handle ),
366 SwishLastErrorMsg( swish_handle )
367 );
368 if ( swish_results ) Free_Results_Object( swish_results );
369 if ( search ) Free_Search_Object( search );
370 SwishClose( swish_handle );
371
372 return 1;
373 }
374

  ViewVC Help
Powered by ViewVC 1.1.26