|
|
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 <btree.h>
8: # include <sccs.h>
9:
10: SCCSID(@(#)btreeupdate.c 8.3 1/18/85)
11:
12: btreeupdate(r)
13: register DESC *r;
14: {
15: register char *p;
16: register int i;
17: int j, domcnt, mode, dom;
18: long oldtid, newtid;
19: long tupcnt;
20: long uptid;
21: char oldtup[MAXTUP], newtup[MAXTUP];
22: char oldkey[2 * LIDSIZE], newkey[2 * LIDSIZE];
23: char dumtup[2 * LIDSIZE];
24: struct relation rkey, rtup;
25: DESC b_desc;
26: extern DESC Reldes;
27: TID tid, btid;
28: char file[MAXNAME + 4], btree[MAXNAME + 4];
29: long oldlid[MAXLID], newlid[MAXLID];
30: struct locator tidpos;
31: long l;
32: extern int Btree_fd;
33: char *tp;
34: long page, t;
35: int lidwid;
36: int dellevel;
37: int compare;
38:
39: # ifdef xZTR1
40: if (tTf(47, 0))
41: printf("BTREEUPDATE\n");
42: # endif
43:
44: mode = Batchhd.mode_up;
45: Batch_dirty = FALSE;
46: opencatalog("relation", OR_READ);
47: capital(trim_relname(r->reldum.relid), file);
48: setkey(&Reldes, &rkey, file, RELID);
49: setkey(&Reldes, &rkey, r->reldum.relowner, RELOWNER);
50:
51: if (!getequal(&Reldes, &rkey, &rtup, &tid))
52: {
53: if (i = openr(&b_desc, OR_WRITE, file))
54: syserr("btreeupdate:can't openr %.12s %d", file, i);
55: /* reposition batch file to the beginning. */
56: if ((i = lseek(Batch_fp, 0L, 0)) < 0)
57: syserr("secupdate:seek %d %d", i, Batch_fp);
58: Batch_cnt = BATCHSIZE;
59: getbatch(&Batchhd, sizeof Batchhd); /* reread header */
60:
61: ingresname(r->reldum.relid, r->reldum.relowner, file);
62: btreename(file, btree);
63: if ((Btree_fd = open(btree, O_RDWR)) < 0)
64: syserr("btreeupdate: can't open %s", btree);
65:
66: /*
67: ** Start reading the batch file and updating
68: ** the secondary indexes.
69: */
70: l = r->reladds;
71: tupcnt = Batchhd.num_updts;
72: lidwid = LIDSIZE * r->reldum.reldim;
73: dellevel = r->reldum.reldim - 1;
74: for (j = 0; j < r->reldum.reldim; ++j)
75: {
76: if (Repl_cnt[j] > 0)
77: {
78: dellevel = j;
79: break;
80: }
81: }
82: while (tupcnt--)
83: {
84: getbatch(&oldtid, Batchhd.tido_size);
85: getbatch(oldtup, Batchhd.tupo_size);
86: getbatch(newtup, Batchhd.tupn_size);
87: getbatch(&newtid, Batchhd.tidn_size);
88:
89: clearkeys(&b_desc);
90: /* if this is a replace or append form the new key */
91: if (mode != mdDEL)
92: {
93: if (newtid < 0)
94: continue;
95: tp = newtup + Batchhd.tupn_size - lidwid;
96: bmove(tp, newlid, lidwid);
97: if (mode == mdREPL)
98: {
99: if (newlid[r->reldum.reldim - 1] < 0)
100: continue;
101: tp = oldtup + Batchhd.tupo_size - lidwid;
102: bmove(tp, oldlid, lidwid);
103: compare = 0;
104: for (j = 0; j < r->reldum.reldim; ++j)
105: {
106: if (newlid[j] > oldlid[j])
107: {
108: compare = 1;
109: }
110: if (newlid[j] != oldlid[j])
111: break;
112: }
113: if (compare == 1)
114: {
115: for (j = dellevel - 1; j >= 0; --j)
116: {
117: if (newlid[j] != oldlid[j])
118: {
119: compare = 0;
120: break;
121: }
122: }
123: }
124: if (compare == 1)
125: /* adjust due to deleted lids */
126: newlid[dellevel] -= Repl_cnt[dellevel];
127: }
128: page = RT;
129: for (j = 0; j < r->reldum.reldim; ++j)
130: {
131: if (!newlid[j])
132: newlid[j] = 1;
133: t = get_tid(page, newlid[j], &tidpos);
134: page = t;
135: }
136: if (page != newtid)
137: {
138: /* try linear search of btree */
139: lin_search(r->reldum.reldim, newtid, &btid, newlid, Batchhd.num_updts);
140: setkey(&b_desc, newkey, &newtid, 1);
141: }
142: else
143: {
144: setkey(&b_desc, newkey, &newtid, 1);
145: stuff_page(&btid, &tidpos.pageno);
146: btid.line_id = tidpos.page.node.leafnode.tid_loc[tidpos.offset];
147: }
148: setkey(&b_desc, newkey, &btid, 2);
149: # ifdef xZTR1
150: if(tTf(47,0))
151: {
152: printf("new key\n");
153: printup(&b_desc, newkey);
154: }
155: # endif
156: }
157:
158: /* if this is delete or replace form the old key */
159: if (mode != mdAPP)
160: {
161: setkey(&b_desc, oldkey, &oldtid, 1);
162: # ifdef xZTR1
163: if(tTf(47,0))
164: {
165: printf("old key\n");
166: printup(&b_desc, oldkey);
167: }
168: # endif
169: }
170:
171: switch (mode)
172: {
173:
174: case mdDEL:
175: if (i = getequal(&b_desc, oldkey, dumtup, &uptid))
176: {
177: if (i > 0)
178: break;
179: syserr("btreeupdate:getequal %d", i);
180: }
181: if ((i = delete(&b_desc, &uptid)) < 0)
182: syserr("btreeupdate:delete %d", i);
183: break;
184:
185: case mdREPL:
186: /* btree tid not provided */
187: b_desc.relgiven[2] = 0;
188: if (i = getequal(&b_desc, oldkey, dumtup, &uptid))
189: {
190: if (Batch_recovery && i > 0)
191: goto btreeinsert;
192: printup(&b_desc, oldkey);
193: syserr("btreeupdate:getequal-repl %d", i);
194: }
195: /* btree tid provided */
196: b_desc.relgiven[2] = 1;
197: if (i = replace(&b_desc, &uptid, newkey, TRUE))
198: {
199: /* if newtuple is dup of old, ok */
200: if (i == 1)
201: break;
202: /* if this is recovery and old tid not there, try an insert */
203: if (Batch_recovery && i == 2)
204: goto btreeinsert;
205: syserr("secupdate:replace %d", i);
206: }
207: break;
208:
209: case mdAPP:
210: btreeinsert:
211: if ((i = insert(&b_desc, &uptid, newkey, TRUE)) < 0)
212: syserr("secupdate:insert %d", i);
213: }
214: }
215: if (i = closer(&b_desc))
216: syserr("btreeupdate:closer %.12s %d", file, i);
217: close(Btree_fd);
218: }
219: if (i < 0)
220: syserr("btreeupdate:bad get from indexes %d", i);
221: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.