1 19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage; 20 21 import java.io.*; 22 import java.util.*; 23 import java.util.zip.*; 24 25 import org.netbeans.mdr.persistence.*; 26 import org.netbeans.mdr.util.Logger; 27 28 31 abstract class ActiveBtreeExtent extends BtreeExtent { 32 33 34 int dataLength; 35 36 37 int dataStart; 38 39 43 ActiveBtreeExtent(DeletedBtreeExtent del) { 44 super(del); 45 headerIsDirty = true; 46 } 47 48 49 54 ActiveBtreeExtent( 55 BtreeDataFile file, int chunkNum, short numChunks) { 56 57 super(file, chunkNum, numChunks); 58 } 59 60 63 abstract int getMyDataLength(); 64 65 68 abstract int setMyDataLength(int length); 69 70 73 abstract int getAvailableDataLength(); 74 75 80 void writeData(byte dataBuffer[], int dataOffset) 81 throws StorageException { 82 int toCopy = dataLength; 83 IntHolder offst = new IntHolder(); 84 int numChunks = 85 (dataStart + toCopy - 1) / BtreeDataFile.BTREE_CHUNK_SIZE + 1; 86 87 if (numChunks > chunks) { 88 StorageException se = new StoragePersistentDataException ("Number of chunks does not match."); 89 Logger.getDefault().annotate(se, "Bad number of chunks: ----------------------"); 90 Logger.getDefault().annotate(se, "start chunk number: " + myChunkNum); 91 Logger.getDefault().annotate(se, "#chunks: " + chunks + " computed #chunks: " + numChunks); 92 Logger.getDefault().annotate(se, "dataLength: " + dataLength + " dataSart: " + dataStart); 93 throw se; 94 } 95 96 CachedPage pages[] = owner.getChunks(myChunkNum, numChunks, offst); 97 try { 98 int pageNum = 0; 99 int pageSize = pages[0].contents.length; 100 int offset = offst.getValue() + dataStart; 101 while (offset >= pageSize) { 102 pageNum++; 103 offset -= pageSize; 104 } 105 while (toCopy > 0) { 106 int thisPage = Math.min(pageSize - offset, toCopy); 107 pages[pageNum].setWritable(); 108 System.arraycopy(dataBuffer, dataOffset, 109 pages[pageNum].contents, offset, thisPage); 110 dataOffset += thisPage; 111 toCopy -= thisPage; 112 pageNum++; 113 offset = 0; 114 } 115 } 116 finally { 117 for (int i = 0; i < pages.length; i++) { 118 pages[i].unpin(); 119 } 120 } 121 } 122 123 127 void addToStream(CachedPageInputStream strm) throws StorageException { 128 IntHolder offst = new IntHolder(); 129 int toAppend = getMyDataLength(); 130 int numChunks = 131 (dataStart + toAppend - 1) / BtreeDataFile.BTREE_CHUNK_SIZE + 1; 132 CachedPage[] pages = owner.getChunks(myChunkNum, numChunks, offst); 133 134 if (numChunks > chunks) { 135 StorageException se = new StoragePersistentDataException ("Number of chunks does not match."); 136 Logger.getDefault().annotate(se, "Bad number of chunks: ----------------------"); 137 Logger.getDefault().annotate(se, "start chunk number: " + myChunkNum); 138 Logger.getDefault().annotate(se, "#chunks: " + chunks + " computed #chunks: " + numChunks); 139 Logger.getDefault().annotate(se, "dataLength: " + dataLength + " dataSart: " + dataStart); 140 throw se; 141 } 142 143 int pageNum = 0; 144 int pageSize = pages[0].contents.length; 145 int offset = offst.getValue() + dataStart; 146 while (offset >= pageSize) { 147 pageNum++; 148 offset -= pageSize; 149 } 150 151 for (; pageNum < pages.length; pageNum++) { 152 int thisPage = Math.min(pageSize - offset, toAppend); 153 strm.addPage(pages[pageNum], offset, thisPage); 154 offset = 0; 155 toAppend -= thisPage; 156 } 157 158 for (; pageNum < pages.length; pageNum++) { 159 pages[pageNum].unpin(); 160 } 161 162 } 163 164 165 168 long getCRC() throws StorageException { 169 CachedPageInputStream dstrm = new CachedPageInputStream(); 170 CheckedInputStream cis = null; 171 try { 172 try { 173 addToStream(dstrm); 174 cis = new CheckedInputStream(dstrm, new CRC32()); 175 while (cis.read() >= 0) 176 ; 177 return cis.getChecksum().getValue(); 178 } 179 finally { 180 if (cis != null) 181 cis.close(); 182 else 183 dstrm.close(); 184 } 185 } 186 catch (IOException exc) { 187 throw new StorageIOException(exc); 188 } 189 } 190 191 194 abstract boolean isMaximum(); 195 196 201 void dump(int level, PrintWriter strm) throws StorageException{ 202 super.dump(level, strm); 203 boolean dumpData = (level & DUMP_DATA) != 0; 204 boolean showCheckSum = (level & DUMP_DATA_CHECKSUM) != 0; 205 206 strm.println("" + dataLength + " data bytes"); 207 if (dumpData) { 208 CachedPageInputStream dstrm = new CachedPageInputStream(); 209 try { 210 try { 211 addToStream(dstrm); 212 if (dumpData) { 213 dumpBytesAsHex(dstrm, strm, "\t"); 214 strm.println(); 215 } 216 } 217 finally { 218 dstrm.close(); 219 } 220 } 221 catch (IOException exc) { 222 throw new StorageIOException(exc); 223 } 224 } 225 226 if (showCheckSum) { 227 strm.println("Data checksum: " + getCRC()); 228 strm.println(); 229 } 230 } 231 } 232 | Popular Tags |