Let's implement a marshaller for a simple class and check its influence on the performance.
We will marshal the following class:
01package com.db4odoc.marshal; 02
03
public class Item{ 04
05
public int _one; 06
07
public long _two; 08
09
public int _three; 10
11
public Item(int one, long two, int three) { 12
_one = one; 13
_two = two; 14
_three = three; 15
16
} 17
18
public Item() { 19
20
} 21
22
public String toString() { 23
return String.format("%h, %h, %d", _one, _two, _three); 24
} 25
26
}
The marshaller - ItemMarshaller - will need to implement writeFields, readFields and marshalledFieldLength methods.
As Item class has only int and long fields we can use PrimitiveCodec class to write the fields to the byte array:
01package com.db4odoc.marshal; 02
03
import com.db4o.config.ObjectMarshaller; 04
import com.db4o.foundation.PrimitiveCodec; 05
06
public class ItemMarshaller implements ObjectMarshaller{ 07
08
// Write field values to a byte array 09
// No reflection is used 10
public void writeFields(Object obj, byte[] slot, int offset) { 11
Item item = (Item)obj; 12
PrimitiveCodec.writeInt(slot, offset, item._one); 13
offset+= PrimitiveCodec.INT_LENGTH; 14
PrimitiveCodec.writeLong(slot, offset, item._two); 15
offset+= PrimitiveCodec.LONG_LENGTH; 16
PrimitiveCodec.writeInt(slot, offset, item._three); 17
} 18
19
// Restore field values from the byte array 20
// No reflection is used 21
public void readFields(Object obj, byte[] slot, int offset) { 22
Item item = (Item)obj; 23
item._one = PrimitiveCodec.readInt(slot, offset); 24
offset+= PrimitiveCodec.INT_LENGTH; 25
item._two = PrimitiveCodec.readLong(slot, offset); 26
offset+= PrimitiveCodec.LONG_LENGTH; 27
item._three = PrimitiveCodec.readInt(slot, offset); 28
} 29
30
public int marshalledFieldLength() { 31
return PrimitiveCodec.INT_LENGTH * 2 + PrimitiveCodec.LONG_LENGTH; 32
} 33
}