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:
01public static void SetObjects() 02
{ 03
File.Delete(YapFileName); 04
IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 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.Milliseconds + " 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.Milliseconds + " ms"); 20
21
} 22
finally 23
{ 24
db.Close(); 25
} 26
}
01public static void GetObjects() 02
{ 03
Db4oFactory.Configure().Io(new RandomAccessFileAdapter()); 04
IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 05
try 06
{ 07
DateTime dt1 = DateTime.UtcNow; 08
IObjectSet result = db.Get(null); 09
DateTime dt2 = DateTime.UtcNow; 10
TimeSpan diff = dt2 - dt1; 11
System.Console.WriteLine("Time elapsed for the query =" + diff.Milliseconds + " ms"); 12
Console.WriteLine("Objects in the database: " + result.Count); 13
} 14
finally 15
{ 16
db.Close(); 17
} 18
}
01Public Shared Sub SetObjects() 02
File.Delete(YapFileName) 03
Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 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.Milliseconds.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.Milliseconds.ToString() + " ms") 21
Finally 22
db.Close() 23
End Try 24
End Sub
01Public Shared Sub GetObjects() 02
Db4oFactory.Configure.Io(New RandomAccessFileAdapter) 03
Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 04
Try 05
Dim dt1 As DateTime = DateTime.UtcNow 06
Dim result As IObjectSet = db.Get(Nothing) 07
Dim dt2 As DateTime = DateTime.UtcNow 08
Dim diff As TimeSpan = dt2 - dt1 09
System.Console.WriteLine("Time elapsed for the query =" + diff.Milliseconds.ToString() + " ms") 10
Console.WriteLine("Objects in the database: " + result.Count.ToString()) 11
Finally 12
db.Close() 13
End Try 14
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:
1public static void 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
Db4oFactory.Configure().Io(adapter); 6
}
1Public Shared Sub ConfigureCache() 2
System.Console.WriteLine("Setting up cached io adapter") 3
' new cached IO adapter with 256 pages 1024 bytes each 4
Dim adapter As CachedIoAdapter = New CachedIoAdapter(New RandomAccessFileAdapter, 1024, 256) 5
Db4oFactory.Configure.Io(adapter) 6
End Sub
The performance delta will be more significant for more objects and bigger cache memory.