/[pgestraier]/trunk/pgest.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/pgest.c

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

revision 12 by dpavlin, Wed May 25 23:38:37 2005 UTC revision 47 by dpavlin, Sun Sep 11 21:44:56 2005 UTC
# Line 14  Line 14 
14   *   *
15   * Based on:   * Based on:
16   * - C example from PostgreSQL documentation (BSD licence)   * - C example from PostgreSQL documentation (BSD licence)
17   * - example002.c from Hyper Estraier (GPL)   * - coreexample002.c and nodeexample002.c from Hyper Estraier (GPL)
18   * - _textin/_textout from pgcurl.c (LGPL)   * - _textin/_textout from pgcurl.c (LGPL)
19   *   *
20   * This code is licenced under GPL   * This code is licenced under GPL
# Line 28  Line 28 
28  #include "miscadmin.h"  #include "miscadmin.h"
29  #include <estraier.h>  #include <estraier.h>
30  #include <cabin.h>  #include <cabin.h>
31    #include <estnode.h>
32    
33  #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))  #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
34  #define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))  #define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))
35  #define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))  #define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
36  #define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))  #define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
37    
38    /* SortMem got renamed in PostgreSQL 8.0 */
39    #ifndef SortMem
40     #define SortMem 16 * 1024
41    #endif
42    
43    #define ATTR_DELIMITER "{{!}}"
44    
45  /* prototype */  /* prototype */
46  char *attr2text(ESTDOC *doc, char *attr);  char *attr2text(ESTDOC *doc, char *attr);
47    char *node_attr2text(ESTRESDOC *rdoc, char *attr);
48    
49  ESTDB *db;  
50  ESTCOND *cond;  /* work in progress */
51  ESTDOC *doc;  PG_FUNCTION_INFO_V1(pgest_attr);
52  const CBLIST *texts;  Datum pgest_attr(PG_FUNCTION_ARGS)
53  int ecode, *est_result, resnum, i, j;  {
54  int limit = 0;          ArrayType       *attr_arr = PG_GETARG_ARRAYTYPE_P(6);
55  int offset = 0;          Oid             attr_element_type = ARR_ELEMTYPE(attr_arr);
56            int             attr_ndims = ARR_NDIM(attr_arr);
57  /* define PostgreSQL v1 function */          int             *attr_dim_counts = ARR_DIMS(attr_arr);
58  PG_FUNCTION_INFO_V1(pgest);          int             *attr_dim_lower_bounds = ARR_LBOUND(attr_arr);
59  Datum pgest(PG_FUNCTION_ARGS) {          int             ncols = 0;
60            int             nrows = 0;
61          FuncCallContext *funcctx;          int             indx[MAXDIM];
62          int             call_cntr;          int16           attr_len;
63          int             max_calls;          bool            attr_byval;
64          TupleDesc       tupdesc;          char            attr_align;
65          TupleTableSlot  *slot;          ReturnSetInfo   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
66          AttInMetadata   *attinmeta;          AttInMetadata   *attinmeta;
67            TupleDesc       tupdesc;
68            Tuplestorestate *tupstore = NULL;
69            HeapTuple       tuple;
70            MemoryContext   per_query_ctx;
71            MemoryContext   oldcontext;
72            Datum           dvalue;
73            char            **values;
74            int             rsinfo_ncols;
75            int             i, j;
76            /* estvars */
77            ESTDB *db;
78            ESTCOND *cond;
79            ESTDOC *doc;
80            const CBLIST *texts;
81            int ecode, *est_result, resnum;
82            int limit = 0;
83            int offset = 0;
84    
85          char            *index_path;          char            *index_path;
86          char            *query;          char            *query;
87          char            *attr;          char            *attr;
88            char            *order;
89    
         /* stuff done only on the first call of the function */  
         if (SRF_IS_FIRSTCALL()) {  
                 MemoryContext   oldcontext;  
   
                 /* 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);  
                 /* take arguments from function */  
   
                 /* index path */  
                 if (PG_ARGISNULL(0)) {  
                         elog(ERROR, "index path can't be null");  
                         SRF_RETURN_DONE(funcctx);  
                 }  
                 index_path = _textout(PG_GETARG_TEXT_P(0));  
90    
91                  /* query string */          /* only allow 1D input array */
92                  if (PG_ARGISNULL(0)) {          if (attr_ndims == 1)
93                          query = "";          {
94                  } else {                  ncols = attr_dim_counts[0];
95                          query = _textout(PG_GETARG_TEXT_P(1));          }
96                  }          else
97                    ereport(ERROR,
98                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
99                                     errmsg("invalid input array"),
100                                     errdetail("Input array must have 1 dimension")));
101                    
102            /* check to see if caller supports us returning a tuplestore */
103            if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
104                    ereport(ERROR,
105                                    (errcode(ERRCODE_SYNTAX_ERROR),
106                                     errmsg("materialize mode required, but it is not " \
107                                                    "allowed in this context")));
108    
109                  /* atribute filter */          /* get info about element type needed to construct the array */
110                  if (PG_ARGISNULL(2)) {          get_typlenbyvalalign(attr_element_type, &attr_len, &attr_byval, &attr_align);
                         attr = "";  
                 } else {  
                         attr = _textout(PG_GETARG_TEXT_P(2));  
                 }  
111    
112                  /* limit */          /* get the requested return tuple description */
113                  if (! PG_ARGISNULL(3)) limit = PG_GETARG_INT32(3);          tupdesc = rsinfo->expectedDesc;
114            rsinfo_ncols = tupdesc->natts;
115    
116                  /* offset */          /*
117                  if (! PG_ARGISNULL(4)) offset = PG_GETARG_INT32(4);           * The requested tuple description better match up with the array
118             * we were given.
119             */
120            if (rsinfo_ncols != ncols)
121                    ereport(ERROR,
122                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
123                                     errmsg("invalid input array"),
124                                     errdetail("Number of elements in array must match number of query specified columns.")));
125    
126            /* OK, use it */
127            attinmeta = TupleDescGetAttInMetadata(tupdesc);
128    
129                  /* open the database */          /* Now go to work */
130                  elog(DEBUG1, "pgest: est_db_open(%s)", index_path);          rsinfo->returnMode = SFRM_Materialize;
                   
                 if(!(db = est_db_open(index_path, ESTDBREADER, &ecode))){  
                         elog(ERROR, "est_db_open: can't open %s [%d]: %s", index_path, ecode, est_err_msg(ecode));  
                         SRF_RETURN_DONE(funcctx);  
                 }  
                   
                 elog(INFO, "pgest: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset);  
                   
                 /* create a search condition object */  
                 if (!(cond = est_cond_new())) {  
                         elog(INFO, "pgest: est_cond_new failed");  
                         SRF_RETURN_DONE(funcctx);  
                 }  
                   
                 /* set the search phrase to the search condition object */  
                 if (! PG_ARGISNULL(1) && strlen(query) > 0)  
                         est_cond_set_phrase(cond, query);  
   
                 /* minimum valid attribute length is 10: @a STREQ a */  
                 if (! PG_ARGISNULL(2) && strlen(attr) >= 10) {  
                         elog(INFO,"est_cond_add_attr(%s)", attr);  
                         est_cond_add_attr(cond, attr);  
                 }  
131    
132                  /* get the result of search */          per_query_ctx = fcinfo->flinfo->fn_mcxt;
133                  est_result = est_db_search(db, cond, &resnum, NULL);          oldcontext = MemoryContextSwitchTo(per_query_ctx);
                   
                 /* total number of tuples to be returned */  
                 if (limit && limit < resnum) {  
                         funcctx->max_calls = limit - offset;  
                 } else {  
                         funcctx->max_calls = resnum - offset;  
                 }  
134    
135                  /* check if results exists */          /* initialize our tuplestore */
136                  if ( 0 == funcctx->max_calls )          tupstore = tuplestore_begin_heap(true, false, SortMem);
                         elog(INFO, "pgest: no results for: %s", query );  
137    
                 elog(DEBUG1, "pgest: found %d hits for %s", resnum, query);  
138    
139                  /* Build a tuple description for a __pgest tuple */          /* take rest of arguments from function */
                 tupdesc = RelationNameGetTupleDesc("__pgest");  
140    
141                  /* allocate a slot for a tuple with this tupdesc */          /* index path */
142                  slot = TupleDescGetSlot(tupdesc);          if (PG_ARGISNULL(0)) {
143                    ereport(ERROR,
144                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
145                                     errmsg("index path can't be null"),
146                                     errdetail("Index path must be valid full path to HyperEstraier index")));
147            }
148            index_path = _textout(PG_GETARG_TEXT_P(0));
149    
150                  /* assign slot to function context */          /* query string */
151                  funcctx->slot = slot;          if (PG_ARGISNULL(1)) {
152                    query = "";
153            } else {
154                    query = _textout(PG_GETARG_TEXT_P(1));
155            }
156    
157                  /*          /* atribute filter */
158                   * generate attribute metadata needed later to produce tuples from raw          if (PG_ARGISNULL(2)) {
159                   * C strings                  attr = "";
160                   */          } else {
161                  attinmeta = TupleDescGetAttInMetadata(tupdesc);                  attr = _textout(PG_GETARG_TEXT_P(2));
162                  funcctx->attinmeta = attinmeta;          }
163            
164            /* sort order */
165            if (PG_ARGISNULL(3)) {
166                    order = "";
167            } else {
168                    order = _textout(PG_GETARG_TEXT_P(3));
169            }
170    
                 MemoryContextSwitchTo(oldcontext);  
171    
172                  elog(DEBUG1, "SRF_IS_FIRSTCALL done");          /* limit */
173            if (PG_ARGISNULL(4)) {
174                    limit = 0;
175            } else {
176                    limit = PG_GETARG_INT32(4);
177          }          }
178    
179          /* stuff done on every call of the function */          /* offset */
180          funcctx = SRF_PERCALL_SETUP();          if (PG_ARGISNULL(5)) {
181                    offset = 0;
182            } else {
183                    offset = PG_GETARG_INT32(5);
184            }
185    
         call_cntr = funcctx->call_cntr;  
         max_calls = funcctx->max_calls;  
         slot = funcctx->slot;  
         attinmeta = funcctx->attinmeta;  
