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 "commands/trigger.h" |
31 |
|
#include "executor/spi.h" |
32 |
|
|
33 |
#include <estraier.h> |
#include <estraier.h> |
34 |
#include <cabin.h> |
#include <cabin.h> |
35 |
#include <estnode.h> |
#include <estnode.h> |
45 |
#endif |
#endif |
46 |
|
|
47 |
#define ATTR_DELIMITER "{{!}}" |
#define ATTR_DELIMITER "{{!}}" |
48 |
|
#define HINTS_PREFIX "HINTS." |
49 |
|
|
50 |
/* prototype */ |
/* prototype */ |
51 |
char *attr2text(ESTDOC *doc, char *attr); |
char *attr2text(ESTDOC *doc, char *attr); |
52 |
char *node_attr2text(ESTRESDOC *rdoc, char *attr); |
char *node_attr2text(ESTRESDOC *rdoc, char *attr); |
53 |
|
void cond_add_attr(ESTCOND *cond, char *attr); |
54 |
|
|
55 |
|
|
56 |
/* work in progress */ |
/* work in progress */ |
83 |
ESTDB *db; |
ESTDB *db; |
84 |
ESTCOND *cond; |
ESTCOND *cond; |
85 |
ESTDOC *doc; |
ESTDOC *doc; |
|
const CBLIST *texts; |
|
86 |
int ecode, *est_result, resnum; |
int ecode, *est_result, resnum; |
87 |
int limit = 0; |
int limit = 0; |
88 |
int offset = 0; |
int offset = 0; |
213 |
/* minimum valid attribute length is 10: @a STREQ a */ |
/* minimum valid attribute length is 10: @a STREQ a */ |
214 |
if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { |
if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { |
215 |
elog(DEBUG1,"attributes: %s", attr); |
elog(DEBUG1,"attributes: %s", attr); |
216 |
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); |
|
|
} |
|
217 |
} |
} |
218 |
|
|
219 |
/* set the search phrase to the search condition object */ |
/* set the search phrase to the search condition object */ |
322 |
|
|
323 |
if (! doc) return (Datum) NULL; |
if (! doc) return (Datum) NULL; |
324 |
|
|
325 |
elog(DEBUG1, "doc: %08x, attr: %s", doc, attr); |
elog(DEBUG1, "doc: %p, attr: %s", doc, attr); |
326 |
|
|
327 |
if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) { |
if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) { |
328 |
val = (char *) palloc(attrlen * sizeof(char)); |
val = (char *) palloc(attrlen * sizeof(char)); |
398 |
ESTCOND *cond; |
ESTCOND *cond; |
399 |
ESTNODERES *nres; |
ESTNODERES *nres; |
400 |
ESTRESDOC *rdoc; |
ESTRESDOC *rdoc; |
401 |
const CBLIST *texts; |
CBMAP *hints; |
402 |
int resnum = 0; |
int resnum = 0; |
403 |
int limit = 0; |
int limit = 0; |
404 |
int offset = 0; |
int offset = 0; |
549 |
/* minimum valid attribute length is 10: @a STREQ a */ |
/* minimum valid attribute length is 10: @a STREQ a */ |
550 |
if (! PG_ARGISNULL(_arg_attr) && strlen(attr) >= 10) { |
if (! PG_ARGISNULL(_arg_attr) && strlen(attr) >= 10) { |
551 |
elog(DEBUG1,"attributes: %s", attr); |
elog(DEBUG1,"attributes: %s", attr); |
552 |
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); |
|
|
} |
|
553 |
} |
} |
554 |
|
|
555 |
/* set the search phrase to the search condition object */ |
/* set the search phrase to the search condition object */ |
595 |
nrows = resnum - offset; |
nrows = resnum - offset; |
596 |
} |
} |
597 |
|
|
598 |
|
/* get hints */ |
599 |
|
hints = est_noderes_hints(nres); |
600 |
|
|
601 |
elog(DEBUG1, "pgest_node: found %d hits for %s", resnum, query); |
elog(DEBUG1, "pgest_node: found %d hits for %s", resnum, query); |
602 |
|
|
603 |
|
|
620 |
for (j = 0; j < ncols; j++) |
for (j = 0; j < ncols; j++) |
621 |
{ |
{ |
622 |
bool isnull; |
bool isnull; |
623 |
|
char *attr; /* current attribute name */ |
624 |
|
char *hint; /* position of current hint in attribute */ |
625 |
|
char *hint_val; |
626 |
|
|
627 |
/* array value of this position */ |
/* array value of this position */ |
628 |
indx[0] = j + attr_dim_lower_bounds[0]; |
indx[0] = j + attr_dim_lower_bounds[0]; |
629 |
|
|
630 |
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); |
631 |
|
attr = (char *)DirectFunctionCall1(textout, dvalue); |
632 |
|
|
633 |
if (!isnull && rdoc) |
if (!isnull && (hint = strstr(attr, HINTS_PREFIX)) != NULL) { |
634 |
values[j] = DatumGetCString( |
/* skip HINTS. prefix */ |
635 |
node_attr2text(rdoc, |
hint += strlen(HINTS_PREFIX); |
636 |
(char *)DirectFunctionCall1(textout, dvalue) |
|
637 |
)); |
hint_val = (char *)cbmapget(hints, hint, -1, NULL); |
638 |
|
elog(DEBUG2, "hint %s = %s", hint, hint_val); |
639 |
|
|
640 |
|
if (hint_val != NULL) { |
641 |
|
values[j] = DatumGetCString( hint_val ); |
642 |
|
} else { |
643 |
|
elog(INFO, "can't get hint in results: %s", hint); |
644 |
|
values[j] = NULL; |
645 |
|
} |
646 |
|
} else if (!isnull && rdoc) |
647 |
|
values[j] = DatumGetCString( node_attr2text(rdoc, attr) ); |
648 |
else |
else |
649 |
values[j] = NULL; |
values[j] = NULL; |
650 |
} |
} |
693 |
|
|
694 |
if (! rdoc) return (Datum) NULL; |
if (! rdoc) return (Datum) NULL; |
695 |
|
|
696 |
elog(DEBUG1, "doc: %08x, attr: %s", rdoc, attr); |
elog(DEBUG1, "doc: %p, attr: %s", rdoc, attr); |
697 |
|
|
698 |
if ( (attrval = est_resdoc_attr(rdoc, attr)) && (attrlen = strlen(attrval)) ) { |
if ( (attrval = est_resdoc_attr(rdoc, attr)) && (attrlen = strlen(attrval)) ) { |
699 |
val = (char *) palloc(attrlen * sizeof(char)); |
val = (char *) palloc(attrlen * sizeof(char)); |
719 |
return val; |
return val; |
720 |
} |
} |
721 |
|
|
722 |
|
/* parse attributes and add them to confition */ |
723 |
|
void cond_add_attr(ESTCOND *cond, char *attr) { |
724 |
|
char *next; |
725 |
|
char *curr_attr; |
726 |
|
while ( strlen(attr) > 0 ) { |
727 |
|
printf("len [%s] = %d\n", attr, strlen(attr)); |
728 |
|
if ((next = strstr(attr, ATTR_DELIMITER)) != NULL) { |
729 |
|
curr_attr = palloc( next - attr + 1 ); |
730 |
|
memcpy(curr_attr, attr, next-attr); |
731 |
|
curr_attr[next-attr] = '\0'; |
732 |
|
next += strlen(ATTR_DELIMITER); |
733 |
|
} else { |
734 |
|
next = ""; |
735 |
|
curr_attr = attr; |
736 |
|
} |
737 |
|
elog(DEBUG1, "est_cond_add_attr(%s)", curr_attr); |
738 |
|
est_cond_add_attr(cond, curr_attr); |
739 |
|
attr = next; |
740 |
|
} |
741 |
|
} |
742 |
|
|
743 |
|
/* trigger to keep data in Hyper Estraier index up-to-date */ |
744 |
|
/* CREATE FUNCTION pgest_trigger() RETURNS TRIGGER AS ... */ |
745 |
|
|
746 |
|
/* |
747 |
|
* UPDATE, INSERT and DELETE triggers are like this: |
748 |
|
|
749 |
|
CREATE TRIGGER pgest_trigger_update AFTER UPDATE |
750 |
|
ON table_name FOR EACH ROW |
751 |
|
EXECUTE PROCEDURE pgest_trigger('http://localhost:1978/node/trivia','admin','admin', |
752 |
|
'name_of_pk', 'column', 'another_column', 'and_so_on' |
753 |
|
) |
754 |
|
|
755 |
|
*/ |
756 |
|
|
757 |
|
PG_FUNCTION_INFO_V1(pgest_trigger); |
758 |
|
Datum pgest_trigger(PG_FUNCTION_ARGS) { |
759 |
|
|
760 |
|
TriggerData *data; |
761 |
|
TupleDesc tupdesc; |
762 |
|
HeapTuple ret; |
763 |
|
|
764 |
|
char **args; |
765 |
|
char *keycol = NULL; |
766 |
|
char *key = NULL; |
767 |
|
char *col_data = NULL; |
768 |
|
int knumber; |
769 |
|
int i; |
770 |
|
int create_doc = 0; |
771 |
|
|
772 |
|
ESTNODE *node; |
773 |
|
ESTDOC *doc; |
774 |
|
|
775 |
|
|
776 |
|
|
777 |
|
if (! CALLED_AS_TRIGGER(fcinfo)) { |
778 |
|
elog(ERROR, "pgest_trigger() must be called as a trigger"); |
779 |
|
} |
780 |
|
|
781 |
|
data = (TriggerData *) fcinfo->context; |
782 |
|
|
783 |
|
if (data->tg_trigger->tgnargs < 5) |
784 |
|
elog(ERROR, "pgest_trigger() requires at least 5 parameters ('http://localhost:1978/node/trivia', 'user', 'passwd', 'pk_column', 'column', ... )"); |
785 |
|
|
786 |
|
args = data->tg_trigger->tgargs; |
787 |
|
keycol = args[3]; |
788 |
|
|
789 |
|
tupdesc = data->tg_relation->rd_att; |
790 |
|
|
791 |
|
knumber = SPI_fnumber(tupdesc, keycol); |
792 |
|
key = SPI_getvalue(data->tg_trigtuple, tupdesc, knumber); |
793 |
|
|
794 |
|
|
795 |
|
/* initialize the network environment */ |
796 |
|
if( ! est_init_net_env() ) |
797 |
|
elog(ERROR, "pgest_trigger: network is unavailable\n"); |
798 |
|
|
799 |
|
/* create and configure the node connection object */ |
800 |
|
node = est_node_new( args[0] ); |
801 |
|
est_node_set_auth(node, args[1], args[2]); |
802 |
|
|
803 |
|
|
804 |
|
if (TRIGGER_FIRED_BY_INSERT(data->tg_event)) { |
805 |
|
/* There is no old data */ |
806 |
|
ret = data->tg_trigtuple; |
807 |
|
|
808 |
|
create_doc++; |
809 |
|
|
810 |
|
} else if (TRIGGER_FIRED_BY_UPDATE(data->tg_event)) { |
811 |
|
ret = data->tg_newtuple; |
812 |
|
|
813 |
|
create_doc++; |
814 |
|
|
815 |
|
} else if (TRIGGER_FIRED_BY_DELETE(data->tg_event)) { |
816 |
|
/* There is no new data */ |
817 |
|
ret = data->tg_trigtuple; |
818 |
|
|
819 |
|
if (! est_node_out_doc_by_uri(node, key) ) |
820 |
|
elog(ERROR, "est_node_doc_by_uri(%s): %d\n", key, est_node_status(node)); |
821 |
|
|
822 |
|
} else { |
823 |
|
elog(ERROR, "pgest_trigger() not called from INSERT/UPDATE/DELETE"); |
824 |
|
} |
825 |
|
|
826 |
|
if ( create_doc ) { |
827 |
|
|
828 |
|
/* create a document object */ |
829 |
|
doc = est_doc_new(); |
830 |
|
est_doc_add_attr(doc, "@uri", key); |
831 |
|
|
832 |
|
elog(DEBUG1, "est_doc_new @uri=%s", key); |
833 |
|
|
834 |
|
for( i = 4; i < data->tg_trigger->tgnargs; i++ ) { |
835 |
|
|
836 |
|
col_data = SPI_getvalue(ret, tupdesc, SPI_fnumber(tupdesc, args[i])); |
837 |
|
|
838 |
|
if (data) { |
839 |
|
elog(DEBUG1, " + %s = %s", args[i], col_data); |
840 |
|
est_doc_add_attr(doc, args[i], col_data); |
841 |
|
est_doc_add_text(doc, col_data); |
842 |
|
} |
843 |
|
|
844 |
|
} |
845 |
|
|
846 |
|
/* register the document object to the node */ |
847 |
|
if( ! est_node_put_doc(node, doc) ) |
848 |
|
elog(ERROR, "est_node_put_doc: %d\n", est_node_status(node)); |
849 |
|
|
850 |
|
/* destroy the document object */ |
851 |
|
est_doc_delete(doc); |
852 |
|
|
853 |
|
} |
854 |
|
|
855 |
|
/* destroy the node object */ |
856 |
|
est_node_delete(node); |
857 |
|
/* free the networking environment */ |
858 |
|
est_free_net_env(); |
859 |
|
|
860 |
|
|
861 |
|
return PointerGetDatum(ret); |
862 |
|
} |
863 |
|
|