/[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 8 by dpavlin, Fri Feb 18 23:34:31 2005 UTC revision 11 by dpavlin, Sat Feb 19 12:27:04 2005 UTC
# Line 10  Line 10 
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 C example from PostgreSQL documentation and swish-e   * Based on:
17   * src/libtest.c, so licence is GPL   * - 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   */   */
23    
24  #include "postgres.h"  #include "postgres.h"
25  #include "fmgr.h"  #include "fmgr.h"
26  #include "funcapi.h"  #include "funcapi.h"
27    #include "utils/builtins.h"
28  #include <swish-e.h>  #include <swish-e.h>
29    
30    #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
31    #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    
40  /* define PostgreSQL v1 function */  /* define PostgreSQL v1 function */
41  PG_FUNCTION_INFO_V1(pgswish);  PG_FUNCTION_INFO_V1(pgswish);
# Line 35  Datum pgswish(PG_FUNCTION_ARGS) { Line 47  Datum pgswish(PG_FUNCTION_ARGS) {
47          TupleDesc       tupdesc;          TupleDesc       tupdesc;
48          TupleTableSlot  *slot;          TupleTableSlot  *slot;
49          AttInMetadata   *attinmeta;          AttInMetadata   *attinmeta;
50          SW_HANDLE       swish_handle = NULL;    /* Database handle */          char            *index_path;
51          SW_SEARCH       search = NULL;          /* search handle -- holds search parameters */          char            *query;
         SW_RESULTS      results = NULL;         /* results handle -- holds list of results */  
52    
53          /* stuff done only on the first call of the function */          /* stuff done only on the first call of the function */
54          if (SRF_IS_FIRSTCALL()) {          if (SRF_IS_FIRSTCALL()) {
55                  MemoryContext   oldcontext;                  MemoryContext   oldcontext;
56    
57                    /* 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                  /* create a function context for cross-call persistence */                  /* create a function context for cross-call persistence */
63                  funcctx = SRF_FIRSTCALL_INIT();                  funcctx = SRF_FIRSTCALL_INIT();
64    
65                  /* switch to memory context appropriate for multiple function calls */                  /* switch to memory context appropriate for multiple function calls */
66                  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);                  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
67    
68                    
69                  /* Send any errors or warnings to stderr (default is stdout) */                  /* Send any errors or warnings to stderr (default is stdout) */
70                  SwishErrorsToStderr();                  SwishErrorsToStderr();
71    
72                  elog(INFO, "pgswish opening %s, query %s",                  elog(INFO, "pgswish: SwishInit(%s)", index_path);
                                 PG_GETARG_TEXT_P(0),  
                                 PG_GETARG_TEXT_P(1)  
                 );  
73                                    
74                  swish_handle = SwishInit( (char *)PG_GETARG_TEXT_P(0) );                  swish_handle = SwishInit( index_path );
75    
76                  if (! swish_handle) {                  if (! swish_handle) {
77                          elog(ERROR, "pgswish: can't open %s", PG_GETARG_TEXT_P(0));                          elog(ERROR, "pgswish: can't open %s", index_path);
78                          SRF_RETURN_DONE(funcctx);                          SRF_RETURN_DONE(funcctx);
79                  }                  }
80                                    
81                  if ( SwishError( swish_handle ) ) error_or_abort( swish_handle );                  if ( SwishError( swish_handle ) ) error_or_abort( swish_handle );
82                  /* set ranking scheme. default is 0 */                  /* set ranking scheme. default is 0 */
83                  SwishRankScheme( swish_handle, 1 );                  SwishRankScheme( swish_handle, 0 );
84    
85                  /* Check for errors after every call */                  /* Check for errors after every call */
86                  if ( SwishError( swish_handle ) )                  if ( SwishError( swish_handle ) )
87                          error_or_abort( swish_handle );  /* print an error or abort -- see below */                          error_or_abort( swish_handle );  /* print an error or abort -- see below */
88    
89                    elog(INFO, "pgswish: SwishQuery(%s)", query);
90                  /* 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 */
91                  results = SwishQuery( swish_handle, (char *)PG_GETARG_TEXT_P(1) );                  swish_results = SwishQuery( swish_handle, query);
   
92                  if ( SwishError( swish_handle ) ) error_or_abort( swish_handle );                  if ( SwishError( swish_handle ) ) error_or_abort( swish_handle );
93    
94                  /* total number of tuples to be returned */                  /* total number of tuples to be returned */
95                  funcctx->max_calls = SwishHits( results );                  funcctx->max_calls = SwishHits( swish_results );
96    
97                  /* check if results exists */                  /* check if results exists */
98                  if ( 0 == funcctx->max_calls )                  if ( 0 == funcctx->max_calls )
99                          elog(INFO, "no results for: %s", PG_GETARG_TEXT_P(1) );                          elog(INFO, "no results for: %s", query );
100    
101                    elog(INFO, "pgswish: SwishHits = %d", funcctx->max_calls);
102    
103                  /* Build a tuple description for a __pgswish tuple */                  /* Build a tuple description for a __pgswish tuple */
104                  tupdesc = RelationNameGetTupleDesc("__pgswish");                  tupdesc = RelationNameGetTupleDesc("__pgswish");
# Line 115  Datum pgswish(PG_FUNCTION_ARGS) { Line 131  Datum pgswish(PG_FUNCTION_ARGS) {
131                  char            **values;                  char            **values;
132                  HeapTuple       tuple;                  HeapTuple       tuple;
133                  Datum           result;                  Datum           result;
134                    char            *prop;
135                    SW_RESULT       *sw_res;        /* one row from swish-e results */
136    
137                    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    
 if (0) {  
147                  /*                  /*
148                   * Prepare a values array for storage in our slot.                   * Prepare a values array for storage in our slot.
149                   * This should be an array of C strings which will                   * This should be an array of C strings which will
150                   * be processed later by the type input functions.                   * be processed later by the type input functions.
151                   */                   */
152                  values = (void **) palloc(5 * sizeof(void *));                  values = (char **) palloc(5 * sizeof(char *));
153                  values[0] = (long *) SwishResultPropertyULong ( result, "swishrank" ),  
154                  values[1] = (char *) SwishResultPropertyStr   ( result, "swishdocpath" ),                  sw_res = SwishNextResult( swish_results );
155                  values[2] = (char *) SwishResultPropertyStr   ( result, "swishtitle" ),                  if (! sw_res) {
156                  values[3] = (long *) SwishResultPropertyStr   ( result, "swishdocsize" ),                          elog(ERROR, "pgswish: swish-e sort result list: %d rows expected %d", call_cntr, max_calls - 1);
157                  values[4] = (char *) SwishResultPropertyStr   ( result, "swishdbfile" ),                          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                  /* build a tuple */                  /* build a tuple */
187                  tuple = BuildTupleFromCStrings(attinmeta, values);                  tuple = BuildTupleFromCStrings(attinmeta, values);

Legend:
Removed from v.8  
changed lines
  Added in v.11

  ViewVC Help
Powered by ViewVC 1.1.26