Let's implement a marshaller for a simple class and check its influence on the performance.
We will marshal the following class:
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02
using System; 03
04
namespace Db4objects.Db4odoc.marshal 05
{ 06
class Item 07
{ 08
public int _one; 09
10
public long _two; 11
12
public int _three; 13
14
public Item(int one, long two, int three) { 15
_one = one; 16
_two = two; 17
_three = three; 18
19
} 20
21
public Item() { 22
23
} 24
25
public override string ToString() { 26
return String.Format("{0:X}, {1:X}, {2:N}", _one, _two, _three); 27
} 28
} 29
}
01' Copyright (C) 2007 db4objects Inc. http://www.db4o.com 02
Imports System 03
Namespace Db4objects.Db4odoc.marshal 04
05
Class Item 06
Public _one As Integer 07
Public _two As Long 08
Public _three As Integer 09
10
Public Sub New(ByVal one As Integer, ByVal two As Long, ByVal three As Integer) 11
_one = one 12
_two = two 13
_three = three 14
End Sub 15
16
Public Sub New() 17
End Sub 18
19
Public Overloads Overrides Function ToString() As String 20
Return String.Format("{0:X}, {1:X}, {2:N}", _one, _two, _three) 21
End Function 22
End Class 23
End Namespace
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:
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02
using Db4objects.Db4o.Config; 03
using Db4objects.Db4o.Foundation; 04
05
namespace Db4objects.Db4odoc.marshal 06
{ 07
class ItemMarshaller: IObjectMarshaller 08
{ 09
// Write field values to a byte array 10
// No reflection is used 11
public void WriteFields(object obj, byte[] slot, int offset) { 12
Item item = (Item)obj; 13
PrimitiveCodec.WriteInt(slot, offset, item._one); 14
offset+= PrimitiveCodec.INT_LENGTH; 15
PrimitiveCodec.WriteLong(slot, offset, item._two); 16
offset+= PrimitiveCodec.LONG_LENGTH; 17
PrimitiveCodec.WriteInt(slot, offset, item._three); 18
} 19
20
// Restore field values from the byte array 21
// No reflection is used 22
public void ReadFields(object obj, byte[] slot, int offset) { 23
Item item = (Item)obj; 24
item._one = PrimitiveCodec.ReadInt(slot, offset); 25
offset+= PrimitiveCodec.INT_LENGTH; 26
item._two = PrimitiveCodec.ReadLong(slot, offset); 27
offset+= PrimitiveCodec.LONG_LENGTH; 28
item._three = PrimitiveCodec.ReadInt(slot, offset); 29
} 30
31
public int MarshalledFieldLength() { 32
return PrimitiveCodec.INT_LENGTH * 2 + PrimitiveCodec.LONG_LENGTH; 33
} 34
} 35
}
01' Copyright (C) 2007 db4objects Inc. http://www.db4o.com 02
Imports Db4objects.Db4o.Config 03
Imports Db4objects.Db4o.Foundation 04
Namespace Db4objects.Db4odoc.marshal 05
06
Class ItemMarshaller 07
Implements IObjectMarshaller 08
09
' Write field values to a byte array 10
' No reflection is used 11
Public Sub WriteFields(ByVal obj As Object, ByVal slot As Byte(), ByVal offset As Integer) Implements IObjectMarshaller.WriteFields 12
Dim item As Item = CType(obj, Item) 13
PrimitiveCodec.WriteInt(slot, offset, item._one) 14
offset += PrimitiveCodec.INT_LENGTH 15
PrimitiveCodec.WriteLong(slot, offset, item._two) 16
offset += PrimitiveCodec.LONG_LENGTH 17
PrimitiveCodec.WriteInt(slot, offset, item._three) 18
End Sub 19
' end WriteFields 20
21
' Restore field values from the byte array 22
' No reflection is used 23
Public Sub ReadFields(ByVal obj As Object, ByVal slot As Byte(), ByVal offset As Integer) Implements IObjectMarshaller.ReadFields 24
Dim item As Item = CType(obj, Item) 25
item._one = PrimitiveCodec.ReadInt(slot, offset) 26
offset += PrimitiveCodec.INT_LENGTH 27
item._two = PrimitiveCodec.ReadLong(slot, offset) 28
offset += PrimitiveCodec.LONG_LENGTH 29
item._three = PrimitiveCodec.ReadInt(slot, offset) 30
End Sub 31
' end ReadFields 32
33
Public Function MarshalledFieldLength() As Integer Implements IObjectMarshaller.MarshalledFieldLength 34
Return PrimitiveCodec.INT_LENGTH * 2 + PrimitiveCodec.LONG_LENGTH 35
End Function 36
' end MarshalledFieldLength 37
38
End Class 39
End Namespace