/[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 14 by dpavlin, Thu May 26 00:06:10 2005 UTC revision 27 by dpavlin, Thu Jun 30 20:01:36 2005 UTC
# Line 34  Line 34 
34  #define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))  #define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
35  #define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))  #define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
36    
37    /* SortMem got renamed in PostgreSQL 8.0 */
38    #ifndef SortMem
39     #define SortMem 16 * 1024
40    #endif
41    
42  /* prototype */  /* prototype */
43  char *attr2text(ESTDOC *doc, char *attr);  char *attr2text(ESTDOC *doc, char *attr);
44    
 ESTDB *db;  
 ESTCOND *cond;  
 ESTDOC *doc;  
 const CBLIST *texts;  
 int ecode, *est_result, resnum, i, j;  
 int limit = 0;  
 int offset = 0;  
   
 /* define PostgreSQL v1 function */  
 PG_FUNCTION_INFO_V1(pgest);  
 Datum pgest(PG_FUNCTION_ARGS) {  
   
         FuncCallContext *funcctx;  
         int             call_cntr;  
         int             max_calls;  
         TupleDesc       tupdesc;  
         TupleTableSlot  *slot;  
         AttInMetadata   *attinmeta;  
         char            *index_path;  
         char            *query;  
         char            *attr;  
   
         /* 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));  
   
                 /* query string */  
                 if (PG_ARGISNULL(0)) {  
                         query = "";  
                 } else {  
                         query = _textout(PG_GETARG_TEXT_P(1));  
                 }  
   
                 /* atribute filter */  
                 if (PG_ARGISNULL(2)) {  
                         attr = "";  
                 } else {  
                         attr = _textout(PG_GETARG_TEXT_P(2));  
                 }  
   
                 /* limit */  
                 if (PG_ARGISNULL(3)) {  
                         limit = 0;  
                 } else {  
                         limit = PG_GETARG_INT32(3);  
                 }  
   
                 /* offset */  
                 if (PG_ARGISNULL(4)) {  
                         offset = 0;  
                 } else {  
                         offset = PG_GETARG_INT32(4);  
                 }  
   
   
                 /* open the database */  
                 elog(DEBUG1, "pgest: est_db_open(%s)", index_path);  
                   
                 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);  
                 }  
   
                 /* get the result of search */  
                 est_result = est_db_search(db, cond, &resnum, NULL);  
                   
                 /* total number of tuples to be returned */  
                 if (limit && limit < resnum) {  
                         funcctx->max_calls = limit - offset;  
                 } else {  
                         funcctx->max_calls = resnum - offset;  
                 }  
   
                 /* check if results exists */  
                 if ( 0 == funcctx->max_calls )  
                         elog(INFO, "pgest: no results for: %s", query );  
   
                 elog(DEBUG1, "pgest: found %d hits for %s", resnum, query);  
   
                 /* Build a tuple description for a __pgest tuple */  
                 tupdesc = RelationNameGetTupleDesc("__pgest");  
   
                 /* allocate a slot for a tuple with this tupdesc */  
                 slot = TupleDescGetSlot(tupdesc);  
   
                 /* assign slot to function context */  
                 funcctx->slot = slot;  
   
                 /*  
                  * generate attribute metadata needed later to produce tuples from raw  
                  * C strings  
                  */  
                 attinmeta = TupleDescGetAttInMetadata(tupdesc);  
                 funcctx->attinmeta = attinmeta;  
   
                 MemoryContextSwitchTo(oldcontext);  
   
                 elog(DEBUG1, "SRF_IS_FIRSTCALL done");  
         }  
   
         /* stuff done on every call of the function */  
         funcctx = SRF_PERCALL_SETUP();  
   
         call_cntr = funcctx->call_cntr;  
         max_calls = funcctx->max_calls;  
         slot = funcctx->slot;  
         attinmeta = funcctx->attinmeta;  
   
         if (limit && call_cntr > limit - 1) {  
                 elog(INFO, "call_cntr: %d limit: %d", call_cntr, limit);  
                 SRF_RETURN_DONE(funcctx);  
         }  
   
         if (call_cntr < max_calls) {  
                 char            **values;  
                 HeapTuple       tuple;  
                 Datum           result;  
   
                 elog(DEBUG1, "pgest: loop count %d", call_cntr);  
   
                 if (! est_result) {  
                         elog(ERROR, "pgest: no estraier results");  
                         SRF_RETURN_DONE(funcctx);  
                 }  
                   
                 /*  
                  * 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.  
                  */  
   
                 if (doc = est_db_get_doc(db, est_result[call_cntr + offset], 0)) {  
                   
                         elog(DEBUG1, "URI: %s\n Title: %s\n",  
                                 est_doc_attr(doc, "@uri"),  
                                 est_doc_attr(doc, "@title")  
                         );  
   
                         values = (char **) palloc(4 * sizeof(char *));  
   
 //                      values[0] = (char *) palloc(strlen(_estval) * sizeof(char));  
   
                         values[0] = (char *) attr2text(doc,"@id");  
                         values[1] = (char *) attr2text(doc,"@uri");  
                         values[2] = (char *) attr2text(doc,"@title");  
                         values[3] = (char *) attr2text(doc,"@type");  
   
                         /* destloy the document object */  
                         elog(DEBUG2, "est_doc_delete");  
                         est_doc_delete(doc);  
                 } else {  
                         elog(INFO, "no result from estraier");  
                         values[0] = DatumGetCString( "" );  
                         values[1] = DatumGetCString( "" );  
                         values[2] = DatumGetCString( "" );  
                         values[3] = DatumGetCString( "" );  
                 }  
   
   
                 elog(DEBUG2, "build tuple");  
                 /* build a tuple */  
                 tuple = BuildTupleFromCStrings(attinmeta, values);  
   
                 elog(DEBUG2, "make tuple into datum");  
                 /* make the tuple into a datum */  
                 result = TupleGetDatum(slot, tuple);  
   
                 elog(DEBUG2, "cleanup");  
                 /* clean up ? */  
 /*  
                 pfree(values[0]);  
                 pfree(values[1]);  
                 pfree(values[2]);  
                 pfree(values[3]);  
                 pfree(values);  
 */  
                   
                 elog(DEBUG2, "cleanup over");  
           
                 SRF_RETURN_NEXT(funcctx, result);  
         } else {  
                 elog(DEBUG1, "loop over");  
   
                 if(!est_db_close(db, &ecode)){  
                         elog(INFO, "est_db_close error: %s", est_err_msg(ecode));  
                 }  
   
                 /* do when there is no more left */  
                 SRF_RETURN_DONE(funcctx);  
         }  
 }  
