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.foundation.*;
027: import com.db4o.internal.*;
028: import com.db4o.internal.handlers.*;
029:
030: /**
031: * @exclude
032: */
033: public class FieldMarshaller0 implements FieldMarshaller {
034:
035: public int marshalledLength(ObjectContainerBase stream,
036: FieldMetadata field) {
037: int len = stream.stringIO().shortLength(field.getName());
038: if (field.needsArrayAndPrimitiveInfo()) {
039: len += 1;
040: }
041: if (field.needsHandlerId()) {
042: len += Const4.ID_LENGTH;
043: }
044: return len;
045: }
046:
047: public RawFieldSpec readSpec(ObjectContainerBase stream,
048: Buffer reader) {
049: String name = StringHandler.readStringNoDebug(stream
050: .transaction().context(), reader);
051: if (name.indexOf(Const4.VIRTUAL_FIELD_PREFIX) == 0) {
052: if (stream._handlers.virtualFieldByName(name) != null) {
053: return new RawFieldSpec(name);
054: }
055: }
056: int handlerID = reader.readInt();
057: byte attribs = reader.readByte();
058: return new RawFieldSpec(name, handlerID, attribs);
059: }
060:
061: public final FieldMetadata read(ObjectContainerBase stream,
062: FieldMetadata field, Buffer reader) {
063: RawFieldSpec spec = readSpec(stream, reader);
064: return fromSpec(spec, stream, field);
065: }
066:
067: protected FieldMetadata fromSpec(RawFieldSpec spec,
068: ObjectContainerBase stream, FieldMetadata field) {
069: if (spec == null) {
070: return field;
071: }
072: String name = spec.name();
073: if (spec.isVirtual()) {
074: return stream._handlers.virtualFieldByName(name);
075: }
076:
077: field.init(field.containingClass(), name);
078: field.init(spec.handlerID(), spec.isPrimitive(),
079: spec.isArray(), spec.isNArray());
080: field.loadHandler(stream);
081: field.alive();
082:
083: return field;
084: }
085:
086: public void write(Transaction trans, ClassMetadata clazz,
087: FieldMetadata field, Buffer writer) {
088:
089: field.alive();
090:
091: writer.writeShortString(trans, field.getName());
092: if (field.isVirtual()) {
093: return;
094: }
095:
096: TypeHandler4 handler = field.getHandler();
097:
098: if (handler instanceof ClassMetadata) {
099:
100: // TODO: ensure there is a test case, to make this happen
101: if (((ClassMetadata) handler).getID() == 0) {
102: trans.container().needsUpdate(clazz);
103: }
104: }
105: writer.writeInt(field.handlerID());
106: BitMap4 bitmap = new BitMap4(3);
107: bitmap.set(0, field.isPrimitive());
108: bitmap.set(1, handler instanceof ArrayHandler);
109: bitmap.set(2, handler instanceof MultidimensionalArrayHandler); // keep the order
110: writer.writeByte(bitmap.getByte(0));
111: }
112:
113: public void defrag(ClassMetadata yapClass, FieldMetadata yapField,
114: LatinStringIO sio, BufferPair readers)
115: throws CorruptionException, IOException {
116: readers.incrementStringOffset(sio);
117: if (yapField.isVirtual()) {
118: return;
119: }
120: // handler ID
121: readers.copyID();
122: // skip primitive/array/narray attributes
123: readers.incrementOffset(1);
124: }
125: }
|