10 |
* NOTES: |
* NOTES: |
11 |
* - clear structures with memset to support hash indexes (who whould like |
* - clear structures with memset to support hash indexes (who whould like |
12 |
* to create hash index on table returned from function?) |
* 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: |
* Based on: |
17 |
* - C example from PostgreSQL documentation (BSD licence) |
* - C example from PostgreSQL documentation (BSD licence) |
29 |
|
|
30 |
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) |
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) |
31 |
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) |
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) |
32 |
|
#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) |
33 |
|
#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) |
34 |
|
|
35 |
|
|
36 |
SW_HANDLE swish_handle = NULL;/* Database handle */ |
SW_HANDLE swish_handle = NULL;/* Database handle */ |
37 |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
38 |
SW_RESULTS results = NULL; /* results handle -- holds list of results */ |
SW_RESULTS swish_results = NULL; /* results handle -- holds list of results */ |
39 |
|
SW_RESULT *sw_res = NULL; /* one row from swish-e results */ |
40 |
|
|
41 |
/* define PostgreSQL v1 function */ |
/* define PostgreSQL v1 function */ |
42 |
PG_FUNCTION_INFO_V1(pgswish); |
PG_FUNCTION_INFO_V1(pgswish); |
48 |
TupleDesc tupdesc; |
TupleDesc tupdesc; |
49 |
TupleTableSlot *slot; |
TupleTableSlot *slot; |
50 |
AttInMetadata *attinmeta; |
AttInMetadata *attinmeta; |
|
SW_HANDLE swish_handle = NULL; /* Database handle */ |
|
|
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
|
|
SW_RESULTS results = NULL; /* results handle -- holds list of results */ |
|
51 |
char *index_path; |
char *index_path; |
52 |
char *query; |
char *query; |
53 |
|
|
79 |
SRF_RETURN_DONE(funcctx); |
SRF_RETURN_DONE(funcctx); |
80 |
} |
} |
81 |
|
|
82 |
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
error_or_abort( swish_handle ); |
83 |
/* set ranking scheme. default is 0 */ |
/* set ranking scheme. default is 0 */ |
84 |
SwishRankScheme( swish_handle, 1 ); |
SwishRankScheme( swish_handle, 0 ); |
85 |
|
error_or_abort( swish_handle ); |
|
/* Check for errors after every call */ |
|
|
if ( SwishError( swish_handle ) ) |
|
|
error_or_abort( swish_handle ); /* print an error or abort -- see below */ |
|
86 |
|
|
87 |
elog(INFO, "pgswish: SwishQuery(%s)", query); |
elog(INFO, "pgswish: SwishQuery(%s)", query); |
88 |
/* Here's a short-cut to searching that creates a search object and searches at the same time */ |
/* Here's a short-cut to searching that creates a search object and searches at the same time */ |
89 |
elog(INFO,"## FIXME: SwishQuery kills back-end?"); |
swish_results = SwishQuery( swish_handle, query); |
90 |
results = SwishQuery( swish_handle, query); |
error_or_abort( swish_handle ); |
|
elog(INFO,"## FIXME: no..."); |
|
|
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
|
91 |
|
|
92 |
/* total number of tuples to be returned */ |
/* total number of tuples to be returned */ |
93 |
funcctx->max_calls = SwishHits( results ); |
funcctx->max_calls = SwishHits( swish_results ); |
94 |
|
|
95 |
/* check if results exists */ |
/* check if results exists */ |
96 |
if ( 0 == funcctx->max_calls ) |
if ( 0 == funcctx->max_calls ) |
115 |
funcctx->attinmeta = attinmeta; |
funcctx->attinmeta = attinmeta; |
116 |
|
|
117 |
MemoryContextSwitchTo(oldcontext); |
MemoryContextSwitchTo(oldcontext); |
118 |
|
|
119 |
|
elog(INFO, "SRF_IS_FIRSTCALL done"); |
120 |
} |
} |
121 |
|
|
122 |
/* stuff done on every call of the function */ |
/* stuff done on every call of the function */ |
132 |
HeapTuple tuple; |
HeapTuple tuple; |
133 |
Datum result; |
Datum result; |
134 |
|
|
135 |
if (0) { |
elog(INFO, "pgswish: loop count %d", call_cntr); |
136 |
|
|
137 |
|
if (! swish_results) { |
138 |
|
elog(ERROR, "pgswish: no swish-e results"); |
139 |
|
SRF_RETURN_DONE(funcctx); |
140 |
|
} |
141 |
|
|
142 |
|
elog(DEBUG1, "pgswish: check for swish-e error"); |
143 |
|
error_or_abort( swish_handle ); |
144 |
|
|
145 |
/* |
/* |
146 |
* Prepare a values array for storage in our slot. |
* Prepare a values array for storage in our slot. |
147 |
* This should be an array of C strings which will |
* This should be an array of C strings which will |
148 |
* be processed later by the type input functions. |
* be processed later by the type input functions. |
149 |
*/ |
*/ |
|
values = (char **) palloc(5 * sizeof(char *)); |
|
|
values[0] = _textout( SwishResultPropertyULong ( result, "swishrank" ) ); |
|
|
values[1] = _textout( SwishResultPropertyStr ( result, "swishdocpath" ) ); |
|
|
values[2] = _textout( SwishResultPropertyStr ( result, "swishtitle" ) ); |
|
|
values[3] = _textout( SwishResultPropertyStr ( result, "swishdocsize" ) ); |
|
|
values[4] = _textout( SwishResultPropertyStr ( result, "swishdbfile" ) ); |
|
150 |
|
|
151 |
|
sw_res = SwishNextResult( swish_results ); |
152 |
|
if (! sw_res) { |
153 |
|
elog(ERROR, "pgswish: swish-e sort result list: %d rows expected %d", call_cntr, max_calls - 1); |
154 |
|
SRF_RETURN_DONE(funcctx); |
155 |
|
} |
156 |
|
|
157 |
|
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", |
158 |
|
SwishResultPropertyStr ( sw_res, "swishdocpath" ), |
159 |
|
SwishResultPropertyULong ( sw_res, "swishrank" ), |
160 |
|
SwishResultPropertyULong ( sw_res, "swishdocsize" ), |
161 |
|
SwishResultPropertyStr ( sw_res, "swishtitle"), |
162 |
|
SwishResultPropertyStr ( sw_res, "swishdbfile" ), |
163 |
|
SwishResultPropertyStr ( sw_res, "swishlastmodified" ), |
164 |
|
SwishResultPropertyULong ( sw_res, "swishreccount" ), /* can figure this out in loop, of course */ |
165 |
|
SwishResultPropertyULong ( sw_res, "swishfilenum" ) |
166 |
|
); |
167 |
|
|
168 |
|
values = (char **) palloc(4 * sizeof(char *)); |
169 |
|
|
170 |
|
values[0] = prop2int( sw_res, "swishrank" ); |
171 |
|
values[1] = prop2text( sw_res, "swishdocpath" ); |
172 |
|
values[2] = prop2text( sw_res, "swishtitle" ); |
173 |
|
values[3] = prop2int( sw_res, "swishdocsize" ); |
174 |
|
|
175 |
|
/* |
176 |
|
values[0] = (char *) palloc(16 * sizeof(char)); |
177 |
|
snprintf(values[0], 16, "%d", 1); |
178 |
|
values[1] = (char *) palloc(16 * sizeof(char)); |
179 |
|
snprintf(values[1], 16, "%d", 2); |
180 |
|
values[2] = (char *) palloc(16 * sizeof(char)); |
181 |
|
snprintf(values[2], 16, "%d", 3); |
182 |
|
values[3] = (char *) palloc(16 * sizeof(char)); |
183 |
|
snprintf(values[3], 16, "%d", 4); |
184 |
|
*/ |
185 |
|
|
186 |
/* build a tuple */ |
/* build a tuple */ |
187 |
tuple = BuildTupleFromCStrings(attinmeta, values); |
tuple = BuildTupleFromCStrings(attinmeta, values); |
188 |
|
|
189 |
/* make the tuple into a datum */ |
/* make the tuple into a datum */ |
190 |
result = TupleGetDatum(slot, tuple); |
result = TupleGetDatum(slot, tuple); |
191 |
|
|
192 |
} |
/* clean up ? */ |
193 |
/* clean up (this is not really necessary) */ |
pfree(values[0]); |
194 |
|
pfree(values[1]); |
195 |
|
pfree(values[2]); |
196 |
|
pfree(values[3]); |
197 |
|
pfree(values); |
198 |
|
|
199 |
|
elog(INFO, "row: %s|%s|%s|%s",values[0],values[1],values[2],values[3]); |
200 |
|
|
201 |
SRF_RETURN_NEXT(funcctx, result); |
SRF_RETURN_NEXT(funcctx, result); |
202 |
} else { |
} else { |
203 |
|
elog(INFO, "loop over"); |
204 |
|
|
205 |
/* free swish object and close */ |
/* free swish object and close */ |
206 |
Free_Search_Object( search ); |
Free_Search_Object( search ); |
207 |
SwishClose( swish_handle ); |
SwishClose( swish_handle ); |
211 |
} |
} |
212 |
} |
} |
213 |
|
|
214 |
|
Datum swtextprop(PG_FUNCTION_ARGS) { |
215 |
|
char *prop; |
216 |
|
char *val; |
217 |
|
|
218 |
|
prop = _textout(PG_GETARG_TEXT_P(0)); |
219 |
|
elog(INFO, "pgswish: swextprop(%s)", prop); |
220 |
|
|
221 |
|
val = prop2int( sw_res, prop ); |
222 |
|
error_or_abort( swish_handle ); |
223 |
|
|
224 |
|
elog(INFO, "pgswish: swextprop(%s) = '%s'", prop, val ); |
225 |
|
|
226 |
|
PG_FREE_IF_COPY(prop, 0); |
227 |
|
PG_RETURN_TEXT_P( _textin(val) ); |
228 |
|
} |
229 |
|
|
230 |
|
/* make text var prom property */ |
231 |
|
char *prop2text(SW_RESULT sw_res, char *propname) { |
232 |
|
char *val; |
233 |
|
char *prop; |
234 |
|
int len; |
235 |
|
|
236 |
|
elog(DEBUG1, "prop2text(%s)", propname); |
237 |
|
|
238 |
|
prop = SwishResultPropertyStr( sw_res, propname ); |
239 |
|
error_or_abort( swish_handle ); |
240 |
|
|
241 |
|
len = strlen(prop); |
242 |
|
elog(INFO, "prop2text(%s) = '%s' %d bytes", propname, prop, len); |
243 |
|
|
244 |
|
len++; |
245 |
|
len *= sizeof(char); |
246 |
|
|
247 |
|
elog(DEBUG1, "palloc(%d)", len); |
248 |
|
|
249 |
|
val = palloc(len); |
250 |
|
|
251 |
|
memset(val, 0, len); |
252 |
|
strncpy(val, prop, len); |
253 |
|
|
254 |
|
elog(DEBUG1, "val=%s", val); |
255 |
|
|
256 |
|
return val; |
257 |
|
} |
258 |
|
|
259 |
|
/* make integer variable from property */ |
260 |
|
char *prop2int(SW_RESULT sw_res, char *propname) { |
261 |
|
char *val; |
262 |
|
unsigned long prop; |
263 |
|
int len; |
264 |
|
|
265 |
|
elog(DEBUG1, "prop2int(%s)", propname); |
266 |
|
|
267 |
|
prop = SwishResultPropertyULong( sw_res, propname ); |
268 |
|
error_or_abort( swish_handle ); |
269 |
|
|
270 |
|
elog(INFO, "prop2int(%s) = %lu", propname, prop); |
271 |
|
|
272 |
|
len = 128 * sizeof(char); |
273 |
|
elog(DEBUG1, "palloc(%d)", len); |
274 |
|
|
275 |
|
val = palloc(len); |
276 |
|
memset(val, 0, len); |
277 |
|
|
278 |
|
snprintf(val, len, "%lu", prop); |
279 |
|
|
280 |
|
elog(DEBUG1, "val=%s", val); |
281 |
|
|
282 |
|
return val; |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
/* |
/* |
287 |
* elog errors |
* check if swish has returned error, and elog it. |
|
* |
|
288 |
*/ |
*/ |
|
|
|
289 |
static void error_or_abort( SW_HANDLE swish_handle ) { |
static void error_or_abort( SW_HANDLE swish_handle ) { |
290 |
if ( !SwishError( swish_handle ) ) |
if ( !SwishError( swish_handle ) ) |
291 |
return; |
return; |
299 |
); |
); |
300 |
if ( search ) Free_Search_Object( search ); |
if ( search ) Free_Search_Object( search ); |
301 |
SwishClose( swish_handle ); |
SwishClose( swish_handle ); |
|
|
|
|
/* do when there is no more left */ |
|
302 |
} |
} |
303 |
|
|