/[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 2 by dpavlin, Fri May 20 13:00:46 2005 UTC revision 9 by dpavlin, Sun May 22 21:18:11 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    /* prototype */
38    char *attr2text(ESTDOC *doc, char *attr);
39    
40  ESTDB *db;  ESTDB *db;
41  ESTCOND *cond;  ESTCOND *cond;
42  ESTDOC *doc;  ESTDOC *doc;
43  const CBLIST *texts;  const CBLIST *texts;
44  int ecode, *est_result, resnum, i, j;  int ecode, *est_result, resnum, i, j;
45    int limit = 0;
46    int offset = 0;
47    
48  /* define PostgreSQL v1 function */  /* define PostgreSQL v1 function */
49  PG_FUNCTION_INFO_V1(pgest);  PG_FUNCTION_INFO_V1(pgest);
# Line 53  Datum pgest(PG_FUNCTION_ARGS) { Line 57  Datum pgest(PG_FUNCTION_ARGS) {
57          AttInMetadata   *attinmeta;          AttInMetadata   *attinmeta;
58          char            *index_path;          char            *index_path;
59          char            *query;          char            *query;
60            char            *attr;
61    
62          /* stuff done only on the first call of the function */          /* stuff done only on the first call of the function */
63          if (SRF_IS_FIRSTCALL()) {          if (SRF_IS_FIRSTCALL()) {
# Line 62  Datum pgest(PG_FUNCTION_ARGS) { Line 67  Datum pgest(PG_FUNCTION_ARGS) {
67                  //index_path = _textout(PG_GETARG_TEXT_P(0));                  //index_path = _textout(PG_GETARG_TEXT_P(0));
68                  index_path = _textout(PG_GETARG_TEXT_P(0));                  index_path = _textout(PG_GETARG_TEXT_P(0));
69                  query = _textout(PG_GETARG_TEXT_P(1));                  query = _textout(PG_GETARG_TEXT_P(1));
70                    attr = _textout(PG_GETARG_TEXT_P(2));
71                    limit = PG_GETARG_INT32(3);
72                    offset = PG_GETARG_INT32(4);
73    
74                  /* create a function context for cross-call persistence */                  /* create a function context for cross-call persistence */
75                  funcctx = SRF_FIRSTCALL_INIT();                  funcctx = SRF_FIRSTCALL_INIT();
# Line 77  Datum pgest(PG_FUNCTION_ARGS) { Line 85  Datum pgest(PG_FUNCTION_ARGS) {
85                          SRF_RETURN_DONE(funcctx);                          SRF_RETURN_DONE(funcctx);
86                  }                  }
87                                    
88                  elog(DEBUG2, "pgest: query=%s", query);                  elog(INFO, "pgest: query[%s] attr[%s] limit %d offset %d", query, attr, limit, offset);
89                                    
90                  /* create a search condition object */                  /* create a search condition object */
91                  if (!(cond = est_cond_new())) {                  if (!(cond = est_cond_new())) {
# Line 86  Datum pgest(PG_FUNCTION_ARGS) { Line 94  Datum pgest(PG_FUNCTION_ARGS) {
94                  }                  }
95                                    
96                  /* set the search phrase to the search condition object */                  /* set the search phrase to the search condition object */
97                  est_cond_set_phrase(cond, query);                  if (strlen(query) > 0)
98                            est_cond_set_phrase(cond, query);
99    
100                    /* minimum valid attribute length is 10: @a STREQ a */
101                    if (attr != NULL && strlen(attr) >= 10) {
102                            elog(INFO,"est_cond_add_attr(%s)", attr);
103                            est_cond_add_attr(cond, attr);
104                    }
105    
106                  /* get the result of search */                  /* get the result of search */
107                  est_result = est_db_search(db, cond, &resnum, NULL);                  est_result = est_db_search(db, cond, &resnum, NULL);
108                                    
109                  /* total number of tuples to be returned */                  /* total number of tuples to be returned */
110                  funcctx->max_calls = resnum;                  if (limit && limit < resnum) {
111                            funcctx->max_calls = limit - offset;
112                    } else {
113                            funcctx->max_calls = resnum - offset;
114                    }
115    
116                  /* check if results exists */                  /* check if results exists */
117                  if ( 0 == funcctx->max_calls )                  if ( 0 == funcctx->max_calls )
118                          elog(INFO, "pgest: no results for: %s", query );                          elog(INFO, "pgest: no results for: %s", query );
119    
120                  elog(DEBUG1, "pgest: found %d hits for %s", funcctx->max_calls, query);                  elog(DEBUG1, "pgest: found %d hits for %s", resnum, query);
121    
122                  /* Build a tuple description for a __pgest tuple */                  /* Build a tuple description for a __pgest tuple */
123                  tupdesc = RelationNameGetTupleDesc("__pgest");                  tupdesc = RelationNameGetTupleDesc("__pgest");
# Line 128  Datum pgest(PG_FUNCTION_ARGS) { Line 147  Datum pgest(PG_FUNCTION_ARGS) {
147          max_calls = funcctx->max_calls;          max_calls = funcctx->max_calls;
148          slot = funcctx->slot;          slot = funcctx->slot;
149          attinmeta = funcctx->attinmeta;          attinmeta = funcctx->attinmeta;
150    
151            if (limit && call_cntr > limit - 1) {
152                    elog(INFO, "call_cntr: %d limit: %d", call_cntr, limit);
153                    SRF_RETURN_DONE(funcctx);
154            }
155    
156          if (call_cntr < max_calls) {          if (call_cntr < max_calls) {
157                  char            **values;                  char            **values;
158                  HeapTuple       tuple;                  HeapTuple       tuple;
# Line 147  Datum pgest(PG_FUNCTION_ARGS) { Line 171  Datum pgest(PG_FUNCTION_ARGS) {
171                   * be processed later by the type input functions.                   * be processed later by the type input functions.
172                   */                   */
173    
174                  if (doc = est_db_get_doc(db, est_result[call_cntr], 0)) {                  if (doc = est_db_get_doc(db, est_result[call_cntr + offset], 0)) {
175                                    
176                          elog(DEBUG1, "URI: %s\n Title: %s\n",                          elog(DEBUG1, "URI: %s\n Title: %s\n",
177                                  est_doc_attr(doc, "@uri"),                                  est_doc_attr(doc, "@uri"),
# Line 158  Datum pgest(PG_FUNCTION_ARGS) { Line 182  Datum pgest(PG_FUNCTION_ARGS) {
182    
183  //                      values[0] = (char *) palloc(strlen(_estval) * sizeof(char));  //                      values[0] = (char *) palloc(strlen(_estval) * sizeof(char));
184    
185                          values[0] = attr2text(doc,"@id");                          values[0] = (char *) attr2text(doc,"@id");
186                          values[1] = attr2text(doc,"@uri");                          values[1] = (char *) attr2text(doc,"@uri");
187                          values[2] = attr2text(doc,"@title");                          values[2] = (char *) attr2text(doc,"@title");
188                          values[3] = attr2text(doc,"@type");                          values[3] = (char *) attr2text(doc,"@type");
189    
190                          /* destloy the document object */                          /* destloy the document object */
191                          elog(DEBUG2, "est_doc_delete");                          elog(DEBUG2, "est_doc_delete");
192                          est_doc_delete(doc);                          est_doc_delete(doc);
193                  } else {                  } else {
194                          elog(INFO, "no result from estraier");                          elog(INFO, "no result from estraier");
195                          values[0] = NULL;                          values[0] = DatumGetCString( "" );
196                          values[1] = NULL;                          values[1] = DatumGetCString( "" );
197                          values[2] = NULL;                          values[2] = DatumGetCString( "" );
198                          values[3] = NULL;                          values[3] = DatumGetCString( "" );
199                  }                  }
200    
201    
# Line 292  char *attr2text(ESTDOC *doc, char *attr) Line 316  char *attr2text(ESTDOC *doc, char *attr)
316          char *val;          char *val;
317          const char *attrval;          const char *attrval;
318          int len;          int len;
319            int attrlen;
320    
321          elog(DEBUG1, "doc: %08x, attr: %s", doc, attr);          elog(DEBUG1, "doc: %08x, attr: %s", doc, attr);
322    
323          if (attrval = est_doc_attr(doc, attr)) {          if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) {
324                  val = (char *) palloc(strlen(attrval) * sizeof(char));                  val = (char *) palloc(attrlen * sizeof(char));
325          } else {          } else {
326                  return (Datum) NULL;                  return (Datum) NULL;
327          }          }

Legend:
Removed from v.2  
changed lines
  Added in v.9

  ViewVC Help
Powered by ViewVC 1.1.26