2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Diagnostics.Contracts;
8 using System.Threading;
9 using Pithos.Interfaces;
12 namespace Pithos.Core.Test
14 public class MockStatusChecker : IStatusChecker, IStatusKeeper
16 public IPithosSettings Settings { get; set; }
18 private readonly string[] _states = { "Normal", "Modified", "Conflict", "Synch" };
20 readonly ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
21 readonly ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
22 readonly ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
24 public FileOverlayStatus GetFileOverlayStatus(string path)
27 if (!_overlayCache.ContainsKey(path))
28 return FileOverlayStatus.Unversioned;
30 var pithosPath = Settings.PithosPath;
31 if (path.StartsWith(pithosPath, true, null))
33 var status = _overlayCache[path];
36 return FileOverlayStatus.Unversioned;
39 public void ProcessExistingFiles(IEnumerable<FileInfo> paths)
42 var newFiles = (from file in paths
43 where !_overlayCache.ContainsKey(file.FullName)
46 FilePath = file.FullName.ToLower(),
47 OverlayStatus = FileOverlayStatus.Unversioned,
48 FileStatus = FileStatus.Created,
49 Checksum = Signature.CalculateMD5(file)
51 var files = new ConcurrentBag<string>();
52 foreach (var state in newFiles)
54 _overlayCache[state.FilePath] = state.OverlayStatus;
55 _statusCache[state.FilePath] = state.FileStatus;
56 _checksums[state.FilePath] = state.Checksum;
57 files.Add(state.FilePath);
64 throw new NotImplementedException();
67 public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
69 if (String.IsNullOrWhiteSpace(path))
70 throw new ArgumentNullException("path", "path can't be empty");
71 SetFileStatus(path, fileStatus);
72 SetFileOverlayStatus(path, overlayStatus);
75 public void StoreInfo(string path, ObjectInfo objectInfo)
77 if (String.IsNullOrWhiteSpace(path))
78 throw new ArgumentNullException("path", "path can't be empty");
79 if (objectInfo == null)
80 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
82 _statusCache[path] = FileStatus.Unchanged;
83 _overlayCache[path] = FileOverlayStatus.Normal;
84 _checksums[path] = objectInfo.Hash;
89 public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
91 throw new NotImplementedException();
94 public void SetStatus(string path, Action<FileState> setter)
96 throw new NotImplementedException();
99 ConcurrentDictionary<string, NetworkOperation> _networkState = new ConcurrentDictionary<string, NetworkOperation>();
102 public void SetNetworkState(string path, NetworkOperation operation)
104 _networkState[path.ToLower()] = operation;
105 //Removing may fail so we store the "None" value anyway
106 if (operation == NetworkOperation.None)
108 NetworkOperation oldOperation;
109 _networkState.TryRemove(path, out oldOperation);
113 public NetworkOperation GetNetworkState(string path)
115 NetworkOperation operation;
116 if (_networkState.TryGetValue(path, out operation))
118 return NetworkOperation.None;
121 public void StartProcessing(CancellationToken token)
126 public string BlockHash { get; set; }
128 public int BlockSize { get; set; }
129 public void ChangeRoots(string oldPath, string newPath)
131 throw new NotImplementedException();
134 public FileState GetStateByFilePath(string path)
136 throw new NotImplementedException();
139 public void ClearFolderStatus(string path)
141 throw new NotImplementedException();
144 public IEnumerable<FileState> GetChildren(FileState fileState)
146 throw new NotImplementedException();
150 private PithosStatus _pithosStatus = PithosStatus.InSynch;
151 public void SetPithosStatus(PithosStatus status)
153 _pithosStatus = status;
156 public PithosStatus GetPithosStatus()
158 return _pithosStatus;
161 public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
163 _overlayCache[path] = overlayStatus;
166 public void RemoveFileOverlayStatus(string path)
168 FileOverlayStatus value;
169 _overlayCache.TryRemove(path, out value);
172 public void RenameFileOverlayStatus(string oldPath, string newPath)
174 var status = _overlayCache[oldPath];
175 _overlayCache[newPath] = status;
176 FileOverlayStatus value;
177 _overlayCache.TryRemove(oldPath, out value);
180 public void UpdateFileChecksum(string path, string checksum)
182 _checksums[path] = checksum;
185 public void SetFileStatus(string path, FileStatus status)
187 _statusCache[path] = status;
190 public FileStatus GetFileStatus(string path)
192 if (!_statusCache.ContainsKey(path))
193 return FileStatus.Missing;
194 return _statusCache[path];
197 public void ClearFileStatus(string path)
200 _statusCache.TryRemove(path,out value);