KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > header > FileHeader1


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.header;
22
23 import java.io.*;
24
25 import com.db4o.*;
26
27
28 /**
29  * @exclude
30  */

31 public class FileHeader1 extends FileHeader {
32     
33     // The header format is:
34

35     // (byte) 'd'
36
// (byte) 'b'
37
// (byte) '4'
38
// (byte) 'o'
39
// (byte) headerVersion
40
// (int) headerLock
41
// (long) openTime
42
// (long) accessTime
43
// (int) Transaction pointer 1
44
// (int) Transaction pointer 2
45
// (int) blockSize
46
// (int) classCollectionID
47
// (int) freespaceID
48
// (int) variablePartID
49

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