001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.fileheader;
022:
023: import com.db4o.*;
024: import com.db4o.ext.OldFormatException;
025: import com.db4o.internal.*;
026:
027: /**
028: * @exclude
029: */
030: public class FileHeader0 extends FileHeader {
031:
032: static final int LENGTH = 2 + (Const4.INT_LENGTH * 4);
033:
034: // The header format is:
035:
036: // Old format
037: // -------------------------
038: // {
039: // Y
040: // [Rest]
041:
042: // New format
043: // -------------------------
044: // (byte)4
045: // block size in bytes 1 to 127
046: // [Rest]
047:
048: // Rest (only ints)
049: // -------------------
050: // address of the extended configuration block, see YapConfigBlock
051: // headerLock
052: // YapClassCollection ID
053: // FreeBySize ID
054:
055: private ConfigBlock _configBlock;
056:
057: private PBootRecord _bootRecord;
058:
059: public void close() throws Db4oIOException {
060: _configBlock.close();
061: }
062:
063: protected FileHeader newOnSignatureMatch(LocalObjectContainer file,
064: Buffer reader) {
065: byte firstFileByte = reader.readByte();
066: if (firstFileByte != Const4.YAPBEGIN) {
067: if (firstFileByte != Const4.YAPFILEVERSION) {
068: return null;
069: }
070: file.blockSizeReadFromFile(reader.readByte());
071: } else {
072: if (reader.readByte() != Const4.YAPFILE) {
073: return null;
074: }
075: }
076: return new FileHeader0();
077: }
078:
079: protected void readFixedPart(LocalObjectContainer file,
080: Buffer reader) throws OldFormatException {
081: _configBlock = ConfigBlock.forExistingFile(file, reader
082: .readInt());
083: skipConfigurationLockTime(reader);
084: readClassCollectionAndFreeSpace(file, reader);
085: }
086:
087: private void skipConfigurationLockTime(Buffer reader) {
088: reader.incrementOffset(Const4.ID_LENGTH);
089: }
090:
091: public void readVariablePart(LocalObjectContainer file) {
092: if (_configBlock._bootRecordID <= 0) {
093: return;
094: }
095: Object bootRecord = Debug.readBootRecord ? getBootRecord(file)
096: : null;
097:
098: if (!(bootRecord instanceof PBootRecord)) {
099: initBootRecord(file);
100: file.generateNewIdentity();
101: return;
102: }
103:
104: _bootRecord = (PBootRecord) bootRecord;
105: file.activate(bootRecord, Integer.MAX_VALUE);
106: file.setNextTimeStampId(_bootRecord.i_versionGenerator);
107:
108: file.systemData().identity(_bootRecord.i_db);
109: }
110:
111: private Object getBootRecord(LocalObjectContainer file) {
112: file.showInternalClasses(true);
113: try {
114: return file.getByID(file.systemTransaction(),
115: _configBlock._bootRecordID);
116: } finally {
117: file.showInternalClasses(false);
118: }
119: }
120:
121: public void initNew(LocalObjectContainer file)
122: throws Db4oIOException {
123: _configBlock = ConfigBlock.forNewFile(file);
124: initBootRecord(file);
125: }
126:
127: private void initBootRecord(LocalObjectContainer file) {
128:
129: file.showInternalClasses(true);
130: try {
131: _bootRecord = new PBootRecord();
132: file.setInternal(file.systemTransaction(), _bootRecord,
133: false);
134:
135: _configBlock._bootRecordID = file.getID(file
136: .systemTransaction(), _bootRecord);
137: writeVariablePart(file, 1);
138: } finally {
139: file.showInternalClasses(false);
140: }
141: }
142:
143: public Transaction interruptedTransaction() {
144: return _configBlock.getTransactionToCommit();
145: }
146:
147: public void writeTransactionPointer(Transaction systemTransaction,
148: int transactionAddress) {
149: writeTransactionPointer(systemTransaction, transactionAddress,
150: _configBlock.address(), ConfigBlock.TRANSACTION_OFFSET);
151: }
152:
153: public MetaIndex getUUIDMetaIndex() {
154: return _bootRecord.getUUIDMetaIndex();
155: }
156:
157: public int length() {
158: return LENGTH;
159: }
160:
161: public void writeFixedPart(LocalObjectContainer file,
162: boolean startFileLockingThread, boolean shuttingDown,
163: StatefulBuffer writer, int blockSize_, int freespaceID) {
164: writer.writeByte(Const4.YAPFILEVERSION);
165: writer.writeByte((byte) blockSize_);
166: writer.writeInt(_configBlock.address());
167: writer.writeInt((int) timeToWrite(_configBlock.openTime(),
168: shuttingDown));
169: writer.writeInt(file.systemData().classCollectionID());
170: writer.writeInt(freespaceID);
171: if (Debug.xbytes && Deploy.overwrite) {
172: writer.setID(Const4.IGNORE_ID);
173: }
174: writer.write();
175: file.syncFiles();
176: }
177:
178: public void writeVariablePart(LocalObjectContainer file, int part) {
179: if (part == 1) {
180: _configBlock.write();
181: } else if (part == 2) {
182: _bootRecord.write(file);
183: }
184: }
185:
186: }
|