/[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 18 - (hide annotations)
Sun Feb 20 22:58:25 2005 UTC (19 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 9994 byte(s)
more cleanup

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

  ViewVC Help
Powered by ViewVC 1.1.26