/[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 1 by dpavlin, Fri May 20 12:19:05 2005 UTC revision 16 by dpavlin, Thu May 26 00:16:48 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()) {
64                  MemoryContext   oldcontext;                  MemoryContext   oldcontext;
65    
                 /* take arguments from function */  
                 //index_path = _textout(PG_GETARG_TEXT_P(0));  
                 index_path = _textout(PG_GETARG_TEXT_P(0));  
                 query = _textout(PG_GETARG_TEXT_P(1));  
   
66                  /* create a function context for cross-call persistence */                  /* create a function context for cross-call persistence */
67                  funcctx = SRF_FIRSTCALL_INIT();                  funcctx = SRF_FIRSTCALL_INIT();
68    
69                  /* switch to memory context appropriate for multiple function calls */                  /* switch to memory context appropriate for multiple function calls */
70                  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);                  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
71                    /* take arguments from function */
72    
73                    /* index path */
74                    if (PG_ARGISNULL(0)) {
75                            elog(ERROR, "index path can't be null");
76                            SRF_RETURN_DONE(funcctx);
77                    }
78                    index_path = _textout(PG_GETARG_TEXT_P(0));
79    
80                    /* query string */
81                    if (PG_ARGISNULL(0)) {
82                            query = "";
83                    } else {
84                            query = _textout(PG_GETARG_TEXT_P(1));
85                    }
86    
87                    /* atribute filter */
88                    if (PG_ARGISNULL(2)) {
89                            attr = "";
90                    } else {
91                            attr = _textout(PG_GETARG_TEXT_P(2));
92                    }
93    
94                    /* limit */
95                    if (PG_ARGISNULL(3)) {
96                            limit = 0;
97                    } else {
98                            limit = PG_GETARG_INT32(3);
99                    }
100    
101                    /* offset */
102                    if (PG_ARGISNULL(4)) {
103                            offset = 0;
104                    } else {
105                            offset = PG_GETARG_INT32(4);
106                    }
107    
108    
109                  /* open the database */                  /* open the database */
110                  elog(DEBUG1, "pgest: est_db_open(%s)", index_path);                  elog(DEBUG1, "pgest: est_db_open(%s)", index_path);
# Line 77  Datum pgest(PG_FUNCTION_ARGS) { Line 114  Datum pgest(PG_FUNCTION_ARGS) {
114                          SRF_RETURN_DONE(funcctx);                          SRF_RETURN_DONE(funcctx);
115                  }                  }
116                                    
117                  elog(DEBUG2, "pgest: query=%s", query);                  elog(INFO, "pgest: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset);
118                                    
119                  /* create a search condition object */                  /* create a search condition object */
120                  if (!(cond = est_cond_new())) {                  if (!(cond = est_cond_new())) {
# Line 86  Datum pgest(PG_FUNCTION_ARGS) { Line 123  Datum pgest(PG_FUNCTION_ARGS) {
123                  }                  }
124                                    
125                  /* set the search phrase to the search condition object */                  /* set the search phrase to the search condition object */
126                  est_cond_set_phrase(cond, query);                  if (! PG_ARGISNULL(1) && strlen(query) > 0)
127                            est_cond_set_phrase(cond, query);
128    
129                    /* minimum valid attribute length is 10: @a STREQ a */
130                    if (! PG_ARGISNULL(2) && strlen(attr) >= 10) {
131                            elog(INFO,"est_cond_add_attr(%s)", attr);
132                            est_cond_add_attr(cond, attr);
133                    }
134    
135                  /* get the result of search */                  /* get the result of search */
136                  est_result = est_db_search(db, cond, &resnum, NULL);                  est_result = est_db_search(db, cond, &resnum, NULL);
137                                    
138                  /* total number of tuples to be returned */                  /* total number of tuples to be returned */
139                  funcctx->max_calls = resnum;                  if (limit && limit < resnum) {
140                            funcctx->max_calls = limit - offset;
141                    } else {
142                            funcctx->max_calls = resnum - offset;
143                    }
144    
145                  /* check if results exists */                  /* check if results exists */
146                  if ( 0 == funcctx->max_calls )                  if ( 0 == funcctx->max_calls )
147                          elog(INFO, "pgest: no results for: %s", query );                          elog(INFO, "pgest: no results for: %s", query );
148    
149                  elog(INFO, "pgest: found %d hits for %s", funcctx->max_calls, query);                  elog(DEBUG1, "pgest: found %d hits for %s", resnum, query);
150    
151                  /* Build a tuple description for a __pgest tuple */                  /* Build a tuple description for a __pgest tuple */
152                  tupdesc = RelationNameGetTupleDesc("__pgest");                  tupdesc = RelationNameGetTupleDesc("__pgest");
# Line 118  Datum pgest(PG_FUNCTION_ARGS) { Line 166  Datum pgest(PG_FUNCTION_ARGS) {
166    
167                  MemoryContextSwitchTo(oldcontext);                  MemoryContextSwitchTo(oldcontext);
168    
169                  elog(INFO, "SRF_IS_FIRSTCALL done");                  elog(DEBUG1, "SRF_IS_FIRSTCALL done");
170          }          }
171    
172          /* stuff done on every call of the function */          /* stuff done on every call of the function */
# Line 128  Datum pgest(PG_FUNCTION_ARGS) { Line 176  Datum pgest(PG_FUNCTION_ARGS) {
176          max_calls = funcctx->max_calls;          max_calls = funcctx->max_calls;
177          slot = funcctx->slot;          slot = funcctx->slot;
178          attinmeta = funcctx->attinmeta;          attinmeta = funcctx->attinmeta;
179    
180            if (limit && call_cntr > limit - 1) {
181                    elog(INFO, "call_cntr: %d limit: %d", call_cntr, limit);
182                    SRF_RETURN_DONE(funcctx);
183            }
184    
185          if (call_cntr < max_calls) {          if (call_cntr < max_calls) {
186                  char            **values;                  char            **values;
187                  HeapTuple       tuple;                  HeapTuple       tuple;
188                  Datum           result;                  Datum           result;
189    
190                  elog(INFO, "pgest: loop count %d", call_cntr);                  elog(DEBUG1, "pgest: loop count %d", call_cntr);
191    
192                  if (! est_result) {                  if (! est_result) {
193                          elog(ERROR, "pgest: no estraier results");                          elog(ERROR, "pgest: no estraier results");
# Line 147  Datum pgest(PG_FUNCTION_ARGS) { Line 200  Datum pgest(PG_FUNCTION_ARGS) {
200                   * be processed later by the type input functions.                   * be processed later by the type input functions.
201                   */                   */
202    
203                  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)) {
204                                    
205                          elog(INFO, "URI: %s\n Title: %s\n",                          elog(DEBUG1, "URI: %s\n Title: %s\n",
206                                  est_doc_attr(doc, "@uri"),                                  est_doc_attr(doc, "@uri"),
207                                  est_doc_attr(doc, "@title")                                  est_doc_attr(doc, "@title")
208                          );                          );
# Line 158  Datum pgest(PG_FUNCTION_ARGS) { Line 211  Datum pgest(PG_FUNCTION_ARGS) {
211    
212  //                      values[0] = (char *) palloc(strlen(_estval) * sizeof(char));  //                      values[0] = (char *) palloc(strlen(_estval) * sizeof(char));
213    
214                          values[0] = attr2text(doc,"@id");                          values[0] = (char *) attr2text(doc,"@id");
215                          values[1] = attr2text(doc,"@uri");                          values[1] = (char *) attr2text(doc,"@uri");
216                          values[2] = attr2text(doc,"@title");                          values[2] = (char *) attr2text(doc,"@title");
217                          values[3] = attr2text(doc,"@type");                          values[3] = (char *) attr2text(doc,"@size");
218    
219                          /* destloy the document object */                          /* destloy the document object */
220                          elog(DEBUG2, "est_doc_delete");                          elog(DEBUG2, "est_doc_delete");
221                          est_doc_delete(doc);                          est_doc_delete(doc);
222                  } else {                  } else {
223                          elog(INFO, "no result from estraier");                          elog(INFO, "no result from estraier");
224                          values[0] = NULL;                          values[0] = DatumGetCString( "" );
225                          values[1] = NULL;                          values[1] = DatumGetCString( "" );
226                          values[2] = NULL;                          values[2] = DatumGetCString( "" );
227                          values[3] = NULL;                          values[3] = DatumGetCString( "" );
228                  }                  }
229    
230    
# Line 197  Datum pgest(PG_FUNCTION_ARGS) { Line 250  Datum pgest(PG_FUNCTION_ARGS) {
250                    
251                  SRF_RETURN_NEXT(funcctx, result);                  SRF_RETURN_NEXT(funcctx, result);
252          } else {          } else {
253                  elog(INFO, "loop over");                  elog(DEBUG1, "loop over");
254    
255                  if(!est_db_close(db, &ecode)){                  if(!est_db_close(db, &ecode)){
256                          elog(INFO, "est_db_close error: %s", est_err_msg(ecode));                          elog(INFO, "est_db_close error: %s", est_err_msg(ecode));
# Line 292  char *attr2text(ESTDOC *doc, char *attr) Line 345  char *attr2text(ESTDOC *doc, char *attr)
345          char *val;          char *val;
346          const char *attrval;          const char *attrval;
347          int len;          int len;
348            int attrlen;
349    
350          elog(INFO, "doc: %08x, attr: %s", doc, attr);          elog(DEBUG1, "doc: %08x, attr: %s", doc, attr);
351    
352          if (attrval = est_doc_attr(doc, attr)) {          if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) {
353                  val = (char *) palloc(strlen(attrval) * sizeof(char));                  val = (char *) palloc(attrlen * sizeof(char));
354          } else {          } else {
355                  return (Datum) NULL;                  return (Datum) NULL;
356          }          }
357    
358          len = strlen(attrval);          len = strlen(attrval);
359          elog(INFO, "attr2text(%s) = '%s' %d bytes", attr, attrval, len);          elog(DEBUG1, "attr2text(%s) = '%s' %d bytes", attr, attrval, len);
360    
361          len++;          len++;
362          len *= sizeof(char);          len *= sizeof(char);

Legend:
Removed from v.1  
changed lines
  Added in v.16

  ViewVC Help
Powered by ViewVC 1.1.26