45    
46  /* work in progress */  /* work in progress */
47  PG_FUNCTION_INFO_V1(pgest2);  PG_FUNCTION_INFO_V1(pgest_attr);
48  Datum pgest2(PG_FUNCTION_ARGS)  Datum pgest_attr(PG_FUNCTION_ARGS)
49  {  {
50          int             nrows = 3;          ArrayType       *attr_arr = PG_GETARG_ARRAYTYPE_P(5);
51          int16           typlen;          Oid             attr_element_type = ARR_ELEMTYPE(attr_arr);
52          bool            typbyval;          int             attr_ndims = ARR_NDIM(attr_arr);
53          char            typalign;          int             *attr_dim_counts = ARR_DIMS(attr_arr);
54            int             *attr_dim_lower_bounds = ARR_LBOUND(attr_arr);
55            int             ncols = 0;
56            int             nrows = 0;
57            int             indx[MAXDIM];
58            int16           attr_len;
59            bool            attr_byval;
60            char            attr_align;
61          ReturnSetInfo   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;          ReturnSetInfo   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
62          AttInMetadata   *attinmeta;          AttInMetadata   *attinmeta;
63          TupleDesc       tupdesc;          TupleDesc       tupdesc;
64          Tuplestorestate *tupstore = NULL;          Tuplestorestate *tupstore = NULL;
65          HeapTuple       tuple;          HeapTuple       tuple;
66          MemoryContext   per_query_ctx;          MemoryContext   per_query_ctx;
67          MemoryContext   oldcontext;          MemoryContext   oldcontext;
68          Datum           dvalue;          Datum           dvalue;
69          char            **values;          char            **values;
70          int             ncols;          int             rsinfo_ncols;
71          int             i, j;          int             i, j;
72            /* estvars */
73            ESTDB *db;
74            ESTCOND *cond;
75            ESTDOC *doc;
76            const CBLIST *texts;
77            int ecode, *est_result, resnum;
78            int limit = 0;
79            int offset = 0;
80    
81            char            *index_path;
82            char            *query;
83            char            *attr;
84    
85    
86            /* only allow 1D input array */
87            if (attr_ndims == 1)
88            {
89                    ncols = attr_dim_counts[0];
90            }
91            else
92                    ereport(ERROR,
93                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
94                                     errmsg("invalid input array"),
95                                     errdetail("Input array must have 1 dimension")));
96                    
97          /* check to see if caller supports us returning a tuplestore */          /* check to see if caller supports us returning a tuplestore */
98          if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))          if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
99                  ereport(ERROR,                  ereport(ERROR,
# Line 288  Datum pgest2(PG_FUNCTION_ARGS) Line 101  Datum pgest2(PG_FUNCTION_ARGS)
101                                   errmsg("materialize mode required, but it is not " \                                   errmsg("materialize mode required, but it is not " \
102                                                  "allowed in this context")));                                                  "allowed in this context")));
103    
104            /* get info about element type needed to construct the array */
105            get_typlenbyvalalign(attr_element_type, &attr_len, &attr_byval, &attr_align);
106    
107          /* get the requested return tuple description */          /* get the requested return tuple description */
108          tupdesc = rsinfo->expectedDesc;          tupdesc = rsinfo->expectedDesc;
109          ncols = tupdesc->natts;          rsinfo_ncols = tupdesc->natts;
110    
111          /*          /*
112           * The requested tuple description better match up with the array           * The requested tuple description better match up with the array
113           * we were given.           * we were given.
114           */           */
115            if (rsinfo_ncols != ncols)
116                    ereport(ERROR,
117                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
118                                     errmsg("invalid input array"),
119                                     errdetail("Number of elements in array must match number of query specified columns.")));
120    
121          /* OK, use it */          /* OK, use it */
122          attinmeta = TupleDescGetAttInMetadata(tupdesc);          attinmeta = TupleDescGetAttInMetadata(tupdesc);
123    
# Line 308  Datum pgest2(PG_FUNCTION_ARGS) Line 130  Datum pgest2(PG_FUNCTION_ARGS)
130          /* initialize our tuplestore */          /* initialize our tuplestore */
131          tupstore = tuplestore_begin_heap(true, false, SortMem);          tupstore = tuplestore_begin_heap(true, false, SortMem);
132    
133    
134            /* take rest of arguments from function */
135    
136            /* index path */
137            if (PG_ARGISNULL(0)) {
138                    ereport(ERROR,
139                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
140                                     errmsg("index path can't be null"),
141                                     errdetail("Index path must be valid full path to HyperEstraier index")));
142            }
143            index_path = _textout(PG_GETARG_TEXT_P(0));
144    
145            /* query string */
146            if (PG_ARGISNULL(0)) {
147                    query = "";
148            } else {
149                    query = _textout(PG_GETARG_TEXT_P(1));
150            }
151    
152            /* atribute filter */
153            if (PG_ARGISNULL(2)) {
154                    attr = "";
155            } else {
156                    attr = _textout(PG_GETARG_TEXT_P(2));
157            }
158    
159            /* limit */
160            if (PG_ARGISNULL(3)) {
161                    limit = 0;
162            } else {
163                    limit = PG_GETARG_INT32(3);
164            }
165    
166            /* offset */
167            if (PG_ARGISNULL(4)) {
168                    offset = 0;
169            } else {
170                    offset = PG_GETARG_INT32(4);
171            }
172    
173    
174            /* open the database */
175            elog(DEBUG1, "pgest_attr: est_db_open(%s)", index_path);
176                    
177            if(!(db = est_db_open(index_path, ESTDBREADER, &ecode))){
178                    ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
179                            errmsg("est_db_open: can't open %s: %d", index_path, ecode),
180                            errdetail(est_err_msg(ecode))));
181            }
182                    
183            elog(DEBUG1, "pgest_attr: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset);
184            
185            /* create a search condition object */
186            if (!(cond = est_cond_new())) {
187                    ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED),
188                            errmsg("pgest_attr: est_cond_new failed")));
189            }
190            
191            /* set the search phrase to the search condition object */
192            if (! PG_ARGISNULL(1) && strlen(query) > 0)
193                    est_cond_set_phrase(cond, query);
194    
195            /* minimum valid attribute length is 10: @a STREQ a */
196            if (! PG_ARGISNULL(2) && strlen(attr) >= 10) {
197                    elog(DEBUG1,"est_cond_add_attr(%s)", attr);
198                    est_cond_add_attr(cond, attr);
199            }
200    
201            /* get the result of search */
202            est_result = est_db_search(db, cond, &resnum, NULL);
203    
204            /* check if results exists */
205            if ( 0 == resnum ) {
206                    elog(INFO, "pgest_attr: no results for: %s", query );
207            }
208    
209            /* total number of tuples to be returned */
210            if (limit && limit < resnum) {
211                    nrows = limit - offset;
212            } else {
213                    nrows = resnum - offset;
214            }
215    
216    
217            elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query);
218    
219    
220          values = (char **) palloc(ncols * sizeof(char *));          values = (char **) palloc(ncols * sizeof(char *));
221    
222          for (i = 0; i < nrows; i++)          for (i = 0; i < nrows; i++)
223          {          {
224    
225                    /* get result from estraier */
226                    if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) {
227                            elog(INFO, "can't find result %d", i + offset);
228                    } else {
229                            elog(DEBUG1, "URI: %s\n Title: %s\n",
230                                    est_doc_attr(doc, "@uri"),
231                                    est_doc_attr(doc, "@title")
232                            );
233                    }
234    
235                    /* iterate over results */
236                  for (j = 0; j < ncols; j++)                  for (j = 0; j < ncols; j++)
237                  {                  {
238                          values[j] = DatumGetCString( "foo" );                          bool    isnull;
239    
240                            /* array value of this position */
241                            indx[0] = j + attr_dim_lower_bounds[0];
242    
243                            dvalue = array_ref(attr_arr, attr_ndims, indx, -1, attr_len, attr_byval, attr_align, &isnull);
244    
245                            if (!isnull && doc)
246                                    values[j] = DatumGetCString(
247                                            attr2text(doc,
248                                                    (char *)DirectFunctionCall1(textout, dvalue)
249                                            ));
250                            else
251                                    values[j] = NULL;
252                  }                  }
253                  /* construct the tuple */                  /* construct the tuple */
254                  tuple = BuildTupleFromCStrings(attinmeta, values);                  tuple = BuildTupleFromCStrings(attinmeta, values);
255    
256                  /* now store it */                  /* now store it */
257                  tuplestore_puttuple(tupstore, tuple);                  tuplestore_puttuple(tupstore, tuple);
258    
259    
260                    /* delete estraier document object */
261                    est_doc_delete(doc);
262          }          }
263    
264          tuplestore_donestoring(tupstore);          tuplestore_donestoring(tupstore);
# Line 336  Datum pgest2(PG_FUNCTION_ARGS) Line 274  Datum pgest2(PG_FUNCTION_ARGS)
274          rsinfo->setDesc = tupdesc;          rsinfo->setDesc = tupdesc;
275          MemoryContextSwitchTo(oldcontext);          MemoryContextSwitchTo(oldcontext);
276    
277            if(!est_db_close(db, &ecode)){
278                    ereport(ERROR, (errcode(ERRCODE_IO_ERROR),
279                            errmsg("est_db_close: %d", ecode),
280                            errdetail(est_err_msg(ecode))));
281            }
282    
283          return (Datum) 0;          return (Datum) 0;
284  }  }
285    
# Line 373  char *attr2text(ESTDOC *doc, char *attr) Line 317  char *attr2text(ESTDOC *doc, char *attr)
317          return val;          return val;
318  }  }
319    
 /* 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.14  
changed lines
  Added in v.27

  ViewVC Help
Powered by ViewVC 1.1.26