| 1 package com.quadcap.sql; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.sql.SQLException ; 47 48 import com.quadcap.sql.file.BlockFile; 49 50 import com.quadcap.sql.index.Btree; 51 52 import com.quadcap.util.Debug; 53 import com.quadcap.util.Util; 54 55 60 public abstract class ModIndexEntry extends LogStep implements Externalizable { 61 transient Btree index; 62 transient Constraint constraint; 63 64 String tableName; 65 String constraintName; 66 67 byte[] key; 68 69 72 public ModIndexEntry() {} 73 74 public ModIndexEntry(Session session, Constraint constraint, byte[] key) { 75 super(session); 76 this.constraint = constraint; 77 this.key = key; 78 } 79 80 Btree getIndex(Session session) throws IOException { 81 if (index == null) { 82 Database db = session.getDatabase(); 83 if (constraint == null) { 84 Table table = (Table)db.getRelation(tableName); 85 constraint = table.getConstraint(constraintName); 86 if (constraint == null) { 87 throw new RuntimeException ("No constraint: " + 88 constraintName); 89 } 90 } 91 index = constraint.getIndex(db); 92 if (index == null) { 93 throw new RuntimeException ("No index for table: " + tableName); 94 } 95 } 96 return index; 97 } 98 99 abstract public void redo(Session session) 100 throws IOException , SQLException  101 ; 102 103 abstract public void undo(Session session) 104 throws IOException , SQLException  105 ; 106 107 public void prepare(Session session) throws IOException {} 108 109 public void readExternal(ObjectInput in) 110 throws IOException , ClassNotFoundException  111 { 112 super.readExternal(in); 113 tableName = (String )in.readObject(); 114 constraintName = (String )in.readObject(); 115 int cnt = in.readInt(); 116 key = new byte[cnt]; 117 in.read(key); 118 } 119 120 public void writeExternal(ObjectOutput out) throws IOException { 121 super.writeExternal(out); 122 if (tableName == null) { 123 tableName = constraint.getTable().getName(); 124 constraintName = constraint.getName(); 125 } 126 out.writeObject(tableName); 127 out.writeObject(constraintName); 128 out.writeInt(key.length); 129 out.write(key); 130 } 131 132 public String toString() { 134 StringBuffer sb = new StringBuffer (super.toString()); 135 sb.append(" ModIndexEntry("); 136 sb.append(Util.hexBytes(key)); 137 sb.append(')'); 138 return sb.toString(); 139 } 140 } 142 143 144 | Popular Tags |