/[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 12 - (hide annotations)
Sat Feb 19 15:09:05 2005 UTC (19 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 7386 byte(s)
allmost work, but not yet

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

  ViewVC Help
Powered by ViewVC 1.1.26