using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel.Composition; using System.Diagnostics.Contracts; using System.Linq; using Pithos.Interfaces; namespace Pithos.Core { //[Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))] public class InMemStatusChecker:IStatusChecker,IStatusKeeper { [Import] public IPithosSettings Settings { get; set; } private readonly string[] _states = {"Normal", "Modified", "Conflict","Synch"}; ConcurrentDictionary _overlayCache = new ConcurrentDictionary(); ConcurrentDictionary _statusCache = new ConcurrentDictionary(); ConcurrentDictionary _checksums = new ConcurrentDictionary(); public FileOverlayStatus GetFileOverlayStatus(string path) { if (!_overlayCache.ContainsKey(path)) return FileOverlayStatus.Unversioned; var pithosPath = Settings.PithosPath; if (path.StartsWith(pithosPath,true,null)) { var status = _overlayCache[path]; return status; } return FileOverlayStatus.Unversioned; } public IEnumerable StoreUnversionedFiles(ParallelQuery paths) { var newFiles = (from file in paths where !_overlayCache.ContainsKey(file) select new { FilePath = file, OverlayStatus = FileOverlayStatus.Unversioned, FileStatus = FileStatus.Created, Checksum = Signature.CalculateHash(file) }); ConcurrentBag files = new ConcurrentBag(); newFiles.ForAll(state => { _overlayCache[state.FilePath] = state.OverlayStatus; _statusCache[state.FilePath] = state.FileStatus; _checksums[state.FilePath] = state.Checksum; files.Add(state.FilePath); }); return files.GetConsumingEnumerable(); } private PithosStatus _pithosStatus = PithosStatus.InSynch; public void SetPithosStatus(PithosStatus status) { _pithosStatus = status; } public PithosStatus GetPithosStatus() { return _pithosStatus; } public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus) { _overlayCache[path] = overlayStatus; } public void RemoveFileOverlayStatus(string path) { FileOverlayStatus value; _overlayCache.TryRemove(path, out value); } public void RenameFileOverlayStatus(string oldPath, string newPath) { var status=_overlayCache[oldPath]; _overlayCache[newPath] = status; FileOverlayStatus value; _overlayCache.TryRemove(oldPath,out value); } public void SetFileStatus(string path, FileStatus status) { _statusCache[path] = status; } public FileStatus GetFileStatus(string path) { if (!_statusCache.ContainsKey(path)) return FileStatus.Missing; return _statusCache[path]; } public void ClearFileStatus(string path) { FileStatus value; _statusCache.TryRemove(path,out value); } public void UpdateFileChecksum(string path, string checksum) { _checksums[path] = checksum; } } }