KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > marshall > ObjectMarshaller0


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.inside.marshall;
22
23 import com.db4o.*;
24 import com.db4o.inside.slots.*;
25
26 /**
27  * @exclude
28  */

29 class ObjectMarshaller0 extends ObjectMarshaller {
30     
31     public void addFieldIndices(final YapClass yc, ObjectHeaderAttributes attributes, final YapWriter writer, final Slot oldSlot) {
32         TraverseFieldCommand command=new TraverseFieldCommand() {
33             public void processField(YapField field, boolean isNull, YapClass containingClass) {
34                 field.addFieldIndex(_family, yc, writer, oldSlot);
35             }
36         };
37         traverseFields(yc, writer, attributes, command);
38     }
39     
40     public TreeInt collectFieldIDs(TreeInt tree, YapClass yc, ObjectHeaderAttributes attributes, final YapWriter writer, final String JavaDoc name) {
41         final TreeInt[] ret={tree};
42         TraverseFieldCommand command=new TraverseFieldCommand() {
43             public void processField(YapField field, boolean isNull, YapClass containingClass) {
44                 if (name.equals(field.getName())) {
45                     ret[0] = field.collectIDs(_family, ret[0], writer);
46                 } else {
47                     field.incrementOffset(writer);
48                 }
49             }
50         };
51         traverseFields(yc, writer, attributes, command);
52         return ret[0];
53     }
54     
55     public void deleteMembers(YapClass yc, ObjectHeaderAttributes attributes, final YapWriter writer, int type, final boolean isUpdate){
56         TraverseFieldCommand command=new TraverseFieldCommand() {
57             public void processField(YapField field, boolean isNull, YapClass containingClass) {
58                 field.delete(_family, writer, isUpdate);
59             }
60         };
61         traverseFields(yc, writer, attributes, command);
62     }
63     
64     public boolean findOffset(YapClass yc, ObjectHeaderAttributes attributes, final YapReader writer, final YapField field) {
65         final boolean[] ret={false};
66         TraverseFieldCommand command=new TraverseFieldCommand() {
67             public void processField(YapField curField, boolean isNull, YapClass containingClass) {
68                 if (curField == field) {
69                     ret[0]=true;
70                     cancel();
71                     return;
72                 }
73                 writer.incrementOffset(curField.linkLength());
74             }
75         };
76         traverseFields(yc, writer, attributes, command);
77         return ret[0];
78     }
79     
80     protected final int headerLength(){
81         return YapConst.OBJECT_LENGTH + YapConst.ID_LENGTH;
82     }
83     
84     public void instantiateFields(YapClass yc, ObjectHeaderAttributes attributes, final YapObject yapObject, final Object JavaDoc onObject, final YapWriter writer) {
85         TraverseFieldCommand command=new TraverseFieldCommand() {
86             public void processField(YapField field, boolean isNull, YapClass containingClass) {
87                 try {
88                     field.instantiate(_family, yapObject, onObject, writer);
89                 } catch (CorruptionException e) {
90                     cancel();
91                 }
92             }
93         };
94         traverseFields(yc, writer, attributes, command);
95     }
96     
97     private int linkLength(YapClass yc, YapObject yo) {
98         int length = YapConst.INT_LENGTH;
99         if (yc.i_fields != null) {
100             for (int i = 0; i < yc.i_fields.length; i++) {
101                 length += linkLength(yc.i_fields[i], yo);
102             }
103         }
104         if (yc.i_ancestor != null) {
105             length += linkLength(yc.i_ancestor, yo);
106         }
107         return length;
108     }
109     
110     protected int linkLength(YapField yf, YapObject yo){
111         return yf.linkLength();
112     }
113     
114     private void marshall(YapClass yapClass, YapObject a_yapObject, Object JavaDoc a_object, YapWriter writer, boolean a_new) {
115         marshallDeclaredFields(yapClass, a_yapObject, a_object, writer, a_new);
116         if (Deploy.debug) {
117             writer.writeEnd();
118             writer.debugCheckBytes();
119         }
120     }
121     
122     private void marshallDeclaredFields(YapClass yapClass, final YapObject yapObject, final Object JavaDoc object, final YapWriter writer, final boolean isNew) {
123         final Config4Class config = yapClass.configOrAncestorConfig();
124         final Transaction trans=writer.getTransaction();
125         TraverseFieldCommand command=new TraverseFieldCommand() {
126             public int fieldCount(YapClass yc, YapReader reader) {
127                 writer.writeInt(yc.i_fields.length);
128                 return yc.i_fields.length;
129             }
130             
131             public void processField(YapField field, boolean isNull, YapClass containingClass) {
132                 Object JavaDoc obj = field.getOrCreate(trans, object);
133                 if (obj instanceof Db4oTypeImpl) {
134                     obj = ((Db4oTypeImpl)obj).storedTo(trans);
135                 }
136                 field.marshall(yapObject, obj, _family, writer, config, isNew);
137             }
138         };
139         traverseFields(yapClass, writer, readHeaderAttributes(writer), command);
140     }
141     
142     protected int marshalledLength(YapField yf, YapObject yo){
143         return 0;
144     }
145
146     public YapWriter marshallNew(Transaction a_trans, YapObject yo, int a_updateDepth){
147         
148         YapWriter writer = createWriterForNew(a_trans, yo, a_updateDepth, objectLength(yo));
149         
150         YapClass yc = yo.getYapClass();
151         Object JavaDoc obj = yo.getObject();
152         
153         if(yc.isPrimitive()){
154             ((YapClassPrimitive)yc).i_handler.writeNew(MarshallerFamily.current(), obj, false, writer, true, false);
155             if (Deploy.debug) {
156                 writer.writeEnd();
157                 writer.debugCheckBytes();
158             }
159         }else{
160             writeObjectClassID(writer,yc.getID());
161             yc.checkUpdateDepth(writer);
162             marshall(yc, yo, obj, writer, true);
163         }
164         return writer;
165     }
166
167     public void marshallUpdate(
168         Transaction trans,
169         int updateDepth,
170         YapObject yapObject,
171         Object JavaDoc obj
172         ) {
173         
174         YapWriter writer = createWriterForUpdate(trans,updateDepth, yapObject.getID(), 0, objectLength(yapObject));
175         
176         YapClass yapClass = yapObject.getYapClass();
177         
178         yapClass.checkUpdateDepth(writer);
179         
180         writer.writeInt(yapClass.getID());
181         marshall(yapClass, yapObject, obj, writer, false);
182         
183         marshallUpdateWrite(trans, yapObject, obj, writer);
184     }
185
186     private int objectLength(YapObject yo) {
187         return headerLength() + linkLength(yo.getYapClass(), yo);
188     }
189
190     public ObjectHeaderAttributes readHeaderAttributes(YapReader reader) {
191         return null;
192     }
193
194     public Object JavaDoc readIndexEntry(YapClass yc, ObjectHeaderAttributes attributes, YapField yf, YapWriter reader) {
195         if(yc == null){
196             return null;
197         }
198         
199         if(! findOffset(yc, attributes, reader, yf)){
200             return null;
201         }
202         
203         return yf.readIndexEntry(_family, reader);
204     }
205     
206     public void readVirtualAttributes(final Transaction trans, YapClass yc, final YapObject yo, ObjectHeaderAttributes attributes, final YapReader reader){
207         TraverseFieldCommand command=new TraverseFieldCommand() {
208             public void processField(YapField field, boolean isNull, YapClass containingClass) {
209                 field.readVirtualAttribute(trans, reader, yo);
210             }
211         };
212         traverseFields(yc, reader, attributes, command);
213     }
214
215     protected boolean isNull(ObjectHeaderAttributes attributes,int fieldIndex) {
216         return false;
217     }
218
219     public void defragFields(YapClass yapClass,ObjectHeader header, ReaderPair readers) {
220     }
221
222     public void writeObjectClassID(YapReader reader, int id) {
223         reader.writeInt(id);
224     }
225
226     public void skipMarshallerInfo(YapReader reader) {
227     }
228 }
229
Popular Tags