KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > data > LogicalUndoOperation


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.LogicalUndoOperation
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.derby.impl.store.raw.data;
23
24 import org.apache.derby.impl.store.raw.data.BasePage;
25
26 import org.apache.derby.iapi.services.io.FormatIdUtil;
27 import org.apache.derby.iapi.services.io.StoredFormatIds;
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29
30 import org.apache.derby.iapi.store.raw.Compensation;
31 import org.apache.derby.iapi.store.raw.Loggable;
32 import org.apache.derby.iapi.store.raw.Transaction;
33 import org.apache.derby.iapi.store.raw.Undoable;
34
35 import org.apache.derby.iapi.store.raw.log.LogInstant;
36
37 import org.apache.derby.iapi.error.StandardException;
38
39 import org.apache.derby.iapi.services.io.CompressedNumber;
40 import org.apache.derby.iapi.util.ByteArray;
41
42 import java.io.OutputStream JavaDoc;
43 import java.io.ObjectOutput JavaDoc;
44 import java.io.ObjectInput JavaDoc;
45 import java.io.IOException JavaDoc;
46
47 import org.apache.derby.iapi.services.io.LimitObjectInput;
48
49 /**
50     LogicalUndoOperation is a compensation operation that rolls back the change of
51     an LogicalUndoable operation. A LogicalUndoOperation itself is not undo-able, i.e,
52     it is loggable but not undoable.
53
54     <PRE>
55     @format_id LOGOP_PAGE_LOGICAL_UNDO
56         the formatId is written by FormatIdOutputStream when this object is
57         written out by writeObject
58     @purpose undo a logical log operation
59     @upgrade
60     @disk_layout
61         PageBasicOperation the super class
62         recordId(CompressedInt) the recordId of the changed row (this may not
63                 be the recordId during rollback if the record moved from one
64                 page to another)
65         OptionalData none (compensation operation never have optional data)
66     @end_format
67     </PRE>
68
69 */

70 public class LogicalUndoOperation extends PageBasicOperation implements Compensation {
71
72     protected int recordId; // the record id to call undoOp.undoMe with
73

74     /** The operation to be rolled back */
75     transient private LogicalPageOperation undoOp = null;
76
77     protected LogicalUndoOperation(BasePage page)
78     {
79         super(page);
80     }
81
82     /** Set up a compensation operation during run time rollback */
83     public LogicalUndoOperation(BasePage page, int recordId, LogicalPageOperation op)
84     {
85         super(page);
86         undoOp = op;
87         this.recordId = recordId;
88     }
89
90     /**
91         Return my format identifier.
92     */

93
94     // no-arg constructor, required by Formatable
95
public LogicalUndoOperation() { super(); }
96
97     public int getTypeFormatId() {
98         return StoredFormatIds.LOGOP_PAGE_LOGICAL_UNDO;
99     }
100
101     /**
102         Write this out.
103         @exception IOException error writing to log stream
104     */

105     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
106     {
107         super.writeExternal(out);
108         CompressedNumber.writeInt(out, recordId);
109     }
110
111
112     /**
113         Read this in
114         @exception IOException error reading from log stream
115         @exception ClassNotFoundException log stream corrupted
116     */

117     public void readExternal(ObjectInput JavaDoc in)
118          throws IOException JavaDoc, ClassNotFoundException JavaDoc
119     {
120         super.readExternal(in);
121         recordId = CompressedNumber.readInt(in);
122     }
123
124     public void restoreMe(Transaction xact, BasePage undoPage,
125                           LogInstant CLRinstant, LimitObjectInput in)
126     {
127         // Not undoable
128
if (SanityManager.DEBUG)
129             SanityManager.THROWASSERT("cannot call restore me on PhysicalUndoOperation");
130     }
131
132     /**
133         Compensation methods
134     */

135
136     /** Set up a LogicalOperation during recovery redo. */
137     public void setUndoOp(Undoable op)
138     {
139         if (SanityManager.DEBUG) {
140             SanityManager.ASSERT(op instanceof LogicalPageOperation);
141         }
142
143         undoOp = (LogicalPageOperation)op;
144     }
145
146
147     /**
148         Loggable methods
149     */

150
151     /** Apply the undo operation, in this implementation of the
152         RawStore, it can only call the undoMe method of undoOp
153
154         @param xact the Transaction that is doing the rollback
155         @param instant the log instant of this undo operation
156         @param in optional data
157
158         @exception IOException Can be thrown by any of the methods of ObjectInput.
159         @exception StandardException Standard Cloudscape policy.
160
161      */

162     public final void doMe(Transaction xact, LogInstant instant, LimitObjectInput in)
163          throws StandardException, IOException JavaDoc
164     {
165
166         long oldversion = 0; // sanity check
167
LogInstant oldLogInstant = null; // sanity check
168
if (SanityManager.DEBUG)
169         {
170             oldLogInstant = this.page.getLastLogInstant();
171             oldversion = this.page.getPageVersion();
172
173             SanityManager.ASSERT(oldversion == this.getPageVersion());
174             SanityManager.ASSERT(oldLogInstant == null || instant == null
175                              || oldLogInstant.lessThan(instant));
176         }
177
178         // if this is called during runtime rollback, PageOp.generateUndo found
179
// the page and have it latched there.
180
// if this is called during recovery redo, this.needsRedo found the page and
181
// have it latched here
182
//
183
// in either case, this.page is the correct page and is latched.
184
//
185
// recordId is generated by generateUndo and is stored here. If this
186
// is a physical undo, recordId is identical to that which is stored in
187
// undoOp. If this is logical undo, it will be different if this.page
188
// is different from the undoOp's page (which is where the rollfoward
189
// change went to, and the record might have moved by now).
190
//
191
undoOp.undoMe(xact, this.page, recordId, instant, in);
192
193         if (SanityManager.DEBUG) {
194             SanityManager.ASSERT(oldversion < this.page.getPageVersion());
195             SanityManager.ASSERT(instant == null || instant.equals(this.page.getLastLogInstant()));
196         }
197
198         releaseResource(xact);
199     }
200
201     /* make sure resource found in undoOp is released */
202     public void releaseResource(Transaction xact)
203     {
204         if (undoOp != null)
205             undoOp.releaseResource(xact);
206         super.releaseResource(xact);
207     }
208
209     /* Undo operation is a COMPENSATION log operation */
210     public int group()
211     {
212         return super.group() | Loggable.COMPENSATION | Loggable.RAWSTORE;
213     }
214
215     public final ByteArray getPreparedLog() {
216         // should never ever write optional data because this implementation of
217
// the recovery system will never read this and pass this on to dome.
218
// Instead, the optional data of the undoOp will be used - since
219
// this.doMe can only call undoOP.undoMe, this has no use for any
220
// optional data.
221
return (ByteArray) null;
222     }
223
224     /**
225       DEBUG: Print self.
226     */

227     public String JavaDoc toString()
228     {
229         if (SanityManager.DEBUG)
230         {
231             String JavaDoc str = "CLR : (logical undo) " + super.toString() +
232                 " undoRecordId = " + recordId;
233             if (undoOp != null)
234                 str += "\n" + undoOp.toString();
235             else
236                 str += " undo Operation not set";
237             return str;
238         }
239         else
240             return null;
241     }
242
243 }
244
Popular Tags