--- trunk/pgest.c 2005/09/10 20:35:09 41 +++ trunk/pgest.c 2006/05/09 22:55:42 51 @@ -246,7 +246,6 @@ elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query); - values = (char **) palloc(ncols * sizeof(char *)); for (i = 0; i < nrows; i++) @@ -254,7 +253,7 @@ /* get result from estraier */ if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) { - elog(INFO, "can't find result %d", i + offset); + elog(INFO, "pgest_attr: can't find result %d", i + offset); } else { elog(DEBUG1, "URI: %s\n Title: %s\n", est_doc_attr(doc, "@uri"), @@ -286,9 +285,8 @@ /* now store it */ tuplestore_puttuple(tupstore, tuple); - /* delete estraier document object */ - est_doc_delete(doc); + if (doc) est_doc_delete(doc); } tuplestore_donestoring(tupstore); @@ -323,6 +321,8 @@ int len; int attrlen; + if (! doc) return (Datum) NULL; + elog(DEBUG1, "doc: %08x, attr: %s", doc, attr); if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) { @@ -355,10 +355,24 @@ * */ +/* select * from pgest( */ +#define _arg_node_uri 0 +#define _arg_login 1 +#define _arg_passwd 2 +#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, ... ); */ + + PG_FUNCTION_INFO_V1(pgest_node); Datum pgest_node(PG_FUNCTION_ARGS) { - ArrayType *attr_arr = PG_GETARG_ARRAYTYPE_P(8); + ArrayType *attr_arr = PG_GETARG_ARRAYTYPE_P(_arg_attr_array); Oid attr_element_type = ARR_ELEMTYPE(attr_arr); int attr_ndims = ARR_NDIM(attr_arr); int *attr_dim_counts = ARR_DIMS(attr_arr); @@ -389,6 +403,7 @@ int resnum = 0; int limit = 0; int offset = 0; + int depth = 0; char *node_url; char *user, *passwd; @@ -448,58 +463,65 @@ /* take rest of arguments from function */ /* node URL */ - if (PG_ARGISNULL(0)) { + if (PG_ARGISNULL(_arg_node_uri)) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("node URL can't be null"), errdetail("Node URL must be valid URL to HyperEstraier node"))); } - node_url = _textout(PG_GETARG_TEXT_P(0)); + node_url = _textout(PG_GETARG_TEXT_P(_arg_node_uri)); /* login and password */ - if (PG_ARGISNULL(1) || PG_ARGISNULL(2)) { + if (PG_ARGISNULL(_arg_login) || PG_ARGISNULL(_arg_passwd)) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("username and password can't be NULL"), errdetail("You must specify valid username and password to HyperEstraier node"))); } - user = _textout(PG_GETARG_TEXT_P(1)); - passwd = _textout(PG_GETARG_TEXT_P(2)); + 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(3)) { + if (PG_ARGISNULL(_arg_query)) { query = ""; } else { - query = _textout(PG_GETARG_TEXT_P(3)); + query = _textout(PG_GETARG_TEXT_P(_arg_query)); } /* atribute filter */ - if (PG_ARGISNULL(4)) { + if (PG_ARGISNULL(_arg_attr)) { attr = ""; } else { - attr = _textout(PG_GETARG_TEXT_P(4)); + attr = _textout(PG_GETARG_TEXT_P(_arg_attr)); } /* sort order */ - if (PG_ARGISNULL(5)) { + if (PG_ARGISNULL(_arg_order)) { order = ""; } else { - order = _textout(PG_GETARG_TEXT_P(5)); + order = _textout(PG_GETARG_TEXT_P(_arg_order)); } /* limit */ - if (PG_ARGISNULL(6)) { + if (PG_ARGISNULL(_arg_limit)) { limit = 0; } else { - limit = PG_GETARG_INT32(6); + limit = PG_GETARG_INT32(_arg_limit); } /* offset */ - if (PG_ARGISNULL(7)) { + if (PG_ARGISNULL(_arg_offset)) { offset = 0; } else { - offset = PG_GETARG_INT32(7); + offset = PG_GETARG_INT32(_arg_offset); } /* initialize the network environment */ @@ -513,7 +535,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(4) ? "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())) { @@ -522,11 +544,11 @@ } /* set the search phrase to the search condition object */ - if (! PG_ARGISNULL(3) && strlen(query) > 0) + if (! PG_ARGISNULL(_arg_query) && strlen(query) > 0) est_cond_set_phrase(cond, query); /* minimum valid attribute length is 10: @a STREQ a */ - if (! PG_ARGISNULL(4) && strlen(attr) >= 10) { + if (! PG_ARGISNULL(_arg_attr) && strlen(attr) >= 10) { elog(DEBUG1,"attributes: %s", attr); char *curr_attr; curr_attr = strtok(attr, ATTR_DELIMITER); @@ -538,7 +560,7 @@ } /* set the search phrase to the search condition object */ - if (! PG_ARGISNULL(5) && strlen(order) > 0) { + if (! PG_ARGISNULL(_arg_order) && strlen(order) > 0) { elog(DEBUG1,"est_cond_set_order(%s)", order); est_cond_set_order(cond, order); } @@ -548,9 +570,22 @@ 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); + est_cond_delete(cond); + est_node_delete(node); + est_free_net_env(); + ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED), + errmsg("pgest_node: search failed, node status %d", status))); + } /* get number of results */ resnum = est_noderes_doc_num(nres); @@ -567,7 +602,6 @@ nrows = resnum - offset; } - elog(DEBUG1, "pgest_node: found %d hits for %s", resnum, query); @@ -577,8 +611,8 @@ { /* get result from estraier */ - if (! ( rdoc = est_noderes_get_doc(nres, i + offset) )) { - elog(INFO, "can't find result %d", 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", est_resdoc_attr(rdoc, "@uri"), @@ -647,6 +681,8 @@ int len; int attrlen; + if (! rdoc) return (Datum) NULL; + elog(DEBUG1, "doc: %08x, attr: %s", rdoc, attr); if ( (attrval = est_resdoc_attr(rdoc, attr)) && (attrlen = strlen(attrval)) ) {