Batch Messaging Example

Let's create a small example to test batch messaging mode behavior. We will use bulk insert with and without batch messaging configuration.

BatchExample.cs: FillUpDb
01private static void FillUpDb(IObjectContainer container) 02 { 03 Console.WriteLine("Testing inserts"); 04 DateTime dt1 = DateTime.UtcNow; 05 for (int i = 0; i < NO_OF_OBJECTS; i++) 06 { 07 Pilot pilot = new Pilot("pilot #" + i, i); 08 container.Set(pilot); 09 } 10 DateTime dt2 = DateTime.UtcNow; 11 TimeSpan diff = dt2 - dt1; 12 Console.WriteLine("Operation time: " + diff.Milliseconds + " ms."); 13 }

BatchExample.vb: FillUpDb
01Private Shared Sub FillUpDb(ByVal container As IObjectContainer) 02 Console.WriteLine("Testing inserts") 03 Dim dt1 As DateTime = DateTime.UtcNow 04 Dim i As Integer = 0 05 While i < NO_OF_OBJECTS 06 Dim pilot As Pilot = New Pilot("pilot #" + i.ToString(), i) 07 container.Set(pilot) 08 System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1) 09 End While 10 Dim dt2 As DateTime = DateTime.UtcNow 11 Dim diff As TimeSpan = dt2 - dt1 12 Console.WriteLine("Operation time: " + diff.Milliseconds.ToString() + " ms.") 13 End Sub

Let's configure the server and run the insert operation first without batch messages, then with batch messages:

BatchExample.cs: Main
01public static void Main(string[] Args) 02 { 03 IObjectServer db4oServer = Db4oFactory.OpenServer(FILE, PORT); 04 try 05 { 06 db4oServer.GrantAccess(USER, PASS); 07 IObjectContainer container = Db4oFactory.OpenClient(HOST, PORT, USER, 08 PASS); 09 try 10 { 11 FillUpDb(container); 12 container.Ext().Configure().ClientServer().BatchMessages(true); 13 FillUpDb(container); 14 } 15 finally 16 { 17 container.Close(); 18 } 19 } 20 finally 21 { 22 db4oServer.Close(); 23 } 24 }

BatchExample.vb: Main
01Public Shared Sub Main(ByVal Args As String()) 02 Dim db4oServer As IObjectServer = Db4oFactory.OpenServer(FILE, PORT) 03 Try 04 db4oServer.GrantAccess(USER, PASS) 05 Dim container As IObjectContainer = Db4oFactory.OpenClient(HOST, PORT, USER, PASS) 06 Try 07 FillUpDb(container) 08 container.Ext.Configure.ClientServer.BatchMessages(True) 09 FillUpDb(container) 10 Finally 11 container.Close() 12 End Try 13 Finally 14 db4oServer.Close() 15 End Try 16 End Sub

You can try different values of NO_OF_OBJECTS constant to see the difference.

If the value of NO_OF_OBJECTS is high (>1,000,000) you may notice that the memory consumption increases a lot. In order to decrease it, try using:

c#: 

container.Ext().Configure().ClientServer().MaxBatchQueueSize(size);

VB: 

container.Ext().Configure().ClientServer().MaxBatchQueueSize(size);

Specify the size parameter according to the desirable memory consumption limit.