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;
022:
023: import com.db4o.*;
024: import com.db4o.internal.handlers.*;
025: import com.db4o.internal.marshall.*;
026: import com.db4o.marshall.*;
027:
028: public class UntypedFieldHandler extends ClassMetadata implements
029: BuiltinTypeHandler {
030:
031: public UntypedFieldHandler(ObjectContainerBase container) {
032: super (container, container._handlers.ICLASS_OBJECT);
033: }
034:
035: public void cascadeActivation(Transaction a_trans, Object a_object,
036: int a_depth, boolean a_activate) {
037: ClassMetadata yc = forObject(a_trans, a_object, false);
038: if (yc != null) {
039: yc
040: .cascadeActivation(a_trans, a_object, a_depth,
041: a_activate);
042: }
043: }
044:
045: public void deleteEmbedded(MarshallerFamily mf,
046: StatefulBuffer reader) throws Db4oIOException {
047: mf._untyped.deleteEmbedded(reader);
048: }
049:
050: public int getID() {
051: return Handlers4.UNTYPED_ID;
052: }
053:
054: public boolean hasField(ObjectContainerBase a_stream, String a_path) {
055: return a_stream.classCollection().fieldExists(a_path);
056: }
057:
058: public boolean hasClassIndex() {
059: return false;
060: }
061:
062: public boolean holdsAnyClass() {
063: return true;
064: }
065:
066: public boolean isStrongTyped() {
067: return false;
068: }
069:
070: public TypeHandler4 readArrayHandler(Transaction a_trans,
071: MarshallerFamily mf, Buffer[] a_bytes) {
072: return mf._untyped.readArrayHandler(a_trans, a_bytes);
073: }
074:
075: public ObjectID readObjectID(InternalReadContext context) {
076: int payloadOffset = context.readInt();
077: if (payloadOffset == 0) {
078: return ObjectID.IS_NULL;
079: }
080: ClassMetadata classMetadata = readClassMetadata(context,
081: payloadOffset);
082: if (classMetadata == null) {
083: return ObjectID.IS_NULL;
084: }
085: seekSecondaryOffset(context, classMetadata);
086: return classMetadata.readObjectID(context);
087: }
088:
089: public void defrag(MarshallerFamily mf, BufferPair readers,
090: boolean redirect) {
091: if (mf._untyped.useNormalClassRead()) {
092: super .defrag(mf, readers, redirect);
093: }
094: mf._untyped.defrag(readers);
095: }
096:
097: private boolean isArray(TypeHandler4 handler) {
098: if (handler instanceof ClassMetadata) {
099: return ((ClassMetadata) handler).isArray();
100: }
101: return handler instanceof ArrayHandler;
102: }
103:
104: public Object read(ReadContext readContext) {
105: InternalReadContext context = (InternalReadContext) readContext;
106: int payloadOffset = context.readInt();
107: if (payloadOffset == 0) {
108: return null;
109: }
110: int savedOffSet = context.offset();
111: ClassMetadata classMetadata = readClassMetadata(context,
112: payloadOffset);
113: if (classMetadata == null) {
114: context.seek(savedOffSet);
115: return null;
116: }
117: seekSecondaryOffset(context, classMetadata);
118: Object obj = classMetadata.read(context);
119: context.seek(savedOffSet);
120: return obj;
121: }
122:
123: private ClassMetadata readClassMetadata(
124: InternalReadContext context, int payloadOffset) {
125: context.seek(payloadOffset);
126: ClassMetadata classMetadata = container().classMetadataForId(
127: context.readInt());
128: return classMetadata;
129: }
130:
131: private void seekSecondaryOffset(InternalReadContext context,
132: ClassMetadata classMetadata) {
133: if (classMetadata instanceof PrimitiveFieldHandler
134: && classMetadata.isArray()) {
135: // unnecessary secondary offset, consistent with old format
136: context.seek(context.readInt());
137: }
138: }
139:
140: public void write(WriteContext context, Object obj) {
141: if (obj == null) {
142: context.writeInt(0);
143: return;
144: }
145: MarshallingContext marshallingContext = (MarshallingContext) context;
146: TypeHandler4 handler = ClassMetadata.forObject(context
147: .transaction(), obj, true);
148: if (handler == null) {
149: context.writeInt(0);
150: return;
151: }
152: MarshallingContextState state = marshallingContext
153: .currentState();
154: marshallingContext.createChildBuffer(false, false);
155: int id = marshallingContext.container().handlers().handlerID(
156: handler);
157: context.writeInt(id);
158: if (isArray(handler)) {
159: // TODO: This indirection is unneccessary, but it is required by the
160: // current old reading format.
161: // Remove in the next version of UntypedFieldHandler
162: marshallingContext.prepareIndirectionOfSecondWrite();
163: } else {
164: marshallingContext.doNotIndirectWrites();
165: }
166: handler.write(context, obj);
167: marshallingContext.restoreState(state);
168: }
169:
170: }
|