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.ByteUtil;
049: import com.quadcap.sql.file.SubPageManager;
050:
051: import com.quadcap.sql.io.Extern;
052:
053: import com.quadcap.sql.index.Btree;
054:
055: import com.quadcap.util.Util;
056:
057: /**
058: * Log step to add a entry to an index.
059: *
060: * @author Stan Bailes
061: */
062: public class AddIndexEntry extends ModIndexEntry implements
063: Externalizable {
064: long rowId;
065:
066: /**
067: * Default constructor
068: */
069: public AddIndexEntry() {
070: }
071:
072: /**
073: * Explicit constructor from key and rowId
074: */
075: public AddIndexEntry(Session session, Constraint constraint,
076: byte[] key, long rowId) {
077: super (session, constraint, key);
078: this .rowId = rowId;
079: }
080:
081: /**
082: * LogStep.redo(): Store the new entry in the index
083: */
084: public void redo(Session session) throws IOException, SQLException {
085: if (index == null) {
086: getIndex(session);
087: }
088: if (session.getConnection().inRecovery()) {
089: rowId = session.getLog().getRowMap(rowId);
090: }
091: index.set(key, key.length, session.getBuf8(rowId), 0, 8);
092: }
093:
094: /**
095: * LogStep.undo(): Remove the entry from the index
096: */
097: public void undo(Session session) throws IOException {
098: if (index == null) {
099: getIndex(session);
100: }
101: index.delete(key);
102: }
103:
104: /**
105: * Externalizable: Read me from a stream
106: */
107: public void readExternal(ObjectInput in) throws IOException,
108: ClassNotFoundException {
109: super .readExternal(in);
110: rowId = in.readLong();
111: }
112:
113: /**
114: * Externalizable: Write me to a stream
115: */
116: public void writeExternal(ObjectOutput out) throws IOException {
117: super .writeExternal(out);
118: out.writeLong(rowId);
119: }
120:
121: /**
122: * My class's Extern object
123: */
124: static Extern extern;
125:
126: public void setExtern(Extern extern) {
127: AddIndexEntry.extern = extern;
128: }
129:
130: public Extern getExtern() {
131: return extern;
132: }
133:
134: //#ifdef DEBUG
135: /**
136: * For debugging, return a string representation
137: */
138: public String toString() {
139: return "Add: " + super .toString() + ", rowId = "
140: + SubPageManager.toString(rowId);
141: }
142: //#endif
143:
144: }
|