| 1 21 package com.db4o.inside.marshall; 22 23 import com.db4o.*; 24 import com.db4o.foundation.*; 25 26 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 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 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 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 + 1; } 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 |