--- trunk/pgswish.c 2005/03/06 21:13:39 19 +++ trunk/pgswish.c 2005/05/29 20:30:18 20 @@ -36,10 +36,11 @@ #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 swish_results = NULL; /* results handle -- holds list of results */ -SW_RESULT *sw_res = NULL; /* one row from swish-e results */ +/* Globals */ +static SW_HANDLE swish_handle = NULL; /* Database handle */ +static SW_SEARCH search = NULL; /* search handle -- search parameters */ +static SW_RESULTS swish_results = NULL; /* results handle -- list of results */ +static SW_RESULT *sw_res = NULL; /* one row from swish-e results */ /* define PostgreSQL v1 function */ PG_FUNCTION_INFO_V1(pgswish); @@ -53,6 +54,7 @@ AttInMetadata *attinmeta; char *index_path; char *query; + FILE *logfh; /* stuff done only on the first call of the function */ if (SRF_IS_FIRSTCALL()) { @@ -70,13 +72,27 @@ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /* Send any errors or warnings to stderr (default is stdout) */ - SwishErrorsToStderr(); + /* Send any errors or warnings to log, as well as + * STDOUT and STDERR (just to be sure) */ + if ( logfh = fopen("/tmp/pgswish.log", "a") ) { + set_error_handle( logfh ); + elog(INFO, "loggin swish-e errors to /tmp/pgswish.log"); + /* redirect STDOUT and STDERR to log */ + dup2(1, logfh); + dup2(2, logfh); + } else { + elog(INFO, "can't open /tmp/pgswish.log -- errors from swish-e won't be cought and may result in back-end crashes!"); + } elog(INFO, "pgswish: SwishInit(%s)", index_path); - + swish_handle = SwishInit( index_path ); + if ( SwishError( swish_handle ) ) + elog(INFO, "pgswish: SwishInit(%s) failed: %s", index_path, SwishErrorString( swish_handle )); + + elog(INFO, "handle: %08x", swish_handle); + if (! swish_handle) { elog(ERROR, "pgswish: can't open %s", index_path); SRF_RETURN_DONE(funcctx); @@ -216,84 +232,6 @@ } } -/* work in progress */ -PG_FUNCTION_INFO_V1(pgswish2); -Datum pgswish2(PG_FUNCTION_ARGS) -{ - int nrows = 3; - int16 typlen; - bool typbyval; - char typalign; - ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; - AttInMetadata *attinmeta; - TupleDesc tupdesc; - Tuplestorestate *tupstore = NULL; - HeapTuple tuple; - MemoryContext per_query_ctx; - MemoryContext oldcontext; - Datum dvalue; - char **values; - int ncols; - int i, j; - - /* check to see if caller supports us returning a tuplestore */ - if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize)) - ereport(ERROR, - (errcode(ERRCODE_SYNTAX_ERROR), - errmsg("materialize mode required, but it is not " \ - "allowed in this context"))); - - /* get the requested return tuple description */ - tupdesc = rsinfo->expectedDesc; - ncols = tupdesc->natts; - - /* - * The requested tuple description better match up with the array - * we were given. - */ - /* OK, use it */ - attinmeta = TupleDescGetAttInMetadata(tupdesc); - - /* Now go to work */ - rsinfo->returnMode = SFRM_Materialize; - - per_query_ctx = fcinfo->flinfo->fn_mcxt; - oldcontext = MemoryContextSwitchTo(per_query_ctx); - - /* initialize our tuplestore */ - tupstore = tuplestore_begin_heap(true, false, SortMem); - - values = (char **) palloc(ncols * sizeof(char *)); - - for (i = 0; i < nrows; i++) - { - for (j = 0; j < ncols; j++) - { - values[j] = DatumGetCString( "foo" ); - } - /* construct the tuple */ - tuple = BuildTupleFromCStrings(attinmeta, values); - - /* now store it */ - tuplestore_puttuple(tupstore, tuple); - } - - tuplestore_donestoring(tupstore); - rsinfo->setResult = tupstore; - - /* - * SFRM_Materialize mode expects us to return a NULL Datum. The actual - * tuples are in our tuplestore and passed back through - * rsinfo->setResult. rsinfo->setDesc is set to the tuple description - * that we actually used to build our tuples with, so the caller can - * verify we did what it was expecting. - */ - rsinfo->setDesc = tupdesc; - MemoryContextSwitchTo(oldcontext); - - return (Datum) 0; -} - /* make text var from property */ char *prop2text(SW_RESULT sw_res, char *propname) {