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

Diff of /trunk/pgswish.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 21 by dpavlin, Sun May 29 20:30:18 2005 UTC revision 22 by dpavlin, Sun May 29 22:41:20 2005 UTC
# Line 8  Line 8 
8   * - support composite type arguments   * - support composite type arguments
9   * - split error_or_abort   * - split error_or_abort
10   * - use getResultPropValue not SwishResultPropertyStr   * - use getResultPropValue not SwishResultPropertyStr
11     * - fix everything about pgswish_arr which is broken
12   *   *
13   * NOTES:   * NOTES:
14   * - clear structures with memset to support hash indexes (who whould like   * - clear structures with memset to support hash indexes (who whould like
# Line 233  Datum pgswish(PG_FUNCTION_ARGS) { Line 234  Datum pgswish(PG_FUNCTION_ARGS) {
234  }  }
235    
236    
237    /*
238     * new function with support for property selection
239     */
240    
241    PG_FUNCTION_INFO_V1(pgswish_arr);
242    Datum pgswish_arr(PG_FUNCTION_ARGS)
243    {
244            ArrayType       *prop_arr = PG_GETARG_ARRAYTYPE_P(5);
245            Oid             prop_element_type = ARR_ELEMTYPE(prop_arr);
246            int             prop_ndims = ARR_NDIM(prop_arr);
247            int             *prop_dim_counts = ARR_DIMS(prop_arr);
248            int             *prop_dim_lower_bounds = ARR_LBOUND(prop_arr);
249            int             ncols = 0;
250            int             nrows = 0;
251            int             indx[MAXDIM];
252            int16           prop_len;
253            bool            prop_byval;
254            char            prop_align;
255            ReturnSetInfo   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
256            AttInMetadata   *attinmeta;
257            TupleDesc       tupdesc;
258            Tuplestorestate *tupstore = NULL;
259            HeapTuple       tuple;
260            MemoryContext   per_query_ctx;
261            MemoryContext   oldcontext;
262            Datum           dvalue;
263            char            **values;
264            int             rsinfo_ncols;
265            int             i, j;
266            /* swish-e */
267            FILE            *logfh;
268            int             resnum;
269            int             limit = 0;
270            int             offset = 0;
271    
272            char            *index_path;
273            char            *query;
274            char            *attr;
275    
276    
277            /* only allow 1D input array */
278            if (prop_ndims == 1)
279            {
280                    ncols = prop_dim_counts[0];
281            }
282            else
283                    ereport(ERROR,
284                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
285                                     errmsg("invalid input array"),
286                                     errdetail("Input array must have 1 dimension")));
287                    
288            /* check to see if caller supports us returning a tuplestore */
289            if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
290                    ereport(ERROR,
291                                    (errcode(ERRCODE_SYNTAX_ERROR),
292                                     errmsg("materialize mode required, but it is not " \
293                                                    "allowed in this context")));
294    
295            /* get info about element type needed to construct the array */
296            get_typlenbyvalalign(prop_element_type, &prop_len, &prop_byval, &prop_align);
297    
298            /* get the requested return tuple description */
299            tupdesc = rsinfo->expectedDesc;
300            rsinfo_ncols = tupdesc->natts;
301    
302            /*
303             * The requested tuple description better match up with the array
304             * we were given.
305             */
306            if (rsinfo_ncols != ncols)
307                    ereport(ERROR,
308                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
309                                     errmsg("invalid input array"),
310                                     errdetail("Number of elements in array must match number of query specified columns.")));
311    
312            /* OK, use it */
313            attinmeta = TupleDescGetAttInMetadata(tupdesc);
314    
315            /* Now go to work */
316            rsinfo->returnMode = SFRM_Materialize;
317    
318            per_query_ctx = fcinfo->flinfo->fn_mcxt;
319            oldcontext = MemoryContextSwitchTo(per_query_ctx);
320    
321            /* initialize our tuplestore */
322            tupstore = tuplestore_begin_heap(true, false, SortMem);
323    
324    
325            /* take rest of arguments from function */
326    
327            /* index path */
328            if (PG_ARGISNULL(0)) {
329                    ereport(ERROR,
330                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
331                                     errmsg("index path can't be null"),
332                                     errdetail("Index path must be valid full path to swish-e index")));
333            }
334            index_path = _textout(PG_GETARG_TEXT_P(0));
335    
336            /* query string */
337            if (PG_ARGISNULL(0)) {
338                    query = "";
339            } else {
340                    query = _textout(PG_GETARG_TEXT_P(1));
341            }
342    
343            /* atribute filter */
344            if (PG_ARGISNULL(2)) {
345                    attr = "";
346            } else {
347                    attr = _textout(PG_GETARG_TEXT_P(2));
348            }
349    
350            /* limit */
351            if (PG_ARGISNULL(3)) {
352                    limit = 0;
353            } else {
354                    limit = PG_GETARG_INT32(3);
355            }
356    
357            /* offset */
358            if (PG_ARGISNULL(4)) {
359                    offset = 0;
360            } else {
361                    offset = PG_GETARG_INT32(4);
362            }
363    
364    
365            /* Send any errors or warnings to log, as well as
366             * STDOUT and STDERR (just to be sure) */
367            if ( logfh = fopen("/tmp/pgswish.log", "a") ) {
368                    set_error_handle( logfh );
369                    elog(INFO, "loggin swish-e errors to /tmp/pgswish.log");
370                    /* redirect STDOUT and STDERR to log */
371                    dup2(1, logfh);
372                    dup2(2, logfh);
373            } else {
374                    elog(INFO, "can't open /tmp/pgswish.log -- errors from swish-e won't be cought and may result in back-end crashes!");
375            }
376    
377            elog(INFO, "pgswish: SwishInit(%s)", index_path);
378    
379            swish_handle = SwishInit( index_path );
380    
381            if ( SwishError( swish_handle ) || ! swish_handle )
382                    ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
383                            errmsg("pgswish: SwishInit(%s) failed", index_path ),
384                            errdetail( SwishErrorString( swish_handle ) )
385                    ));
386    
387            elog(DEBUG1, "pgswish: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset);
388    
389    
390            /* set ranking scheme. default is 0 */
391            SwishRankScheme( swish_handle, 0 );
392            error_or_abort( swish_handle );
393    
394            elog(INFO, "pgswish: SwishQuery(%s)", query);
395            /* Here's a short-cut to searching that creates a search object
396             * and searches at the same time */
397            
398            /* set the search phrase to the search condition object */
399            if (! PG_ARGISNULL(1) && strlen(query) > 0)
400                    swish_results = SwishQuery( swish_handle, query);
401            error_or_abort( swish_handle );
402    
403            /* total number of tuples to be returned */
404            resnum = SwishHits( swish_results );
405    
406            /* FIXME */
407            if (! PG_ARGISNULL(2) && strlen(attr) >= 10) {
408                    elog(DEBUG1,"ignored: %s", attr);
409            }
410    
411            /* check if results exists */
412            if ( 0 == resnum ) {
413                    elog(INFO, "pgswish: no results for: %s", query );
414            }
415    
416            /* total number of tuples to be returned */
417            if (limit && limit < resnum) {
418                    nrows = limit - offset;
419            } else {
420                    nrows = resnum - offset;
421            }
422    
423    
424            elog(DEBUG1, "pgswish: found %d hits for %s", resnum, query);
425    
426    
427            values = (char **) palloc(ncols * sizeof(char *));
428    
429            for (i = 0; i < nrows; i++)
430            {
431                    SwishSeekResult( swish_results, i + offset );
432                    sw_res = SwishNextResult( swish_results );
433    
434                    /* get result from swish-e */
435                    if (! ( SwishErrorString( swish_handle ) ) ) {
436                            elog(INFO, "can't find result %d", i + offset);
437                    } else {
438                            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",
439                                    SwishResultPropertyStr   ( sw_res, "swishdocpath" ),
440                                    SwishResultPropertyULong ( sw_res, "swishrank" ),
441                                    SwishResultPropertyULong ( sw_res, "swishdocsize" ),
442                                    SwishResultPropertyStr   ( sw_res, "swishtitle"),
443                                    SwishResultPropertyStr   ( sw_res, "swishdbfile" ),
444                                    SwishResultPropertyStr   ( sw_res, "swishlastmodified" ),
445                                    SwishResultPropertyULong ( sw_res, "swishreccount" ),  /* can figure this out in loop, of course */
446                                    SwishResultPropertyULong ( sw_res, "swishfilenum" )
447                            );
448                    }
449    
450                    /* iterate over results */
451                    for (j = 0; j < ncols; j++)
452                    {
453                            bool    isnull;
454    
455                            /* array value of this position */
456                            indx[0] = j + prop_dim_lower_bounds[0];
457    
458                            dvalue = array_ref(prop_arr, prop_ndims, indx, -1, prop_len, prop_byval, prop_align, &isnull);
459    
460                            if (!isnull && sw_res)
461                                    values[j] = DatumGetCString(
462                                            prop2text( sw_res,
463                                                    (char *)DirectFunctionCall1(textout, dvalue)
464                                            ));
465                            else
466                                    values[j] = NULL;
467                    }
468                    /* construct the tuple */
469                    tuple = BuildTupleFromCStrings(attinmeta, values);
470    
471                    /* now store it */
472                    tuplestore_puttuple(tupstore, tuple);
473    
474            }
475    
476            tuplestore_donestoring(tupstore);
477            rsinfo->setResult = tupstore;
478    
479            /*
480             * SFRM_Materialize mode expects us to return a NULL Datum. The actual
481             * tuples are in our tuplestore and passed back through
482             * rsinfo->setResult. rsinfo->setDesc is set to the tuple description
483             * that we actually used to build our tuples with, so the caller can
484             * verify we did what it was expecting.
485             */
486            rsinfo->setDesc = tupdesc;
487            MemoryContextSwitchTo(oldcontext);
488    
489            /* free swish object and close */
490            Free_Search_Object( search );
491            SwishClose( swish_handle );
492    
493            return (Datum) 0;
494    }
495    
496    
497    
498    
499  /* make text var from property */  /* make text var from property */
500  char *prop2text(SW_RESULT sw_res, char *propname) {  char *prop2text(SW_RESULT sw_res, char *propname) {
501          char *val;          char *val;

Legend:
Removed from v.21  
changed lines
  Added in v.22

  ViewVC Help
Powered by ViewVC 1.1.26