/[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 20 - (hide annotations)
Sun May 29 20:30:18 2005 UTC (18 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 8950 byte(s)
logging to file in /tmp/ (not really best solution), removed pgswish2

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 dpavlin 20 /* Globals */
40     static SW_HANDLE swish_handle = NULL; /* Database handle */
41     static SW_SEARCH search = NULL; /* search handle -- search parameters */
42     static SW_RESULTS swish_results = NULL; /* results handle -- list of results */
43     static SW_RESULT *sw_res = NULL; /* one row from swish-e results */
44 dpavlin 8
45     /* define PostgreSQL v1 function */
46     PG_FUNCTION_INFO_V1(pgswish);
47     Datum pgswish(PG_FUNCTION_ARGS) {
48    
49     FuncCallContext *funcctx;
50     int call_cntr;
51     int max_calls;
52     TupleDesc tupdesc;
53     TupleTableSlot *slot;
54     AttInMetadata *attinmeta;
55 dpavlin 9 char *index_path;
56     char *query;
57 dpavlin 20 FILE *logfh;
58 dpavlin 8
59     /* stuff done only on the first call of the function */
60     if (SRF_IS_FIRSTCALL()) {
61     MemoryContext oldcontext;
62    
63 dpavlin 9 /* take arguments from function */
64     //index_path = _textout(PG_GETARG_TEXT_P(0));
65     index_path = _textout(PG_GETARG_TEXT_P(0));
66     query = _textout(PG_GETARG_TEXT_P(1));
67    
68 dpavlin 8 /* create a function context for cross-call persistence */
69     funcctx = SRF_FIRSTCALL_INIT();
70    
71     /* switch to memory context appropriate for multiple function calls */
72     oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
73    
74 dpavlin 9
75 dpavlin 20 /* Send any errors or warnings to log, as well as
76     * STDOUT and STDERR (just to be sure) */
77     if ( logfh = fopen("/tmp/pgswish.log", "a") ) {
78     set_error_handle( logfh );
79     elog(INFO, "loggin swish-e errors to /tmp/pgswish.log");
80     /* redirect STDOUT and STDERR to log */
81     dup2(1, logfh);
82     dup2(2, logfh);
83     } else {
84     elog(INFO, "can't open /tmp/pgswish.log -- errors from swish-e won't be cought and may result in back-end crashes!");
85     }
86 dpavlin 8
87 dpavlin 9 elog(INFO, "pgswish: SwishInit(%s)", index_path);
88 dpavlin 20
89 dpavlin 9 swish_handle = SwishInit( index_path );
90 dpavlin 8
91 dpavlin 20 if ( SwishError( swish_handle ) )
92     elog(INFO, "pgswish: SwishInit(%s) failed: %s", index_path, SwishErrorString( swish_handle ));
93    
94     elog(INFO, "handle: %08x", swish_handle);
95    
96 dpavlin 8 if (! swish_handle) {
97 dpavlin 9 elog(ERROR, "pgswish: can't open %s", index_path);
98 dpavlin 8 SRF_RETURN_DONE(funcctx);
99     }
100    
101 dpavlin 19 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
102 dpavlin 8 /* set ranking scheme. default is 0 */
103 dpavlin 10 SwishRankScheme( swish_handle, 0 );
104 dpavlin 19 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
105 dpavlin 8
106 dpavlin 9 elog(INFO, "pgswish: SwishQuery(%s)", query);
107 dpavlin 8 /* Here's a short-cut to searching that creates a search object and searches at the same time */
108 dpavlin 11 swish_results = SwishQuery( swish_handle, query);
109 dpavlin 19 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
110 dpavlin 8
111     /* total number of tuples to be returned */
112 dpavlin 11 funcctx->max_calls = SwishHits( swish_results );
113 dpavlin 8
114     /* check if results exists */
115     if ( 0 == funcctx->max_calls )
116 dpavlin 9 elog(INFO, "no results for: %s", query );
117 dpavlin 8
118 dpavlin 9 elog(INFO, "pgswish: SwishHits = %d", funcctx->max_calls);
119    
120 dpavlin 8 /* Build a tuple description for a __pgswish tuple */
121     tupdesc = RelationNameGetTupleDesc("__pgswish");
122    
123     /* allocate a slot for a tuple with this tupdesc */
124     slot = TupleDescGetSlot(tupdesc);
125    
126     /* assign slot to function context */
127     funcctx->slot = slot;
128    
129     /*
130     * generate attribute metadata needed later to produce tuples from raw
131     * C strings
132     */
133     attinmeta = TupleDescGetAttInMetadata(tupdesc);
134     funcctx->attinmeta = attinmeta;
135    
136     MemoryContextSwitchTo(oldcontext);
137 dpavlin 13
138     elog(INFO, "SRF_IS_FIRSTCALL done");
139 dpavlin 8 }
140    
141     /* stuff done on every call of the function */
142     funcctx = SRF_PERCALL_SETUP();
143    
144     call_cntr = funcctx->call_cntr;
145     max_calls = funcctx->max_calls;
146     slot = funcctx->slot;
147     attinmeta = funcctx->attinmeta;
148    
149     if (call_cntr < max_calls) {
150     char **values;
151     HeapTuple tuple;
152     Datum result;
153    
154 dpavlin 13 elog(INFO, "pgswish: loop count %d", call_cntr);
155 dpavlin 11
156     if (! swish_results) {
157     elog(ERROR, "pgswish: no swish-e results");
158     SRF_RETURN_DONE(funcctx);
159     }
160    
161 dpavlin 14 elog(DEBUG1, "pgswish: check for swish-e error");
162 dpavlin 19 if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx);
163 dpavlin 11
164 dpavlin 8 /*
165     * Prepare a values array for storage in our slot.
166     * This should be an array of C strings which will
167     * be processed later by the type input functions.
168     */
169    
170 dpavlin 11 sw_res = SwishNextResult( swish_results );
171     if (! sw_res) {
172     elog(ERROR, "pgswish: swish-e sort result list: %d rows expected %d", call_cntr, max_calls - 1);
173 dpavlin 19 Free_Results_Object( swish_results );
174     Free_Search_Object( search );
175 dpavlin 11 SRF_RETURN_DONE(funcctx);
176     }
177    
178 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",
179     SwishResultPropertyStr ( sw_res, "swishdocpath" ),
180     SwishResultPropertyULong ( sw_res, "swishrank" ),
181     SwishResultPropertyULong ( sw_res, "swishdocsize" ),
182     SwishResultPropertyStr ( sw_res, "swishtitle"),
183     SwishResultPropertyStr ( sw_res, "swishdbfile" ),
184     SwishResultPropertyStr ( sw_res, "swishlastmodified" ),
185     SwishResultPropertyULong ( sw_res, "swishreccount" ), /* can figure this out in loop, of course */
186     SwishResultPropertyULong ( sw_res, "swishfilenum" )
187     );
188 dpavlin 11
189 dpavlin 13 values = (char **) palloc(4 * sizeof(char *));
190    
191 dpavlin 12 values[0] = prop2int( sw_res, "swishrank" );
192     values[1] = prop2text( sw_res, "swishdocpath" );
193     values[2] = prop2text( sw_res, "swishtitle" );
194     values[3] = prop2int( sw_res, "swishdocsize" );
195 dpavlin 13
196     /*
197     values[0] = (char *) palloc(16 * sizeof(char));
198     snprintf(values[0], 16, "%d", 1);
199     values[1] = (char *) palloc(16 * sizeof(char));
200     snprintf(values[1], 16, "%d", 2);
201     values[2] = (char *) palloc(16 * sizeof(char));
202     snprintf(values[2], 16, "%d", 3);
203     values[3] = (char *) palloc(16 * sizeof(char));
204     snprintf(values[3], 16, "%d", 4);
205     */
206 dpavlin 16
207 dpavlin 8 /* build a tuple */
208     tuple = BuildTupleFromCStrings(attinmeta, values);
209    
210     /* make the tuple into a datum */
211     result = TupleGetDatum(slot, tuple);
212    
213 dpavlin 12 /* clean up ? */
214 dpavlin 13 pfree(values[0]);
215     pfree(values[1]);
216     pfree(values[2]);
217     pfree(values[3]);
218     pfree(values);
219    
220 dpavlin 17 elog(DEBUG1, "row: %s|%s|%s|%s",values[0],values[1],values[2],values[3]);
221 dpavlin 12
222 dpavlin 8 SRF_RETURN_NEXT(funcctx, result);
223     } else {
224 dpavlin 13 elog(INFO, "loop over");
225    
226 dpavlin 8 /* free swish object and close */
227     Free_Search_Object( search );
228     SwishClose( swish_handle );
229    
230     /* do when there is no more left */
231     SRF_RETURN_DONE(funcctx);
232     }
233     }
234    
235    
236 dpavlin 19 /* make text var from property */
237 dpavlin 12 char *prop2text(SW_RESULT sw_res, char *propname) {
238     char *val;
239     char *prop;
240     int len;
241    
242 dpavlin 17 elog(DEBUG2, "prop2text(%s)", propname);
243 dpavlin 12
244     prop = SwishResultPropertyStr( sw_res, propname );
245 dpavlin 19 if (error_or_abort( swish_handle )) return NULL;
246 dpavlin 12
247     len = strlen(prop);
248 dpavlin 17 elog(DEBUG1, "prop2text(%s) = '%s' %d bytes", propname, prop, len);
249 dpavlin 12
250     len++;
251     len *= sizeof(char);
252    
253 dpavlin 17 elog(DEBUG2, "palloc(%d)", len);
254 dpavlin 12
255     val = palloc(len);
256    
257     memset(val, 0, len);
258     strncpy(val, prop, len);
259    
260 dpavlin 17 elog(DEBUG2, "val=%s", val);
261 dpavlin 12
262     return val;
263     }
264    
265 dpavlin 14 /* make integer variable from property */
266 dpavlin 12 char *prop2int(SW_RESULT sw_res, char *propname) {
267     char *val;
268     unsigned long prop;
269     int len;
270    
271 dpavlin 17 elog(DEBUG2, "prop2int(%s)", propname);
272 dpavlin 12
273     prop = SwishResultPropertyULong( sw_res, propname );
274 dpavlin 19 if (error_or_abort( swish_handle )) return NULL;
275 dpavlin 12
276 dpavlin 17 elog(DEBUG1, "prop2int(%s) = %lu", propname, prop);
277 dpavlin 12
278     len = 128 * sizeof(char);
279 dpavlin 17 elog(DEBUG2, "palloc(%d)", len);
280 dpavlin 12
281     val = palloc(len);
282     memset(val, 0, len);
283    
284     snprintf(val, len, "%lu", prop);
285    
286 dpavlin 17 elog(DEBUG2, "val=%s", val);
287 dpavlin 12
288     return val;
289     }
290    
291    
292 dpavlin 14 /*
293     * check if swish has returned error, and elog it.
294     */
295 dpavlin 19 static int error_or_abort( SW_HANDLE swish_handle ) {
296 dpavlin 8 if ( !SwishError( swish_handle ) )
297 dpavlin 19 return 0;
298 dpavlin 8
299     /* print a message */
300     elog(ERROR,
301     "pgswish error: Number [%d], Type [%s], Optional Message: [%s]\n",
302     SwishError( swish_handle ),
303     SwishErrorString( swish_handle ),
304     SwishLastErrorMsg( swish_handle )
305     );
306 dpavlin 19 if ( swish_results ) Free_Results_Object( swish_results );
307 dpavlin 8 if ( search ) Free_Search_Object( search );
308     SwishClose( swish_handle );
309 dpavlin 19
310     return 1;
311 dpavlin 8 }
312    

  ViewVC Help
Powered by ViewVC 1.1.26