/[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 56 by dpavlin, Thu May 11 15:52:50 2006 UTC revision 61 by dpavlin, Mon Aug 7 11:14:52 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 42  Line 45 
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);
# Line 394  Datum pgest_node(PG_FUNCTION_ARGS) Line 398  Datum pgest_node(PG_FUNCTION_ARGS)
398          ESTCOND *cond;          ESTCOND *cond;
399          ESTNODERES *nres;          ESTNODERES *nres;
400          ESTRESDOC *rdoc;          ESTRESDOC *rdoc;
401            CBMAP *hints;
402          int resnum = 0;          int resnum = 0;
403          int limit = 0;          int limit = 0;
404          int offset = 0;          int offset = 0;
# Line 590  Datum pgest_node(PG_FUNCTION_ARGS) Line 595  Datum pgest_node(PG_FUNCTION_ARGS)
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    
# Line 612  Datum pgest_node(PG_FUNCTION_ARGS) Line 620  Datum pgest_node(PG_FUNCTION_ARGS)
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 = 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                  }                  }
# Line 717  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    
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    

Legend:
Removed from v.56  
changed lines
  Added in v.61

  ViewVC Help
Powered by ViewVC 1.1.26