1: # include "../ingres.h" 2: # include "../aux.h" 3: # include "../unix.h" 4: # include "../catalog.h" 5: 6: 7: /* 8: ** USERDESTROY -- auxiliary cleanup for destroy of a user relation 9: ** 10: ** userdestroy is called during the destroy of a non system 11: ** relation. If the relation is indexed or is itself an index 12: ** then the appropriate action is taken. If it is indexed, 13: ** then all secondary indices on the relation are also destroyed. 14: ** If it is a secondary index, then the entry in the indexes relation 15: ** is removed and the "relindxd" bit on the primary relation is 16: ** cleared if this was the last index on the relation. 17: ** 18: ** If the relation was a view or had integrity constraints or 19: ** protection constraints on it, then those definitions are 20: ** removed from the appropriate system catalogues. 21: ** 22: ** Parameters: 23: ** reltup -- the relation relation tuple. 24: ** 25: ** Returns: 26: ** none 27: ** 28: ** Side Effects: 29: ** zero or more system catalogues will be updated. 30: ** 31: ** Called By: 32: ** destroy 33: ** 34: ** History: 35: ** 12/15/78 (rse) -- written (and taken from destroy.c) 36: */ 37: 38: userdestroy(reltup) 39: struct relation *reltup; 40: { 41: register int i; 42: register struct relation *rel; 43: struct tup_id tid, limtid; 44: char newrelname[MAXNAME + 3]; 45: extern struct descriptor Reldes, Attdes, Inddes; 46: extern struct descriptor Treedes, Intdes, Prodes; 47: struct relation relt, relk; 48: struct index indk, indt; 49: 50: rel = reltup; 51: 52: /* handle special case of destroying a secondary index */ 53: if (rel->relindxd < 0) 54: { 55: opencatalog("indexes", 2); 56: setkey(&Inddes, &indk, rel->relid, IRELIDI); 57: setkey(&Inddes, &indk, rel->relowner, IOWNERP); 58: if ((i = getequal(&Inddes, &indk, &indt, &tid)) != 0) 59: syserr("destroy: geteq(ind,%.12s) %d", rel->relid, i); 60: 61: /* remove entry in INDEX catalog */ 62: bmove(indt.irelidp, newrelname, MAXNAME); 63: bmove(indt.iownerp, &newrelname[MAXNAME], 2); 64: if ((i = delete(&Inddes, &tid)) != 0) 65: syserr("DESTROY: delete(ind/%.12s) %d", rel->relid, i); 66: clearkeys(&Inddes); 67: setkey(&Inddes, &indk, newrelname, IRELIDP); 68: setkey(&Inddes, &indk, &newrelname[MAXNAME], IOWNERP); 69: 70: /* reset relindxd field in relation catalog if no other indexes exist on this primary */ 71: if (getequal(&Inddes, &indk, &indt, &tid) != 0) 72: { 73: clearkeys(&Reldes); 74: setkey(&Reldes, &relk, newrelname, RELID); 75: setkey(&Reldes, &relk, &newrelname[MAXNAME], RELOWNER); 76: if (i = getequal(&Reldes, &relk, &relt, &tid)) 77: syserr("destroy: getequal(rel, %s) %d", newrelname, i); 78: relt.relindxd = 0; 79: if ((i = replace(&Reldes, &tid, &relt, 0)) != 0) 80: syserr("destroy: replace(rel) %d", i); 81: } 82: } 83: 84: /* check special case of destroying primary relation */ 85: if (rel->relindxd > 0) 86: { 87: opencatalog("indexes", 2); 88: setkey(&Inddes, &indk, rel->relid, IRELIDP); 89: setkey(&Inddes, &indk, rel->relowner, IOWNERP); 90: if (i = find(&Inddes, EXACTKEY, &tid, &limtid, &indk)) 91: syserr("destroy: find(ind,%.12s) %d", rel->relid, i); 92: while ((i = get(&Inddes, &tid, &limtid, &indt, TRUE)) == 0) 93: { 94: if (kcompare(&Inddes, &indk, &indt) != 0) 95: continue; 96: if ((i = delete(&Inddes, &tid)) != 0) 97: syserr("DESTROY: delete(ind/%.12s) %d", rel->relid, i); 98: clearkeys(&Reldes); 99: purgetup(&Reldes, indt.irelidi, RELID, indt.iownerp, RELOWNER, 0); 100: if (i = flush_rel(&Reldes, FALSE)) /* flush for recovery & concurrency reasons */ 101: syserr("destroy:flush irel %d", i); 102: purgetup(&Attdes, indt.irelidi, ATTRELID, indt.iownerp, ATTOWNER, 0); 103: ingresname(indt.irelidi, indt.iownerp, newrelname); 104: if (unlink(newrelname)) 105: syserr("destroy: unlink(%s)", newrelname); 106: } 107: if (i < 0) 108: { 109: syserr("destroy: get(ind) %d", i); 110: } 111: } 112: 113: /* if any integrity constraints exist, remove them */ 114: if (rel->relstat & S_INTEG) 115: { 116: opencatalog("integrities", 2); 117: purgetup(&Intdes, rel->relid, INTRELID, rel->relowner, INTRELOWNER); 118: } 119: 120: /* if any protection clauses exist, remove them */ 121: if (rel->relstat & S_PROTUPS) 122: { 123: opencatalog("protect", 2); 124: purgetup(&Prodes, rel->relid, PRORELID, rel->relowner, PRORELOWN); 125: } 126: 127: /* remove any trees associated with the relation */ 128: if (rel->relstat & (S_PROTUPS | S_VIEW | S_INTEG)) 129: { 130: opencatalog("tree", 2); 131: purgetup(&Treedes, rel->relid, TREERELID, rel->relowner, TREEOWNER); 132: } 133: }