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.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: import java.sql.SQLException;
047:
048: import com.quadcap.sql.file.BlockFile;
049:
050: import com.quadcap.sql.index.Btree;
051:
052: import com.quadcap.util.Debug;
053: import com.quadcap.util.Util;
054:
055: /**
056: * Abstract log step for all operations that modify indexes.
057: *
058: * @author Stan Bailes
059: */
060: public abstract class ModIndexEntry extends LogStep implements
061: Externalizable {
062: transient Btree index;
063: transient Constraint constraint;
064:
065: String tableName;
066: String constraintName;
067:
068: byte[] key;
069:
070: /**
071: * Default constructor
072: */
073: public ModIndexEntry() {
074: }
075:
076: public ModIndexEntry(Session session, Constraint constraint,
077: byte[] key) {
078: super (session);
079: this .constraint = constraint;
080: this .key = key;
081: }
082:
083: Btree getIndex(Session session) throws IOException {
084: if (index == null) {
085: Database db = session.getDatabase();
086: if (constraint == null) {
087: Table table = (Table) db.getRelation(tableName);
088: constraint = table.getConstraint(constraintName);
089: if (constraint == null) {
090: throw new RuntimeException("No constraint: "
091: + constraintName);
092: }
093: }
094: index = constraint.getIndex(db);
095: if (index == null) {
096: throw new RuntimeException("No index for table: "
097: + tableName);
098: }
099: }
100: return index;
101: }
102:
103: abstract public void redo(Session session) throws IOException,
104: SQLException;
105:
106: abstract public void undo(Session session) throws IOException,
107: SQLException;
108:
109: public void prepare(Session session) throws IOException {
110: }
111:
112: public void readExternal(ObjectInput in) throws IOException,
113: ClassNotFoundException {
114: super .readExternal(in);
115: tableName = (String) in.readObject();
116: constraintName = (String) in.readObject();
117: int cnt = in.readInt();
118: key = new byte[cnt];
119: in.read(key);
120: }
121:
122: public void writeExternal(ObjectOutput out) throws IOException {
123: super .writeExternal(out);
124: if (tableName == null) {
125: tableName = constraint.getTable().getName();
126: constraintName = constraint.getName();
127: }
128: out.writeObject(tableName);
129: out.writeObject(constraintName);
130: out.writeInt(key.length);
131: out.write(key);
132: }
133:
134: //#ifdef DEBUG
135: public String toString() {
136: StringBuffer sb = new StringBuffer(super .toString());
137: sb.append(" ModIndexEntry(");
138: sb.append(Util.hexBytes(key));
139: sb.append(')');
140: return sb.toString();
141: }
142: //#endif
143: }
|