186    
187          if (limit && call_cntr > limit - 1) {          /* open the database */
188                  elog(INFO, "call_cntr: %d limit: %d", call_cntr, limit);          elog(DEBUG1, "pgest_attr: est_db_open(%s)", index_path);
189                  SRF_RETURN_DONE(funcctx);                  
190            if(!(db = est_db_open(index_path, ESTDBREADER, &ecode))){
191                    ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
192                            errmsg("est_db_open: can't open %s: %d", index_path, ecode),
193                            errdetail(est_err_msg(ecode))));
194            }
195                    
196            elog(DEBUG1, "pgest_attr: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset);
197            
198            /* create a search condition object */
199            if (!(cond = est_cond_new())) {
200                    ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED),
201                            errmsg("pgest_attr: est_cond_new failed")));
202            }
203            
204            /* set the search phrase to the search condition object */
205            if (! PG_ARGISNULL(1) && strlen(query) > 0)
206                    est_cond_set_phrase(cond, query);
207    
208            /* minimum valid attribute length is 10: @a STREQ a */
209            if (! PG_ARGISNULL(2) && strlen(attr) >= 10) {
210                    elog(DEBUG1,"attributes: %s", attr);
211                    char *curr_attr;
212                    curr_attr = strtok(attr, ATTR_DELIMITER);
213                    while (curr_attr) {
214                            elog(DEBUG1,"est_cond_add_attr(%s)", curr_attr);
215                            est_cond_add_attr(cond, curr_attr);
216                            curr_attr = strtok(NULL, ATTR_DELIMITER);
217                    }
218            }
219    
220            /* set the search phrase to the search condition object */
221            if (! PG_ARGISNULL(3) && strlen(order) > 0) {
222                    elog(DEBUG1,"est_cond_set_order(%s)", order);
223                    est_cond_set_order(cond, order);
224          }          }
225    
226          if (call_cntr < max_calls) {          if (limit) {
227                  char            **values;                  elog(DEBUG1,"est_cond_set_max(%d)", limit + offset);
228                  HeapTuple       tuple;                  est_cond_set_max(cond, limit + offset);
229                  Datum           result;          }
230    
231                  elog(DEBUG1, "pgest: loop count %d", call_cntr);          /* get the result of search */
232            est_result = est_db_search(db, cond, &resnum, NULL);
233    
234                  if (! est_result) {          /* check if results exists */
235                          elog(ERROR, "pgest: no estraier results");          if ( 0 == resnum ) {
236                          SRF_RETURN_DONE(funcctx);                  elog(INFO, "pgest_attr: no results for: %s", query );
237                  }          }
                   
                 /*  
                  * 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.  
                  */  
238    
239                  if (doc = est_db_get_doc(db, est_result[call_cntr + offset], 0)) {          /* total number of tuples to be returned */
240                            if (limit && limit < resnum) {
241                    nrows = limit;
242            } else {
243                    nrows = resnum - offset;
244            }
245    
246    
247            elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query);
248    
249            values = (char **) palloc(ncols * sizeof(char *));
250    
251            for (i = 0; i < nrows; i++)
252            {
253    
254                    /* get result from estraier */
255                    if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) {
256                            elog(INFO, "pgest_attr: can't find result %d", i + offset);
257                    } else {
258                          elog(DEBUG1, "URI: %s\n Title: %s\n",                          elog(DEBUG1, "URI: %s\n Title: %s\n",
259                                  est_doc_attr(doc, "@uri"),                                  est_doc_attr(doc, "@uri"),
260                                  est_doc_attr(doc, "@title")                                  est_doc_attr(doc, "@title")
261                          );                          );
262                    }
263    
264                          values = (char **) palloc(4 * sizeof(char *));                  /* iterate over results */
265                    for (j = 0; j < ncols; j++)
266                    {
267                            bool    isnull;
268    
269  //                      values[0] = (char *) palloc(strlen(_estval) * sizeof(char));                          /* array value of this position */
270                            indx[0] = j + attr_dim_lower_bounds[0];
271    
272                          values[0] = (char *) attr2text(doc,"@id");                          dvalue = array_ref(attr_arr, attr_ndims, indx, -1, attr_len, attr_byval, attr_align, &isnull);
273                          values[1] = (char *) attr2text(doc,"@uri");  
274                          values[2] = (char *) attr2text(doc,"@title");                          if (!isnull && doc)
275                          values[3] = (char *) attr2text(doc,"@type");                                  values[j] = DatumGetCString(
276                                            attr2text(doc,
277                          /* destloy the document object */                                                  (char *)DirectFunctionCall1(textout, dvalue)
278                          elog(DEBUG2, "est_doc_delete");                                          ));
279                          est_doc_delete(doc);                          else
280                  } else {                                  values[j] = NULL;
                         elog(INFO, "no result from estraier");  
                         values[0] = DatumGetCString( "" );  
                         values[1] = DatumGetCString( "" );  
                         values[2] = DatumGetCString( "" );  
                         values[3] = DatumGetCString( "" );  
281                  }                  }
282                    /* construct the tuple */
283                    tuple = BuildTupleFromCStrings(attinmeta, values);
284    
285                    /* now store it */
286                    tuplestore_puttuple(tupstore, tuple);
287    
288                  elog(DEBUG2, "build tuple");                  /* delete estraier document object */
289                  /* build a tuple */                  if (doc) est_doc_delete(doc);
290                  tuple = BuildTupleFromCStrings(attinmeta, values);          }
291    
292                  elog(DEBUG2, "make tuple into datum");          tuplestore_donestoring(tupstore);
293                  /* make the tuple into a datum */          rsinfo->setResult = tupstore;
                 result = TupleGetDatum(slot, tuple);  
