KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.ContainerBasicOperation
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.iapi.reference.SQLState;
25
26 import org.apache.derby.iapi.store.raw.ContainerKey;
27
28 import org.apache.derby.iapi.store.raw.ContainerHandle;
29 import org.apache.derby.iapi.store.raw.Loggable;
30 import org.apache.derby.iapi.store.raw.LockingPolicy;
31 import org.apache.derby.iapi.store.raw.Transaction;
32
33 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
34 import org.apache.derby.iapi.store.raw.data.RawContainerHandle;
35
36 import org.apache.derby.iapi.error.StandardException;
37 import org.apache.derby.iapi.services.sanity.SanityManager;
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.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45 import java.io.IOException JavaDoc;
46
47 /**
48 A Container Operation change the state of the container.
49 A ContainerBasicOperation is the base class for all container operations.
50 */

51
52 public abstract class ContainerBasicOperation implements Loggable
53 {
54     /* page info this operation changed */
55     private long containerVersion;
56     protected ContainerKey containerId;
57
58     transient protected RawContainerHandle containerHdl = null;
59     transient private boolean foundHere = false;
60
61     protected ContainerBasicOperation(RawContainerHandle hdl) throws StandardException
62     {
63         containerHdl = hdl;
64         containerId = hdl.getId();
65         containerVersion = hdl.getContainerVersion();
66     }
67
68     /*
69      * Formatable methods
70      */

71
72     // no-arg constructor, required by Formatable
73
public ContainerBasicOperation() { super(); }
74
75     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
76     {
77         containerId.writeExternal(out);
78         CompressedNumber.writeLong(out, containerVersion);
79     }
80
81     public void readExternal(ObjectInput JavaDoc in)
82          throws IOException JavaDoc, ClassNotFoundException JavaDoc
83     {
84         containerId = ContainerKey.read(in);
85         containerVersion = CompressedNumber.readLong(in);
86     }
87
88
89     /**
90         Loggable methods
91     */

92
93     /**
94         the default for prepared log is always null for all the operations
95         that don't have optionalData. If an operation has optional data,
96         the operation need to prepare the optional data for this method.
97
98         Space Operation has no optional data to write out
99     */

100     public ByteArray getPreparedLog()
101     {
102         return (ByteArray) null;
103     }
104
105     public void releaseResource(Transaction tran)
106     {
107         if (!foundHere)
108             return;
109
110         if (containerHdl != null)
111         {
112             containerHdl.close();
113             containerHdl = null;
114         }
115
116         foundHere = false;
117     }
118
119     /**
120         A space operation is a RAWSTORE log record
121     */

122     public int group()
123     {
124         return Loggable.RAWSTORE;
125     }
126
127     /**
128         Methods specific to this class
129     */

130
131     /**
132       Open the container with this segmentId and containerId.
133       This method should only be called if the container has already been
134       created.
135
136       @exception StandardException the container cannot be found or cannot be
137       opened.
138      */

139     protected RawContainerHandle findContainer(Transaction tran)
140          throws StandardException
141     {
142         releaseResource(tran);
143
144         RawTransaction rtran = (RawTransaction)tran;
145         containerHdl = rtran.openDroppedContainer(
146             containerId, (LockingPolicy) null);
147
148         //If we are in roll forward recovery, missing container will be
149
//recreated becuase we might have hit a log record which has a
150
//reused the container id that was dropped earlier.
151
if (rtran.inRollForwardRecovery())
152         {
153             if (containerHdl == null)
154             {
155                 if (SanityManager.DEBUG)
156                 {
157                     if(SanityManager.DEBUG_ON("LoadTran"))
158                     {
159                         SanityManager.DEBUG_PRINT(
160                             "Trace",
161                             "cannot find container " + containerId +
162                                   ", now attempt last ditch effort");
163                     }
164                 }
165                 
166
167                 containerHdl = findContainerForRedoRecovery(rtran);
168
169                 if (SanityManager.DEBUG)
170                 {
171                     if(SanityManager.DEBUG_ON("LoadTran"))
172                     {
173                         SanityManager.DEBUG_PRINT("Trace",
174                             " findContainerForRedoRecovery, got container=" +
175                             (containerHdl != null));
176                     }
177                 }
178
179             }
180         }
181         
182         if (containerHdl == null)
183         {
184             throw StandardException.newException(
185                     SQLState.DATA_CONTAINER_VANISHED, containerId);
186         }
187
188         foundHere = true;
189         return containerHdl;
190     }
191
192     /**
193         Subclass (e.g., ContainerOperation) that wishes to do something abou
194         missing container in load tran should override this method to return
195         the recreated container
196
197         @exception StandardException Cloudscape Standard error policy
198      */

199     protected RawContainerHandle findContainerForRedoRecovery(
200     RawTransaction tran)
201          throws StandardException
202     {
203         return null;
204     }
205     
206
207     /**
208         @exception StandardException Standard Cloudscape error policy
209     */

210     public boolean needsRedo(Transaction xact)
211          throws StandardException
212     {
213         findContainer(xact);
214
215         long cVersion = containerHdl.getContainerVersion();
216
217         if (cVersion == containerVersion)
218             return true;
219
220         releaseResource(xact);
221
222         if (cVersion > containerVersion)
223             return false;
224         else
225         {
226             // RESOLVE - correct error handling
227
if (SanityManager.DEBUG)
228             {
229                 SanityManager.THROWASSERT("log corrupted, missing log record: "+
230                                           "log container version = " +
231                                           containerVersion +
232                                           " container header version " + cVersion);
233             }
234             return false;
235         }
236     }
237
238
239     public String JavaDoc toString()
240     {
241         if (SanityManager.DEBUG)
242         {
243             return "Space Operation: " + containerId ;
244         }
245         else
246             return null;
247     }
248
249 }
250
251
252
Popular Tags