| 1 package com.quadcap.sql; 2 3 40 41 import java.io.IOException ; 42 43 import java.util.Enumeration ; 44 45 import java.sql.SQLException ; 46 47 import com.quadcap.sql.index.BCursor; 48 import com.quadcap.sql.index.Btree; 49 50 import com.quadcap.util.Debug; 51 import com.quadcap.util.Util; 52 53 61 public class ExportedKeys implements StatementContext { 62 Session session; 63 ExportedKeyConstraint ec; 64 Btree index; 65 BCursor bc; 66 byte[] buf = new byte[1]; 67 68 82 static final int valid = 0x22; 83 84 public ExportedKeys(Session session, ExportedKeyConstraint ec) 85 throws IOException  86 { 87 this.session = session; 88 this.ec = ec; 89 this.index = session.makeTempTree(); 90 this.bc = index.getCursor(false); 91 } 92 93 void doKey(byte[] key, int bit) throws IOException { 94 if (bc.seek(key) && bc.getVal(buf) == 1) { 95 buf[0] |= bit; 96 bc.replace(buf); 97 } else { 98 buf[0] = (byte)bit; 99 bc.insert(key, buf); 100 } 101 } 102 103 public void addDeleteSelfRef(byte[] key, byte[] fkey) 104 throws IOException , SQLException  105 { 106 try { 107 doKey(key, 1); 108 doKey(fkey, 4); 109 } finally { 110 bc.close(); 111 } 112 } 113 114 public void addEntry(byte[] oldkey, byte[] newkey) 115 throws IOException , SQLException  116 { 117 try { 118 doKey(oldkey, 1); 119 doKey(newkey, 2); 120 } finally { 121 bc.close(); 122 } 123 } 124 125 public void addSelfRefEntry(byte[] oldkey, byte[] newkey, 126 byte[] oldfkey, byte[] newfkey) 127 throws IOException  128 { 129 try { 130 doKey(oldkey, 1); 131 doKey(newkey, 2); 132 doKey(oldfkey, 4); 133 doKey(newfkey, 8); 134 } finally { 135 bc.close(); 136 } 137 } 138 139 public void finish(boolean abort) throws SQLException , IOException { 140 try { 141 if (!abort) { 142 bc.beforeFirst(); 143 while (bc.next()) { 144 if (bc.getVal(buf) == 1) { 145 if ((valid & (1 << buf[0])) != 0) { 146 ec.checkKeyRemoval(session, bc.getKey()); 147 } 148 } 149 } 150 } 151 } finally { 152 try { 153 if (bc != null) bc.release(); 154 } finally { 155 bc = null; 156 try { 157 if (index != null) index.free(); 158 } finally { 159 session.getDatabase().releaseTempFile(); 160 index = null; 161 session = null; 162 } 163 } 164 } 165 } 166 167 public int priority() { return 2; } 169 170 173 public void removeKey(int refSpec, byte[] key) throws SQLException { 174 if (refSpec == Constraint.CASCADE) { 175 } 176 } 177 } 178 | Popular Tags |