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.file.SubPageManager; 48 import com.quadcap.sql.file.ByteUtil; 49 50 import com.quadcap.sql.index.Btree; 51 import com.quadcap.sql.index.BCursor; 52 53 import com.quadcap.util.Debug; 54 import com.quadcap.util.Util; 55 56 64 public class UpdateIndex implements StatementContext { 65 Session session; 66 Constraint constraint; 67 Btree index; 68 Btree temp; 69 70 public UpdateIndex(Session session, Constraint constraint) 71 throws IOException 72 { 73 this.session = session; 74 this.constraint = constraint; 75 this.index = constraint.getIndex(session.getDatabase()); 76 this.temp = session.makeTempTree(); 77 } 78 79 public void addEntry(byte[] key, byte[] old, long rowId) 80 throws IOException , SQLException 81 { 82 if (temp.get(key) != null) { 83 throw new SQLException ("Index constraint violated", "23000"); 84 } 85 byte[] td = new byte[old.length + 8]; 86 System.arraycopy(old, 0, td, 0, old.length); 87 ByteUtil.putLong(td, old.length, rowId); 88 temp.set(key, td); 89 } 90 91 public void finish(boolean abort) throws SQLException , IOException { 92 BCursor c = null; 93 try { 94 if (!abort) { 95 c = temp.getCursor(); 96 while (c.next()) { 97 byte[] old = c.getValBuf(); 98 byte[] td = new byte[c.getValLen() - 8]; 99 System.arraycopy(old, 0, td, 0, td.length); 100 session.doStep(new DeleteIndexEntry(session, 101 constraint, td)); 102 } 103 104 c.beforeFirst(); 105 while (c.next()) { 106 byte[] key = c.getKey(); 107 byte[] td = c.getValBuf(); 108 int len = c.getValLen(); 109 long rowId = ByteUtil.getLong(td, len-8); 110 if (index.get(key) != null) { 111 throw new SQLException ("Index constraint violated", 112 "23000"); 113 } 114 session.doStep(new AddIndexEntry(session, 115 constraint, key, rowId)); 116 } 117 } 118 } finally { 119 try { 120 if (c != null) c.release(); 121 } finally { 122 try { 123 if (temp != null) temp.free(); 124 } finally { 125 if (temp != null) session.getDatabase().releaseTempFile(); 126 c = null; 127 index = null; 128 session = null; 129 try { 130 } finally { 131 temp = null; 132 } 133 } 134 } 135 } 136 } 137 138 public int priority() { return 0; } 139 } 140 141 142 | Popular Tags |