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

Defined functions

addbatch defined in line 152; used 1 times
Last modified: 1995-02-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2950
Valid CSS Valid XHTML 1.0 Strict