Quadcap Embeddable Database

com/quadcap/sql/ExportedKeys.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 00043 import java.util.Enumeration; 00044 00045 import java.sql.SQLException; 00046 00047 import com.quadcap.sql.index.BCursor; 00048 import com.quadcap.sql.index.Btree; 00049 00050 import com.quadcap.util.Debug; 00051 import com.quadcap.util.Util; 00052 00053 /** 00054 * A statement context which keeps track of the set of changes to the 00055 * keys in a foreign-key constraint during the statement execution. 00056 * At statement end, we check the entire set to make sure referential 00057 * integrity has been maintained. 00058 * 00059 * @author Stan Bailes 00060 */ 00061 public class ExportedKeys implements StatementContext { 00062 Session session; 00063 ExportedKeyConstraint ec; 00064 Btree index; 00065 BCursor bc; 00066 byte[] buf = new byte[1]; 00067 00068 /** 00069 * fkey -> key 00070 * 00071 * 'k' means "insert k" 00072 * '~k' means "delete k" 00073 * 00074 * First case, key deleted which is not referenced: 00075 * lsb Encoding of 'ok, ~nk, ~of, ~nf' -> 1 00076 * 00077 * Second case, key deleted where foreign key is also deleted: 00078 * lsb Encoding of 'ok, ~nk, of, ~nf' -> 5 00079 * 00080 * Encoding of 1, 5: 0x0022 00081 */ 00082 static final int valid = 0x22; 00083 00084 public ExportedKeys(Session session, ExportedKeyConstraint ec) 00085 throws IOException 00086 { 00087 this.session = session; 00088 this.ec = ec; 00089 this.index = session.makeTempTree(); 00090 this.bc = index.getCursor(false); 00091 } 00092 00093 void doKey(byte[] key, int bit) throws IOException { 00094 if (bc.seek(key) && bc.getVal(buf) == 1) { 00095 buf[0] |= bit; 00096 bc.replace(buf); 00097 } else { 00098 buf[0] = (byte)bit; 00099 bc.insert(key, buf); 00100 } 00101 } 00102 00103 public void addDeleteSelfRef(byte[] key, byte[] fkey) 00104 throws IOException, SQLException 00105 { 00106 try { 00107 doKey(key, 1); 00108 doKey(fkey, 4); 00109 } finally { 00110 bc.close(); 00111 } 00112 } 00113 00114 public void addEntry(byte[] oldkey, byte[] newkey) 00115 throws IOException, SQLException 00116 { 00117 try { 00118 doKey(oldkey, 1); 00119 doKey(newkey, 2); 00120 } finally { 00121 bc.close(); 00122 } 00123 } 00124 00125 public void addSelfRefEntry(byte[] oldkey, byte[] newkey, 00126 byte[] oldfkey, byte[] newfkey) 00127 throws IOException 00128 { 00129 try { 00130 doKey(oldkey, 1); 00131 doKey(newkey, 2); 00132 doKey(oldfkey, 4); 00133 doKey(newfkey, 8); 00134 } finally { 00135 bc.close(); 00136 } 00137 } 00138 00139 public void finish(boolean abort) throws SQLException, IOException { 00140 try { 00141 if (!abort) { 00142 bc.beforeFirst(); 00143 while (bc.next()) { 00144 if (bc.getVal(buf) == 1) { 00145 if ((valid & (1 << buf[0])) != 0) { 00146 ec.checkKeyRemoval(session, bc.getKey()); 00147 } 00148 } 00149 } 00150 } 00151 } finally { 00152 try { 00153 if (bc != null) bc.release(); 00154 } finally { 00155 bc = null; 00156 try { 00157 if (index != null) index.free(); 00158 } finally { 00159 session.getDatabase().releaseTempFile(); 00160 index = null; 00161 session = null; 00162 } 00163 } 00164 } 00165 } 00166 00167 // must be after UpdateIndex to properly handle self ref foreign keys 00168 public int priority() { return 2; } 00169 00170 /** 00171 * removal with cascade. NYI. XXX 00172 */ 00173 public void removeKey(int refSpec, byte[] key) throws SQLException { 00174 if (refSpec == Constraint.CASCADE) { 00175 } 00176 } 00177 }