The simplest defragmentation can look like this:
1private static void simplestDefragment(){ 2
try { 3
Defragment.defrag(DB_FILE); 4
} catch (IOException ex){ 5
System.out.println(ex.toString()); 6
} 7
}
The following example shows how to implement defragmentation listener:
01private static void defragmentWithListener(){ 02
DefragmentConfig config=new DefragmentConfig(DB_FILE); 03
try { 04
Defragment.defrag(config, new DefragmentListener() { 05
public void notifyDefragmentInfo(DefragmentInfo info) { 06
System.err.println(info); 07
} 08
}); 09
} catch (Exception ex){ 10
System.out.println(ex.toString()); 11
} 12
}
The following example will run defragment using TreeIDMapping and commit frequency of 1 commit per 5000 objects. The backup file will be deleted if already exists, only available to the classloader classes will be left in the database (java version) and the file will be upgraded if necessary.
01private static void configuredDefragment(){ 02
DefragmentConfig config=new DefragmentConfig(DB_FILE, BACKUP_FILE, new TreeIDMapping()); 03
config.objectCommitFrequency(5000); 04
config.db4oConfig(Db4o.cloneConfiguration()); 05
config.forceBackupDelete(true); 06
config.storedClassFilter(new AvailableClassFilter()); 07
config.upgradeFile(DB_FILE + ".upg"); 08
try { 09
Defragment.defrag(config); 10
} catch (Exception ex){ 11
System.out.println(ex.toString()); 12
} 13
}