home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / iutil / batch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-01-23  |  5.9 KB  |  236 lines

  1. # include    <ingres.h>
  2. # include    <aux.h>
  3. # include    <catalog.h>
  4. # include    <symbol.h>
  5. # include    <access.h>
  6. # include    <batch.h>
  7. # include    <btree.h>
  8. # include    <sccs.h>
  9.  
  10. SCCSID(@(#)batch.c    8.2    1/17/85)
  11.  
  12. /*
  13. **    Open batch prepares for batch processing.
  14. **    1. If the batch is already open, return an error
  15. **    2. Create the batch file.
  16. **    3. clear domain flags.
  17. **    4. If the relation is indexed, Identify all the domains
  18. **        which must be saved to speed the index update.
  19. **    5. Set up specifics for each type of update.
  20. **    6. Write out the batch structure.
  21. **
  22. **    The following itemizes what is saved (in bytes):
  23. **    f(si) means it's a function of the secondary index keys
  24. **    space for newtid is saved only if there is a sec. index.
  25. **
  26. **            mdDEL    mdREPL    mdAPP
  27. **
  28. **    oldtid        4    4    0
  29. **    oldtuple    f(si)    f(si)    0
  30. **    newtuple    0    tupwid    tupwid
  31. **    newtid        0    4    4
  32. */
  33.  
  34. openbatch(rel_desc, index_desc, mode)
  35. DESC    *rel_desc, *index_desc;
  36. int    mode;
  37. {
  38.     register struct si_doms    *sp;
  39.     register DESC        *rel, *indx;
  40.     int            i, j, saveoff, dom;
  41.     char            *p, *batchname();
  42.     TID            lotid, hitid;
  43.     struct index        itup;
  44.     extern char        *Database;
  45.  
  46.     if (Batchhd.mode_up)
  47.         return (-1);    /* batch already open */
  48.     rel = rel_desc;
  49.     indx = index_desc;
  50.     p = batchname();    /* form batch name */
  51. #    ifdef xATR1
  52.     if (tTf(25, -1))
  53.         printf("Openbatch %s\n", p);
  54. #    endif
  55.     if ((Batch_fp = creat(p, FILEMODE)) < 0)
  56.         syserr("openbatch:can't creat %s,%d", p, Batch_fp);
  57.     Batch_cnt = 0;
  58.  
  59.     /* copy the important info */
  60.     smove(Fileset, Batchbuf.file_id);
  61.     smove(Database, Batchhd.db_name);
  62.     bmove(rel->reldum.relid, Batchhd.rel_name, MAXNAME);
  63.     bmove(rel->reldum.relowner, Batchhd.userid, 2);
  64.     Batchhd.mode_up = mode;
  65.     Batchhd.num_updts = 0;
  66.  
  67.     /* clear out the secondary index domain flags */
  68.     sp = Batchhd.si;    /* sp points to the structure */
  69.     for (i = 1; i <= MAXDOM; i++)
  70.     {
  71.         sp->dom_size = 0;
  72.         sp++;
  73.     }
  74.     Batchhd.si_dcount = 0;
  75.  
  76.     /* set up the tid and tuple sizes by type of update */
  77.     /* assume size of tido, tidn, and tupn */
  78.     Batchhd.tido_size = 4;    /* assume old tid is needed */
  79.     Batchhd.tupo_size = 0;    /* assume old tuple isn't needed */
  80.     Batchhd.tupn_size = rel->reldum.relwid;    /* assume new tuple is needed */
  81.     Batchhd.tidn_size = 4;    /* assume space is needed for new tid */
  82.     switch(Batchhd.mode_up)
  83.     {
  84.  
  85.       case mdDEL:
  86.         Batchhd.tupn_size = 0;    /* new tuple isn't needed */
  87.         Batchhd.tidn_size = 0;    /* new tid isn't needed */
  88.  
  89.       case mdREPL:
  90.         break;
  91.  
  92.       case mdAPP:
  93.         Batchhd.tido_size = 0;    /* old tid isn't needed */
  94.         break;
  95.  
  96.       default:
  97.         syserr("openbatch:mode %d", Batchhd.mode_up);
  98.     }
  99.     /* if there are no secondary indexes then tipn isn't needed, unless 
  100.     ** relation is ordered
  101.     */
  102.     if (rel->reldum.relindxd <= 0 && rel->reldum.reldim <= 0)
  103.         Batchhd.tidn_size = 0;
  104.  
  105.     /* if this relation has a secondary index or an ordered relation,
  106.     ** figure out what to save 
  107.     */
  108.     if ((rel->reldum.relindxd > 0 || rel->reldum.reldim > 0) && mode != mdAPP)
  109.     {
  110.         if (rel->reldum.relindxd > 0)
  111.         {
  112.             setkey(indx, (char *) &itup, rel->reldum.relid, IRELIDP);
  113.             setkey(indx, (char *) &itup, rel->reldum.relowner, IOWNERP);
  114.     
  115.             if (find(indx, EXACTKEY, &lotid, &hitid, (char *) &itup))
  116.                 syserr("openbatch:bad find %.12s", rel);
  117.     
  118.             /* check each entry in "index" relation for useful index */
  119.             while(!(i = get(indx, &lotid, &hitid, (char *) &itup, TRUE)))
  120.             {
  121.                 if (!bequal(itup.irelidp, rel->reldum.relid, MAXNAME) ||
  122.                     !bequal(itup.iownerp, rel->reldum.relowner, 2))
  123.                     continue;
  124.                 /* found one. copy the used domains */
  125.                 p = itup.idom;        /* get address of first */
  126.                 i = 6;
  127.                 while (i--)
  128.                 {
  129.                     if ((dom = *p++) == 0)
  130.                         break;    /* no more domains */
  131.                     sp = &Batchhd.si[dom];
  132.                     if (sp->dom_size != 0)
  133.                         continue;    /* domain has already been done once */
  134.                     Batchhd.si_dcount++;
  135.                     Batchhd.tupo_size += rel->relfrml[dom] & I1MASK;
  136.                     sp->rel_off = rel->reloff[dom];
  137.                     sp->dom_size = rel->relfrml[dom] & I1MASK;
  138.                 }
  139.             }
  140.         }
  141.         for (j = rel->reldum.reldim - 1; j >= 0; --j)
  142.         {
  143.             /* point to lid field */
  144.             sp = &Batchhd.si[rel->reldum.relatts - j];
  145.             Batchhd.si_dcount++;
  146.             Batchhd.tupo_size += LIDSIZE;
  147.             sp->rel_off = rel->reloff[rel->reldum.relatts - j];
  148.             sp->dom_size = LIDSIZE;
  149.         }
  150.         if (i < 0)
  151.             syserr("openbatch:bad get index %d", i);
  152.         /* compute offsets of domains in saved "oldtuple" */
  153.         saveoff = 0;
  154.         sp = Batchhd.si;
  155.         i = Batchhd.si_dcount;
  156.         while (i--)
  157.         {
  158.             /* skip to next domain */
  159.             while (sp->dom_size == 0)
  160.                 sp++;
  161.             sp->tupo_off = saveoff;
  162.             saveoff += sp->dom_size;
  163.             sp++;
  164.         }
  165.     }
  166.     wrbatch((char *) &Batchhd, sizeof Batchhd);
  167.     return (0);
  168. }
  169. /*
  170. **  ADDBATCH -- add to batch file
  171. */
  172.  
  173. addbatch(oldtid, newtuple, oldtuple)
  174. TID    *oldtid;
  175. char    *newtuple, *oldtuple;
  176. {
  177.     TID            newtid;
  178.     register int        i;
  179.     register struct si_doms    *sp;
  180.     register char        *old;
  181.  
  182. #    ifdef xATR1
  183.     if (tTf(25,3))
  184.         printf("addbatch\n");
  185. #    endif
  186.     if (Batchhd.mode_up == 0)
  187.         return (-1);
  188.     Batchhd.num_updts++;    /* increment the number of add batches */
  189.     old = oldtuple;
  190.     /* write out the old tid */
  191.     wrbatch((char *) oldtid, Batchhd.tido_size);
  192.  
  193.     /* write out each of the old tuple domains */
  194.     i = Batchhd.si_dcount;    /* i get the number of domains */
  195.     sp = Batchhd.si;    /* sp points to the domain structures */
  196.  
  197.     while (i--)
  198.     {
  199.         /* skip to the next domain */
  200.         while (sp->dom_size == 0)
  201.             sp++;
  202.  
  203.         wrbatch(&old[sp->rel_off], sp->dom_size);
  204.         sp++;
  205.     }
  206.  
  207.     /* write out the new tuple */
  208.     wrbatch(newtuple, Batchhd.tupn_size);
  209.  
  210.     /* reserve space for the new tid. Init to -1 */
  211.     *((long *) &newtid) = -1;
  212.     wrbatch((char *) &newtid, Batchhd.tidn_size);
  213.     return (0);
  214. }
  215. /*
  216. **  CLOSEBATCH -- close batch file
  217. */
  218.  
  219. closebatch()
  220. {
  221.     register int    i;
  222.     extern long    lseek();
  223.  
  224.     if (Batchhd.mode_up == 0)
  225.         return (-1);
  226.     flushbatch();    /* write out any remainder */
  227.     if ((i = lseek(Batch_fp, 0l, 0)) == -1)
  228.         syserr("closebatch:seek %d", i);
  229.     wrbatch((char *) &Batchhd, sizeof Batchhd);    /* update num_updts */
  230.     flushbatch();
  231.     if (i = close(Batch_fp))
  232.         syserr("closebatch:close %d", i);
  233.     Batchhd.mode_up = 0;
  234.     return (0);
  235. }
  236.