/[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 10 - (hide annotations)
Sat Feb 19 11:39:11 2005 UTC (19 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 5592 byte(s)
found why back-end crash (swish-e need aditional parametar for this ranking
scheme)

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 8
33 dpavlin 9
34 dpavlin 8 SW_HANDLE swish_handle = NULL;/* Database handle */
35     SW_SEARCH search = NULL; /* search handle -- holds search parameters */
36     SW_RESULTS results = NULL; /* results handle -- holds list of results */
37    
38     /* define PostgreSQL v1 function */
39     PG_FUNCTION_INFO_V1(pgswish);
40     Datum pgswish(PG_FUNCTION_ARGS) {
41    
42     FuncCallContext *funcctx;
43     int call_cntr;
44     int max_calls;
45     TupleDesc tupdesc;
46     TupleTableSlot *slot;
47     AttInMetadata *attinmeta;
48     SW_HANDLE swish_handle = NULL; /* Database handle */
49     SW_SEARCH search = NULL; /* search handle -- holds search parameters */
50     SW_RESULTS results = NULL; /* results handle -- holds list of results */
51 dpavlin 9 char *index_path;
52     char *query;
53 dpavlin 8
54     /* stuff done only on the first call of the function */
55     if (SRF_IS_FIRSTCALL()) {
56     MemoryContext oldcontext;
57    
58 dpavlin 9 /* take arguments from function */
59     //index_path = _textout(PG_GETARG_TEXT_P(0));
60     index_path = _textout(PG_GETARG_TEXT_P(0));
61     query = _textout(PG_GETARG_TEXT_P(1));
62    
63 dpavlin 8 /* create a function context for cross-call persistence */
64     funcctx = SRF_FIRSTCALL_INIT();
65    
66     /* switch to memory context appropriate for multiple function calls */
67     oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
68    
69 dpavlin 9
70 dpavlin 8 /* Send any errors or warnings to stderr (default is stdout) */
71     SwishErrorsToStderr();
72    
73 dpavlin 9 elog(INFO, "pgswish: SwishInit(%s)", index_path);
74 dpavlin 8
75 dpavlin 9 swish_handle = SwishInit( index_path );
76 dpavlin 8
77     if (! swish_handle) {
78 dpavlin 9 elog(ERROR, "pgswish: can't open %s", index_path);
79 dpavlin 8 SRF_RETURN_DONE(funcctx);
80     }
81    
82     if ( SwishError( swish_handle ) ) error_or_abort( swish_handle );
83     /* set ranking scheme. default is 0 */
84 dpavlin 10 SwishRankScheme( swish_handle, 0 );
85 dpavlin 8
86     /* Check for errors after every call */
87     if ( SwishError( swish_handle ) )
88     error_or_abort( swish_handle ); /* print an error or abort -- see below */
89    
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 9 results = SwishQuery( swish_handle, query);
93 dpavlin 8 if ( SwishError( swish_handle ) ) error_or_abort( swish_handle );
94    
95     /* total number of tuples to be returned */
96     funcctx->max_calls = SwishHits( results );
97    
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     }
122    
123     /* stuff done on every call of the function */
124     funcctx = SRF_PERCALL_SETUP();
125    
126     call_cntr = funcctx->call_cntr;
127     max_calls = funcctx->max_calls;
128     slot = funcctx->slot;
129     attinmeta = funcctx->attinmeta;
130    
131     if (call_cntr < max_calls) {
132     char **values;
133     HeapTuple tuple;
134     Datum result;
135    
136     if (0) {
137     /*
138     * Prepare a values array for storage in our slot.
139     * This should be an array of C strings which will
140     * be processed later by the type input functions.
141     */
142 dpavlin 9 values = (char **) palloc(5 * sizeof(char *));
143     values[0] = _textout( SwishResultPropertyULong ( result, "swishrank" ) );
144     values[1] = _textout( SwishResultPropertyStr ( result, "swishdocpath" ) );
145     values[2] = _textout( SwishResultPropertyStr ( result, "swishtitle" ) );
146     values[3] = _textout( SwishResultPropertyStr ( result, "swishdocsize" ) );
147     values[4] = _textout( SwishResultPropertyStr ( result, "swishdbfile" ) );
148 dpavlin 8
149     /* build a tuple */
150     tuple = BuildTupleFromCStrings(attinmeta, values);
151    
152     /* make the tuple into a datum */
153     result = TupleGetDatum(slot, tuple);
154    
155     }
156     /* clean up (this is not really necessary) */
157    
158     SRF_RETURN_NEXT(funcctx, result);
159     } else {
160     /* free swish object and close */
161     Free_Search_Object( search );
162     SwishClose( swish_handle );
163    
164     /* do when there is no more left */
165     SRF_RETURN_DONE(funcctx);
166     }
167     }
168    
169     /*
170     * elog errors
171     *
172     */
173    
174     static void error_or_abort( SW_HANDLE swish_handle ) {
175     if ( !SwishError( swish_handle ) )
176     return;
177    
178     /* print a message */
179     elog(ERROR,
180     "pgswish error: Number [%d], Type [%s], Optional Message: [%s]\n",
181     SwishError( swish_handle ),
182     SwishErrorString( swish_handle ),
183     SwishLastErrorMsg( swish_handle )
184     );
185     if ( search ) Free_Search_Object( search );
186     SwishClose( swish_handle );
187    
188     /* do when there is no more left */
189     }
190    

  ViewVC Help
Powered by ViewVC 1.1.26