KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreestorage > ContinuationBtreeExtent


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage;
20
21 import java.io.*;
22 import java.util.*;
23 import org.netbeans.mdr.util.Logger;
24
25 /**
26 * A ContinuationBtreeExtent in an extent which is the second or subsequent
27 * extent in a record.
28 *
29 * <p>
30 * Disk format:
31 * <ol>
32 * <li>
33 * bytes 0-7 generic BtreeExtent header
34 * <li>
35 * bytes 8-11 data length (DL)
36 * <li>
37 * bytes 12-11+DL data
38 * </ol>
39 */

40 class ContinuationBtreeExtent extends ActiveBtreeExtent {
41
42     /** size of fixed part of header */
43     static final int CONTINUATION_FIXED_LENGTH = 12;
44
45     /** data which will fit in biggest continuation extent */
46     static final int MAX_CONTINUATION_DATA_LENGTH =
47         BtreeDataFile.MAX_BYTES_IN_EXTENT - CONTINUATION_FIXED_LENGTH;
48
49     /** initialize a new ContinuationBtreeExtent
50     * @param file the BtreeDataFile this extent will belong to
51     * @param chunkNum where this extent begins
52     * @param numChunks the size of the extent
53     */

54     ContinuationBtreeExtent(BtreeDataFile file, int chunkNum, short numChunks) {
55         super(file, chunkNum, numChunks);
56         dataStart = CONTINUATION_FIXED_LENGTH;
57     }
58
59     /** Convert a deleted extent to an active one. The deleted extent has
60     * already been removed from its chain
61     * @param del the extent to convert
62     * @param length data length
63     */

64     ContinuationBtreeExtent(DeletedBtreeExtent del, int length) {
65         super(del);
66         setMyDataLength(length);
67         dataStart = CONTINUATION_FIXED_LENGTH;
68     }
69
70     /** create a new ContinuationBtreeExtent
71     * @param file the BtreeDataFile this extent will belong to
72     * @param chunkNum where this extent begins
73     * @param numChunks the size of the extent
74     * @param dLen how much data this extent will contain
75     */

76     ContinuationBtreeExtent(
77         BtreeDataFile file, int chunkNum, short numChunks, int dLen) {
78
79         super(file, chunkNum, numChunks);
80         dataStart = CONTINUATION_FIXED_LENGTH;
81         setMyDataLength(dLen);
82         headerIsDirty = true;
83     }
84
85     /** read the type-specific parts of the extent header from the
86     * buffer.
87     * @param buffer the buffer to read from
88     * @param offset the offset to being reading at
89     */

90     void readHeaderFromPage(byte buffer[], IntHolder offset) {
91         dataLength = Converter.readInt(buffer, offset);
92         if (dataLength > getAvailableDataLength()) {
93             Logger.getDefault().log("Bad data length read: ----------------------");
94             Logger.getDefault().log("chunk number: " + myChunkNum);
95             Logger.getDefault().log("dataLength: " + dataLength);
96             Logger.getDefault().log("available: " + getAvailableDataLength());
97         }
98     }
99
100     /** write the type-specific part of the header to the file cache
101     * @param page the page to write to
102     * @param offset the offset to begin an
103     */

104     protected void writeHeaderToPage(CachedPage page, int offset) {
105         Converter.writeInt(page.contents, offset, dataLength);
106     }
107
108     /** get the amount of data contained in this extent
109     * @return amount of data
110     */

111     int getMyDataLength() {
112         return dataLength;
113     }
114
115     /** get how much data this extent could contain
116     * @return maximum amount of data which would fit
117     */

118     int getAvailableDataLength() {
119         return (chunks * BtreeDataFile.BTREE_CHUNK_SIZE) -
120                 CONTINUATION_FIXED_LENGTH;
121     }
122
123     /** Get the magic number for this type of extent
124     * @return the magic number
125     */

126     short getMagic() {
127       return CONTINUATION_MAGIC;
128     }
129
130     /** return the number of chunks an extent with this much data would
131     * occupy. If an extent of maximum size wouldn't hold this much,
132     * return the maximum extent size.
133     * @param dataLength amount of data to hold.
134     * @return size of extent in chunks
135     */

136     static int getNumChunks(int dataLength) {
137         int size = (dataLength + CONTINUATION_FIXED_LENGTH - 1) /
138                             BtreeDataFile.BTREE_CHUNK_SIZE + 1;
139         return Math.min(size, BtreeDataFile.MAX_CHUNKS_IN_EXTENT);
140     }
141
142     /** set the amount of data contained in this extent
143     * @param length amount of data
144     */

145     int setMyDataLength(int length) {
146         int oldDatalength = dataLength;
147         dataLength = Math.min(length, getAvailableDataLength());
148         if (oldDatalength != dataLength) {
149             headerIsDirty = true;
150         }
151         return dataLength;
152     }
153
154     /** is the extent already full of data
155     * @return true if the extent has no room for more data
156     */

157     boolean isMaximum() {
158         return dataLength == MAX_CONTINUATION_DATA_LENGTH;
159     }
160
161     /** return type of extent
162     * @return IS_CONTINUATION
163     */

164     byte getType() {
165         return IS_CONTINUATION;
166     }
167
168     /** return name of type of extent
169     * @return CONTINUATION_NAME
170     */

171     String JavaDoc getTypeName() {
172         return CONTINUATION_NAME;
173     }
174 }
175
Popular Tags