00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00071
00072
00073
00074 public class InsertBlob extends LogStep implements
Externalizable {
00075 long tempBlock;
00076 long permBlock;
00077
00078
00079
00080
00081 public InsertBlob() {}
00082
00083
00084
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
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
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
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();
00164 }
00165 }