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

Defined functions

userdestroy defined in line 8; used 1 times
Last modified: 1986-04-17
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1019
Valid CSS Valid XHTML 1.0 Strict