--- trunk/pgswish.c 2005/02/18 23:34:31 8 +++ trunk/pgswish.c 2005/02/19 12:27:04 11 @@ -10,20 +10,32 @@ * NOTES: * - clear structures with memset to support hash indexes (who whould like * to create hash index on table returned from function?) + * - number of returned rows is set by PostgreSQL evaluator, see: + * http://archives.postgresql.org/pgsql-hackers/2005-02/msg00546.php * - * Based on C example from PostgreSQL documentation and swish-e - * src/libtest.c, so licence is GPL + * Based on: + * - C example from PostgreSQL documentation (BSD licence) + * - swish-e example src/libtest.c (GPL) + * - _textin/_textout from pgcurl.c (LGPL) + * + * This code is licenced under GPL */ #include "postgres.h" #include "fmgr.h" #include "funcapi.h" +#include "utils/builtins.h" #include +#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) +#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) +#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) +#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) + 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 */ +SW_RESULTS swish_results = NULL; /* results handle -- holds list of results */ /* define PostgreSQL v1 function */ PG_FUNCTION_INFO_V1(pgswish); @@ -35,54 +47,58 @@ TupleDesc tupdesc; TupleTableSlot *slot; 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 */ + char *index_path; + char *query; /* stuff done only on the first call of the function */ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; + /* take arguments from function */ + //index_path = _textout(PG_GETARG_TEXT_P(0)); + index_path = _textout(PG_GETARG_TEXT_P(0)); + query = _textout(PG_GETARG_TEXT_P(1)); + /* create a function context for cross-call persistence */ funcctx = SRF_FIRSTCALL_INIT(); /* switch to memory context appropriate for multiple function calls */ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); + /* Send any errors or warnings to stderr (default is stdout) */ SwishErrorsToStderr(); - elog(INFO, "pgswish opening %s, query %s", - PG_GETARG_TEXT_P(0), - PG_GETARG_TEXT_P(1) - ); + elog(INFO, "pgswish: SwishInit(%s)", index_path); - swish_handle = SwishInit( (char *)PG_GETARG_TEXT_P(0) ); + swish_handle = SwishInit( index_path ); if (! swish_handle) { - elog(ERROR, "pgswish: can't open %s", PG_GETARG_TEXT_P(0)); + elog(ERROR, "pgswish: can't open %s", index_path); SRF_RETURN_DONE(funcctx); } if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); /* set ranking scheme. default is 0 */ - SwishRankScheme( swish_handle, 1 ); + SwishRankScheme( swish_handle, 0 ); /* Check for errors after every call */ if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); /* print an error or abort -- see below */ + elog(INFO, "pgswish: SwishQuery(%s)", query); /* Here's a short-cut to searching that creates a search object and searches at the same time */ - results = SwishQuery( swish_handle, (char *)PG_GETARG_TEXT_P(1) ); - + swish_results = SwishQuery( swish_handle, query); if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); /* total number of tuples to be returned */ - funcctx->max_calls = SwishHits( results ); + funcctx->max_calls = SwishHits( swish_results ); /* check if results exists */ if ( 0 == funcctx->max_calls ) - elog(INFO, "no results for: %s", PG_GETARG_TEXT_P(1) ); + elog(INFO, "no results for: %s", query ); + + elog(INFO, "pgswish: SwishHits = %d", funcctx->max_calls); /* Build a tuple description for a __pgswish tuple */ tupdesc = RelationNameGetTupleDesc("__pgswish"); @@ -115,19 +131,57 @@ char **values; HeapTuple tuple; Datum result; + char *prop; + SW_RESULT *sw_res; /* one row from swish-e results */ + + elog(INFO, "pgswish: in loop %d", call_cntr); + + if (! swish_results) { + elog(ERROR, "pgswish: no swish-e results"); + SRF_RETURN_DONE(funcctx); + } + + elog(INFO, "pgswish: check for swish-e error"); + if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); -if (0) { /* * Prepare a values array for storage in our slot. * This should be an array of C strings which will * be processed later by the type input functions. */ - values = (void **) palloc(5 * sizeof(void *)); - values[0] = (long *) SwishResultPropertyULong ( result, "swishrank" ), - values[1] = (char *) SwishResultPropertyStr ( result, "swishdocpath" ), - values[2] = (char *) SwishResultPropertyStr ( result, "swishtitle" ), - values[3] = (long *) SwishResultPropertyStr ( result, "swishdocsize" ), - values[4] = (char *) SwishResultPropertyStr ( result, "swishdbfile" ), + values = (char **) palloc(5 * sizeof(char *)); + + sw_res = SwishNextResult( swish_results ); + if (! sw_res) { + elog(ERROR, "pgswish: swish-e sort result list: %d rows expected %d", call_cntr, max_calls - 1); + SRF_RETURN_DONE(funcctx); + } + + 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", + SwishResultPropertyStr ( sw_res, "swishdocpath" ), + SwishResultPropertyULong ( sw_res, "swishrank" ), + SwishResultPropertyULong ( sw_res, "swishdocsize" ), + SwishResultPropertyStr ( sw_res, "swishtitle"), + SwishResultPropertyStr ( sw_res, "swishdbfile" ), + SwishResultPropertyStr ( sw_res, "swishlastmodified" ), + SwishResultPropertyULong ( sw_res, "swishreccount" ), /* can figure this out in loop, of course */ + SwishResultPropertyULong ( sw_res, "swishfilenum" ) + ); + + elog(INFO, "pgswish: get prop"); + prop = SwishResultPropertyStr ( sw_res, "swishdocpath" ); + elog(INFO, "pgswish: got error?"); + if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); + elog(INFO, "swishdocpath: %s", prop); + +// values[0] = GET_STR( SwishResultPropertyULong ( sw_res, "swishrank" ) ); + values[0] = NULL; +// values[1] = GET_TEXT( SwishResultPropertyStr ( sw_res, "swishdocpath" ) ); +if (0) { + values[1] = GET_TEXT( SwishResultPropertyStr ( sw_res, "swishdocpath" ) ); + values[2] = _textout( SwishResultPropertyStr ( sw_res, "swishtitle" ) ); + values[3] = _textout( SwishResultPropertyStr ( sw_res, "swishdocsize" ) ); + values[4] = _textout( SwishResultPropertyStr ( sw_res, "swishdbfile" ) ); /* build a tuple */ tuple = BuildTupleFromCStrings(attinmeta, values);