KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.PhysicalUndoOperation
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.Page;
33 import org.apache.derby.iapi.store.raw.Transaction;
34 import org.apache.derby.iapi.store.raw.Undoable;
35
36 import org.apache.derby.iapi.store.raw.log.LogInstant;
37
38 import org.apache.derby.iapi.error.StandardException;
39 import org.apache.derby.iapi.util.ByteArray;
40
41 import java.io.OutputStream JavaDoc;
42 import java.io.ObjectInput JavaDoc;
43 import java.io.IOException JavaDoc;
44 import org.apache.derby.iapi.services.io.LimitObjectInput;
45
46
47 /**
48     PhysicalUndoOperation is a compensation operation that rolls back the change of
49     an Undo-able operation. A PhysicalUndoOperation itself is not undo-able, i.e,
50     it is loggable but not undoable.
51
52     <PRE>
53     @format_id LOGOP_PAGE_PHYSICAL_UNDO
54         the formatId is written by FormatIdOutputStream when this object is
55         written out by writeObject
56     @purpose update a physiacl log operation
57     @upgrade
58     @disk_layout
59         PageBasicOperation the super class
60         OptionalData none (compensation operation never have optional data)
61     @end_format
62     </PRE>
63
64 */

65 public class PhysicalUndoOperation extends PageBasicOperation implements Compensation {
66
67     /** The operation to be rolled back */
68     transient private PhysicalPageOperation undoOp;
69
70     protected PhysicalUndoOperation(BasePage page)
71     {
72         super(page);
73     }
74
75     /** Set up a compensation operation during run time rollback */
76     public PhysicalUndoOperation(BasePage page, PhysicalPageOperation op)
77     {
78         super(page);
79         undoOp = op;
80     }
81
82     /**
83         Return my format identifier.
84     */

85
86     // no-arg constructor, required by Formatable
87
public PhysicalUndoOperation() { super(); }
88
89     public int getTypeFormatId() {
90         return StoredFormatIds.LOGOP_PAGE_PHYSICAL_UNDO;
91     }
92
93     // no fields, therefore no writeExternal or readExternal
94

95     /**
96         Compensation methods
97     */

98
99     /** Set up a PageUndoOperation during recovery redo. */
100     public void setUndoOp(Undoable op)
101     {
102         if (SanityManager.DEBUG) {
103             SanityManager.ASSERT(op instanceof PhysicalPageOperation);
104         }
105         undoOp = (PhysicalPageOperation)op;
106     }
107
108
109     /**
110         Loggable methods
111     */

112
113     /** Apply the undo operation, in this implementation of the
114         RawStore, it can only call the undoMe method of undoOp
115
116         @param xact the Transaction that is doing the rollback
117         @param instant the log instant of this undo operation
118         @param in optional data
119
120         @exception IOException Can be thrown by any of the methods of InputStream.
121         @exception StandardException Standard Cloudscape policy.
122
123      */

124     public final void doMe(Transaction xact, LogInstant instant, LimitObjectInput in)
125          throws StandardException, IOException JavaDoc
126     {
127
128         long oldversion = 0; // sanity check
129
LogInstant oldLogInstant = null; // sanity check
130
if (SanityManager.DEBUG)
131         {
132             oldLogInstant = this.page.getLastLogInstant();
133             oldversion = this.page.getPageVersion();
134
135             SanityManager.ASSERT(oldversion == this.getPageVersion());
136             SanityManager.ASSERT(oldLogInstant == null || instant == null
137                              || oldLogInstant.lessThan(instant));
138         }
139
140         // if this is called during runtime rollback, PageOp.generateUndo found
141
// the page and have it latched there.
142
// if this is called during recovery redo, this.needsRedo found the page and
143
// have it latched here
144
//
145
// in either case, this.page is the correct page and is latched.
146
//
147
undoOp.undoMe(xact, this.page, instant, in);
148
149         if (SanityManager.DEBUG) {
150
151             if (oldversion >= this.page.getPageVersion())
152             {
153                 SanityManager.THROWASSERT(
154                     "oldversion = " + oldversion +
155                     ";page version = " + this.page.getPageVersion() +
156                     "page = " + page +
157                     "; my class name is " + getClass().getName() +
158                     " undoOp is " + undoOp.getClass().getName() );
159             }
160             SanityManager.ASSERT(
161                 oldversion < this.page.getPageVersion());
162
163             if (instant != null &&
164                 ! instant.equals(this.page.getLastLogInstant()))
165                 SanityManager.THROWASSERT(
166                                  "my class name is " + getClass().getName() +
167                                  " undoOp is " + undoOp.getClass().getName() );
168         }
169
170         releaseResource(xact);
171     }
172
173     /* make sure resource found in undoOp is released */
174     public void releaseResource(Transaction xact)
175     {
176         if (undoOp != null)
177             undoOp.releaseResource(xact);
178         super.releaseResource(xact);
179     }
180
181     /* Undo operation is a COMPENSATION log operation */
182     public int group()
183     {
184         return super.group() | Loggable.COMPENSATION | Loggable.RAWSTORE;
185     }
186
187     public final ByteArray getPreparedLog() {
188         // should never ever write optional data because this implementation of
189
// the recovery system will never read this and pass this on to dome.
190
// Instead, the optional data of the undoOp will be used - since
191
// this.doMe can only call undoOP.undoMe, this has no use for any
192
// optional data.
193
return (ByteArray) null;
194     }
195
196     public void restoreMe(Transaction xact, BasePage undoPage,
197                           LogInstant CLRinstant, LimitObjectInput in)
198     {
199         // Not undoable
200
if (SanityManager.DEBUG)
201             SanityManager.THROWASSERT("cannot call restore me on PhysicalUndoOperation");
202     }
203
204     /**
205       DEBUG: Print self.
206     */

207     public String JavaDoc toString()
208     {
209         if (SanityManager.DEBUG)
210         {
211             String JavaDoc str = "CLR (Physical Undo): " + super.toString();
212             if (undoOp != null)
213                 str += "\n" + undoOp.toString();
214             else
215                 str += "undo Operation not set";
216
217             return str;
218         }
219         else
220             return null;
221     }
222
223 }
224
Popular Tags