1 19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage; 20 21 import java.io.*; 22 import java.text.*; 23 import java.util.*; 24 25 import org.netbeans.mdr.persistence.*; 26 55 class NormalBtreeExtent extends ActiveBtreeExtent { 56 57 58 static final int NORMAL_FIXED_LENGTH = 18; 59 60 61 static final int MAX_KEY_LENGTH = BtreeDataFile.BTREE_CHUNK_SIZE - NORMAL_FIXED_LENGTH; 62 63 64 byte key[]; 65 66 67 int totalLength; 68 69 74 NormalBtreeExtent(BtreeDataFile file, int chunkNum, short numChunks) { 75 super(file, chunkNum, numChunks); 76 } 77 78 83 NormalBtreeExtent(DeletedBtreeExtent del, byte k[], int length) { 84 super(del); 85 key = k; 86 dataStart = NORMAL_FIXED_LENGTH + key.length; 87 setMyDataLength(length); 88 } 89 90 98 NormalBtreeExtent( 99 BtreeDataFile file, int chunkNum, short numChunks, int totLen, byte k[]) 100 throws StorageException { 101 102 super(file, chunkNum, numChunks); 103 if (k.length > MAX_KEY_LENGTH) { 104 throw new StorageBadRequestException( 105 MessageFormat.format("Invalid key length: {0}", 106 new Object [] {new Integer (k.length)} )); 107 } 108 key = k; 109 setMyDataLength(totLen); 110 totalLength = totLen; 111 dataStart = NORMAL_FIXED_LENGTH + key.length; 112 this.headerIsDirty = true; 113 } 114 115 120 void readHeaderFromPage(byte buffer[], IntHolder offset) { 121 short keyLen = Converter.readShort(buffer, offset); 122 key = new byte[keyLen]; 123 dataLength = Converter.readInt(buffer, offset); 124 totalLength = Converter.readInt(buffer, offset); 125 System.arraycopy(buffer, offset.getValue(), key, 0, keyLen); 126 dataStart = NORMAL_FIXED_LENGTH + key.length; 127 } 128 129 133 protected void writeHeaderToPage(CachedPage page, int offset) { 134 offset = Converter.writeShort(page.contents, offset, (short)key.length); 135 offset = Converter.writeInt(page.contents, offset, dataLength); 136 offset = Converter.writeInt(page.contents, offset, totalLength); 137 System.arraycopy(key, 0, page.contents, offset, key.length); 138 } 139 140 143 int getMyDataLength() { 144 return dataLength; 145 } 146 147 150 int getTotalDataLength() { 151 return totalLength; 152 } 153 154 157 int getAvailableDataLength() { 158 return getAvailableDataLength(chunks, key.length); 159 } 160 161 164 short getMagic() { 165 return NORMAL_MAGIC; 166 } 167 168 174 static int getNumChunks(int keyLength, int dataLength) { 175 int size = (keyLength + dataLength + NORMAL_FIXED_LENGTH - 1) / 176 BtreeDataFile.BTREE_CHUNK_SIZE + 1; 177 return Math.min(size, BtreeDataFile.MAX_CHUNKS_IN_EXTENT); 178 } 179 180 187 static int getAvailableDataLength(int numChunks, int keyLength) { 188 return (BtreeDataFile.BTREE_CHUNK_SIZE * numChunks) 189 - NORMAL_FIXED_LENGTH - keyLength; 190 } 191 192 195 int setMyDataLength(int length) { 196 int oldDataLength = dataLength; 197 dataLength = Math.min(length, getAvailableDataLength()); 198 if (dataLength != oldDataLength) { 199 headerIsDirty = true; 200 } 201 if (totalLength != length) { 202 totalLength = length; 203 headerIsDirty = true; 204 } 205 return dataLength; 206 } 207 208 211 boolean isMaximum() { 212 return key.length + dataLength + NORMAL_FIXED_LENGTH 213 == BtreeDataFile.MAX_BYTES_IN_EXTENT; 214 } 215 216 219 byte getType() { 220 return IS_NORMAL; 221 } 222 223 226 String getTypeName() { 227 return NORMAL_NAME; 228 } 229 230 235 void dump(int level, PrintWriter strm) 236 throws StorageException{ 237 238 super.dump(level, strm); 239 if ((level & DUMP_BASIC) != 0) { 240 strm.println("" + totalLength + " bytes in record"); 241 strm.println("" + key.length + " bytes in key"); 242 } 243 244 if ((level & DUMP_KEY) != 0) { 245 dumpBytesAsHex(new ByteArrayInputStream(key), strm, "\t"); 246 } 247 } 248 } 249 | Popular Tags |