25 |
#include "funcapi.h" |
#include "funcapi.h" |
26 |
#include "utils/builtins.h" |
#include "utils/builtins.h" |
27 |
#include "utils/array.h" |
#include "utils/array.h" |
28 |
|
#include "utils/lsyscache.h" |
29 |
#include "miscadmin.h" |
#include "miscadmin.h" |
30 |
#include <estraier.h> |
#include <estraier.h> |
31 |
#include <cabin.h> |
#include <cabin.h> |
46 |
/* prototype */ |
/* prototype */ |
47 |
char *attr2text(ESTDOC *doc, char *attr); |
char *attr2text(ESTDOC *doc, char *attr); |
48 |
char *node_attr2text(ESTRESDOC *rdoc, char *attr); |
char *node_attr2text(ESTRESDOC *rdoc, char *attr); |
49 |
|
void cond_add_attr(ESTCOND *cond, char *attr); |
50 |
|
|
51 |
|
|
52 |
/* work in progress */ |
/* work in progress */ |
79 |
ESTDB *db; |
ESTDB *db; |
80 |
ESTCOND *cond; |
ESTCOND *cond; |
81 |
ESTDOC *doc; |
ESTDOC *doc; |
|
const CBLIST *texts; |
|
82 |
int ecode, *est_result, resnum; |
int ecode, *est_result, resnum; |
83 |
int limit = 0; |
int limit = 0; |
84 |
int offset = 0; |
int offset = 0; |
209 |
/* minimum valid attribute length is 10: @a STREQ a */ |
/* minimum valid attribute length is 10: @a STREQ a */ |
210 |
if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { |
if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { |
211 |
elog(DEBUG1,"attributes: %s", attr); |
elog(DEBUG1,"attributes: %s", attr); |
212 |
char *curr_attr; |
cond_add_attr(cond, 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); |
|
|
} |
|
213 |
} |
} |
214 |
|
|
215 |
/* set the search phrase to the search condition object */ |
/* set the search phrase to the search condition object */ |
241 |
|
|
242 |
elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query); |
elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query); |
243 |
|
|
|
|
|
244 |
values = (char **) palloc(ncols * sizeof(char *)); |
values = (char **) palloc(ncols * sizeof(char *)); |
245 |
|
|
246 |
for (i = 0; i < nrows; i++) |
for (i = 0; i < nrows; i++) |
248 |
|
|
249 |
/* get result from estraier */ |
/* get result from estraier */ |
250 |
if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) { |
if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) { |
251 |
elog(INFO, "can't find result %d", i + offset); |
elog(INFO, "pgest_attr: can't find result %d", i + offset); |
252 |
} else { |
} else { |
253 |
elog(DEBUG1, "URI: %s\n Title: %s\n", |
elog(DEBUG1, "URI: %s\n Title: %s\n", |
254 |
est_doc_attr(doc, "@uri"), |
est_doc_attr(doc, "@uri"), |
280 |
/* now store it */ |
/* now store it */ |
281 |
tuplestore_puttuple(tupstore, tuple); |
tuplestore_puttuple(tupstore, tuple); |
282 |
|
|
|
|
|
283 |
/* delete estraier document object */ |
/* delete estraier document object */ |
284 |
est_doc_delete(doc); |
if (doc) est_doc_delete(doc); |
285 |
} |
} |
286 |
|
|
287 |
tuplestore_donestoring(tupstore); |
tuplestore_donestoring(tupstore); |
316 |
int len; |
int len; |
317 |
int attrlen; |
int attrlen; |
318 |
|
|
319 |
elog(DEBUG1, "doc: %08x, attr: %s", doc, attr); |
if (! doc) return (Datum) NULL; |
320 |
|
|
321 |
|
elog(DEBUG1, "doc: %p, attr: %s", doc, attr); |
322 |
|
|
323 |
if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) { |
if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) { |
324 |
val = (char *) palloc(attrlen * sizeof(char)); |
val = (char *) palloc(attrlen * sizeof(char)); |
350 |
* |
* |
351 |
*/ |
*/ |
352 |
|
|
353 |
|
/* select * from pgest( */ |
354 |
|
#define _arg_node_uri 0 |
355 |
|
#define _arg_login 1 |
356 |
|
#define _arg_passwd 2 |
357 |
|
#define _arg_depth 3 |
358 |
|
#define _arg_query 4 |
359 |
|
#define _arg_attr 5 |
360 |
|
#define _arg_order 6 |
361 |
|
#define _arg_limit 7 |
362 |
|
#define _arg_offset 8 |
363 |
|
#define _arg_attr_array 9 |
364 |
|
/* as (foo text, ... ); */ |
365 |
|
|
366 |
|
|
367 |
PG_FUNCTION_INFO_V1(pgest_node); |
PG_FUNCTION_INFO_V1(pgest_node); |
368 |
Datum pgest_node(PG_FUNCTION_ARGS) |
Datum pgest_node(PG_FUNCTION_ARGS) |
369 |
{ |
{ |
370 |
ArrayType *attr_arr = PG_GETARG_ARRAYTYPE_P(8); |
ArrayType *attr_arr = PG_GETARG_ARRAYTYPE_P(_arg_attr_array); |
371 |
Oid attr_element_type = ARR_ELEMTYPE(attr_arr); |
Oid attr_element_type = ARR_ELEMTYPE(attr_arr); |
372 |
int attr_ndims = ARR_NDIM(attr_arr); |
int attr_ndims = ARR_NDIM(attr_arr); |
373 |
int *attr_dim_counts = ARR_DIMS(attr_arr); |
int *attr_dim_counts = ARR_DIMS(attr_arr); |
394 |
ESTCOND *cond; |
ESTCOND *cond; |
395 |
ESTNODERES *nres; |
ESTNODERES *nres; |
396 |
ESTRESDOC *rdoc; |
ESTRESDOC *rdoc; |
|
const CBLIST *texts; |
|
397 |
int resnum = 0; |
int resnum = 0; |
398 |
int limit = 0; |
int limit = 0; |
399 |
int offset = 0; |
int offset = 0; |
400 |
|
int depth = 0; |
401 |
|
|
402 |
char *node_url; |
char *node_url; |
403 |
char *user, *passwd; |
char *user, *passwd; |
457 |
/* take rest of arguments from function */ |
/* take rest of arguments from function */ |
458 |
|
|
459 |
/* node URL */ |
/* node URL */ |
460 |
if (PG_ARGISNULL(0)) { |
if (PG_ARGISNULL(_arg_node_uri)) { |
461 |
ereport(ERROR, |
ereport(ERROR, |
462 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
463 |
errmsg("node URL can't be null"), |
errmsg("node URL can't be null"), |
464 |
errdetail("Node URL must be valid URL to HyperEstraier node"))); |
errdetail("Node URL must be valid URL to HyperEstraier node"))); |
465 |
} |
} |
466 |
node_url = _textout(PG_GETARG_TEXT_P(0)); |
node_url = _textout(PG_GETARG_TEXT_P(_arg_node_uri)); |
467 |
|
|
468 |
/* login and password */ |
/* login and password */ |
469 |
if (PG_ARGISNULL(1) || PG_ARGISNULL(2)) { |
if (PG_ARGISNULL(_arg_login) || PG_ARGISNULL(_arg_passwd)) { |
470 |
ereport(ERROR, |
ereport(ERROR, |
471 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
472 |
errmsg("username and password can't be NULL"), |
errmsg("username and password can't be NULL"), |
473 |
errdetail("You must specify valid username and password to HyperEstraier node"))); |
errdetail("You must specify valid username and password to HyperEstraier node"))); |
474 |
} |
} |
475 |
user = _textout(PG_GETARG_TEXT_P(1)); |
user = _textout(PG_GETARG_TEXT_P(_arg_login)); |
476 |
passwd = _textout(PG_GETARG_TEXT_P(2)); |
passwd = _textout(PG_GETARG_TEXT_P(_arg_passwd)); |
477 |
|
|
478 |
|
/* depth of search */ |
479 |
|
if (PG_ARGISNULL(_arg_depth)) { |
480 |
|
depth = 0; |
481 |
|
} else { |
482 |
|
depth = PG_GETARG_INT32(_arg_depth); |
483 |
|
} |
484 |
|
|
485 |
/* query string */ |
/* query string */ |
486 |
if (PG_ARGISNULL(3)) { |
if (PG_ARGISNULL(_arg_query)) { |
487 |
query = ""; |
query = ""; |
488 |
} else { |
} else { |
489 |
query = _textout(PG_GETARG_TEXT_P(3)); |
query = _textout(PG_GETARG_TEXT_P(_arg_query)); |
490 |
} |
} |
491 |
|
|
492 |
/* atribute filter */ |
/* atribute filter */ |
493 |
if (PG_ARGISNULL(4)) { |
if (PG_ARGISNULL(_arg_attr)) { |
494 |
attr = ""; |
attr = ""; |
495 |
} else { |
} else { |
496 |
attr = _textout(PG_GETARG_TEXT_P(4)); |
attr = _textout(PG_GETARG_TEXT_P(_arg_attr)); |
497 |
} |
} |
498 |
|
|
499 |
/* sort order */ |
/* sort order */ |
500 |
if (PG_ARGISNULL(5)) { |
if (PG_ARGISNULL(_arg_order)) { |
501 |
order = ""; |
order = ""; |
502 |
} else { |
} else { |
503 |
order = _textout(PG_GETARG_TEXT_P(5)); |
order = _textout(PG_GETARG_TEXT_P(_arg_order)); |
504 |
} |
} |
505 |
|
|
506 |
|
|
507 |
/* limit */ |
/* limit */ |
508 |
if (PG_ARGISNULL(6)) { |
if (PG_ARGISNULL(_arg_limit)) { |
509 |
limit = 0; |
limit = 0; |
510 |
} else { |
} else { |
511 |
limit = PG_GETARG_INT32(6); |
limit = PG_GETARG_INT32(_arg_limit); |
512 |
} |
} |
513 |
|
|
514 |
/* offset */ |
/* offset */ |
515 |
if (PG_ARGISNULL(7)) { |
if (PG_ARGISNULL(_arg_offset)) { |
516 |
offset = 0; |
offset = 0; |
517 |
} else { |
} else { |
518 |
offset = PG_GETARG_INT32(7); |
offset = PG_GETARG_INT32(_arg_offset); |
519 |
} |
} |
520 |
|
|
521 |
/* initialize the network environment */ |
/* initialize the network environment */ |
529 |
node = est_node_new(node_url); |
node = est_node_new(node_url); |
530 |
est_node_set_auth(node, user, passwd); |
est_node_set_auth(node, user, passwd); |
531 |
|
|
532 |
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); |
533 |
|
|
534 |
/* create a search condition object */ |
/* create a search condition object */ |
535 |
if (!(cond = est_cond_new())) { |
if (!(cond = est_cond_new())) { |
538 |
} |
} |
539 |
|
|
540 |
/* set the search phrase to the search condition object */ |
/* set the search phrase to the search condition object */ |
541 |
if (! PG_ARGISNULL(3) && strlen(query) > 0) |
if (! PG_ARGISNULL(_arg_query) && strlen(query) > 0) |
542 |
est_cond_set_phrase(cond, query); |
est_cond_set_phrase(cond, query); |
543 |
|
|
544 |
/* minimum valid attribute length is 10: @a STREQ a */ |
/* minimum valid attribute length is 10: @a STREQ a */ |
545 |
if (! PG_ARGISNULL(4) && strlen(attr) >= 10) { |
if (! PG_ARGISNULL(_arg_attr) && strlen(attr) >= 10) { |
546 |
elog(DEBUG1,"attributes: %s", attr); |
elog(DEBUG1,"attributes: %s", attr); |
547 |
char *curr_attr; |
cond_add_attr(cond, 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); |
|
|
} |
|
548 |
} |
} |
549 |
|
|
550 |
/* set the search phrase to the search condition object */ |
/* set the search phrase to the search condition object */ |
551 |
if (! PG_ARGISNULL(5) && strlen(order) > 0) { |
if (! PG_ARGISNULL(_arg_order) && strlen(order) > 0) { |
552 |
elog(DEBUG1,"est_cond_set_order(%s)", order); |
elog(DEBUG1,"est_cond_set_order(%s)", order); |
553 |
est_cond_set_order(cond, order); |
est_cond_set_order(cond, order); |
554 |
} |
} |
558 |
est_cond_set_max(cond, limit + offset); |
est_cond_set_max(cond, limit + offset); |
559 |
} |
} |
560 |
|
|
561 |
|
if (offset) { |
562 |
|
elog(DEBUG1,"est_cond_set_skip(%d)", offset); |
563 |
|
est_cond_set_skip(cond, offset); |
564 |
|
} |
565 |
|
|
566 |
/* get the result of search */ |
/* get the result of search */ |
567 |
/* FIXME: allow user to specify depath of search */ |
nres = est_node_search(node, cond, depth); |
|
nres = est_node_search(node, cond, 0); |
|
568 |
|
|
569 |
if (! nres) { |
if (! nres) { |
570 |
int status = est_node_status(node); |
int status = est_node_status(node); |
590 |
nrows = resnum - offset; |
nrows = resnum - offset; |
591 |
} |
} |
592 |
|
|
|
|
|
593 |
elog(DEBUG1, "pgest_node: found %d hits for %s", resnum, query); |
elog(DEBUG1, "pgest_node: found %d hits for %s", resnum, query); |
594 |
|
|
595 |
|
|
599 |
{ |
{ |
600 |
|
|
601 |
/* get result from estraier */ |
/* get result from estraier */ |
602 |
if (! ( rdoc = est_noderes_get_doc(nres, i + offset) )) { |
if (! ( rdoc = est_noderes_get_doc(nres, i) )) { |
603 |
elog(INFO, "can't find result %d", i + offset); |
elog(INFO, "pgest_node: can't find result %d", i + offset); |
604 |
} else { |
} else { |
605 |
elog(DEBUG1, "URI: %s\n Title: %s\n", |
elog(DEBUG1, "URI: %s\n Title: %s\n", |
606 |
est_resdoc_attr(rdoc, "@uri"), |
est_resdoc_attr(rdoc, "@uri"), |
669 |
int len; |
int len; |
670 |
int attrlen; |
int attrlen; |
671 |
|
|
672 |
elog(DEBUG1, "doc: %08x, attr: %s", rdoc, attr); |
if (! rdoc) return (Datum) NULL; |
673 |
|
|
674 |
|
elog(DEBUG1, "doc: %p, attr: %s", rdoc, attr); |
675 |
|
|
676 |
if ( (attrval = est_resdoc_attr(rdoc, attr)) && (attrlen = strlen(attrval)) ) { |
if ( (attrval = est_resdoc_attr(rdoc, attr)) && (attrlen = strlen(attrval)) ) { |
677 |
val = (char *) palloc(attrlen * sizeof(char)); |
val = (char *) palloc(attrlen * sizeof(char)); |
697 |
return val; |
return val; |
698 |
} |
} |
699 |
|
|
700 |
|
/* parse attributes and add them to confition */ |
701 |
|
void cond_add_attr(ESTCOND *cond, char *attr) { |
702 |
|
char *next; |
703 |
|
char *curr_attr; |
704 |
|
while ( strlen(attr) > 0 ) { |
705 |
|
printf("len [%s] = %d\n", attr, strlen(attr)); |
706 |
|
if ((next = strstr(attr, ATTR_DELIMITER)) != NULL) { |
707 |
|
curr_attr = palloc( next - attr + 1 ); |
708 |
|
memcpy(curr_attr, attr, next-attr); |
709 |
|
curr_attr[next-attr] = '\0'; |
710 |
|
next += strlen(ATTR_DELIMITER); |
711 |
|
} else { |
712 |
|
next = ""; |
713 |
|
curr_attr = attr; |
714 |
|
} |
715 |
|
elog(DEBUG1, "est_cond_add_attr(%s)", curr_attr); |
716 |
|
est_cond_add_attr(cond, curr_attr); |
717 |
|
attr = next; |
718 |
|
} |
719 |
|
} |