Quadcap Embeddable Database

com/quadcap/sql/InsertBlob.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.BufferedOutputStream; 00042 import java.io.Externalizable; 00043 import java.io.IOException; 00044 import java.io.InputStream; 00045 import java.io.ObjectInput; 00046 import java.io.ObjectOutput; 00047 import java.io.OutputStream; 00048 00049 import java.sql.SQLException; 00050 00051 import com.quadcap.sql.io.ObjectInputStream; 00052 import com.quadcap.sql.io.ObjectOutputStream; 00053 import com.quadcap.sql.io.Extern; 00054 00055 import com.quadcap.sql.file.BlockAccess; 00056 import com.quadcap.sql.file.BlockFile; 00057 import com.quadcap.sql.file.Datafile; 00058 import com.quadcap.sql.file.Log; 00059 import com.quadcap.sql.file.PageManager; 00060 import com.quadcap.sql.file.SubPageManager; 00061 00062 import com.quadcap.sql.types.Value; 00063 import com.quadcap.sql.types.ValueBlob; 00064 00065 import com.quadcap.util.Debug; 00066 00067 import com.quadcap.io.IO; 00068 00069 /** 00070 * Log step to insert a BLOB value into the database. 00071 * 00072 * @author Stan Bailes 00073 */ 00074 public class InsertBlob extends LogStep implements Externalizable { 00075 long tempBlock; 00076 long permBlock; 00077 00078 /** 00079 * Default constructor for serialization 00080 */ 00081 public InsertBlob() {} 00082 00083 /** 00084 * Construct InsertBlob for a given blob 00085 */ 00086 public InsertBlob(Session session, ValueBlob blob) 00087 throws IOException 00088 { 00089 super(session); 00090 00091 if (blob.getBytesVal() != null) { 00092 blob.passivate(session.getDatabase(), session.getTransactionId()); 00093 } 00094 this.tempBlock = blob.getTempBlock(); 00095 this.permBlock = blob.getPermBlock(); 00096 } 00097 00098 /** 00099 * LogStep.redo(): 00100 */ 00101 public void redo(Session session) throws IOException, SQLException { 00102 Log log = session.getLog(); 00103 if (session.getConnection().inRecovery() && tempBlock >= 0) { 00104 BlockFile perm = session.getFile(); 00105 BlockFile temp = session.getDatabase().getTempFile(); 00106 InputStream is = temp.getInputStream(tempBlock); 00107 00108 long newPermBlock = perm.newPage(); 00109 OutputStream os = perm.getOutputStream(newPermBlock); 00110 IO.copyStream(is, os); 00111 os.close(); 00112 is.close(); 00113 00114 log.putRowMap(permBlock, newPermBlock); 00115 permBlock = newPermBlock; 00116 } 00117 session.getDatabase().addMorgue(permBlock, session.getTransactionId()); 00118 } 00119 00120 public void undo(Session session) throws IOException, SQLException { 00121 final Database db = session.getDatabase(); 00122 final BlockFile file = session.getFile(); 00123 00124 file.freeStream(permBlock); 00125 db.delMorgue(permBlock); 00126 } 00127 00128 public void prepare(Session session) throws IOException { 00129 } 00130 00131 public void readExternal(ObjectInput in) 00132 throws IOException, ClassNotFoundException 00133 { 00134 super.readExternal(in); 00135 tempBlock = in.readLong(); 00136 permBlock = in.readLong(); 00137 } 00138 00139 public void writeExternal(ObjectOutput out) throws IOException { 00140 super.writeExternal(out); 00141 out.writeLong(tempBlock); 00142 out.writeLong(permBlock); 00143 } 00144 00145 //#ifdef DEBUG 00146 public String toString() { 00147 StringBuffer sb = new StringBuffer(super.toString()); 00148 sb.append(" InsertBlob("); 00149 sb.append(Long.toString(tempBlock)); 00150 sb.append(','); 00151 sb.append(Long.toString(permBlock)); 00152 sb.append(')'); 00153 return sb.toString(); 00154 } 00155 //#endif 00156 00157 static Extern extern; 00158 public void setExtern(Extern extern) { InsertBlob.extern = extern; } 00159 public Extern getExtern() { return extern; } 00160 00161 public void discard(Datafile db) throws IOException { 00162 db.getTempFile(false).freeSegment(tempBlock); 00163 db.releaseTempFile(); // Release refcount obtained when the blob was created 00164 } 00165 }