/[pgestraier]/trunk/pgest.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/pgest.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 57 by dpavlin, Thu May 11 16:19:38 2006 UTC revision 80 by dpavlin, Tue Aug 8 12:15:55 2006 UTC
# Line 27  Line 27 
27  #include "utils/array.h"  #include "utils/array.h"
28  #include "utils/lsyscache.h"  #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>
# Line 589  Datum pgest_node(PG_FUNCTION_ARGS) Line 592  Datum pgest_node(PG_FUNCTION_ARGS)
592          if (limit && limit < resnum) {          if (limit && limit < resnum) {
593                  nrows = limit;                  nrows = limit;
594          } else {          } else {
595                  nrows = resnum - offset;                  nrows = resnum;
596          }          }
597    
598          /* get hints */          /* get hints */
# Line 631  Datum pgest_node(PG_FUNCTION_ARGS) Line 634  Datum pgest_node(PG_FUNCTION_ARGS)
634                                  /* skip HINTS. prefix */                                  /* skip HINTS. prefix */
635                                  hint += strlen(HINTS_PREFIX);                                  hint += strlen(HINTS_PREFIX);
636    
637                                  hint_val = cbmapget(hints, hint, -1, NULL);                                  hint_val = (char *)cbmapget(hints, hint, -1, NULL);
638                                  elog(DEBUG2, "hint %s = %s", hint, hint_val);                                  elog(DEBUG2, "hint %s = %s", hint, hint_val);
639    
640                                  if (hint_val != NULL) {                                  if (hint_val != NULL) {
# Line 721  void cond_add_attr(ESTCOND *cond, char * Line 724  void cond_add_attr(ESTCOND *cond, char *
724          char *next;          char *next;
725          char *curr_attr;          char *curr_attr;
726          while ( strlen(attr) > 0 ) {          while ( strlen(attr) > 0 ) {
727                  printf("len [%s] = %d\n", attr, strlen(attr));                  printf("len [%s] = %zd\n", attr, strlen(attr));
728                  if ((next = strstr(attr, ATTR_DELIMITER)) != NULL) {                  if ((next = strstr(attr, ATTR_DELIMITER)) != NULL) {
729                          curr_attr = palloc( next - attr + 1 );                          curr_attr = palloc( next - attr + 1 );
730                          memcpy(curr_attr, attr, next-attr);                          memcpy(curr_attr, attr, next-attr);
# Line 736  void cond_add_attr(ESTCOND *cond, char * Line 739  void cond_add_attr(ESTCOND *cond, char *
739                  attr = next;                  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            int   edit_doc = 0;
772    
773            ESTNODE *node;
774            ESTDOC *doc;
775    
776    
777    
778            if (! CALLED_AS_TRIGGER(fcinfo)) {
779                    elog(ERROR, "pgest_trigger() must be called as a trigger");
780            }
781    
782            data = (TriggerData *) fcinfo->context;
783    
784            if (data->tg_trigger->tgnargs < 5)
785                    elog(ERROR, "pgest_trigger() requires at least 5 parameters ('http://localhost:1978/node/trivia', 'user', 'passwd', 'pk_column', 'column', ... )");
786    
787            args       = data->tg_trigger->tgargs;
788            keycol     = args[3];
789    
790            tupdesc = data->tg_relation->rd_att;
791    
792            knumber = SPI_fnumber(tupdesc, keycol);
793            key = SPI_getvalue(data->tg_trigtuple, tupdesc, knumber);
794    
795    
796            /* initialize the network environment */
797            if( ! est_init_net_env() )
798                    elog(ERROR, "pgest_trigger: network is unavailable\n");
799    
800            /* create and configure the node connection object */
801            node = est_node_new( args[0] );
802            est_node_set_auth(node, args[1], args[2]);
803    
804    
805            if (TRIGGER_FIRED_BY_INSERT(data->tg_event)) {
806                    /* There is no old data */
807                    ret = data->tg_trigtuple;
808    
809                    create_doc++;
810    
811            } else if (TRIGGER_FIRED_BY_UPDATE(data->tg_event)) {
812                    ret = data->tg_newtuple;
813    
814                    edit_doc++;
815    
816            } else if (TRIGGER_FIRED_BY_DELETE(data->tg_event)) {
817                    /* There is no new data */
818                    ret = data->tg_trigtuple;
819    
820                    if (! est_node_out_doc_by_uri(node, key) )
821                            elog(ERROR, "est_node_doc_by_uri(%s): %d\n", key, est_node_status(node));
822    
823            } else {
824                    elog(ERROR, "pgest_trigger() not called from INSERT/UPDATE/DELETE");
825            }
826    
827            if ( create_doc || edit_doc ) {
828    
829                    if ( create_doc ) {
830                            /* create a document object */
831                            doc = est_doc_new();
832                            est_doc_add_attr(doc, "@uri", key);
833    
834                            elog(DEBUG1, "est_doc_new @uri=%s", key);
835                    } else {
836                            /* edit existing document */
837                            doc = est_node_get_doc_by_uri(node, key);
838                            if (doc == NULL)
839                                    elog(ERROR, "est_node_get_doc_by_uri(%s): %d\n", key, est_node_status(node));
840                            elog(DEBUG1, "est_node_get_doc_by_uri(%s)", key);
841                    }
842    
843                    for( i = 4; i < data->tg_trigger->tgnargs; i++ ) {
844    
845                            col_data = SPI_getvalue(ret, tupdesc, SPI_fnumber(tupdesc, args[i]));
846    
847                            if (data) {
848                                    elog(DEBUG1, " + %s = %s", args[i], col_data);
849                                    est_doc_add_attr(doc, args[i], col_data);
850                                    est_doc_add_text(doc, col_data);
851                            }
852    
853                    }
854    
855                    if ( edit_doc ) {
856                            /* update existing document */
857                            if( ! est_node_edit_doc(node, doc) )
858                                    elog(ERROR, "est_node_edit_doc: %d\n", est_node_status(node));
859                    } else {
860                            /* register the document object to the node */
861                            if( ! est_node_put_doc(node, doc) )
862                                    elog(ERROR, "est_node_put_doc: %d\n", est_node_status(node));
863                    }
864    
865                    /* destroy the document object */
866                    est_doc_delete(doc);
867    
868            }
869    
870            /* destroy the node object */
871            est_node_delete(node);
872            /* free the networking environment */
873            est_free_net_env();
874    
875    
876            return PointerGetDatum(ret);
877    }
878    

Legend:
Removed from v.57  
changed lines
  Added in v.80

  ViewVC Help
Powered by ViewVC 1.1.26