CachedIoAdapter Example

Let's look on an example at the benefits of CachedIoAdapter.

We will use the following methods to initiate write and read from a database file:

CachedIOExample.cs: SetObjects
01private static void SetObjects(IConfiguration configuration) 02 { 03 File.Delete(Db4oFileName); 04 IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 05 try 06 { 07 DateTime dt1 = DateTime.UtcNow; 08 for (int i = 0; i< 50000; i++){ 09 Pilot pilot = new Pilot("Pilot #"+i); 10 db.Set(pilot); 11 } 12 DateTime dt2 = DateTime.UtcNow; 13 TimeSpan diff = dt2 - dt1; 14 System.Console.WriteLine("Time elapsed for setting objects ="+ diff.TotalMilliseconds + " ms"); 15 dt1 = DateTime.UtcNow; 16 db.Commit(); ; 17 dt2 = DateTime.UtcNow; 18 diff = dt2 - dt1; 19 System.Console.WriteLine("Time elapsed for commit =" + diff.TotalMilliseconds + " ms"); 20 21 } 22 finally 23 { 24 db.Close(); 25 } }
CachedIOExample.cs: GetObjects
01private static void GetObjects(IConfiguration configuration) 02 { 03 IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 04 try 05 { 06 DateTime dt1 = DateTime.UtcNow; 07 IObjectSet result = db.Get(null); 08 DateTime dt2 = DateTime.UtcNow; 09 TimeSpan diff = dt2 - dt1; 10 System.Console.WriteLine("Time elapsed for the query =" + diff.TotalMilliseconds + " ms"); 11 Console.WriteLine("Objects in the database: " + result.Count); 12 } 13 finally 14 { 15 db.Close(); 16 } 17 }
CachedIOExample.vb: SetObjects
01Private Shared Sub SetObjects(ByVal configuration As IConfiguration) 02 File.Delete(Db4oFileName) 03 Dim db As IObjectContainer = Db4oFactory.OpenFile(configuration, Db4oFileName) 04 Try 05 Dim dt1 As DateTime = DateTime.UtcNow 06 Dim i As Integer = 0 07 While i < 50000 08 Dim pilot As Pilot = New Pilot("Pilot #" + i.ToString()) 09 db.Set(pilot) 10 System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1) 11 End While 12 Dim dt2 As DateTime = DateTime.UtcNow 13 Dim diff As TimeSpan = dt2 - dt1 14 System.Console.WriteLine("Time elapsed for setting objects =" + diff.TotalMilliseconds.ToString() + " ms") 15 dt1 = DateTime.UtcNow 16 db.Commit() 17 18 dt2 = DateTime.UtcNow 19 diff = dt2 - dt1 20 System.Console.WriteLine("Time elapsed for commit =" + diff.TotalMilliseconds.ToString() + " ms") 21 Finally 22 db.Close() 23 End Try 24 End Sub
CachedIOExample.vb: GetObjects
01Private Shared Sub GetObjects(ByVal configuration As IConfiguration) 02 Dim db As IObjectContainer = Db4oFactory.OpenFile(configuration, Db4oFileName) 03 Try 04 Dim dt1 As DateTime = DateTime.UtcNow 05 Dim result As IObjectSet = db.Get(Nothing) 06 Dim dt2 As DateTime = DateTime.UtcNow 07 Dim diff As TimeSpan = dt2 - dt1 08 System.Console.WriteLine("Time elapsed for the query =" + diff.TotalMilliseconds.ToString() + " ms") 09 Console.WriteLine("Objects in the database: " + result.Count.ToString()) 10 Finally 11 db.Close() 12 End Try 13 End Sub

Try to execute the code with the default settings and write down the results. Then configure CachedIoAdapter using the code below and test the performance again:

CachedIOExample.cs: ConfigureCache
1private static IConfiguration ConfigureCache(){ 2 System.Console.WriteLine("Setting up cached io adapter"); 3 // new cached IO adapter with 256 pages 1024 bytes each 4 CachedIoAdapter adapter = new CachedIoAdapter(new RandomAccessFileAdapter(), 1024, 256); 5 IConfiguration configuration = Db4oFactory.NewConfiguration(); 6 configuration.Io(adapter); 7 return configuration; 8 }
CachedIOExample.vb: ConfigureCache
1Private Shared Function ConfigureCache() As IConfiguration 2 System.Console.WriteLine("Setting up cached io adapter") 3 Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 4 ' new cached IO adapter with 256 pages 1024 bytes each 5 Dim adapter As CachedIoAdapter = New CachedIoAdapter(New RandomAccessFileAdapter, 1024, 256) 6 configuration.Io(adapter) 7 Return configuration 8 End Function

The performance delta will be more significant for more objects and bigger cache memory.