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

Contents of /trunk/pgswish.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show 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 /*
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 * - number of returned rows is set by PostgreSQL evaluator, see:
14 * http://archives.postgresql.org/pgsql-hackers/2005-02/msg00546.php
15 *
16 * 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 */
23
24 #include "postgres.h"
25 #include "fmgr.h"
26 #include "funcapi.h"
27 #include "utils/builtins.h"
28 #include <swish-e.h>
29
30 #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
31 #define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))
32
33
34 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 char *index_path;
52 char *query;
53
54 /* stuff done only on the first call of the function */
55 if (SRF_IS_FIRSTCALL()) {
56 MemoryContext oldcontext;
57
58 /* 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 /* 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
70 /* Send any errors or warnings to stderr (default is stdout) */
71 SwishErrorsToStderr();
72
73 elog(INFO, "pgswish: SwishInit(%s)", index_path);
74
75 swish_handle = SwishInit( index_path );
76
77 if (! swish_handle) {
78 elog(ERROR, "pgswish: can't open %s", index_path);
79 SRF_RETURN_DONE(funcctx);
80 }
81
82 if ( SwishError( swish_handle ) ) error_or_abort( swish_handle );
83 /* set ranking scheme. default is 0 */
84 SwishRankScheme( swish_handle, 0 );
85
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 elog(INFO, "pgswish: SwishQuery(%s)", query);
91 /* Here's a short-cut to searching that creates a search object and searches at the same time */
92 results = SwishQuery( swish_handle, query);
93 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 elog(INFO, "no results for: %s", query );
101
102 elog(INFO, "pgswish: SwishHits = %d", funcctx->max_calls);
103
104 /* 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 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
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