/[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

Annotation of /trunk/pgswish.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 19 - (hide 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 dpavlin 8 /*
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 dpavlin 19 * - split error_or_abort
10     * - use getResultPropValue not SwishResultPropertyStr
11 dpavlin 8 *
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 dpavlin 10 * - number of returned rows is set by PostgreSQL evaluator, see:
16     * http://archives.postgresql.org/pgsql-hackers/2005-02/msg00546.php
17 dpavlin 8 *
18 dpavlin 9 * 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 dpavlin 8 */
25    
26     #include "postgres.h"
27     #include "fmgr.h"
28     #include "funcapi.h"
29 dpavlin 9 #include "utils/builtins.h"
30 dpavlin 16 #include "utils/array.h"
31     #include "miscadmin.h"
32 dpavlin 8 #include <swish-e.h>
33    
34 dpavlin 9 #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
35     #define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))
36 dpavlin 11 #define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
37     #define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
38 dpavlin 8
39     SW_HANDLE swish_handle = NULL;/* Database handle */
40     SW_SEARCH search = NULL; /* search handle -- holds search parameters */
41 dpavlin 11 SW_RESULTS swish_results = NULL; /* results handle -- holds list of results */
42 dpavlin 14 SW_RESULT *sw_res = NULL; /* one row from swish-e results */
43 dpavlin 8
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 dpavlin 9 char *index_path;
55     char *query;
56 dpavlin 8
57     /* stuff done only on the first call of the function */
58     if (SRF_IS_FIRSTCALL()) {
59     MemoryContext oldcontext;
60    
61 dpavlin 9 /* 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 dpavlin 8 /* 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 dpavlin 9
73 dpavlin 8 /* Send any errors or warnings to stderr (default is stdout) */
74     SwishErrorsToStderr();
75    
76 dpavlin 9 elog(INFO, "pgswish: SwishInit(%s)", index_path);
77 dpavlin 8
78 dpavlin 9 swish_handle = SwishInit( index_path );
79 dpavlin 8
80     if (! swish_handle) {
81 dpavlin 9 elog(ERROR, "pgswish: can't open %s", index_path);
82 dpavlin 8 SRF_RETURN_DONE(funcctx);
83     }
84    
85 dpavlin 19 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
86 dpavlin 8 /* set ranking scheme. default is 0 */
87 dpavlin 10 SwishRankScheme( swish_handle, 0 );
88 dpavlin 19 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
89 dpavlin 8
90 dpavlin 9 elog(INFO, "pgswish: SwishQuery(%s)", query);
91 dpavlin 8 /* Here's a short-cut to searching that creates a search object and searches at the same time */
92 dpavlin 11 swish_results = SwishQuery( swish_handle, query);
93 dpavlin 19 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
94 dpavlin 8
95     /* total number of tuples to be returned */
96 dpavlin 11 funcctx->max_calls = SwishHits( swish_results );
97 dpavlin 8
98     /* check if results exists */
99     if ( 0 == funcctx->max_calls )
100 dpavlin 9 elog(INFO, "no results for: %s", query );
101 dpavlin 8
102 dpavlin 9 elog(INFO, "pgswish: SwishHits = %d", funcctx->max_calls);
103    
104 dpavlin 8 /* 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 dpavlin 13
122     elog(INFO, "SRF_IS_FIRSTCALL done");
123 dpavlin 8 }
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 dpavlin 13 elog(INFO, "pgswish: loop count %d", call_cntr);
139 dpavlin 11
140     if (! swish_results) {
141     elog(ERROR, "pgswish: no swish-e results");
142     SRF_RETURN_DONE(funcctx);
143     }
144    
145 dpavlin 14 elog(DEBUG1, "pgswish: check for swish-e error");
146 dpavlin 19 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
147 dpavlin 11
148 dpavlin 8 /*
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 dpavlin 11 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 dpavlin 19 Free_Results_Object( swish_results );
158     Free_Search_Object( search );
159 dpavlin 11 SRF_RETURN_DONE(funcctx);
160     }
161    
162 dpavlin 12 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 dpavlin 11
173 dpavlin 13 values = (char **) palloc(4 * sizeof(char *));
174    
175 dpavlin 12 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 dpavlin 13
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 dpavlin 16
191 dpavlin 8 /* build a tuple */
192     tuple = BuildTupleFromCStrings(attinmeta, values);
193    
194     /* make the tuple into a datum */
195     result = TupleGetDatum(slot, tuple);
196    
197 dpavlin 12 /* clean up ? */
198 dpavlin 13 pfree(values[0]);
199     pfree(values[1]);
200     pfree(values[2]);
201     pfree(values[3]);
202     pfree(values);
203    
204 dpavlin 17 elog(DEBUG1, "row: %s|%s|%s|%s",values[0],values[1],values[2],values[3]);
205 dpavlin 12
206 dpavlin 8 SRF_RETURN_NEXT(funcctx, result);
207     } else {
208 dpavlin 13 elog(INFO, "loop over");
209    
210 dpavlin 8 /* 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 dpavlin 16 /* 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 dpavlin 18 int ncols;
237 dpavlin 16 int i, j;
238 dpavlin 8
239 dpavlin 16 /* 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 dpavlin 14
246 dpavlin 16 /* get the requested return tuple description */
247     tupdesc = rsinfo->expectedDesc;
248 dpavlin 18 ncols = tupdesc->natts;
249 dpavlin 14
250 dpavlin 16 /*
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 dpavlin 14 }
296    
297 dpavlin 16
298 dpavlin 19 /* make text var from property */
299 dpavlin 12 char *prop2text(SW_RESULT sw_res, char *propname) {
300     char *val;
301     char *prop;
302     int len;
303    
304 dpavlin 17 elog(DEBUG2, "prop2text(%s)", propname);
305 dpavlin 12
306     prop = SwishResultPropertyStr( sw_res, propname );
307 dpavlin 19 if (error_or_abort( swish_handle )) return NULL;
308 dpavlin 12
309     len = strlen(prop);
310 dpavlin 17 elog(DEBUG1, "prop2text(%s) = '%s' %d bytes", propname, prop, len);
311 dpavlin 12
312     len++;
313     len *= sizeof(char);
314    
315 dpavlin 17 elog(DEBUG2, "palloc(%d)", len);
316 dpavlin 12
317     val = palloc(len);
318    
319     memset(val, 0, len);
320     strncpy(val, prop, len);
321    
322 dpavlin 17 elog(DEBUG2, "val=%s", val);
323 dpavlin 12
324     return val;
325     }
326    
327 dpavlin 14 /* make integer variable from property */
328 dpavlin 12 char *prop2int(SW_RESULT sw_res, char *propname) {
329     char *val;
330     unsigned long prop;
331     int len;
332    
333 dpavlin 17 elog(DEBUG2, "prop2int(%s)", propname);
334 dpavlin 12
335     prop = SwishResultPropertyULong( sw_res, propname );
336 dpavlin 19 if (error_or_abort( swish_handle )) return NULL;
337 dpavlin 12
338 dpavlin 17 elog(DEBUG1, "prop2int(%s) = %lu", propname, prop);
339 dpavlin 12
340     len = 128 * sizeof(char);
341 dpavlin 17 elog(DEBUG2, "palloc(%d)", len);
342 dpavlin 12
343     val = palloc(len);
344     memset(val, 0, len);
345    
346     snprintf(val, len, "%lu", prop);
347    
348 dpavlin 17 elog(DEBUG2, "val=%s", val);
349 dpavlin 12
350     return val;
351     }
352    
353    
354 dpavlin 14 /*
355     * check if swish has returned error, and elog it.
356     */
357 dpavlin 19 static int error_or_abort( SW_HANDLE swish_handle ) {
358 dpavlin 8 if ( !SwishError( swish_handle ) )
359 dpavlin 19 return 0;
360 dpavlin 8
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 dpavlin 19 if ( swish_results ) Free_Results_Object( swish_results );
369 dpavlin 8 if ( search ) Free_Search_Object( search );
370     SwishClose( swish_handle );
371 dpavlin 19
372     return 1;
373 dpavlin 8 }
374    

  ViewVC Help
Powered by ViewVC 1.1.26