KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
57  * StatementContext which manages index update operations generated by a
58  * statement, defers those operations so consistency can be checked,
59  * and at the end of the statement's execution, performs all of the
60  * updates.
61  *
62  * @author Stan Bailes
63  */

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 JavaDoc
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 JavaDoc, SQLException JavaDoc
81     {
82     if (temp.get(key) != null) {
83         throw new SQLException JavaDoc("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 JavaDoc, IOException JavaDoc {
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 JavaDoc("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