示例#1
0
        public void CompactStorageHF()
        {
            lock (_lock)
            {
                try
                {
                    _log.Debug("Compacting storage file ...");
                    if (Directory.Exists(_Path + "temp"))
                    {
                        Directory.Delete(_Path + "temp", true);
                    }

                    KeyStoreHF newfile = new KeyStoreHF(_Path + "temp");
                    string[]   keys    = _keys.GetKeys().Cast <string>().ToArray();
                    _log.Debug("Number of keys : " + keys.Length);
                    foreach (var k in keys)
                    {
                        newfile.SetObjectHF(k, GetObjectHF(k));
                    }
                    newfile.Shutdown();
                    _log.Debug("Compact done.");
                    // shutdown and move files and restart here
                    if (Directory.Exists(_Path + "old"))
                    {
                        Directory.Delete(_Path + "old", true);
                    }
                    Directory.CreateDirectory(_Path + "old");
                    _datastore.Shutdown();
                    _keys.Shutdown();
                    _log.Debug("Moving files...");
                    foreach (var f in Directory.GetFiles(_Path, "*.*"))
                    {
                        File.Move(f, _Path + "old" + _S + Path.GetFileName(f));
                    }

                    foreach (var f in Directory.GetFiles(_Path + "temp", "*.*"))
                    {
                        File.Move(f, _Path + Path.GetFileName(f));
                    }

                    Directory.Delete(_Path + "temp", true);
                    //Directory.Delete(_Path + "old", true); // FEATURE : delete or keep?
                    _log.Debug("Re-opening storage file");
                    _datastore = new StorageFileHF(Path.Combine(_Path, "data.mghf"), Global.HighFrequencyKVDiskBlockSize);
                    _keys      = new MGIndex <string>(_Path, "keys.idx", 255, /*Global.PageItemCount,*/ false);

                    _BlockSize = _datastore.GetBlockSize();
                }
                catch (Exception ex)
                {
                    _log.Error(ex);
                }
            }
        }