001: /*
002:
003: Derby - Class org.apache.derby.impl.store.raw.data.InitPageOperation
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.store.raw.data;
023:
024: import org.apache.derby.impl.store.raw.data.BasePage;
025:
026: import org.apache.derby.iapi.services.sanity.SanityManager;
027: import org.apache.derby.iapi.services.io.FormatIdUtil;
028: import org.apache.derby.iapi.services.io.StoredFormatIds;
029:
030: import org.apache.derby.iapi.error.StandardException;
031:
032: import org.apache.derby.iapi.store.raw.RecordHandle;
033: import org.apache.derby.iapi.store.raw.Page;
034: import org.apache.derby.iapi.store.raw.Transaction;
035:
036: import org.apache.derby.iapi.store.raw.log.LogInstant;
037:
038: import org.apache.derby.iapi.services.io.CompressedNumber;
039:
040: import java.io.OutputStream;
041: import java.io.ObjectOutput;
042: import java.io.ObjectInput;
043: import java.io.IOException;
044: import org.apache.derby.iapi.services.io.LimitObjectInput;
045:
046: /**
047: This operation initializes the page that is being allocated,
048: this operation does not change the alloc page information.
049:
050: <PRE>
051: @format_id LOGOP_INIT_PAGE
052: the formatId is written by FormatIdOutputStream when this object is
053: written out by writeObject
054: @purpose initialized a page
055: @upgrade
056: @disk_layout
057: PhysicalPageOperation the superclass
058: nextRecordId(CompressedInt) the next recordId this page should give out
059: initFlag(CompressedInt) initialization flag: reuse, overflow
060: pageformat(int) the page's formatId
061:
062: OptionalData none
063: @end_format
064: </PRE>
065: */
066: public final class InitPageOperation extends PhysicalPageOperation {
067: protected int nextRecordId; // next recordId
068: protected int initFlag;
069: protected int pageFormatId;
070: protected long pageOffset;
071:
072: protected boolean reuse; // is this page being initialize for reuse, or for first time
073: protected boolean overflowPage; // is this page an overflow page
074:
075: public InitPageOperation(BasePage page, int flag, int formatid,
076: long offset) throws StandardException {
077: super (page);
078:
079: initFlag = flag;
080: pageFormatId = formatid;
081: pageOffset = offset;
082:
083: // unless we specified recordId should be reusable, when we reuse a
084: // page, we keep incrementing the existing recordId
085: if ((initFlag & BasePage.INIT_PAGE_REUSE_RECORDID) == 0)
086: nextRecordId = page.newRecordId();
087: else
088: nextRecordId = RecordHandle.FIRST_RECORD_ID;
089: }
090:
091: /*
092: * Formatable methods
093: */
094:
095: // no-arg constructor, required by Formatable
096: public InitPageOperation() {
097: super ();
098: }
099:
100: /**
101: Write this out.
102: @exception IOException error writing to log stream
103: */
104: public void writeExternal(ObjectOutput out) throws IOException {
105: super .writeExternal(out);
106: CompressedNumber.writeInt(out, nextRecordId);
107: CompressedNumber.writeInt(out, initFlag);
108: CompressedNumber.writeLong(out, pageOffset);
109: out.writeInt(pageFormatId);
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 in) throws IOException,
118: ClassNotFoundException {
119: super .readExternal(in);
120: nextRecordId = CompressedNumber.readInt(in);
121: initFlag = CompressedNumber.readInt(in);
122: pageOffset = CompressedNumber.readLong(in);
123: pageFormatId = in.readInt();
124: }
125:
126: /**
127: Return my format identifier.
128: */
129: public int getTypeFormatId() {
130: return StoredFormatIds.LOGOP_INIT_PAGE;
131: }
132:
133: /*
134: * Loggable methods
135: */
136: /**
137: Mark the page as valid, and clear out any crud from the page
138:
139: @exception IOException Can be thrown by any of the methods of ObjectInput.
140: @exception StandardException Standard Cloudscape policy.
141:
142: @see org.apache.derby.iapi.store.raw.Loggable#doMe
143: */
144: public void doMe(Transaction xact, LogInstant instant,
145: LimitObjectInput in) throws StandardException, IOException {
146: boolean overflowPage = ((initFlag & BasePage.INIT_PAGE_OVERFLOW) != 0);
147: boolean reuse = ((initFlag & BasePage.INIT_PAGE_REUSE) != 0);
148:
149: this .page.initPage(instant, BasePage.VALID_PAGE, nextRecordId,
150: overflowPage, reuse);
151: }
152:
153: /*
154: * Override PageBasicOperation's getPageForRedoRecovery
155: */
156: /**
157: If we are in load tran, this page may not exist for the container yet.
158: We need to create it first.
159:
160: This routine is called as the last resort of find page, the container
161: handle has already been found and it is not dropped.
162:
163: @exception StandardException Standard Cloudscape policy.
164: */
165: protected BasePage getPageForRedoRecovery(Transaction xact)
166: throws StandardException {
167: BasePage p = super .getPageForRedoRecovery(xact);
168: if (p != null)
169: return p;
170:
171: // create the page
172: // RESOLVE: we need the page format to properly recreate an Alloc page
173: // NEED TO UPGRADE this log record.
174: p = (BasePage) containerHdl.reCreatePageForRedoRecovery(
175: pageFormatId, getPageId().getPageNumber(), pageOffset);
176: return p;
177: }
178:
179: /*
180: * PhysicalPageOperation method
181: */
182:
183: /**
184: Mark the page as free
185:
186: @exception StandardException Thrown by methods I call
187: @exception IOException Thrown by methods I call
188:
189: @see PhysicalPageOperation#undoMe
190: */
191: public void undoMe(Transaction xact, BasePage undoPage,
192: LogInstant CLRInstant, LimitObjectInput in)
193: throws StandardException, IOException {
194: undoPage.setPageStatus(CLRInstant, BasePage.INVALID_PAGE);
195: // only set the page to invalid, cannot wipe out the page to zero's
196: // becuase recovery may need to redo some operations that depend on the
197: // content of the page.
198:
199: undoPage.setAuxObject(null);
200: }
201:
202: /*
203: * PageBasicOperation methods
204: */
205:
206: /**
207: * restore the before image of the page
208: *
209: * @exception StandardException Standard Cloudscape Error Policy
210: * @exception IOException problem reading the complete log record from the
211: * input stream
212: */
213: public void restoreMe(Transaction xact, BasePage undoPage,
214: LogInstant CLRInstant, LimitObjectInput in)
215: throws StandardException, IOException {
216: undoMe(xact, undoPage, CLRInstant, in);
217: }
218:
219: public String toString() {
220: if (SanityManager.DEBUG) {
221: boolean overflowPage = ((initFlag & BasePage.INIT_PAGE_OVERFLOW) != 0);
222: boolean reuse = ((initFlag & BasePage.INIT_PAGE_REUSE) != 0);
223:
224: return super .toString() + "Init Page. Overflow = "
225: + overflowPage + " reuse " + reuse
226: + " nextRecordId " + nextRecordId;
227: } else
228: return null;
229: }
230:
231: }
|