KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > store > T_Undoable


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.store.T_DaemonService
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.store;
23
24 import org.apache.derbyTesting.unitTests.harness.T_Fail;
25
26 import org.apache.derby.iapi.store.raw.*;
27
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29 import org.apache.derby.iapi.services.io.FormatIdUtil;
30 import org.apache.derby.iapi.services.io.Formatable;
31 import org.apache.derby.iapi.services.io.StoredFormatIds;
32 import org.apache.derby.iapi.error.StandardException;
33 import org.apache.derby.iapi.store.raw.log.LogInstant;
34 import org.apache.derby.iapi.util.ByteArray;
35 import org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream;
36 import java.io.IOException JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.ObjectInput JavaDoc;
40 import java.io.ObjectOutput JavaDoc;
41 import java.io.StreamCorruptedException JavaDoc;
42 import java.util.Vector JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import org.apache.derby.iapi.services.io.LimitObjectInput;
46
47 //
48
// Tracing can be done like so (commented out)
49
//import org.apache.derbyTesting.unitTests.util.MsgTrace;
50

51 public class T_Undoable
52 implements Undoable
53 {
54     static final int REMOVE_NONE = 0;
55     static final int REMOVE_ME = 1;
56     static final int REMOVE_TO_ME = 2;
57
58     protected ByteArray preparedLog;
59     protected DynamicByteArrayOutputStream logBuffer;
60
61     GlobalTransactionId tid = null;
62     int tranSeq = -1;
63     int recordSeq = -1;
64     int removeWhat = REMOVE_NONE;
65     int LWMTranSeq = 0;
66     boolean rollBack = true;
67     int lastRecord = -1;
68     boolean lastTransaction = false;
69     int optionalDataLen;
70     boolean verbose;
71
72     // no-arg constructor, required by Formatable
73
public T_Undoable() { super(); }
74
75
76     T_Undoable(GlobalTransactionId tid, int tranSeq, int recordSeq,
77                int removeWhat, int LWMTranSeq,
78                boolean rollBack, int lastRecord, boolean lastTransaction,
79                int optionalDataLen,boolean verbose)
80          throws T_Fail
81     {
82 //MsgTrace.traceString("{{{tu.new");
83
T_Fail.T_ASSERT((removeWhat >= REMOVE_NONE) &&
84                              (removeWhat <= REMOVE_TO_ME));
85         T_Fail.T_ASSERT(rollBack == (recordSeq < 0));
86         T_Fail.T_ASSERT(rollBack == (tranSeq < 0));
87         this.tid = tid;
88         this.tranSeq = tranSeq;
89         this.recordSeq = recordSeq;
90         this.removeWhat = removeWhat;
91         this.LWMTranSeq = LWMTranSeq;
92         this.rollBack = rollBack;
93         this.lastRecord = lastRecord;
94         this.lastTransaction = lastTransaction;
95         this.optionalDataLen = optionalDataLen;
96         this.verbose = verbose;
97         
98         try {
99             writeOptionalDataToBuffer();
100         } catch (IOException JavaDoc ioe) {
101             throw T_Fail.exceptionFail(ioe);
102         } catch (StandardException se) {
103             throw T_Fail.exceptionFail(se);
104         }
105
106 //MsgTrace.traceString("}}}tu.new");
107

108     }
109     
110     private void writeOptionalDataToBuffer()
111         throws StandardException, IOException JavaDoc
112     {
113
114         if (logBuffer == null) {
115             // YYZ: need to revisit this. Do we really want to allocate this much for a buffer every time?
116
logBuffer = new DynamicByteArrayOutputStream(1024); // init size 1K
117
} else {
118             logBuffer.reset();
119         }
120
121         int optionalDataStart = logBuffer.getPosition();
122
123         if (SanityManager.DEBUG) {
124             SanityManager.ASSERT(optionalDataStart == 0,
125                 "Buffer for writing the optional data should start at position 0");
126         }
127
128 //MsgTrace.traceString("{{{tu.writeOpetionalData");
129
if (optionalDataLen > 0)
130         {
131             byte[] buf = new byte[optionalDataLen];
132             for (int ix=0;ix <optionalDataLen;ix++)
133                 buf[ix] = (byte)ix;
134             logBuffer.write(buf);
135         }
136 //MsgTrace.traceString("}}}tu.writeOpetionalData");
137

138         int optionalDataLength = logBuffer.getPosition() - optionalDataStart;
139         
140         if (SanityManager.DEBUG) {
141             if (optionalDataLength != logBuffer.getUsed())
142                 SanityManager.THROWASSERT("wrong optional data length, optionalDataLength = "
143                     + optionalDataLength + ", logBuffer.getUsed() = " + logBuffer.getUsed());
144         }
145
146         // set the position to the beginning of the buffer
147
logBuffer.setPosition(optionalDataStart);
148
149         this.preparedLog = new ByteArray (logBuffer.getByteArray(), optionalDataStart,
150             optionalDataLength);
151     }
152     
153     /*
154       Loggable methods
155       */

156     public void doMe(Transaction xact, LogInstant instant,
157                      LimitObjectInput in)
158     {
159         if (verbose)
160             System.out.println("Loggable.doMe("+toString()+")");
161         return;
162     }
163
164     /*
165         methods to support prepared log
166         the following two methods should not be called during recover
167     */

168
169     public ByteArray getPreparedLog()
170     {
171         return this.preparedLog;
172     }
173
174     public boolean needsRedo(Transaction xact) {return false;}
175     public void releaseResource(Transaction xact) {return;}
176     public int group () { return Loggable.RAWSTORE ; };
177
178     /*
179       Undoable methods.
180      */

181     public Compensation generateUndo(Transaction xact, LimitObjectInput in)
182          throws StandardException, IOException JavaDoc
183     {
184 //MsgTrace.traceString("+++tu.generateUndo");
185
return new T_Compensation();
186     }
187
188     /*
189       Formatable methods
190       */

191
192     /**
193      @exception IOException thrown on error
194      */

195     public void writeExternal(ObjectOutput JavaDoc out)
196     throws IOException JavaDoc
197     {
198 //MsgTrace.traceString("{{{tu.writeExternal");
199
if (SanityManager.DEBUG)
200         {
201             SanityManager.ASSERT((removeWhat >= REMOVE_NONE) &&
202                                  (removeWhat <= REMOVE_TO_ME));
203             SanityManager.ASSERT(rollBack == (recordSeq < 0));
204             SanityManager.ASSERT(rollBack == (tranSeq < 0));
205         }
206         out.writeObject(tid);
207         out.writeInt(tranSeq);
208         out.writeInt(recordSeq);
209         out.writeInt(removeWhat);
210         out.writeInt(LWMTranSeq);
211         out.writeBoolean(rollBack);
212         out.writeInt(lastRecord);
213         out.writeBoolean(lastTransaction);
214         out.writeInt(optionalDataLen);
215 //MsgTrace.traceString("}}}tu.writeExternal");
216
}
217
218     public void readExternal(ObjectInput JavaDoc in)
219     throws IOException JavaDoc,ClassNotFoundException JavaDoc
220     {
221 //MsgTrace.traceString("{{{tu.readExternal");
222
try
223         {
224             tid = (GlobalTransactionId)in.readObject();
225             tranSeq = in.readInt();
226             recordSeq = in.readInt();
227             removeWhat = in.readInt();
228             LWMTranSeq = in.readInt();
229             rollBack = in.readBoolean();
230             lastRecord = in.readInt();
231             lastTransaction = in.readBoolean();
232             optionalDataLen = in.readInt();
233         }
234
235         catch ( ClassCastException JavaDoc exception ) {
236 //MsgTrace.traceString("{{{tu.readExternal---exception");
237
throw new StreamCorruptedException JavaDoc();
238         }
239 //MsgTrace.traceString("}}}tu.readExternal");
240
}
241
242     public int getTypeFormatId()
243     {
244         return StoredFormatIds.SERIALIZABLE_FORMAT_ID;
245     }
246
247     /*
248       Object methods.
249       */

250     public String JavaDoc toString()
251     {
252         String JavaDoc traceTid = "tid: null";
253
254         if (tid !=null) traceTid = "tid: "+tid;
255
256         String JavaDoc traceRemoveWhat;
257         switch (removeWhat)
258         {
259         case REMOVE_NONE:
260             traceRemoveWhat = "REMOVE_NONE";
261             break;
262         case REMOVE_ME:
263             traceRemoveWhat = "REMOVE_ME";
264             break;
265         case REMOVE_TO_ME:
266             traceRemoveWhat = "REMOVE_TO_ME";
267             break;
268         default:
269             traceRemoveWhat = "removeWhat: invalidValue";
270             break;
271         }
272
273         return
274             traceTid+" "+
275             "tranSeq: "+tranSeq+" "+
276             "recordSeq: "+recordSeq+" "+
277             traceRemoveWhat+" "+
278             "LWMTranSeq: "+LWMTranSeq+" "+
279             "rollback: "+rollBack+" "+
280             "lastRecord: "+lastRecord+" "+
281             "optionalDataLen: "+optionalDataLen+" "+
282             "lastTransaction: "+lastTransaction;
283     }
284
285 }
286
Popular Tags