KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @exclude
29  */

30 public class FileHeader0 extends FileHeader {
31     
32     static final int LENGTH = 2 + (YapConst.INT_LENGTH * 4);
33
34     // The header format is:
35

36     // Old format
37
// -------------------------
38
// {
39
// Y
40
// [Rest]
41

42     
43     // New format
44
// -------------------------
45
// (byte)4
46
// block size in bytes 1 to 127
47
// [Rest]
48

49
50     // Rest (only ints)
51
// -------------------
52
// address of the extended configuration block, see YapConfigBlock
53
// headerLock
54
// YapClassCollection ID
55
// FreeBySize ID
56

57     
58     private YapConfigBlock _configBlock;
59     
60     private PBootRecord _bootRecord;
61     
62
63     public void close() throws IOException {
64         _configBlock.close();
65     }
66     
67     protected FileHeader newOnSignatureMatch(YapFile file, YapReader reader) {
68         byte firstFileByte = reader.readByte();
69         if (firstFileByte != YapConst.YAPBEGIN) {
70             if(firstFileByte != YapConst.YAPFILEVERSION){
71                 return null;
72             }
73             file.blockSizeReadFromFile(reader.readByte());
74         }else{
75             if (reader.readByte() != YapConst.YAPFILE) {
76                 return null;
77             }
78         }
79         return new FileHeader0();
80     }
81
82     
83     protected void readFixedPart(YapFile file, YapReader reader) throws IOException {
84         _configBlock = YapConfigBlock.forExistingFile(file, reader.readInt());
85         skipConfigurationLockTime(reader);
86         readClassCollectionAndFreeSpace(file, reader);
87     }
88
89     private void skipConfigurationLockTime(YapReader reader) {
90         reader.incrementOffset(YapConst.ID_LENGTH);
91     }
92
93     public void readVariablePart(YapFile file){
94         if (_configBlock._bootRecordID <= 0) {
95             return;
96         }
97         file.showInternalClasses(true);
98         Object JavaDoc bootRecord = file.getByID1(file.getSystemTransaction(), _configBlock._bootRecordID);
99         file.showInternalClasses(false);
100         
101         if (! (bootRecord instanceof PBootRecord)) {
102             initBootRecord(file);
103             file.generateNewIdentity();
104             return;
105         }
106         
107         _bootRecord = (PBootRecord) bootRecord;
108         file.activate(bootRecord, Integer.MAX_VALUE);
109         file.setNextTimeStampId(_bootRecord.i_versionGenerator);
110         
111         file.systemData().identity(_bootRecord.i_db);
112     }
113
114     public void initNew(YapFile file) throws IOException {
115         _configBlock = YapConfigBlock.forNewFile(file);
116         initBootRecord(file);
117     }
118     
119     private void initBootRecord(YapFile file){
120         
121         file.showInternalClasses(true);
122         
123         _bootRecord = new PBootRecord();
124         file.setInternal(file.getSystemTransaction(), _bootRecord, false);
125         
126         _configBlock._bootRecordID = file.getID1(_bootRecord);
127         writeVariablePart(file, 1);
128         
129         file.showInternalClasses(false);
130     }
131
132     public Transaction interruptedTransaction() {
133         return _configBlock.getTransactionToCommit();
134     }
135
136     public void writeTransactionPointer(Transaction systemTransaction, int transactionAddress) {
137         writeTransactionPointer(systemTransaction, transactionAddress, _configBlock.address(), YapConfigBlock.TRANSACTION_OFFSET);
138     }
139
140     public MetaIndex getUUIDMetaIndex() {
141         return _bootRecord.getUUIDMetaIndex();
142     }
143
144     public int length(){
145         return LENGTH;
146     }
147
148     public void writeFixedPart(YapFile file, boolean shuttingDown, YapWriter writer, int blockSize_, int freespaceID) {
149         writer.append(YapConst.YAPFILEVERSION);
150         writer.append((byte)blockSize_);
151         writer.writeInt(_configBlock.address());
152         writer.writeInt((int)timeToWrite(_configBlock.openTime(), shuttingDown));
153         writer.writeInt(file.systemData().classCollectionID());
154         writer.writeInt(freespaceID);
155         if (Debug.xbytes && Deploy.overwrite) {
156             writer.setID(YapConst.IGNORE_ID);
157         }
158         writer.write();
159     }
160     
161     public void writeVariablePart(YapFile file, int part) {
162         if(part == 1){
163             _configBlock.write();
164         }else if(part == 2){
165             _bootRecord.write(file);
166         }
167     }
168
169 }
170
Popular Tags