--- trunk/pgest.c 2005/10/20 16:24:26 48 +++ trunk/pgest.c 2006/05/11 15:52:50 56 @@ -25,6 +25,7 @@ #include "funcapi.h" #include "utils/builtins.h" #include "utils/array.h" +#include "utils/lsyscache.h" #include "miscadmin.h" #include #include @@ -45,6 +46,7 @@ /* prototype */ char *attr2text(ESTDOC *doc, char *attr); char *node_attr2text(ESTRESDOC *rdoc, char *attr); +void cond_add_attr(ESTCOND *cond, char *attr); /* work in progress */ @@ -77,7 +79,6 @@ ESTDB *db; ESTCOND *cond; ESTDOC *doc; - const CBLIST *texts; int ecode, *est_result, resnum; int limit = 0; int offset = 0; @@ -208,13 +209,7 @@ /* minimum valid attribute length is 10: @a STREQ a */ if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { elog(DEBUG1,"attributes: %s", attr); - char *curr_attr; - curr_attr = strtok(attr, ATTR_DELIMITER); - while (curr_attr) { - elog(DEBUG1,"est_cond_add_attr(%s)", curr_attr); - est_cond_add_attr(cond, curr_attr); - curr_attr = strtok(NULL, ATTR_DELIMITER); - } + cond_add_attr(cond, attr); } /* set the search phrase to the search condition object */ @@ -323,7 +318,7 @@ if (! doc) return (Datum) NULL; - elog(DEBUG1, "doc: %08x, attr: %s", doc, attr); + elog(DEBUG1, "doc: %p, attr: %s", doc, attr); if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) { val = (char *) palloc(attrlen * sizeof(char)); @@ -359,12 +354,13 @@ #define _arg_node_uri 0 #define _arg_login 1 #define _arg_passwd 2 -#define _arg_query 3 -#define _arg_attr 4 -#define _arg_order 5 -#define _arg_limit 6 -#define _arg_offset 7 -#define _arg_attr_array 8 +#define _arg_depth 3 +#define _arg_query 4 +#define _arg_attr 5 +#define _arg_order 6 +#define _arg_limit 7 +#define _arg_offset 8 +#define _arg_attr_array 9 /* as (foo text, ... ); */ @@ -398,10 +394,10 @@ ESTCOND *cond; ESTNODERES *nres; ESTRESDOC *rdoc; - const CBLIST *texts; int resnum = 0; int limit = 0; int offset = 0; + int depth = 0; char *node_url; char *user, *passwd; @@ -479,6 +475,13 @@ user = _textout(PG_GETARG_TEXT_P(_arg_login)); passwd = _textout(PG_GETARG_TEXT_P(_arg_passwd)); + /* depth of search */ + if (PG_ARGISNULL(_arg_depth)) { + depth = 0; + } else { + depth = PG_GETARG_INT32(_arg_depth); + } + /* query string */ if (PG_ARGISNULL(_arg_query)) { query = ""; @@ -526,7 +529,7 @@ node = est_node_new(node_url); est_node_set_auth(node, user, passwd); - elog(DEBUG1, "pgest_node: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(_arg_attr) ? "NULL" : attr), limit, offset); + elog(DEBUG1, "pgest_node: node: %s (d:%d) query[%s] attr[%s] limit %d offset %d", node_url, depth, query, (PG_ARGISNULL(_arg_attr) ? "NULL" : attr), limit, offset); /* create a search condition object */ if (!(cond = est_cond_new())) { @@ -541,13 +544,7 @@ /* minimum valid attribute length is 10: @a STREQ a */ if (! PG_ARGISNULL(_arg_attr) && strlen(attr) >= 10) { elog(DEBUG1,"attributes: %s", attr); - char *curr_attr; - curr_attr = strtok(attr, ATTR_DELIMITER); - while (curr_attr) { - elog(DEBUG1,"est_cond_add_attr(%s)", curr_attr); - est_cond_add_attr(cond, curr_attr); - curr_attr = strtok(NULL, ATTR_DELIMITER); - } + cond_add_attr(cond, attr); } /* set the search phrase to the search condition object */ @@ -561,9 +558,13 @@ est_cond_set_max(cond, limit + offset); } + if (offset) { + elog(DEBUG1,"est_cond_set_skip(%d)", offset); + est_cond_set_skip(cond, offset); + } + /* get the result of search */ - /* FIXME: allow user to specify depath of search */ - nres = est_node_search(node, cond, 0); + nres = est_node_search(node, cond, depth); if (! nres) { int status = est_node_status(node); @@ -589,7 +590,6 @@ nrows = resnum - offset; } - elog(DEBUG1, "pgest_node: found %d hits for %s", resnum, query); @@ -599,7 +599,7 @@ { /* get result from estraier */ - if (! ( rdoc = est_noderes_get_doc(nres, i + offset) )) { + if (! ( rdoc = est_noderes_get_doc(nres, i) )) { elog(INFO, "pgest_node: can't find result %d", i + offset); } else { elog(DEBUG1, "URI: %s\n Title: %s\n", @@ -671,7 +671,7 @@ if (! rdoc) return (Datum) NULL; - elog(DEBUG1, "doc: %08x, attr: %s", rdoc, attr); + elog(DEBUG1, "doc: %p, attr: %s", rdoc, attr); if ( (attrval = est_resdoc_attr(rdoc, attr)) && (attrlen = strlen(attrval)) ) { val = (char *) palloc(attrlen * sizeof(char)); @@ -697,3 +697,23 @@ return val; } +/* parse attributes and add them to confition */ +void cond_add_attr(ESTCOND *cond, char *attr) { + char *next; + char *curr_attr; + while ( strlen(attr) > 0 ) { + printf("len [%s] = %d\n", attr, strlen(attr)); + if ((next = strstr(attr, ATTR_DELIMITER)) != NULL) { + curr_attr = palloc( next - attr + 1 ); + memcpy(curr_attr, attr, next-attr); + curr_attr[next-attr] = '\0'; + next += strlen(ATTR_DELIMITER); + } else { + next = ""; + curr_attr = attr; + } + elog(DEBUG1, "est_cond_add_attr(%s)", curr_attr); + est_cond_add_attr(cond, curr_attr); + attr = next; + } +}