1 21 package com.db4o.header; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 27 28 31 public class FileHeader1 extends FileHeader { 32 33 35 50 private static final byte[] SIGNATURE = {(byte)'d', (byte)'b', (byte)'4', (byte)'o'}; 51 52 private static byte VERSION = 1; 53 54 private static final int HEADER_LOCK_OFFSET = SIGNATURE.length + 1; 55 private static final int OPEN_TIME_OFFSET = HEADER_LOCK_OFFSET + YapConst.INT_LENGTH; 56 private static final int ACCESS_TIME_OFFSET = OPEN_TIME_OFFSET + YapConst.LONG_LENGTH; 57 private static final int TRANSACTION_POINTER_OFFSET = ACCESS_TIME_OFFSET + YapConst.LONG_LENGTH; 58 59 static final int LENGTH = TRANSACTION_POINTER_OFFSET + (YapConst.INT_LENGTH * 6); 60 61 private TimerFileLock _timerFileLock; 62 63 private Transaction _interruptedTransaction; 64 65 private FileHeaderVariablePart1 _variablePart; 66 67 public void close() throws IOException { 68 _timerFileLock.close(); 69 } 70 71 public void initNew(YapFile file) throws IOException { 72 commonTasksForNewAndRead(file); 73 _variablePart = new FileHeaderVariablePart1(0, file.systemData()); 74 writeVariablePart(file, 0); 75 } 76 77 protected FileHeader newOnSignatureMatch(YapFile file, YapReader reader) { 78 if(signatureMatches(reader, SIGNATURE, VERSION)){ 79 return new FileHeader1(); 80 } 81 return null; 82 } 83 84 private void newTimerFileLock(YapFile file) { 85 _timerFileLock = TimerFileLock.forFile(file); 86 } 87 88 public Transaction interruptedTransaction() { 89 return _interruptedTransaction; 90 } 91 92 public int length() { 93 return LENGTH; 94 } 95 96 protected void readFixedPart(YapFile file, YapReader reader) throws IOException { 97 commonTasksForNewAndRead(file); 98 reader.seek(TRANSACTION_POINTER_OFFSET); 99 _interruptedTransaction = Transaction.readInterruptedTransaction(file, reader); 100 file.blockSizeReadFromFile(reader.readInt()); 101 readClassCollectionAndFreeSpace(file, reader); 102 _variablePart = new FileHeaderVariablePart1(reader.readInt(), file.systemData()); 103 } 104 105 private void commonTasksForNewAndRead(YapFile file){ 106 newTimerFileLock(file); 107 file.i_handlers.oldEncryptionOff(); 108 } 109 110 public void readVariablePart(YapFile file) { 111 _variablePart.read(file.getSystemTransaction()); 112 } 113 114 public void writeFixedPart( 115 YapFile file, boolean shuttingDown, YapWriter writer, int blockSize, int freespaceID) { 116 writer.append(SIGNATURE); 117 writer.append(VERSION); 118 writer.writeInt((int)timeToWrite(_timerFileLock.openTime(), shuttingDown)); 119 writer.writeLong(timeToWrite(_timerFileLock.openTime(), shuttingDown)); 120 writer.writeLong(timeToWrite(System.currentTimeMillis(), shuttingDown)); 121 writer.writeInt(0); 122 writer.writeInt(0); 123 writer.writeInt(blockSize); 124 writer.writeInt(file.systemData().classCollectionID()); 125 writer.writeInt(freespaceID); 126 writer.writeInt(_variablePart.getID()); 127 writer.noXByteCheck(); 128 writer.write(); 129 } 130 131 public void writeTransactionPointer(Transaction systemTransaction, int transactionAddress) { 132 writeTransactionPointer(systemTransaction, transactionAddress, 0, TRANSACTION_POINTER_OFFSET); 133 } 134 135 public void writeVariablePart(YapFile file, int part) { 136 _variablePart.setStateDirty(); 137 _variablePart.write(file.getSystemTransaction()); 138 } 139 140 } 141 | Popular Tags |