KickJava   Java API By Example, From Geeks To Geeks.

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


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.foundation.*;
25
26 /**
27  * @exclude
28  */

29 public class ObjectHeaderAttributes1 extends ObjectHeaderAttributes{
30     
31     private static final byte VERSION = (byte)1;
32     
33     private final int _fieldCount;
34     
35     private final BitMap4 _nullBitMap;
36     
37     private int _baseLength;
38     
39     private int _payLoadLength;
40     
41     
42     public ObjectHeaderAttributes1(YapObject yo) {
43         _fieldCount = yo.getYapClass().fieldCount();
44         _nullBitMap = new BitMap4(_fieldCount);
45         calculateLengths(yo);
46     }
47     
48     public ObjectHeaderAttributes1(YapReader reader){
49         _fieldCount = reader.readInt();
50         _nullBitMap = reader.readBitMap(_fieldCount);
51     }
52     
53     public void addBaseLength(int length){
54         _baseLength += length;
55     }
56     
57     public void addPayLoadLength(int length){
58         _payLoadLength += length;
59     }
60     
61     private void calculateLengths(YapObject yo) {
62         _baseLength = headerLength() + nullBitMapLength();
63         _payLoadLength = 0;
64         YapClass yc = yo.getYapClass();
65         Transaction trans = yo.getTrans();
66         Object JavaDoc obj = yo.getObject();
67         calculateLengths(trans, yc, obj, 0);
68         _baseLength = yo.getStream().alignToBlockSize(_baseLength);
69     }
70     
71     private void calculateLengths(Transaction trans, YapClass yc, Object JavaDoc obj, int fieldIndex) {
72         _baseLength += YapConst.INT_LENGTH;
73         if (yc.i_fields != null) {
74             for (int i = 0; i < yc.i_fields.length; i++) {
75                 YapField yf = yc.i_fields[i];
76                 Object JavaDoc child = yf.getOrCreate(trans, obj);
77                 if( child == null && yf.canUseNullBitmap()){
78                     _nullBitMap.setTrue(fieldIndex);
79                 }else{
80                     yf.calculateLengths(trans, this, child);
81                 }
82                 fieldIndex ++;
83             }
84         }
85         if (yc.i_ancestor == null) {
86             return;
87         }
88         calculateLengths(trans, yc.i_ancestor, obj, fieldIndex);
89     }
90
91     private int headerLength(){
92         return YapConst.OBJECT_LENGTH
93             + YapConst.ID_LENGTH // YapClass ID
94
+ 1; // Marshaller Version
95
}
96     
97     public boolean isNull(int fieldIndex){
98         return _nullBitMap.isTrue(fieldIndex);
99     }
100     
101     private int nullBitMapLength(){
102         return YapConst.INT_LENGTH + _nullBitMap.marshalledLength();
103     }
104
105     public int objectLength(){
106         return _baseLength + _payLoadLength;
107     }
108     
109     public void prepareIndexedPayLoadEntry(Transaction trans){
110         _payLoadLength = trans.stream().alignToBlockSize(_payLoadLength);
111     }
112     
113     public void write(YapWriter writer){
114         writer.append(VERSION);
115         writer.writeInt(_fieldCount);
116         writer.writeBitMap(_nullBitMap);
117         writer._payloadOffset = _baseLength;
118     }
119
120 }
121
Popular Tags