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.BufferedOutputStream;
042: import java.io.Externalizable;
043: import java.io.IOException;
044: import java.io.InputStream;
045: import java.io.ObjectInput;
046: import java.io.ObjectOutput;
047: import java.io.OutputStream;
048:
049: import java.sql.SQLException;
050:
051: import com.quadcap.sql.io.ObjectInputStream;
052: import com.quadcap.sql.io.ObjectOutputStream;
053: import com.quadcap.sql.io.Extern;
054:
055: import com.quadcap.sql.file.BlockAccess;
056: import com.quadcap.sql.file.BlockFile;
057: import com.quadcap.sql.file.Datafile;
058: import com.quadcap.sql.file.Log;
059: import com.quadcap.sql.file.PageManager;
060: import com.quadcap.sql.file.SubPageManager;
061:
062: import com.quadcap.sql.types.Value;
063: import com.quadcap.sql.types.ValueBlob;
064:
065: import com.quadcap.util.Debug;
066:
067: import com.quadcap.io.IO;
068:
069: /**
070: * Log step to insert a BLOB value into the database.
071: *
072: * @author Stan Bailes
073: */
074: public class InsertBlob extends LogStep implements Externalizable {
075: long tempBlock;
076: long permBlock;
077:
078: /**
079: * Default constructor for serialization
080: */
081: public InsertBlob() {
082: }
083:
084: /**
085: * Construct InsertBlob for a given blob
086: */
087: public InsertBlob(Session session, ValueBlob blob)
088: throws IOException {
089: super (session);
090:
091: if (blob.getBytesVal() != null) {
092: blob.passivate(session.getDatabase(), session
093: .getTransactionId());
094: }
095: this .tempBlock = blob.getTempBlock();
096: this .permBlock = blob.getPermBlock();
097: }
098:
099: /**
100: * LogStep.redo():
101: */
102: public void redo(Session session) throws IOException, SQLException {
103: Log log = session.getLog();
104: if (session.getConnection().inRecovery() && tempBlock >= 0) {
105: BlockFile perm = session.getFile();
106: BlockFile temp = session.getDatabase().getTempFile();
107: InputStream is = temp.getInputStream(tempBlock);
108:
109: long newPermBlock = perm.newPage();
110: OutputStream os = perm.getOutputStream(newPermBlock);
111: IO.copyStream(is, os);
112: os.close();
113: is.close();
114:
115: log.putRowMap(permBlock, newPermBlock);
116: permBlock = newPermBlock;
117: }
118: session.getDatabase().addMorgue(permBlock,
119: session.getTransactionId());
120: }
121:
122: public void undo(Session session) throws IOException, SQLException {
123: final Database db = session.getDatabase();
124: final BlockFile file = session.getFile();
125:
126: file.freeStream(permBlock);
127: db.delMorgue(permBlock);
128: }
129:
130: public void prepare(Session session) throws IOException {
131: }
132:
133: public void readExternal(ObjectInput in) throws IOException,
134: ClassNotFoundException {
135: super .readExternal(in);
136: tempBlock = in.readLong();
137: permBlock = in.readLong();
138: }
139:
140: public void writeExternal(ObjectOutput out) throws IOException {
141: super .writeExternal(out);
142: out.writeLong(tempBlock);
143: out.writeLong(permBlock);
144: }
145:
146: //#ifdef DEBUG
147: public String toString() {
148: StringBuffer sb = new StringBuffer(super .toString());
149: sb.append(" InsertBlob(");
150: sb.append(Long.toString(tempBlock));
151: sb.append(',');
152: sb.append(Long.toString(permBlock));
153: sb.append(')');
154: return sb.toString();
155: }
156:
157: //#endif
158:
159: static Extern extern;
160:
161: public void setExtern(Extern extern) {
162: InsertBlob.extern = extern;
163: }
164:
165: public Extern getExtern() {
166: return extern;
167: }
168:
169: public void discard(Datafile db) throws IOException {
170: db.getTempFile(false).freeSegment(tempBlock);
171: db.releaseTempFile(); // Release refcount obtained when the blob was created
172: }
173: }
|