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.marshall;
022:
023: import java.io.IOException;
024:
025: import com.db4o.*;
026: import com.db4o.internal.*;
027: import com.db4o.internal.btree.*;
028:
029: /**
030: * @exclude
031: */
032: public class FieldMarshaller1 extends FieldMarshaller0 {
033:
034: private boolean hasBTreeIndex(FieldMetadata field) {
035: return !field.isVirtual();
036: }
037:
038: public void write(Transaction trans, ClassMetadata clazz,
039: FieldMetadata field, Buffer writer) {
040: super .write(trans, clazz, field, writer);
041: if (!hasBTreeIndex(field)) {
042: return;
043: }
044: writer.writeIDOf(trans, field.getIndex(trans));
045: }
046:
047: public RawFieldSpec readSpec(ObjectContainerBase stream,
048: Buffer reader) {
049: RawFieldSpec spec = super .readSpec(stream, reader);
050: if (spec == null) {
051: return null;
052: }
053: if (spec.isVirtual()) {
054: return spec;
055: }
056: int indexID = reader.readInt();
057: spec.indexID(indexID);
058: return spec;
059: }
060:
061: protected FieldMetadata fromSpec(RawFieldSpec spec,
062: ObjectContainerBase stream, FieldMetadata field) {
063: FieldMetadata actualField = super .fromSpec(spec, stream, field);
064: if (spec == null) {
065: return field;
066: }
067: if (spec.indexID() != 0) {
068: actualField.initIndex(stream.systemTransaction(), spec
069: .indexID());
070: }
071: return actualField;
072: }
073:
074: public int marshalledLength(ObjectContainerBase stream,
075: FieldMetadata field) {
076: int len = super .marshalledLength(stream, field);
077: if (!hasBTreeIndex(field)) {
078: return len;
079: }
080: final int BTREE_ID = Const4.ID_LENGTH;
081: return len + BTREE_ID;
082: }
083:
084: public void defrag(ClassMetadata yapClass, FieldMetadata yapField,
085: LatinStringIO sio, final BufferPair readers)
086: throws CorruptionException, IOException {
087: super .defrag(yapClass, yapField, sio, readers);
088: if (yapField.isVirtual()) {
089: return;
090: }
091: if (yapField.hasIndex()) {
092: BTree index = yapField.getIndex(readers.systemTrans());
093: int targetIndexID = readers.copyID();
094: if (targetIndexID != 0) {
095: index.defragBTree(readers.context());
096: }
097: } else {
098: readers.writeInt(0);
099: }
100: }
101: }
|