/// <exception cref="System.IO.IOException"></exception> private static void MoveToBackup(DefragmentConfig config) { IStorage origStorage = config.Db4oConfig().Storage; if (origStorage == config.BackupStorage()) { origStorage.Rename(config.OrigPath(), config.BackupPath()); return; } CopyBin(origStorage, config.BackupStorage(), config.OrigPath(), config.BackupPath ()); origStorage.Delete(config.OrigPath()); }
/// <summary> /// Renames the file at the configured original path to the configured backup /// path and then builds a defragmented version of the file in the original /// place. /// </summary> /// <remarks> /// Renames the file at the configured original path to the configured backup /// path and then builds a defragmented version of the file in the original /// place. /// </remarks> /// <param name="config">The configuration for this defragmentation run.</param> /// <param name="listener"> /// A listener for status notifications during the defragmentation /// process. /// </param> /// <exception cref="System.IO.IOException">if the original file cannot be moved to the backup location /// </exception> public static void Defrag(DefragmentConfig config, IDefragmentListener listener) { IStorage storage = config.Db4oConfig().Storage; EnsureFileExists(storage, config.OrigPath()); IStorage backupStorage = config.BackupStorage(); if (backupStorage.Exists(config.BackupPath())) { if (!config.ForceBackupDelete()) { throw new IOException("Could not use '" + config.BackupPath() + "' as backup path - file exists." ); } } // Always delete, because !exists can indicate length == 0 backupStorage.Delete(config.BackupPath()); MoveToBackup(config); if (config.FileNeedsUpgrade()) { UpgradeFile(config); } DefragmentServicesImpl services = new DefragmentServicesImpl(config, listener); try { FirstPass(services, config); services.CommitIds(); SecondPass(services, config); services.CommitIds(); DefragUnindexed(services); services.CommitIds(); services.DefragIdToTimestampBtree(); services.ReplaceClassMetadataRepository(); } catch (CorruptionException exc) { Sharpen.Runtime.PrintStackTrace(exc); } finally { services.Close(); } }
/// <exception cref="System.IO.IOException"></exception> private static void UpgradeFile(DefragmentConfig config) { CopyBin(config.BackupStorage(), config.BackupStorage(), config.BackupPath(), config .TempPath()); IConfiguration db4oConfig = (IConfiguration)((Config4Impl)config.Db4oConfig()).DeepClone (null); db4oConfig.Storage = config.BackupStorage(); db4oConfig.AllowVersionUpdates(true); IObjectContainer db = Db4oFactory.OpenFile(db4oConfig, config.TempPath()); db.Close(); }