KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.InitPageOperation
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.sanity.SanityManager;
27 import org.apache.derby.iapi.services.io.FormatIdUtil;
28 import org.apache.derby.iapi.services.io.StoredFormatIds;
29
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.store.raw.RecordHandle;
33 import org.apache.derby.iapi.store.raw.Page;
34 import org.apache.derby.iapi.store.raw.Transaction;
35
36 import org.apache.derby.iapi.store.raw.log.LogInstant;
37
38 import org.apache.derby.iapi.services.io.CompressedNumber;
39
40 import java.io.OutputStream JavaDoc;
41 import java.io.ObjectOutput 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     This operation initializes the page that is being allocated,
48     this operation does not change the alloc page information.
49
50     <PRE>
51     @format_id LOGOP_INIT_PAGE
52         the formatId is written by FormatIdOutputStream when this object is
53         written out by writeObject
54     @purpose initialized a page
55     @upgrade
56     @disk_layout
57         PhysicalPageOperation the superclass
58         nextRecordId(CompressedInt) the next recordId this page should give out
59         initFlag(CompressedInt) initialization flag: reuse, overflow
60         pageformat(int) the page's formatId
61
62         OptionalData none
63     @end_format
64     </PRE>
65 */

66 public final class InitPageOperation extends PhysicalPageOperation
67 {
68     protected int nextRecordId; // next recordId
69
protected int initFlag;
70     protected int pageFormatId;
71     protected long pageOffset;
72
73     protected boolean reuse; // is this page being initialize for reuse, or for first time
74
protected boolean overflowPage; // is this page an overflow page
75

76     public InitPageOperation(BasePage page, int flag, int formatid,
77                              long offset)
78          throws StandardException
79     {
80         super(page);
81
82         initFlag = flag;
83         pageFormatId = formatid;
84         pageOffset = offset;
85
86         // unless we specified recordId should be reusable, when we reuse a
87
// page, we keep incrementing the existing recordId
88
if ((initFlag & BasePage.INIT_PAGE_REUSE_RECORDID) == 0)
89             nextRecordId = page.newRecordId();
90         else
91             nextRecordId = RecordHandle.FIRST_RECORD_ID;
92     }
93
94     /*
95      * Formatable methods
96      */

97
98     // no-arg constructor, required by Formatable
99
public InitPageOperation() { super(); }
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, nextRecordId);
109         CompressedNumber.writeInt(out, initFlag);
110         CompressedNumber.writeLong(out, pageOffset);
111         out.writeInt(pageFormatId);
112     }
113
114     /**
115         Read this in
116         @exception IOException error reading from log stream
117         @exception ClassNotFoundException log stream corrupted
118     */

119     public void readExternal(ObjectInput JavaDoc in)
120          throws IOException JavaDoc, ClassNotFoundException JavaDoc
121     {
122         super.readExternal(in);
123         nextRecordId = CompressedNumber.readInt(in);
124         initFlag = CompressedNumber.readInt(in);
125         pageOffset = CompressedNumber.readLong(in);
126         pageFormatId = in.readInt();
127     }
128
129     /**
130         Return my format identifier.
131     */

132     public int getTypeFormatId() {
133         return StoredFormatIds.LOGOP_INIT_PAGE;
134     }
135     /*
136      * Loggable methods
137      */

138     /**
139         Mark the page as valid, and clear out any crud from the page
140
141         @exception IOException Can be thrown by any of the methods of ObjectInput.
142         @exception StandardException Standard Cloudscape policy.
143
144         @see org.apache.derby.iapi.store.raw.Loggable#doMe
145     */

146     public void doMe(Transaction xact, LogInstant instant, LimitObjectInput in)
147          throws StandardException, IOException JavaDoc
148     {
149         boolean overflowPage = ((initFlag & BasePage.INIT_PAGE_OVERFLOW) != 0);
150         boolean reuse = ((initFlag & BasePage.INIT_PAGE_REUSE) != 0);
151
152         this.page.initPage(instant,
153                            BasePage.VALID_PAGE,
154                            nextRecordId,
155                            overflowPage, reuse);
156     }
157
158     /*
159      * Override PageBasicOperation's getPageForRedoRecovery
160      */

161     /**
162         If we are in load tran, this page may not exist for the container yet.
163         We need to create it first.
164
165         This routine is called as the last resort of find page, the container
166         handle has already been found and it is not dropped.
167
168         @exception StandardException Standard Cloudscape policy.
169     */

170     protected BasePage getPageForRedoRecovery(Transaction xact)
171          throws StandardException
172     {
173         BasePage p = super.getPageForRedoRecovery(xact);
174         if (p != null)
175             return p;
176
177         // create the page
178
// RESOLVE: we need the page format to properly recreate an Alloc page
179
// NEED TO UPGRADE this log record.
180
p = (BasePage)containerHdl.reCreatePageForRedoRecovery(
181                         pageFormatId,
182                         getPageId().getPageNumber(),
183                         pageOffset);
184         return p;
185     }
186     
187
188     /*
189      * PhysicalPageOperation method
190      */

191
192     /**
193         Mark the page as free
194
195         @exception StandardException Thrown by methods I call
196         @exception IOException Thrown by methods I call
197
198         @see PhysicalPageOperation#undoMe
199     */

200     public void undoMe(Transaction xact, BasePage undoPage, LogInstant CLRInstant,
201                        LimitObjectInput in)
202          throws StandardException, IOException JavaDoc
203     {
204         undoPage.setPageStatus(CLRInstant, BasePage.INVALID_PAGE);
205         // only set the page to invalid, cannot wipe out the page to zero's
206
// becuase recovery may need to redo some operations that depend on the
207
// content of the page.
208

209         undoPage.setAuxObject(null);
210     }
211
212
213     /*
214      * PageBasicOperation methods
215      */

216     
217     /**
218      * restore the before image of the page
219      *
220      * @exception StandardException Standard Cloudscape Error Policy
221      * @exception IOException problem reading the complete log record from the
222      * input stream
223      */

224     public void restoreMe(Transaction xact, BasePage undoPage,
225                        LogInstant CLRInstant, LimitObjectInput in)
226          throws StandardException, IOException JavaDoc
227     {
228         undoMe(xact, undoPage, CLRInstant, in);
229     }
230
231     public String JavaDoc toString()
232     {
233         if (SanityManager.DEBUG)
234         {
235             boolean overflowPage = ((initFlag & BasePage.INIT_PAGE_OVERFLOW) != 0);
236             boolean reuse = ((initFlag & BasePage.INIT_PAGE_REUSE) != 0);
237
238             return super.toString() + "Init Page. Overflow = "
239                 + overflowPage + " reuse " + reuse + " nextRecordId " + nextRecordId;
240         }
241         else
242             return null;
243     }
244
245 }
246
Popular Tags