/[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 16 by dpavlin, Thu May 26 00:16:48 2005 UTC revision 19 by dpavlin, Thu May 26 17:56:53 2005 UTC
# Line 262  Datum pgest(PG_FUNCTION_ARGS) { Line 262  Datum pgest(PG_FUNCTION_ARGS) {
262  }  }
263    
264  /* work in progress */  /* work in progress */
265  PG_FUNCTION_INFO_V1(pgest2);  PG_FUNCTION_INFO_V1(pgest_attr);
266  Datum pgest2(PG_FUNCTION_ARGS)  Datum pgest_attr(PG_FUNCTION_ARGS)
267  {  {
268          int             nrows = 3;          ArrayType       *attr_arr = PG_GETARG_ARRAYTYPE_P(5);
269            Oid             element_type = ARR_ELEMTYPE(attr_arr);
270            int             ndims = ARR_NDIM(attr_arr);
271            int             *dim_counts = ARR_DIMS(attr_arr);
272            int             *dim_lower_bounds = ARR_LBOUND(attr_arr);
273            int             ncols = 0;
274            int             nrows = 0;
275            int             indx[MAXDIM];
276          int16           typlen;          int16           typlen;
277          bool            typbyval;          bool            typbyval;
278          char            typalign;          char            typalign;
279          ReturnSetInfo   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;          ReturnSetInfo   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
280          AttInMetadata   *attinmeta;          AttInMetadata   *attinmeta;
281          TupleDesc       tupdesc;          TupleDesc       tupdesc;
282          Tuplestorestate *tupstore = NULL;          Tuplestorestate *tupstore = NULL;
283          HeapTuple       tuple;          HeapTuple       tuple;
284          MemoryContext   per_query_ctx;          MemoryContext   per_query_ctx;
285          MemoryContext   oldcontext;          MemoryContext   oldcontext;
286          Datum           dvalue;          Datum           dvalue;
287          char            **values;          char            **values;
288          int             ncols;          int             rsinfo_ncols;
289          int             i, j;          int             i, j;
290            /* estvars */
291            char            *index_path;
292            char            *query;
293            char            *attr;
294    
295    
296            /* only allow 1D input array */
297            if (ndims == 1)
298            {
299                    ncols = dim_counts[0];
300            }
301            else
302                    ereport(ERROR,
303                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
304                                     errmsg("invalid input array"),
305                                     errdetail("Input array must have 1 dimension")));
306                    
307          /* check to see if caller supports us returning a tuplestore */          /* check to see if caller supports us returning a tuplestore */
308          if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))          if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
309                  ereport(ERROR,                  ereport(ERROR,
# Line 288  Datum pgest2(PG_FUNCTION_ARGS) Line 311  Datum pgest2(PG_FUNCTION_ARGS)
311                                   errmsg("materialize mode required, but it is not " \                                   errmsg("materialize mode required, but it is not " \
312                                                  "allowed in this context")));                                                  "allowed in this context")));
313    
314            /* get info about element type needed to construct the array */
315            get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
316    
317          /* get the requested return tuple description */          /* get the requested return tuple description */
318          tupdesc = rsinfo->expectedDesc;          tupdesc = rsinfo->expectedDesc;
319          ncols = tupdesc->natts;          rsinfo_ncols = tupdesc->natts;
320    
321          /*          /*
322           * The requested tuple description better match up with the array           * The requested tuple description better match up with the array
323           * we were given.           * we were given.
324           */           */
325            if (rsinfo_ncols != ncols)
326                    ereport(ERROR,
327                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
328                                     errmsg("invalid input array"),
329                                     errdetail("Number of elements in array must match number of query specified columns.")));
330    
331          /* OK, use it */          /* OK, use it */
332          attinmeta = TupleDescGetAttInMetadata(tupdesc);          attinmeta = TupleDescGetAttInMetadata(tupdesc);
333    
# Line 308  Datum pgest2(PG_FUNCTION_ARGS) Line 340  Datum pgest2(PG_FUNCTION_ARGS)
340          /* initialize our tuplestore */          /* initialize our tuplestore */
341          tupstore = tuplestore_begin_heap(true, false, SortMem);          tupstore = tuplestore_begin_heap(true, false, SortMem);
342    
343    
344            /* take rest of arguments from function */
345    
346            /* index path */
347            if (PG_ARGISNULL(0)) {
348                    ereport(ERROR,
349                                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
350                                     errmsg("index path can't be null"),
351                                     errdetail("Index path must be valid full path to HyperEstraier index")));
352            }
353            index_path = _textout(PG_GETARG_TEXT_P(0));
354    
355            /* query string */
356            if (PG_ARGISNULL(0)) {
357                    query = "";
358            } else {
359                    query = _textout(PG_GETARG_TEXT_P(1));
360            }
361    
362            /* atribute filter */
363            if (PG_ARGISNULL(2)) {
364                    attr = "";
365            } else {
366                    attr = _textout(PG_GETARG_TEXT_P(2));
367            }
368    
369            /* limit */
370            if (PG_ARGISNULL(3)) {
371                    limit = 0;
372            } else {
373                    limit = PG_GETARG_INT32(3);
374            }
375    
376            /* offset */
377            if (PG_ARGISNULL(4)) {
378                    offset = 0;
379            } else {
380                    offset = PG_GETARG_INT32(4);
381            }
382    
383    
384            /* open the database */
385            elog(DEBUG1, "pgest_attr: est_db_open(%s)", index_path);
386                    
387            if(!(db = est_db_open(index_path, ESTDBREADER, &ecode))){
388                    ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
389                            errmsg("est_db_open: can't open %s: %d", index_path, ecode),
390                            errdetail(est_err_msg(ecode))));
391            }
392                    
393            elog(INFO, "pgest_attr: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset);
394            
395            /* create a search condition object */
396            if (!(cond = est_cond_new())) {
397                    ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED),
398                            errmsg("pgest_attr: est_cond_new failed")));
399            }
400            
401            /* set the search phrase to the search condition object */
402            if (! PG_ARGISNULL(1) && strlen(query) > 0)
403                    est_cond_set_phrase(cond, query);
404    
405            /* minimum valid attribute length is 10: @a STREQ a */
406            if (! PG_ARGISNULL(2) && strlen(attr) >= 10) {
407                    elog(INFO,"est_cond_add_attr(%s)", attr);
408                    est_cond_add_attr(cond, attr);
409            }
410    
411            /* get the result of search */
412            est_result = est_db_search(db, cond, &resnum, NULL);
413    
414            /* check if results exists */
415            if ( 0 == resnum ) {
416                    elog(INFO, "pgest_attr: no results for: %s", query );
417            }
418    
419            /* total number of tuples to be returned */
420            if (limit && limit < resnum) {
421                    nrows = limit - offset;
422            } else {
423                    nrows = resnum - offset;
424            }
425    
426    
427            elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query);
428    
429    
430          values = (char **) palloc(ncols * sizeof(char *));          values = (char **) palloc(ncols * sizeof(char *));
431    
432          for (i = 0; i < nrows; i++)          for (i = 0; i < nrows; i++)
433          {          {
434    
435                    /* get result from estraier */
436                    if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) {
437                            elog(INFO, "can't find result %d", i + offset);
438                    } else {
439                            elog(DEBUG1, "URI: %s\n Title: %s\n",
440                                    est_doc_attr(doc, "@uri"),
441                                    est_doc_attr(doc, "@title")
442                            );
443                    }
444    
445                    /* iterate over results */
446                  for (j = 0; j < ncols; j++)                  for (j = 0; j < ncols; j++)
447                  {                  {
448                          values[j] = DatumGetCString( "foo" );                          bool    isnull;
449    
450                            /* array value of this position */
451                            indx[0] = j + dim_lower_bounds[0];
452    
453                            dvalue = array_ref(attr_arr, ndims, indx, -1, typlen, typbyval, typalign, &isnull);
454    
455                            if (!isnull && doc)
456                                    values[j] = DatumGetCString(
457                                            attr2text(doc,
458                                                    DirectFunctionCall1(textout, dvalue)
459                                            ));
460                            else
461                                    values[j] = NULL;
462                  }                  }
463                  /* construct the tuple */                  /* construct the tuple */
464                  tuple = BuildTupleFromCStrings(attinmeta, values);                  tuple = BuildTupleFromCStrings(attinmeta, values);
465    
466                  /* now store it */                  /* now store it */
467                  tuplestore_puttuple(tupstore, tuple);                  tuplestore_puttuple(tupstore, tuple);
468    
469    
470                    /* delete estraier document object */
471                    est_doc_delete(doc);
472          }          }
473    
474          tuplestore_donestoring(tupstore);          tuplestore_donestoring(tupstore);
# Line 336  Datum pgest2(PG_FUNCTION_ARGS) Line 484  Datum pgest2(PG_FUNCTION_ARGS)
484          rsinfo->setDesc = tupdesc;          rsinfo->setDesc = tupdesc;
485          MemoryContextSwitchTo(oldcontext);          MemoryContextSwitchTo(oldcontext);
486    
487            if(!est_db_close(db, &ecode)){
488                    ereport(ERROR, (errcode(ERRCODE_IO_ERROR),
489                            errmsg("est_db_close: %d", ecode),
490                            errdetail(est_err_msg(ecode))));
491            }
492    
493          return (Datum) 0;          return (Datum) 0;
494  }  }
495    
# Line 373  char *attr2text(ESTDOC *doc, char *attr) Line 527  char *attr2text(ESTDOC *doc, char *attr)
527          return val;          return val;
528  }  }
529    
 /* 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.16  
changed lines
  Added in v.19

  ViewVC Help
Powered by ViewVC 1.1.26