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> |
42 |
#endif |
#endif |
43 |
|
|
44 |
#define ATTR_DELIMITER "{{!}}" |
#define ATTR_DELIMITER "{{!}}" |
45 |
|
#define HINTS_PREFIX "HINTS." |
46 |
|
|
47 |
/* prototype */ |
/* prototype */ |
48 |
char *attr2text(ESTDOC *doc, char *attr); |
char *attr2text(ESTDOC *doc, char *attr); |
49 |
char *node_attr2text(ESTRESDOC *rdoc, char *attr); |
char *node_attr2text(ESTRESDOC *rdoc, char *attr); |
50 |
|
void cond_add_attr(ESTCOND *cond, char *attr); |
51 |
|
|
52 |
|
|
53 |
/* work in progress */ |
/* work in progress */ |
80 |
ESTDB *db; |
ESTDB *db; |
81 |
ESTCOND *cond; |
ESTCOND *cond; |
82 |
ESTDOC *doc; |
ESTDOC *doc; |
|
const CBLIST *texts; |
|
83 |
int ecode, *est_result, resnum; |
int ecode, *est_result, resnum; |
84 |
int limit = 0; |
int limit = 0; |
85 |
int offset = 0; |
int offset = 0; |
210 |
/* minimum valid attribute length is 10: @a STREQ a */ |
/* minimum valid attribute length is 10: @a STREQ a */ |
211 |
if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { |
if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { |
212 |
elog(DEBUG1,"attributes: %s", attr); |
elog(DEBUG1,"attributes: %s", attr); |
213 |
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); |
|
|
} |
|
214 |
} |
} |
215 |
|
|
216 |
/* set the search phrase to the search condition object */ |
/* set the search phrase to the search condition object */ |
242 |
|
|
243 |
elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query); |
elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query); |
244 |
|
|
|
|
|
245 |
values = (char **) palloc(ncols * sizeof(char *)); |
values = (char **) palloc(ncols * sizeof(char *)); |
246 |
|
|
247 |
for (i = 0; i < nrows; i++) |
for (i = 0; i < nrows; i++) |
249 |
|
|
250 |
/* get result from estraier */ |
/* get result from estraier */ |
251 |
if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) { |
if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) { |
252 |
elog(INFO, "can't find result %d", i + offset); |
elog(INFO, "pgest_attr: can't find result %d", i + offset); |
253 |
} else { |
} else { |
254 |
elog(DEBUG1, "URI: %s\n Title: %s\n", |
elog(DEBUG1, "URI: %s\n Title: %s\n", |
255 |
est_doc_attr(doc, "@uri"), |
est_doc_attr(doc, "@uri"), |
281 |
/* now store it */ |
/* now store it */ |
282 |
tuplestore_puttuple(tupstore, tuple); |
tuplestore_puttuple(tupstore, tuple); |
283 |
|
|
|
|
|
284 |
/* delete estraier document object */ |
/* delete estraier document object */ |
285 |
est_doc_delete(doc); |
if (doc) est_doc_delete(doc); |
286 |
} |
} |
287 |
|
|
288 |
tuplestore_donestoring(tupstore); |
tuplestore_donestoring(tupstore); |
317 |
int len; |
int len; |
318 |
int attrlen; |
int attrlen; |
319 |
|
|
320 |
elog(DEBUG1, "doc: %08x, attr: %s", doc, attr); |
if (! doc) return (Datum) NULL; |
321 |
|
|
322 |
|
elog(DEBUG1, "doc: %p, attr: %s", doc, attr); |
323 |
|
|
324 |
if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) { |
if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) { |
325 |
val = (char *) palloc(attrlen * sizeof(char)); |
val = (char *) palloc(attrlen * sizeof(char)); |
351 |
* |
* |
352 |
*/ |
*/ |
353 |
|
|
354 |
|
/* select * from pgest( */ |
355 |
|
#define _arg_node_uri 0 |
356 |
|
#define _arg_login 1 |
357 |
|
#define _arg_passwd 2 |
358 |
|
#define _arg_depth 3 |
359 |
|
#define _arg_query 4 |
360 |
|
#define _arg_attr 5 |
361 |
|
#define _arg_order 6 |
362 |
|
#define _arg_limit 7 |
363 |
|
#define _arg_offset 8 |
364 |
|
#define _arg_attr_array 9 |
365 |
|
/* as (foo text, ... ); */ |
366 |
|
|
367 |
|
|
368 |
PG_FUNCTION_INFO_V1(pgest_node); |
PG_FUNCTION_INFO_V1(pgest_node); |
369 |
Datum pgest_node(PG_FUNCTION_ARGS) |
Datum pgest_node(PG_FUNCTION_ARGS) |
370 |
{ |
{ |
371 |
ArrayType *attr_arr = PG_GETARG_ARRAYTYPE_P(8); |
ArrayType *attr_arr = PG_GETARG_ARRAYTYPE_P(_arg_attr_array); |
372 |
Oid attr_element_type = ARR_ELEMTYPE(attr_arr); |
Oid attr_element_type = ARR_ELEMTYPE(attr_arr); |
373 |
int attr_ndims = ARR_NDIM(attr_arr); |
int attr_ndims = ARR_NDIM(attr_arr); |
374 |
int *attr_dim_counts = ARR_DIMS(attr_arr); |
int *attr_dim_counts = ARR_DIMS(attr_arr); |
395 |
ESTCOND *cond; |
ESTCOND *cond; |
396 |
ESTNODERES *nres; |
ESTNODERES *nres; |
397 |
ESTRESDOC *rdoc; |
ESTRESDOC *rdoc; |
398 |
const CBLIST *texts; |
CBMAP *hints; |
399 |
int resnum = 0; |
int resnum = 0; |
400 |
int limit = 0; |
int limit = 0; |
401 |
int offset = 0; |
int offset = 0; |
402 |
|
int depth = 0; |
403 |
|
|
404 |
char *node_url; |
char *node_url; |
405 |
char *user, *passwd; |
char *user, *passwd; |
459 |
/* take rest of arguments from function */ |
/* take rest of arguments from function */ |
460 |
|
|
461 |
/* node URL */ |
/* node URL */ |
462 |
if (PG_ARGISNULL(0)) { |
if (PG_ARGISNULL(_arg_node_uri)) { |
463 |
ereport(ERROR, |
ereport(ERROR, |
464 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
465 |
errmsg("node URL can't be null"), |
errmsg("node URL can't be null"), |
466 |
errdetail("Node URL must be valid URL to HyperEstraier node"))); |
errdetail("Node URL must be valid URL to HyperEstraier node"))); |
467 |
} |
} |
468 |
node_url = _textout(PG_GETARG_TEXT_P(0)); |
node_url = _textout(PG_GETARG_TEXT_P(_arg_node_uri)); |
469 |
|
|
470 |
/* login and password */ |
/* login and password */ |
471 |
if (PG_ARGISNULL(1) || PG_ARGISNULL(2)) { |
if (PG_ARGISNULL(_arg_login) || PG_ARGISNULL(_arg_passwd)) { |
472 |
ereport(ERROR, |
ereport(ERROR, |
473 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
474 |
errmsg("username and password can't be NULL"), |
errmsg("username and password can't be NULL"), |
475 |
errdetail("You must specify valid username and password to HyperEstraier node"))); |
errdetail("You must specify valid username and password to HyperEstraier node"))); |
476 |
} |
} |
477 |
user = _textout(PG_GETARG_TEXT_P(1)); |
user = _textout(PG_GETARG_TEXT_P(_arg_login)); |
478 |
passwd = _textout(PG_GETARG_TEXT_P(2)); |
passwd = _textout(PG_GETARG_TEXT_P(_arg_passwd)); |
479 |
|
|
480 |
|
/* depth of search */ |
481 |
|
if (PG_ARGISNULL(_arg_depth)) { |
482 |
|
depth = 0; |
483 |
|
} else { |
484 |
|
depth = PG_GETARG_INT32(_arg_depth); |
485 |
|
} |
486 |
|
|
487 |
/* query string */ |
/* query string */ |
488 |
if (PG_ARGISNULL(3)) { |
if (PG_ARGISNULL(_arg_query)) { |
489 |
query = ""; |
query = ""; |
490 |
} else { |
} else { |
491 |
query = _textout(PG_GETARG_TEXT_P(3)); |
query = _textout(PG_GETARG_TEXT_P(_arg_query)); |
492 |
} |
} |
493 |
|
|
494 |
/* atribute filter */ |
/* atribute filter */ |
495 |
if (PG_ARGISNULL(4)) { |
if (PG_ARGISNULL(_arg_attr)) { |
496 |
attr = ""; |
attr = ""; |
497 |
} else { |
} else { |
498 |
attr = _textout(PG_GETARG_TEXT_P(4)); |
attr = _textout(PG_GETARG_TEXT_P(_arg_attr)); |
499 |
} |
} |
500 |
|
|
501 |
/* sort order */ |
/* sort order */ |
502 |
if (PG_ARGISNULL(5)) { |
if (PG_ARGISNULL(_arg_order)) { |
503 |
order = ""; |
order = ""; |
504 |
} else { |
} else { |
505 |
order = _textout(PG_GETARG_TEXT_P(5)); |
order = _textout(PG_GETARG_TEXT_P(_arg_order)); |
506 |
} |
} |
507 |
|
|
508 |
|
|
509 |
/* limit */ |
/* limit */ |
510 |
if (PG_ARGISNULL(6)) { |
if (PG_ARGISNULL(_arg_limit)) { |
511 |
limit = 0; |
limit = 0; |
512 |
} else { |
} else { |
513 |
limit = PG_GETARG_INT32(6); |
limit = PG_GETARG_INT32(_arg_limit); |
514 |
} |
} |
515 |
|
|
516 |
/* offset */ |
/* offset */ |
517 |
if (PG_ARGISNULL(7)) { |
if (PG_ARGISNULL(_arg_offset)) { |
518 |
offset = 0; |
offset = 0; |
519 |
} else { |
} else { |
520 |
offset = PG_GETARG_INT32(7); |
offset = PG_GETARG_INT32(_arg_offset); |
521 |
} |
} |
522 |
|
|
523 |
/* initialize the network environment */ |
/* initialize the network environment */ |
531 |
node = est_node_new(node_url); |
node = est_node_new(node_url); |
532 |
est_node_set_auth(node, user, passwd); |
est_node_set_auth(node, user, passwd); |
533 |
|
|
534 |
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); |
535 |
|
|
536 |
/* create a search condition object */ |
/* create a search condition object */ |
537 |
if (!(cond = est_cond_new())) { |
if (!(cond = est_cond_new())) { |
540 |
} |
} |
541 |
|
|
542 |
/* set the search phrase to the search condition object */ |
/* set the search phrase to the search condition object */ |
543 |
if (! PG_ARGISNULL(3) && strlen(query) > 0) |
if (! PG_ARGISNULL(_arg_query) && strlen(query) > 0) |
544 |
est_cond_set_phrase(cond, query); |
est_cond_set_phrase(cond, query); |
545 |
|
|
546 |
/* minimum valid attribute length is 10: @a STREQ a */ |
/* minimum valid attribute length is 10: @a STREQ a */ |
547 |
if (! PG_ARGISNULL(4) && strlen(attr) >= 10) { |
if (! PG_ARGISNULL(_arg_attr) && strlen(attr) >= 10) { |
548 |
elog(DEBUG1,"attributes: %s", attr); |
elog(DEBUG1,"attributes: %s", attr); |
549 |
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); |
|
|
} |
|
550 |
} |
} |
551 |
|
|
552 |
/* set the search phrase to the search condition object */ |
/* set the search phrase to the search condition object */ |
553 |
if (! PG_ARGISNULL(5) && strlen(order) > 0) { |
if (! PG_ARGISNULL(_arg_order) && strlen(order) > 0) { |
554 |
elog(DEBUG1,"est_cond_set_order(%s)", order); |
elog(DEBUG1,"est_cond_set_order(%s)", order); |
555 |
est_cond_set_order(cond, order); |
est_cond_set_order(cond, order); |
556 |
} |
} |
560 |
est_cond_set_max(cond, limit + offset); |
est_cond_set_max(cond, limit + offset); |
561 |
} |
} |
562 |
|
|
563 |
|
if (offset) { |
564 |
|
elog(DEBUG1,"est_cond_set_skip(%d)", offset); |
565 |
|
est_cond_set_skip(cond, offset); |
566 |
|
} |
567 |
|
|
568 |
/* get the result of search */ |
/* get the result of search */ |
569 |
/* FIXME: allow user to specify depath of search */ |
nres = est_node_search(node, cond, depth); |
570 |
nres = est_node_search(node, cond, 0); |
|
571 |
|
if (! nres) { |
572 |
|
int status = est_node_status(node); |
573 |
|
est_cond_delete(cond); |
574 |
|
est_node_delete(node); |
575 |
|
est_free_net_env(); |
576 |
|
ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED), |
577 |
|
errmsg("pgest_node: search failed, node status %d", status))); |
578 |
|
} |
579 |
|
|
580 |
/* get number of results */ |
/* get number of results */ |
581 |
resnum = est_noderes_doc_num(nres); |
resnum = est_noderes_doc_num(nres); |
592 |
nrows = resnum - offset; |
nrows = resnum - offset; |
593 |
} |
} |
594 |
|
|
595 |
|
/* get hints */ |
596 |
|
hints = est_noderes_hints(nres); |
597 |
|
|
598 |
elog(DEBUG1, "pgest_node: found %d hits for %s", resnum, query); |
elog(DEBUG1, "pgest_node: found %d hits for %s", resnum, query); |
599 |
|
|
604 |
{ |
{ |
605 |
|
|
606 |
/* get result from estraier */ |
/* get result from estraier */ |
607 |
if (! ( rdoc = est_noderes_get_doc(nres, i + offset) )) { |
if (! ( rdoc = est_noderes_get_doc(nres, i) )) { |
608 |
elog(INFO, "can't find result %d", i + offset); |
elog(INFO, "pgest_node: can't find result %d", i + offset); |
609 |
} else { |
} else { |
610 |
elog(DEBUG1, "URI: %s\n Title: %s\n", |
elog(DEBUG1, "URI: %s\n Title: %s\n", |
611 |
est_resdoc_attr(rdoc, "@uri"), |
est_resdoc_attr(rdoc, "@uri"), |
617 |
for (j = 0; j < ncols; j++) |
for (j = 0; j < ncols; j++) |
618 |
{ |
{ |
619 |
bool isnull; |
bool isnull; |
620 |
|
char *attr; /* current attribute name */ |
621 |
|
char *hint; /* position of current hint in attribute */ |
622 |
|
char *hint_val; |
623 |
|
|
624 |
/* array value of this position */ |
/* array value of this position */ |
625 |
indx[0] = j + attr_dim_lower_bounds[0]; |
indx[0] = j + attr_dim_lower_bounds[0]; |
626 |
|
|
627 |
dvalue = array_ref(attr_arr, attr_ndims, indx, -1, attr_len, attr_byval, attr_align, &isnull); |
dvalue = array_ref(attr_arr, attr_ndims, indx, -1, attr_len, attr_byval, attr_align, &isnull); |
628 |
|
attr = (char *)DirectFunctionCall1(textout, dvalue); |
629 |
|
|
630 |
if (!isnull && rdoc) |
if (!isnull && (hint = strstr(attr, HINTS_PREFIX)) != NULL) { |
631 |
values[j] = DatumGetCString( |
/* skip HINTS. prefix */ |
632 |
node_attr2text(rdoc, |
hint += strlen(HINTS_PREFIX); |
633 |
(char *)DirectFunctionCall1(textout, dvalue) |
|
634 |
)); |
hint_val = cbmapget(hints, hint, -1, NULL); |
635 |
|
elog(DEBUG2, "hint %s = %s", hint, hint_val); |
636 |
|
|
637 |
|
if (hint_val != NULL) { |
638 |
|
values[j] = DatumGetCString( hint_val ); |
639 |
|
} else { |
640 |
|
elog(INFO, "can't get hint in results: %s", hint); |
641 |
|
values[j] = NULL; |
642 |
|
} |
643 |
|
} else if (!isnull && rdoc) |
644 |
|
values[j] = DatumGetCString( node_attr2text(rdoc, attr) ); |
645 |
else |
else |
646 |
values[j] = NULL; |
values[j] = NULL; |
647 |
} |
} |
688 |
int len; |
int len; |
689 |
int attrlen; |
int attrlen; |
690 |
|
|
691 |
elog(DEBUG1, "doc: %08x, attr: %s", rdoc, attr); |
if (! rdoc) return (Datum) NULL; |
692 |
|
|
693 |
|
elog(DEBUG1, "doc: %p, attr: %s", rdoc, attr); |
694 |
|
|
695 |
if ( (attrval = est_resdoc_attr(rdoc, attr)) && (attrlen = strlen(attrval)) ) { |
if ( (attrval = est_resdoc_attr(rdoc, attr)) && (attrlen = strlen(attrval)) ) { |
696 |
val = (char *) palloc(attrlen * sizeof(char)); |
val = (char *) palloc(attrlen * sizeof(char)); |
716 |
return val; |
return val; |
717 |
} |
} |
718 |
|
|
719 |
|
/* parse attributes and add them to confition */ |
720 |
|
void cond_add_attr(ESTCOND *cond, char *attr) { |
721 |
|
char *next; |
722 |
|
char *curr_attr; |
723 |
|
while ( strlen(attr) > 0 ) { |
724 |
|
printf("len [%s] = %d\n", attr, strlen(attr)); |
725 |
|
if ((next = strstr(attr, ATTR_DELIMITER)) != NULL) { |
726 |
|
curr_attr = palloc( next - attr + 1 ); |
727 |
|
memcpy(curr_attr, attr, next-attr); |
728 |
|
curr_attr[next-attr] = '\0'; |
729 |
|
next += strlen(ATTR_DELIMITER); |
730 |
|
} else { |
731 |
|
next = ""; |
732 |
|
curr_attr = attr; |
733 |
|
} |
734 |
|
elog(DEBUG1, "est_cond_add_attr(%s)", curr_attr); |
735 |
|
est_cond_add_attr(cond, curr_attr); |
736 |
|
attr = next; |
737 |
|
} |
738 |
|
} |