294    
295                  elog(DEBUG2, "cleanup");          /*
296                  /* clean up ? */           * SFRM_Materialize mode expects us to return a NULL Datum. The actual
297  /*           * tuples are in our tuplestore and passed back through
298                  pfree(values[0]);           * rsinfo->setResult. rsinfo->setDesc is set to the tuple description
299                  pfree(values[1]);           * that we actually used to build our tuples with, so the caller can
300                  pfree(values[2]);           * verify we did what it was expecting.
301                  pfree(values[3]);           */
302                  pfree(values);          rsinfo->setDesc = tupdesc;
303  */          MemoryContextSwitchTo(oldcontext);
                   
                 elog(DEBUG2, "cleanup over");  
           
                 SRF_RETURN_NEXT(funcctx, result);  
         } else {  
                 elog(DEBUG1, "loop over");  
304    
305                  if(!est_db_close(db, &ecode)){          est_cond_delete(cond);
                         elog(INFO, "est_db_close error: %s", est_err_msg(ecode));  
                 }  
306    
307                  /* do when there is no more left */          if(!est_db_close(db, &ecode)){
308                  SRF_RETURN_DONE(funcctx);                  ereport(ERROR, (errcode(ERRCODE_IO_ERROR),
309                            errmsg("est_db_close: %d", ecode),
310                            errdetail(est_err_msg(ecode))));
311          }          }
312    
313            return (Datum) 0;
314  }  }
315    
316  /* work in progress */  
317  PG_FUNCTION_INFO_V1(pgest2);  /* make text var from attr */
318  Datum pgest2(PG_FUNCTION_ARGS)  char *attr2text(ESTDOC *doc, char *attr) {
319            char *val;
320            const char *attrval;
321            int len;
322            int attrlen;
323    
324            if (! doc) return (Datum) NULL;
325    
326            elog(DEBUG1, "doc: %08x, attr: %s", doc, attr);
327    
328            if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) {
329                    val = (char *) palloc(attrlen * sizeof(char));
330            } else {
331                    return (Datum) NULL;
332            }
333    
334            len = strlen(attrval);
335            elog(DEBUG1, "attr2text(%s) = '%s' %d bytes", attr, attrval, len);
336    
337            len++;
338            len *= sizeof(char);
339    
340            elog(DEBUG2, "palloc(%d)", len);
341    
342            val = palloc(len);
343    
344            memset(val, 0, len);
345            strncpy(val, attrval, len);
346    
347            elog(DEBUG2, "val=%s", val);
348    
349            return val;
350    }
351    
352    /*
353     * variation on theme: use node API which doesn't open index on
354     * every query which is much faster for large indexes
355     *
356     */
357    
358    PG_FUNCTION_INFO_V1(pgest_node);
359    Datum pgest_node(PG_FUNCTION_ARGS)
360  {  {
361          int             nrows = 3;          ArrayType       *attr_arr = PG_GETARG_ARRAYTYPE_P(8);
362          int16           typlen;          Oid             attr_element_type = ARR_ELEMTYPE(attr_arr);
363          bool            typbyval;          int             attr_ndims = ARR_NDIM(attr_arr);
364          char            typalign;          int             *attr_dim_counts = ARR_DIMS(attr_arr);
365            int             *attr_dim_lower_bounds = ARR_LBOUND(attr_arr);
366            int             ncols = 0;
367            int             nrows = 0;
368            int             indx[MAXDIM];
369            int16           attr_len;
370            bool            attr_byval;
371            char            attr_align;
372          ReturnSetInfo   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;          ReturnSetInfo   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
373          AttInMetadata   *attinmeta;          AttInMetadata   *attinmeta;
374          TupleDesc       tupdesc;          TupleDesc       tupdesc;
375          Tuplestorestate *tupstore = NULL;          Tuplestorestate *tupstore = NULL;
376          HeapTuple       tuple;          HeapTuple       tuple;
377          MemoryContext   per_query_ctx;          MemoryContext   per_query_ctx;
378          MemoryContext   oldcontext;          MemoryContext   oldcontext;
379          Datum           dvalue;          Datum           dvalue;
380          char            **values;          char            **values;
381          int             ncols;          int             rsinfo_ncols;
382          int             i, j;          int             i, j;
383            /* estvars */
384            ESTNODE *node;
385            ESTCOND *cond;
386            ESTNODERES *nres;
387            ESTRESDOC *rdoc;
388            const CBLIST *texts;
389            int resnum = 0;
390            int limit = 0;
391            int offset = 0;
392    
393            char            *node_url;
394            char            *user, *passwd;
395            char            *query;
396            char            *attr;
397            char            *order;
398    
399    
400            /* only allow 1D input array */
401            if (attr_ndims == 1)
402            {
403                    ncols = attr_dim_counts[0];
404            }
405            else
406                    ereport(ERROR,
407                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
408                                     errmsg("invalid input array"),
409                                     errdetail("Input array must have 1 dimension")));
410                    
411          /* check to see if caller supports us returning a tuplestore */          /* check to see if caller supports us returning a tuplestore */
412          if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))          if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
413                  ereport(ERROR,                  ereport(ERROR,
# Line 280  Datum pgest2(PG_FUNCTION_ARGS) Line 415  Datum pgest2(PG_FUNCTION_ARGS)
415                                   errmsg("materialize mode required, but it is not " \                                   errmsg("materialize mode required, but it is not " \
416                                                  "allowed in this context")));                                                  "allowed in this context")));
417    
418            /* get info about element type needed to construct the array */
419            get_typlenbyvalalign(attr_element_type, &attr_len, &attr_byval, &attr_align);
420    
421          /* get the requested return tuple description */          /* get the requested return tuple description */
422          tupdesc = rsinfo->expectedDesc;          tupdesc = rsinfo->expectedDesc;
423          ncols = tupdesc->natts;          rsinfo_ncols = tupdesc->natts;
424    
425          /*          /*
426           * The requested tuple description better match up with the array           * The requested tuple description better match up with the array
427           * we were given.           * we were given.
428           */           */
429            if (rsinfo_ncols != ncols)
430                    ereport(ERROR,
431                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
432                                     errmsg("invalid input array"),
433                                     errdetail("Number of elements in array must match number of query specified columns.")));
434    
435          /* OK, use it */          /* OK, use it */
436          attinmeta = TupleDescGetAttInMetadata(tupdesc);          attinmeta = TupleDescGetAttInMetadata(tupdesc);
437    
# Line 300  Datum pgest2(PG_FUNCTION_ARGS) Line 444  Datum pgest2(PG_FUNCTION_ARGS)
444          /* initialize our tuplestore */          /* initialize our tuplestore */
445          tupstore = tuplestore_begin_heap(true, false, SortMem);          tupstore = tuplestore_begin_heap(true, false, SortMem);
446    
447    
448            /* take rest of arguments from function */
449    
450            /* node URL */
451            if (PG_ARGISNULL(0)) {
452                    ereport(ERROR,
453                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
454                                     errmsg("node URL can't be null"),
455                                     errdetail("Node URL must be valid URL to HyperEstraier node")));
456            }
457            node_url = _textout(PG_GETARG_TEXT_P(0));
458    
459            /* login and password */
460            if (PG_ARGISNULL(1) || PG_ARGISNULL(2)) {
461                    ereport(ERROR,
462                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
463                                     errmsg("username and password can't be NULL"),
464                                     errdetail("You must specify valid username and password to HyperEstraier node")));
465            }
466            user = _textout(PG_GETARG_TEXT_P(1));
467            passwd = _textout(PG_GETARG_TEXT_P(2));
468    
469            /* query string */
470            if (PG_ARGISNULL(3)) {
471                    query = "";
472            } else {
473                    query = _textout(PG_GETARG_TEXT_P(3));
474            }
475    
476            /* atribute filter */
477            if (PG_ARGISNULL(4)) {
478                    attr = "";
479            } else {
480                    attr = _textout(PG_GETARG_TEXT_P(4));
481            }
482            
483            /* sort order */
484            if (PG_ARGISNULL(5)) {
485                    order = "";
486            } else {
487                    order = _textout(PG_GETARG_TEXT_P(5));
488            }
489    
490    
491            /* limit */
492            if (PG_ARGISNULL(6)) {
493                    limit = 0;
494            } else {
495                    limit = PG_GETARG_INT32(6);
496            }
497    
498            /* offset */
499            if (PG_ARGISNULL(7)) {
500                    offset = 0;
501            } else {
502                    offset = PG_GETARG_INT32(7);
503            }
504    
505            /* initialize the network environment */
506            if(!est_init_net_env()){
507                    ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED),
508                            errmsg("pgest_node: can't create network enviroment")));
509            }
510    
511            /* create the node connection object */
512            elog(DEBUG1, "pgest_node: est_node_new(%s) as %s", node_url, user);
513            node = est_node_new(node_url);
514            est_node_set_auth(node, user, passwd);
515    
516            elog(DEBUG1, "pgest_node: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(4) ? "NULL" : attr), limit, offset);
517            
518            /* create a search condition object */
519            if (!(cond = est_cond_new())) {
520                    ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED),
521                            errmsg("pgest_node: est_cond_new failed")));
522            }
523            
524            /* set the search phrase to the search condition object */
525            if (! PG_ARGISNULL(3) && strlen(query) > 0)
526                    est_cond_set_phrase(cond, query);
527    
528            /* minimum valid attribute length is 10: @a STREQ a */
529            if (! PG_ARGISNULL(4) && strlen(attr) >= 10) {
530                    elog(DEBUG1,"attributes: %s", attr);
531                    char *curr_attr;
532                    curr_attr = strtok(attr, ATTR_DELIMITER);
533                    while (curr_attr) {
534                            elog(DEBUG1,"est_cond_add_attr(%s)", curr_attr);
535                            est_cond_add_attr(cond, curr_attr);
536                            curr_attr = strtok(NULL, ATTR_DELIMITER);
537                    }
538            }
539    
540            /* set the search phrase to the search condition object */
541            if (! PG_ARGISNULL(5) && strlen(order) > 0) {
542                    elog(DEBUG1,"est_cond_set_order(%s)", order);
543                    est_cond_set_order(cond, order);
544            }
545    
546            if (limit) {
547                    elog(DEBUG1,"est_cond_set_max(%d)", limit + offset);
548                    est_cond_set_max(cond, limit + offset);
549            }
550    
551            /* get the result of search */
552            /* FIXME: allow user to specify depath of search */
553            nres = est_node_search(node, cond, 0);
554    
555            if (! nres) {
556                    int status = est_node_status(node);
557                    est_cond_delete(cond);
558                    est_node_delete(node);
559                    est_free_net_env();
560                    ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED),
561                            errmsg("pgest_node: search failed, node status %d", status)));
562            }
563    
564            /* get number of results */
565            resnum = est_noderes_doc_num(nres);
566    
567            /* check if results exists */
568            if ( 0 == resnum ) {
569                    elog(INFO, "pgest_node: no results for: %s", query );
570            }
571    
572            /* total number of tuples to be returned */
573            if (limit && limit < resnum) {
574                    nrows = limit;
575            } else {
576                    nrows = resnum - offset;
577            }
578    
579    
580            elog(DEBUG1, "pgest_node: found %d hits for %s", resnum, query);
581    
582    
583          values = (char **) palloc(ncols * sizeof(char *));          values = (char **) palloc(ncols * sizeof(char *));
584    
585          for (i = 0; i < nrows; i++)          for (i = 0; i < nrows; i++)
586          {          {
587    
588                    /* get result from estraier */
589                    if (! ( rdoc = est_noderes_get_doc(nres, i + offset) )) {
590                            elog(INFO, "pgest_node: can't find result %d", i + offset);
591                    } else {
592                            elog(DEBUG1, "URI: %s\n Title: %s\n",
593                                    est_resdoc_attr(rdoc, "@uri"),
594                                    est_resdoc_attr(rdoc, "@title")
595                            );
596                    }
597    
598                    /* iterate over results */
599                  for (j = 0; j < ncols; j++)                  for (j = 0; j < ncols; j++)
600                  {                  {
601                          values[j] = DatumGetCString( "foo" );                          bool    isnull;
602    
603                            /* array value of this position */
604                            indx[0] = j + attr_dim_lower_bounds[0];
605    
606                            dvalue = array_ref(attr_arr, attr_ndims, indx, -1, attr_len, attr_byval, attr_align, &isnull);
607    
608                            if (!isnull && rdoc)
609                                    values[j] = DatumGetCString(
610                                            node_attr2text(rdoc,
611                                                    (char *)DirectFunctionCall1(textout, dvalue)
612                                            ));
613                            else
614                                    values[j] = NULL;
615                  }                  }
616                  /* construct the tuple */                  /* construct the tuple */
617                  tuple = BuildTupleFromCStrings(attinmeta, values);                  tuple = BuildTupleFromCStrings(attinmeta, values);
618    
619                  /* now store it */                  /* now store it */
620                  tuplestore_puttuple(tupstore, tuple);                  tuplestore_puttuple(tupstore, tuple);
621    
622          }          }
623    
624          tuplestore_donestoring(tupstore);          tuplestore_donestoring(tupstore);
# Line 328  Datum pgest2(PG_FUNCTION_ARGS) Line 634  Datum pgest2(PG_FUNCTION_ARGS)
634          rsinfo->setDesc = tupdesc;          rsinfo->setDesc = tupdesc;
635          MemoryContextSwitchTo(oldcontext);          MemoryContextSwitchTo(oldcontext);
636    
637            /* delete the node result object */
638            est_noderes_delete(nres);
639    
640            /* destroy the search condition object */                          
641            est_cond_delete(cond);
642    
643            /* destroy the node object */
644            est_node_delete(node);
645    
646            /* free the networking environment */
647            est_free_net_env();
648    
649          return (Datum) 0;          return (Datum) 0;
650  }  }
651    
652    /* make text var from node attr */
653  /* make text var from attr */  char *node_attr2text(ESTRESDOC *rdoc, char *attr) {
 char *attr2text(ESTDOC *doc, char *attr) {  
654          char *val;          char *val;
655          const char *attrval;          const char *attrval;
656          int len;          int len;
657          int attrlen;          int attrlen;
658    
659          elog(DEBUG1, "doc: %08x, attr: %s", doc, attr);          if (! rdoc) return (Datum) NULL;
660    
661          if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) {          elog(DEBUG1, "doc: %08x, attr: %s", rdoc, attr);
662    
663            if ( (attrval = est_resdoc_attr(rdoc, attr)) && (attrlen = strlen(attrval)) ) {
664                  val = (char *) palloc(attrlen * sizeof(char));                  val = (char *) palloc(attrlen * sizeof(char));
665          } else {          } else {
666                  return (Datum) NULL;                  return (Datum) NULL;
667          }          }
668    
669          len = strlen(attrval);          len = strlen(attrval);
670          elog(DEBUG1, "attr2text(%s) = '%s' %d bytes", attr, attrval, len);          elog(DEBUG1, "node_attr2text(%s) = '%s' %d bytes", attr, attrval, len);
671    
672          len++;          len++;
673          len *= sizeof(char);          len *= sizeof(char);
# Line 365  char *attr2text(ESTDOC *doc, char *attr) Line 684  char *attr2text(ESTDOC *doc, char *attr)
684          return val;          return val;
685  }  }
686    
 /* make integer variable from property */  
 /*  
 char *prop2int(SW_RESULT sw_res, char *propname) {  
         char *val;  
         unsigned long prop;  
         int len;  
   
         elog(DEBUG2, "prop2int(%s)", propname);  
   
         prop = estResultPropertyULong( sw_res, propname );  
         if (error_or_abort( est_handle )) return NULL;  
   
         elog(DEBUG1, "prop2int(%s) = %lu", propname, prop);  
   
         len = 128 * sizeof(char);  
         elog(DEBUG2, "palloc(%d)", len);  
   
         val = palloc(len);  
         memset(val, 0, len);  
   
         snprintf(val, len, "%lu", prop);  
   
         elog(DEBUG2, "val=%s", val);  
   
         return val;  
 }  
 */  

Legend:
Removed from v.12  
changed lines
  Added in v.47

  ViewVC Help
Powered by ViewVC 1.1.26