/[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 4 by dpavlin, Fri May 20 18:45:01 2005 UTC revision 12 by dpavlin, Wed May 25 23:38:37 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;
# Line 55  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));  
                 limit = PG_GETARG_INT32(2);  
                 offset = PG_GETARG_INT32(3);  
   
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)) limit = PG_GETARG_INT32(3);
96    
97                    /* offset */
98                    if (! PG_ARGISNULL(4)) offset = PG_GETARG_INT32(4);
99    
100    
101                  /* open the database */                  /* open the database */
102                  elog(DEBUG1, "pgest: est_db_open(%s)", index_path);                  elog(DEBUG1, "pgest: est_db_open(%s)", index_path);
# Line 81  Datum pgest(PG_FUNCTION_ARGS) { Line 106  Datum pgest(PG_FUNCTION_ARGS) {
106                          SRF_RETURN_DONE(funcctx);                          SRF_RETURN_DONE(funcctx);
107                  }                  }
108                                    
109                  elog(INFO, "pgest: query=%s limit %d offset %d", query, limit, offset);                  elog(INFO, "pgest: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset);
110                                    
111                  /* create a search condition object */                  /* create a search condition object */
112                  if (!(cond = est_cond_new())) {                  if (!(cond = est_cond_new())) {
# Line 90  Datum pgest(PG_FUNCTION_ARGS) { Line 115  Datum pgest(PG_FUNCTION_ARGS) {
115                  }                  }
116                                    
117                  /* set the search phrase to the search condition object */                  /* set the search phrase to the search condition object */
118                  est_cond_set_phrase(cond, query);                  if (! PG_ARGISNULL(1) && strlen(query) > 0)
119                            est_cond_set_phrase(cond, query);
120    
121                    /* minimum valid attribute length is 10: @a STREQ a */
122                    if (! PG_ARGISNULL(2) && strlen(attr) >= 10) {
123                            elog(INFO,"est_cond_add_attr(%s)", attr);
124                            est_cond_add_attr(cond, attr);
125                    }
126    
127                  /* get the result of search */                  /* get the result of search */
128                  est_result = est_db_search(db, cond, &resnum, NULL);                  est_result = est_db_search(db, cond, &resnum, NULL);
129                                    
130                  /* total number of tuples to be returned */                  /* total number of tuples to be returned */
131                  funcctx->max_calls = resnum;                  if (limit && limit < resnum) {
132                            funcctx->max_calls = limit - offset;
133                    } else {
134                            funcctx->max_calls = resnum - offset;
135                    }
136    
137                  /* check if results exists */                  /* check if results exists */
138                  if ( 0 == funcctx->max_calls )                  if ( 0 == funcctx->max_calls )
139                          elog(INFO, "pgest: no results for: %s", query );                          elog(INFO, "pgest: no results for: %s", query );
140    
141                  elog(DEBUG1, "pgest: found %d hits for %s", funcctx->max_calls, query);                  elog(DEBUG1, "pgest: found %d hits for %s", resnum, query);
142    
143                  /* Build a tuple description for a __pgest tuple */                  /* Build a tuple description for a __pgest tuple */
144                  tupdesc = RelationNameGetTupleDesc("__pgest");                  tupdesc = RelationNameGetTupleDesc("__pgest");
# Line 167  Datum pgest(PG_FUNCTION_ARGS) { Line 203  Datum pgest(PG_FUNCTION_ARGS) {
203    
204  //                      values[0] = (char *) palloc(strlen(_estval) * sizeof(char));  //                      values[0] = (char *) palloc(strlen(_estval) * sizeof(char));
205    
206                          values[0] = attr2text(doc,"@id");                          values[0] = (char *) attr2text(doc,"@id");
207                          values[1] = attr2text(doc,"@uri");                          values[1] = (char *) attr2text(doc,"@uri");
208                          values[2] = attr2text(doc,"@title");                          values[2] = (char *) attr2text(doc,"@title");
209                          values[3] = attr2text(doc,"@type");                          values[3] = (char *) attr2text(doc,"@type");
210    
211                          /* destloy the document object */                          /* destloy the document object */
212                          elog(DEBUG2, "est_doc_delete");                          elog(DEBUG2, "est_doc_delete");
213                          est_doc_delete(doc);                          est_doc_delete(doc);
214                  } else {                  } else {
215                          elog(INFO, "no result from estraier");                          elog(INFO, "no result from estraier");
216                          values[0] = NULL;                          values[0] = DatumGetCString( "" );
217                          values[1] = NULL;                          values[1] = DatumGetCString( "" );
218                          values[2] = NULL;                          values[2] = DatumGetCString( "" );
219                          values[3] = NULL;                          values[3] = DatumGetCString( "" );
220                  }                  }
221    
222    

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

  ViewVC Help
Powered by ViewVC 1.1.26