Defragment

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:

  • DefragmentConfig(String origPath, String backupPath,String mappingPath)
    The constructor takes the path to the original file (which is also the location where the defragmented file will be located), the path to the backup of the fragmented version of the file, and the path where the temporary mapping file should be created.
  • db4oConfig(com.db4o.config.Configuration config)
    The db4o configuration passed in will be used to look up settings that influence low-level file layout details, like UUID settings, etc. If your application uses non-standard configuration settings, you should always pass in a configuration just like you use it within your application itself. If you're still using the 'global' configuration approach via Db4o.configure(), you can obtain a faithful copy of the global configuration for use with defragment afterwards by calling Db4o.cloneConfiguration(). However,  the non-static approach (i.e. obtain a fresh configuration instance via Db4o.newConfiguration() and modify it as needed) is recommended in general.
  • storedClassFilter(com.db4o.defragment.StoredClassFilter storedClassFilter)
    Sets a filter that may skip specific StoredClasses during the defragmentation process (as well as their instances, associated indices, etc.) For example, com.db4o.defragment.AvailableClassFilter will exclude all classes from defragmentation that are not available on the classpath anymore.
  • forceBackupDelete(boolean forceBackupDelete)
    If set to true, will delete any existing file at the backup location before moving the original file. If set to false, an IOException will be thrown and no action will be taken if a file already exists in the backup location.

DefragmentExample.cs: RunDefragment
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 }

DefragmentExample.vb: RunDefragment
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

This will move the file sample.yap to sample.bap, then create a defragmented version of this file in the original position. If the backup file already exists, it will be deleted.

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.