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