KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class ObjectMarshaller1 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                 if (isNull) {
35                     field.addIndexEntry(writer.getTransaction(), writer.getID(), null);
36                 }
37                 else {
38                     field.addFieldIndex(_family, yc, writer, oldSlot);
39                 }
40             }
41         };
42         traverseFields(yc, writer, attributes, command);
43     }
44     
45     public TreeInt collectFieldIDs(TreeInt tree, YapClass yc, ObjectHeaderAttributes attributes, final YapWriter writer, final String JavaDoc name) {
46         final TreeInt[] ret={tree};
47         TraverseFieldCommand command = new TraverseFieldCommand() {
48             public void processField(YapField field, boolean isNull, YapClass containingClass) {
49                 if(isNull) {
50                     return;
51                 }
52                 if (name.equals(field.getName())) {
53                     ret[0] = field.collectIDs(_family, ret[0], writer);
54                 }
55                 else {
56                     field.incrementOffset(writer);
57                 }
58             }
59         };
60         traverseFields(yc, writer, attributes, command);
61         return ret[0];
62     }
63
64     public void deleteMembers(YapClass yc, ObjectHeaderAttributes attributes, final YapWriter writer, int type, final boolean isUpdate){
65         TraverseFieldCommand command=new TraverseFieldCommand() {
66             public void processField(YapField field, boolean isNull, YapClass containingClass) {
67                 if(isNull){
68                     field.removeIndexEntry(writer.getTransaction(), writer.getID(), null);
69                 }else{
70                     field.delete(_family, writer, isUpdate);
71                 }
72             }
73         };
74         traverseFields(yc, writer, attributes, command);
75     }
76
77     public boolean findOffset(YapClass yc, ObjectHeaderAttributes attributes, final YapReader reader, final YapField field) {
78         final boolean[] ret={false};
79         TraverseFieldCommand command=new TraverseFieldCommand() {
80             public void processField(YapField curField, boolean isNull, YapClass containingClass) {
81                 if (curField == field) {
82                     ret[0]=!isNull;
83                     cancel();
84                     return;
85                 }
86                 if(!isNull){
87                     curField.incrementOffset(reader);
88                 }
89             }
90         };
91         traverseFields(yc, reader, attributes, command);
92         return ret[0];
93     }
94     
95     public void instantiateFields(YapClass yc, ObjectHeaderAttributes attributes, final YapObject yapObject, final Object JavaDoc onObject, final YapWriter writer) {
96         TraverseFieldCommand command = new TraverseFieldCommand() {
97             public void processField(YapField field, boolean isNull, YapClass containingClass) {
98                 if (isNull) {
99                     field.set(onObject, null);
100                     return;
101                 }
102                 try {
103                     field.instantiate(_family, yapObject,onObject, writer);
104                 } catch (CorruptionException e) {
105                     cancel();
106                 }
107             }
108         };
109         traverseFields(yc, writer, attributes, command);
110     }
111     
112     private void marshall(final YapObject yo, final Object JavaDoc obj,ObjectHeaderAttributes1 attributes, final YapWriter writer, final boolean isNew) {
113         YapClass yc = yo.getYapClass();
114         writeObjectClassID(writer,yc.getID());
115         attributes.write(writer);
116         yc.checkUpdateDepth(writer);
117         final Transaction trans = writer.getTransaction();
118
119         TraverseFieldCommand command = new TraverseFieldCommand() {
120             public int fieldCount(YapClass yapClass, YapReader reader) {
121                 reader.writeInt(yapClass.i_fields.length);
122                 return yapClass.i_fields.length;
123             }
124             
125             public void processField(YapField field, boolean isNull, YapClass containingClass) {
126                 if(isNull) {
127                     field.addIndexEntry(trans, writer.getID(), null);
128                     return;
129                 }
130                 Object JavaDoc child = field.getOrCreate(trans, obj);
131                 if (child instanceof Db4oTypeImpl) {
132                     child = ((Db4oTypeImpl) child).storedTo(trans);
133                 }
134                 field.marshall(yo, child, _family, writer, containingClass.configOrAncestorConfig(), isNew);
135             }
136         };
137         traverseFields(yc, writer, attributes, command);
138         if (Deploy.debug) {
139             writer.writeEnd();
140             writer.debugCheckBytes();
141         }
142     }
143
144     public YapWriter marshallNew(Transaction a_trans, YapObject yo, int a_updateDepth){
145         
146         ObjectHeaderAttributes1 attributes = new ObjectHeaderAttributes1(yo);
147         
148         YapWriter writer = createWriterForNew(
149             a_trans,
150             yo,
151             a_updateDepth,
152             attributes.objectLength());
153         
154         marshall(yo, yo.getObject(), attributes, writer, true);
155         
156         return writer;
157     }
158     
159     public void marshallUpdate(
160         Transaction trans,
161         int updateDepth,
162         YapObject yo,
163         Object JavaDoc obj
164         ) {
165         
166         ObjectHeaderAttributes1 attributes = new ObjectHeaderAttributes1(yo);
167         
168         YapWriter writer = createWriterForUpdate(
169             trans,
170             updateDepth,
171             yo.getID(),
172             0,
173             attributes.objectLength());
174         
175         if(trans.i_file != null){
176             // Running in single mode or on server.
177
// We need the slot now, so indexes can adjust to address.
178
trans.i_file.getSlotForUpdate(writer);
179         }
180         
181         marshall(yo, obj, attributes, writer, false);
182         
183         marshallUpdateWrite(trans, yo, obj, writer);
184     }
185     
186     public ObjectHeaderAttributes readHeaderAttributes(YapReader reader) {
187         return new ObjectHeaderAttributes1(reader);
188     }
189     
190     public Object JavaDoc readIndexEntry(YapClass yc, ObjectHeaderAttributes attributes, YapField yf, YapWriter reader) {
191         if(yc == null){
192             return null;
193         }
194         
195         if(! findOffset(yc, attributes, reader, yf)){
196             return null;
197         }
198         
199         return yf.readIndexEntry(_family, reader);
200     }
201     
202     public void readVirtualAttributes(final Transaction trans, YapClass yc, final YapObject yo, ObjectHeaderAttributes attributes, final YapReader reader) {
203         TraverseFieldCommand command = new TraverseFieldCommand() {
204             public void processField(YapField field, boolean isNull, YapClass containingClass) {
205                 if (!isNull) {
206                     field.readVirtualAttribute(trans, reader, yo);
207                 }
208             }
209         };
210         traverseFields(yc, reader, attributes, command);
211     }
212
213     protected boolean isNull(ObjectHeaderAttributes attributes,int fieldIndex) {
214         return ((ObjectHeaderAttributes1)attributes).isNull(fieldIndex);
215     }
216
217     public void defragFields(YapClass yc,ObjectHeader header, final ReaderPair readers) {
218         TraverseFieldCommand command = new TraverseFieldCommand() {
219             
220             public int fieldCount(YapClass yapClass, YapReader reader) {
221                 return readers.readInt();
222             }
223             
224             public void processField(YapField field, boolean isNull, YapClass containingClass) {
225                 if (!isNull) {
226                     field.defragField(_family,readers);
227                 }
228             }
229         };
230         traverseFields(yc, null, header._headerAttributes, command);
231     }
232
233     public void writeObjectClassID(YapReader reader, int id) {
234         reader.writeInt(-id);
235     }
236
237     public void skipMarshallerInfo(YapReader reader) {
238         reader.incrementOffset(1);
239     }
240 }
241
Popular Tags