001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import java.util.Enumeration;
044:
045: import java.sql.SQLException;
046:
047: import com.quadcap.sql.file.SubPageManager;
048: import com.quadcap.sql.file.ByteUtil;
049:
050: import com.quadcap.sql.index.Btree;
051: import com.quadcap.sql.index.BCursor;
052:
053: import com.quadcap.util.Debug;
054: import com.quadcap.util.Util;
055:
056: /**
057: * StatementContext which manages index update operations generated by a
058: * statement, defers those operations so consistency can be checked,
059: * and at the end of the statement's execution, performs all of the
060: * updates.
061: *
062: * @author Stan Bailes
063: */
064: public class UpdateIndex implements StatementContext {
065: Session session;
066: Constraint constraint;
067: Btree index;
068: Btree temp;
069:
070: public UpdateIndex(Session session, Constraint constraint)
071: throws IOException {
072: this .session = session;
073: this .constraint = constraint;
074: this .index = constraint.getIndex(session.getDatabase());
075: this .temp = session.makeTempTree();
076: }
077:
078: public void addEntry(byte[] key, byte[] old, long rowId)
079: throws IOException, SQLException {
080: if (temp.get(key) != null) {
081: throw new SQLException("Index constraint violated", "23000");
082: }
083: byte[] td = new byte[old.length + 8];
084: System.arraycopy(old, 0, td, 0, old.length);
085: ByteUtil.putLong(td, old.length, rowId);
086: temp.set(key, td);
087: }
088:
089: public void finish(boolean abort) throws SQLException, IOException {
090: BCursor c = null;
091: try {
092: if (!abort) {
093: c = temp.getCursor();
094: while (c.next()) {
095: byte[] old = c.getValBuf();
096: byte[] td = new byte[c.getValLen() - 8];
097: System.arraycopy(old, 0, td, 0, td.length);
098: session.doStep(new DeleteIndexEntry(session,
099: constraint, td));
100: }
101:
102: c.beforeFirst();
103: while (c.next()) {
104: byte[] key = c.getKey();
105: byte[] td = c.getValBuf();
106: int len = c.getValLen();
107: long rowId = ByteUtil.getLong(td, len - 8);
108: if (index.get(key) != null) {
109: throw new SQLException(
110: "Index constraint violated", "23000");
111: }
112: session.doStep(new AddIndexEntry(session,
113: constraint, key, rowId));
114: }
115: }
116: } finally {
117: try {
118: if (c != null)
119: c.release();
120: } finally {
121: try {
122: if (temp != null)
123: temp.free();
124: } finally {
125: if (temp != null)
126: session.getDatabase().releaseTempFile();
127: c = null;
128: index = null;
129: session = null;
130: try {
131: } finally {
132: temp = null;
133: }
134: }
135: }
136: }
137: }
138:
139: public int priority() {
140: return 0;
141: }
142: }
|