14 |
* |
* |
15 |
* Based on: |
* Based on: |
16 |
* - C example from PostgreSQL documentation (BSD licence) |
* - C example from PostgreSQL documentation (BSD licence) |
17 |
* - example002.c from Hyper Estraier (GPL) |
* - coreexample002.c and nodeexample002.c from Hyper Estraier (GPL) |
18 |
* - _textin/_textout from pgcurl.c (LGPL) |
* - _textin/_textout from pgcurl.c (LGPL) |
19 |
* |
* |
20 |
* This code is licenced under GPL |
* This code is licenced under GPL |
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> |
32 |
|
#include <estnode.h> |
33 |
|
|
34 |
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) |
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) |
35 |
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) |
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) |
41 |
#define SortMem 16 * 1024 |
#define SortMem 16 * 1024 |
42 |
#endif |
#endif |
43 |
|
|
44 |
|
#define ATTR_DELIMITER "{{!}}" |
45 |
|
|
46 |
/* prototype */ |
/* prototype */ |
47 |
char *attr2text(ESTDOC *doc, char *attr); |
char *attr2text(ESTDOC *doc, char *attr); |
48 |
|
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; |
208 |
|
|
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,"est_cond_add_attr(%s)", attr); |
elog(DEBUG1,"attributes: %s", attr); |
212 |
est_cond_add_attr(cond, attr); |
cond_add_attr(cond, attr); |
213 |
} |
} |
214 |
|
|
215 |
/* set the search phrase to the search condition object */ |
/* set the search phrase to the search condition object */ |
234 |
/* total number of tuples to be returned */ |
/* total number of tuples to be returned */ |
235 |
if (limit && limit < resnum) { |
if (limit && limit < resnum) { |
236 |
nrows = limit; |
nrows = limit; |
|
elog(INFO, "This is probably a bug in limit implementation. Please report it to dpavlin@rot13.org"); |
|
237 |
} else { |
} else { |
238 |
nrows = resnum - offset; |
nrows = resnum - offset; |
239 |
} |
} |
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)); |
344 |
return val; |
return val; |
345 |
} |
} |
346 |
|
|
347 |
|
/* |
348 |
|
* variation on theme: use node API which doesn't open index on |
349 |
|
* every query which is much faster for large indexes |
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); |
368 |
|
Datum pgest_node(PG_FUNCTION_ARGS) |
369 |
|
{ |
370 |
|
ArrayType *attr_arr = PG_GETARG_ARRAYTYPE_P(_arg_attr_array); |
371 |
|
Oid attr_element_type = ARR_ELEMTYPE(attr_arr); |
372 |
|
int attr_ndims = ARR_NDIM(attr_arr); |
373 |
|
int *attr_dim_counts = ARR_DIMS(attr_arr); |
374 |
|
int *attr_dim_lower_bounds = ARR_LBOUND(attr_arr); |
375 |
|
int ncols = 0; |
376 |
|
int nrows = 0; |
377 |
|
int indx[MAXDIM]; |
378 |
|
int16 attr_len; |
379 |
|
bool attr_byval; |
380 |
|
char attr_align; |
381 |
|
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
382 |
|
AttInMetadata *attinmeta; |
383 |
|
TupleDesc tupdesc; |
384 |
|
Tuplestorestate *tupstore = NULL; |
385 |
|
HeapTuple tuple; |
386 |
|
MemoryContext per_query_ctx; |
387 |
|
MemoryContext oldcontext; |
388 |
|
Datum dvalue; |
389 |
|
char **values; |
390 |
|
int rsinfo_ncols; |
391 |
|
int i, j; |
392 |
|
/* estvars */ |
393 |
|
ESTNODE *node; |
394 |
|
ESTCOND *cond; |
395 |
|
ESTNODERES *nres; |
396 |
|
ESTRESDOC *rdoc; |
397 |
|
int resnum = 0; |
398 |
|
int limit = 0; |
399 |
|
int offset = 0; |
400 |
|
int depth = 0; |
401 |
|
|
402 |
|
char *node_url; |
403 |
|
char *user, *passwd; |
404 |
|
char *query; |
405 |
|
char *attr; |
406 |
|
char *order; |
407 |
|
|
408 |
|
|
409 |
|
/* only allow 1D input array */ |
410 |
|
if (attr_ndims == 1) |
411 |
|
{ |
412 |
|
ncols = attr_dim_counts[0]; |
413 |
|
} |
414 |
|
else |
415 |
|
ereport(ERROR, |
416 |
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
417 |
|
errmsg("invalid input array"), |
418 |
|
errdetail("Input array must have 1 dimension"))); |
419 |
|
|
420 |
|
/* check to see if caller supports us returning a tuplestore */ |
421 |
|
if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize)) |
422 |
|
ereport(ERROR, |
423 |
|
(errcode(ERRCODE_SYNTAX_ERROR), |
424 |
|
errmsg("materialize mode required, but it is not " \ |
425 |
|
"allowed in this context"))); |
426 |
|
|
427 |
|
/* get info about element type needed to construct the array */ |
428 |
|
get_typlenbyvalalign(attr_element_type, &attr_len, &attr_byval, &attr_align); |
429 |
|
|
430 |
|
/* get the requested return tuple description */ |
431 |
|
tupdesc = rsinfo->expectedDesc; |
432 |
|
rsinfo_ncols = tupdesc->natts; |
433 |
|
|
434 |
|
/* |
435 |
|
* The requested tuple description better match up with the array |
436 |
|
* we were given. |
437 |
|
*/ |
438 |
|
if (rsinfo_ncols != ncols) |
439 |
|
ereport(ERROR, |
440 |
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
441 |
|
errmsg("invalid input array"), |
442 |
|
errdetail("Number of elements in array must match number of query specified columns."))); |
443 |
|
|
444 |
|
/* OK, use it */ |
445 |
|
attinmeta = TupleDescGetAttInMetadata(tupdesc); |
446 |
|
|
447 |
|
/* Now go to work */ |
448 |
|
rsinfo->returnMode = SFRM_Materialize; |
449 |
|
|
450 |
|
per_query_ctx = fcinfo->flinfo->fn_mcxt; |
451 |
|
oldcontext = MemoryContextSwitchTo(per_query_ctx); |
452 |
|
|
453 |
|
/* initialize our tuplestore */ |
454 |
|
tupstore = tuplestore_begin_heap(true, false, SortMem); |
455 |
|
|
456 |
|
|
457 |
|
/* take rest of arguments from function */ |
458 |
|
|
459 |
|
/* node URL */ |
460 |
|
if (PG_ARGISNULL(_arg_node_uri)) { |
461 |
|
ereport(ERROR, |
462 |
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
463 |
|
errmsg("node URL can't be null"), |
464 |
|
errdetail("Node URL must be valid URL to HyperEstraier node"))); |
465 |
|
} |
466 |
|
node_url = _textout(PG_GETARG_TEXT_P(_arg_node_uri)); |
467 |
|
|
468 |
|
/* login and password */ |
469 |
|
if (PG_ARGISNULL(_arg_login) || PG_ARGISNULL(_arg_passwd)) { |
470 |
|
ereport(ERROR, |
471 |
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
472 |
|
errmsg("username and password can't be NULL"), |
473 |
|
errdetail("You must specify valid username and password to HyperEstraier node"))); |
474 |
|
} |
475 |
|
user = _textout(PG_GETARG_TEXT_P(_arg_login)); |
476 |
|
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 */ |
486 |
|
if (PG_ARGISNULL(_arg_query)) { |
487 |
|
query = ""; |
488 |
|
} else { |
489 |
|
query = _textout(PG_GETARG_TEXT_P(_arg_query)); |
490 |
|
} |
491 |
|
|
492 |
|
/* atribute filter */ |
493 |
|
if (PG_ARGISNULL(_arg_attr)) { |
494 |
|
attr = ""; |
495 |
|
} else { |
496 |
|
attr = _textout(PG_GETARG_TEXT_P(_arg_attr)); |
497 |
|
} |
498 |
|
|
499 |
|
/* sort order */ |
500 |
|
if (PG_ARGISNULL(_arg_order)) { |
501 |
|
order = ""; |
502 |
|
} else { |
503 |
|
order = _textout(PG_GETARG_TEXT_P(_arg_order)); |
504 |
|
} |
505 |
|
|
506 |
|
|
507 |
|
/* limit */ |
508 |
|
if (PG_ARGISNULL(_arg_limit)) { |
509 |
|
limit = 0; |
510 |
|
} else { |
511 |
|
limit = PG_GETARG_INT32(_arg_limit); |
512 |
|
} |
513 |
|
|
514 |
|
/* offset */ |
515 |
|
if (PG_ARGISNULL(_arg_offset)) { |
516 |
|
offset = 0; |
517 |
|
} else { |
518 |
|
offset = PG_GETARG_INT32(_arg_offset); |
519 |
|
} |
520 |
|
|
521 |
|
/* initialize the network environment */ |
522 |
|
if(!est_init_net_env()){ |
523 |
|
ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED), |
524 |
|
errmsg("pgest_node: can't create network enviroment"))); |
525 |
|
} |
526 |
|
|
527 |
|
/* create the node connection object */ |
528 |
|
elog(DEBUG1, "pgest_node: est_node_new(%s) as %s", node_url, user); |
529 |
|
node = est_node_new(node_url); |
530 |
|
est_node_set_auth(node, user, passwd); |
531 |
|
|
532 |
|
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 */ |
535 |
|
if (!(cond = est_cond_new())) { |
536 |
|
ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED), |
537 |
|
errmsg("pgest_node: est_cond_new failed"))); |
538 |
|
} |
539 |
|
|
540 |
|
/* set the search phrase to the search condition object */ |
541 |
|
if (! PG_ARGISNULL(_arg_query) && strlen(query) > 0) |
542 |
|
est_cond_set_phrase(cond, query); |
543 |
|
|
544 |
|
/* minimum valid attribute length is 10: @a STREQ a */ |
545 |
|
if (! PG_ARGISNULL(_arg_attr) && strlen(attr) >= 10) { |
546 |
|
elog(DEBUG1,"attributes: %s", attr); |
547 |
|
cond_add_attr(cond, attr); |
548 |
|
} |
549 |
|
|
550 |
|
/* set the search phrase to the search condition object */ |
551 |
|
if (! PG_ARGISNULL(_arg_order) && strlen(order) > 0) { |
552 |
|
elog(DEBUG1,"est_cond_set_order(%s)", order); |
553 |
|
est_cond_set_order(cond, order); |
554 |
|
} |
555 |
|
|
556 |
|
if (limit) { |
557 |
|
elog(DEBUG1,"est_cond_set_max(%d)", limit + offset); |
558 |
|
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 */ |
567 |
|
nres = est_node_search(node, cond, depth); |
568 |
|
|
569 |
|
if (! nres) { |
570 |
|
int status = est_node_status(node); |
571 |
|
est_cond_delete(cond); |
572 |
|
est_node_delete(node); |
573 |
|
est_free_net_env(); |
574 |
|
ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED), |
575 |
|
errmsg("pgest_node: search failed, node status %d", status))); |
576 |
|
} |
577 |
|
|
578 |
|
/* get number of results */ |
579 |
|
resnum = est_noderes_doc_num(nres); |
580 |
|
|
581 |
|
/* check if results exists */ |
582 |
|
if ( 0 == resnum ) { |
583 |
|
elog(INFO, "pgest_node: no results for: %s", query ); |
584 |
|
} |
585 |
|
|
586 |
|
/* total number of tuples to be returned */ |
587 |
|
if (limit && limit < resnum) { |
588 |
|
nrows = limit; |
589 |
|
} else { |
590 |
|
nrows = resnum - offset; |
591 |
|
} |
592 |
|
|
593 |
|
elog(DEBUG1, "pgest_node: found %d hits for %s", resnum, query); |
594 |
|
|
595 |
|
|
596 |
|
values = (char **) palloc(ncols * sizeof(char *)); |
597 |
|
|
598 |
|
for (i = 0; i < nrows; i++) |
599 |
|
{ |
600 |
|
|
601 |
|
/* get result from estraier */ |
602 |
|
if (! ( rdoc = est_noderes_get_doc(nres, i) )) { |
603 |
|
elog(INFO, "pgest_node: can't find result %d", i + offset); |
604 |
|
} else { |
605 |
|
elog(DEBUG1, "URI: %s\n Title: %s\n", |
606 |
|
est_resdoc_attr(rdoc, "@uri"), |
607 |
|
est_resdoc_attr(rdoc, "@title") |
608 |
|
); |
609 |
|
} |
610 |
|
|
611 |
|
/* iterate over results */ |
612 |
|
for (j = 0; j < ncols; j++) |
613 |
|
{ |
614 |
|
bool isnull; |
615 |
|
|
616 |
|
/* array value of this position */ |
617 |
|
indx[0] = j + attr_dim_lower_bounds[0]; |
618 |
|
|
619 |
|
dvalue = array_ref(attr_arr, attr_ndims, indx, -1, attr_len, attr_byval, attr_align, &isnull); |
620 |
|
|
621 |
|
if (!isnull && rdoc) |
622 |
|
values[j] = DatumGetCString( |
623 |
|
node_attr2text(rdoc, |
624 |
|
(char *)DirectFunctionCall1(textout, dvalue) |
625 |
|
)); |
626 |
|
else |
627 |
|
values[j] = NULL; |
628 |
|
} |
629 |
|
/* construct the tuple */ |
630 |
|
tuple = BuildTupleFromCStrings(attinmeta, values); |
631 |
|
|
632 |
|
/* now store it */ |
633 |
|
tuplestore_puttuple(tupstore, tuple); |
634 |
|
|
635 |
|
} |
636 |
|
|
637 |
|
tuplestore_donestoring(tupstore); |
638 |
|
rsinfo->setResult = tupstore; |
639 |
|
|
640 |
|
/* |
641 |
|
* SFRM_Materialize mode expects us to return a NULL Datum. The actual |
642 |
|
* tuples are in our tuplestore and passed back through |
643 |
|
* rsinfo->setResult. rsinfo->setDesc is set to the tuple description |
644 |
|
* that we actually used to build our tuples with, so the caller can |
645 |
|
* verify we did what it was expecting. |
646 |
|
*/ |
647 |
|
rsinfo->setDesc = tupdesc; |
648 |
|
MemoryContextSwitchTo(oldcontext); |
649 |
|
|
650 |
|
/* delete the node result object */ |
651 |
|
est_noderes_delete(nres); |
652 |
|
|
653 |
|
/* destroy the search condition object */ |
654 |
|
est_cond_delete(cond); |
655 |
|
|
656 |
|
/* destroy the node object */ |
657 |
|
est_node_delete(node); |
658 |
|
|
659 |
|
/* free the networking environment */ |
660 |
|
est_free_net_env(); |
661 |
|
|
662 |
|
return (Datum) 0; |
663 |
|
} |
664 |
|
|
665 |
|
/* make text var from node attr */ |
666 |
|
char *node_attr2text(ESTRESDOC *rdoc, char *attr) { |
667 |
|
char *val; |
668 |
|
const char *attrval; |
669 |
|
int len; |
670 |
|
int attrlen; |
671 |
|
|
672 |
|
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)) ) { |
677 |
|
val = (char *) palloc(attrlen * sizeof(char)); |
678 |
|
} else { |
679 |
|
return (Datum) NULL; |
680 |
|
} |
681 |
|
|
682 |
|
len = strlen(attrval); |
683 |
|
elog(DEBUG1, "node_attr2text(%s) = '%s' %d bytes", attr, attrval, len); |
684 |
|
|
685 |
|
len++; |
686 |
|
len *= sizeof(char); |
687 |
|
|
688 |
|
elog(DEBUG2, "palloc(%d)", len); |
689 |
|
|
690 |
|
val = palloc(len); |
691 |
|
|
692 |
|
memset(val, 0, len); |
693 |
|
strncpy(val, attrval, len); |
694 |
|
|
695 |
|
elog(DEBUG2, "val=%s", val); |
696 |
|
|
697 |
|
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 |
|
} |