using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.IO; using System.Linq; using System.Text; using System.Threading; using Pithos.Interfaces; using Pithos.Network; namespace Pithos.Core.Test { public class MockStatusChecker : IStatusChecker, IStatusKeeper { 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) { Contract.Requires(!String.IsNullOrWhiteSpace(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 void ProcessExistingFiles(IEnumerable paths) { var newFiles = (from file in paths where !_overlayCache.ContainsKey(file.FullName) select new { FilePath = file.FullName.ToLower(), OverlayStatus = FileOverlayStatus.Unversioned, FileStatus = FileStatus.Created, Checksum = Signature.CalculateMD5(file) }); var files = new ConcurrentBag(); foreach (var state in newFiles) { _overlayCache[state.FilePath] = state.OverlayStatus; _statusCache[state.FilePath] = state.FileStatus; _checksums[state.FilePath] = state.Checksum; files.Add(state.FilePath); } } public void Stop() { throw new NotImplementedException(); } public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path", "path can't be empty"); SetFileStatus(path, fileStatus); SetFileOverlayStatus(path, overlayStatus); } public void StoreInfo(string path, ObjectInfo objectInfo) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path", "path can't be empty"); if (objectInfo == null) throw new ArgumentNullException("objectInfo", "objectInfo can't be empty"); _statusCache[path] = FileStatus.Unchanged; _overlayCache[path] = FileOverlayStatus.Normal; _checksums[path] = objectInfo.Hash; } public T GetStatus(string path, Func getter, T defaultValue) { throw new NotImplementedException(); } public void SetStatus(string path, Action setter) { throw new NotImplementedException(); } ConcurrentDictionary _networkState = new ConcurrentDictionary(); public void SetNetworkState(string path, NetworkOperation operation) { _networkState[path.ToLower()] = operation; //Removing may fail so we store the "None" value anyway if (operation == NetworkOperation.None) { NetworkOperation oldOperation; _networkState.TryRemove(path, out oldOperation); } } public NetworkOperation GetNetworkState(string path) { NetworkOperation operation; if (_networkState.TryGetValue(path, out operation)) return operation; return NetworkOperation.None; } public void StartProcessing(CancellationToken token) { } public string BlockHash { get; set; } public int BlockSize { get; set; } public void ChangeRoots(string oldPath, string newPath) { throw new NotImplementedException(); } 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 UpdateFileChecksum(string path, string checksum) { _checksums[path] = checksum; } 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); } } }