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