KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > ExportedKeys


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import java.util.Enumeration JavaDoc;
44
45 import java.sql.SQLException JavaDoc;
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 /**
54  * A statement context which keeps track of the set of changes to the
55  * keys in a foreign-key constraint during the statement execution.
56  * At statement end, we check the entire set to make sure referential
57  * integrity has been maintained.
58  *
59  * @author Stan Bailes
60  */

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     /**
69      * fkey -> key
70      *
71      * 'k' means "insert k"
72      * '~k' means "delete k"
73      *
74      * First case, key deleted which is not referenced:
75      * lsb Encoding of 'ok, ~nk, ~of, ~nf' -> 1
76      *
77      * Second case, key deleted where foreign key is also deleted:
78      * lsb Encoding of 'ok, ~nk, of, ~nf' -> 5
79      *
80      * Encoding of 1, 5: 0x0022
81      */

82     static final int valid = 0x22;
83     
84     public ExportedKeys(Session session, ExportedKeyConstraint ec)
85     throws IOException JavaDoc
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 JavaDoc {
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 JavaDoc, SQLException JavaDoc
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 JavaDoc, SQLException JavaDoc
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 JavaDoc
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 JavaDoc, IOException JavaDoc {
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     // must be after UpdateIndex to properly handle self ref foreign keys
168
public int priority() { return 2; }
169
170     /**
171      * removal with cascade. NYI. XXX
172      */

173     public void removeKey(int refSpec, byte[] key) throws SQLException JavaDoc {
174     if (refSpec == Constraint.CASCADE) {
175     }
176     }
177 }
178
Popular Tags