Db4o database file is structured as sets of free and occupied slots, very much like a file system - and just like a file system it can be fragmented, resulting in a file that is larger than it needs to be.
Defragment tool helps to fix this problem, creating a new database file and copying all objects from the current database file to the new database file. All indexes are recreated. The resulting database file will be smaller and faster. It is recommended to apply defragmentation on a regular basis to achieve better performance.
The simplest way to defragment a yap file would be:
c#: Db4objects.Db4o.Defragment.Defragment.Defrag(filename)
VB:Db4objects.Db4o.Defragment.Defragment.Defrag(filename)
The file must not be opened by another process during defragmentation!
This will move the file sample.yap to sample.yap.backup, then create a defragmented version of this file in the original position, using a temporary file sample.yap.mapping. If the backup file already exists, this will throw an IOException and no action will be taken.
Please note that the defragment process relies on database configuration information. If you're setting non-standard configuration options for your database from your application, please make sure you pass an equivalent configuration into the defragmentation process as described below.
DefragmentConfig gives you a more fine-grained control over the defragmentation process. This class provides the following API:
01public static void RunDefragment() 02
{ 03
DefragmentConfig config = new DefragmentConfig("sample.yap", "sample.bap"); 04
config.ForceBackupDelete(true); 05
config.Db4oConfig(CreateDb4oConfiguration()); 06
config.StoredClassFilter(new AvailableTypeFilter()); 07
try 08
{ 09
Defragment.Defrag(config); 10
} 11
catch (Exception ex) 12
{ 13
System.Console.WriteLine(ex.Message); 14
} 15
}
01Public Shared Sub RunDefragment() 02
Dim config As DefragmentConfig = New DefragmentConfig("sample.yap", "sample.bap") 03
config.ForceBackupDelete(True) 04
config.Db4oConfiguration(CreateDb4oConfiguration()) 05
config.StoredClassFilter(New AvailableTypeFilter()) 06
Try 07
Defragment.Defrag(config) 08
Catch ex As Exception 09
System.Console.WriteLine(ex.Message) 10
End Try 11
End Sub
The defragmentation process will skip all classes that have instances stored within the yap file, but that are not available on the class path (through the current classloader) anymore.