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 |
|
|
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
82 |
|
|
/* set ranking scheme. default is 0 */ |
83 |
dpavlin |
10 |
SwishRankScheme( swish_handle, 0 ); |
84 |
dpavlin |
8 |
|
85 |
|
|
/* Check for errors after every call */ |
86 |
|
|
if ( SwishError( swish_handle ) ) |
87 |
|
|
error_or_abort( swish_handle ); /* print an error or abort -- see below */ |
88 |
|
|
|
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 |
8 |
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
93 |
|
|
|
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 |
|
|
} |
121 |
|
|
|
122 |
|
|
/* stuff done on every call of the function */ |
123 |
|
|
funcctx = SRF_PERCALL_SETUP(); |
124 |
|
|
|
125 |
|
|
call_cntr = funcctx->call_cntr; |
126 |
|
|
max_calls = funcctx->max_calls; |
127 |
|
|
slot = funcctx->slot; |
128 |
|
|
attinmeta = funcctx->attinmeta; |
129 |
|
|
|
130 |
|
|
if (call_cntr < max_calls) { |
131 |
|
|
char **values; |
132 |
|
|
HeapTuple tuple; |
133 |
|
|
Datum result; |
134 |
dpavlin |
11 |
char *prop; |
135 |
|
|
SW_RESULT *sw_res; /* one row from swish-e results */ |
136 |
dpavlin |
8 |
|
137 |
dpavlin |
11 |
elog(INFO, "pgswish: in loop %d", call_cntr); |
138 |
|
|
|
139 |
|
|
if (! swish_results) { |
140 |
|
|
elog(ERROR, "pgswish: no swish-e results"); |
141 |
|
|
SRF_RETURN_DONE(funcctx); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
elog(INFO, "pgswish: check for swish-e error"); |
145 |
|
|
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
146 |
|
|
|
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 |
dpavlin |
9 |
values = (char **) palloc(5 * sizeof(char *)); |
153 |
dpavlin |
8 |
|
154 |
dpavlin |
11 |
sw_res = SwishNextResult( swish_results ); |
155 |
|
|
if (! sw_res) { |
156 |
|
|
elog(ERROR, "pgswish: swish-e sort result list: %d rows expected %d", call_cntr, max_calls - 1); |
157 |
|
|
SRF_RETURN_DONE(funcctx); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
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", |
161 |
|
|
SwishResultPropertyStr ( sw_res, "swishdocpath" ), |
162 |
|
|
SwishResultPropertyULong ( sw_res, "swishrank" ), |
163 |
|
|
SwishResultPropertyULong ( sw_res, "swishdocsize" ), |
164 |
|
|
SwishResultPropertyStr ( sw_res, "swishtitle"), |
165 |
|
|
SwishResultPropertyStr ( sw_res, "swishdbfile" ), |
166 |
|
|
SwishResultPropertyStr ( sw_res, "swishlastmodified" ), |
167 |
|
|
SwishResultPropertyULong ( sw_res, "swishreccount" ), /* can figure this out in loop, of course */ |
168 |
|
|
SwishResultPropertyULong ( sw_res, "swishfilenum" ) |
169 |
|
|
); |
170 |
|
|
|
171 |
|
|
elog(INFO, "pgswish: get prop"); |
172 |
|
|
prop = SwishResultPropertyStr ( sw_res, "swishdocpath" ); |
173 |
|
|
elog(INFO, "pgswish: got error?"); |
174 |
|
|
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
175 |
|
|
elog(INFO, "swishdocpath: %s", prop); |
176 |
|
|
|
177 |
|
|
// values[0] = GET_STR( SwishResultPropertyULong ( sw_res, "swishrank" ) ); |
178 |
|
|
values[0] = NULL; |
179 |
|
|
// values[1] = GET_TEXT( SwishResultPropertyStr ( sw_res, "swishdocpath" ) ); |
180 |
|
|
if (0) { |
181 |
|
|
values[1] = GET_TEXT( SwishResultPropertyStr ( sw_res, "swishdocpath" ) ); |
182 |
|
|
values[2] = _textout( SwishResultPropertyStr ( sw_res, "swishtitle" ) ); |
183 |
|
|
values[3] = _textout( SwishResultPropertyStr ( sw_res, "swishdocsize" ) ); |
184 |
|
|
values[4] = _textout( SwishResultPropertyStr ( sw_res, "swishdbfile" ) ); |
185 |
|
|
|
186 |
dpavlin |
8 |
/* build a tuple */ |
187 |
|
|
tuple = BuildTupleFromCStrings(attinmeta, values); |
188 |
|
|
|
189 |
|
|
/* make the tuple into a datum */ |
190 |
|
|
result = TupleGetDatum(slot, tuple); |
191 |
|
|
|
192 |
|
|
} |
193 |
|
|
/* clean up (this is not really necessary) */ |
194 |
|
|
|
195 |
|
|
SRF_RETURN_NEXT(funcctx, result); |
196 |
|
|
} else { |
197 |
|
|
/* free swish object and close */ |
198 |
|
|
Free_Search_Object( search ); |
199 |
|
|
SwishClose( swish_handle ); |
200 |
|
|
|
201 |
|
|
/* do when there is no more left */ |
202 |
|
|
SRF_RETURN_DONE(funcctx); |
203 |
|
|
} |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
/* |
207 |
|
|
* elog errors |
208 |
|
|
* |
209 |
|
|
*/ |
210 |
|
|
|
211 |
|
|
static void error_or_abort( SW_HANDLE swish_handle ) { |
212 |
|
|
if ( !SwishError( swish_handle ) ) |
213 |
|
|
return; |
214 |
|
|
|
215 |
|
|
/* print a message */ |
216 |
|
|
elog(ERROR, |
217 |
|
|
"pgswish error: Number [%d], Type [%s], Optional Message: [%s]\n", |
218 |
|
|
SwishError( swish_handle ), |
219 |
|
|
SwishErrorString( swish_handle ), |
220 |
|
|
SwishLastErrorMsg( swish_handle ) |
221 |
|
|
); |
222 |
|
|
if ( search ) Free_Search_Object( search ); |
223 |
|
|
SwishClose( swish_handle ); |
224 |
|
|
|
225 |
|
|
/* do when there is no more left */ |
226 |
|
|
} |
227 |
|
|
|