2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Diagnostics.Contracts;
7 using System.Threading;
8 using Pithos.Interfaces;
10 namespace Pithos.Core.Test
12 public class MockStatusChecker : IStatusChecker, IStatusKeeper
14 public IPithosSettings Settings { get; set; }
16 private readonly string[] _states = { "Normal", "Modified", "Conflict", "Synch" };
18 ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
19 ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
20 ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
22 public FileOverlayStatus GetFileOverlayStatus(string path)
24 Contract.Requires(!String.IsNullOrWhiteSpace(path));
25 if (!_overlayCache.ContainsKey(path))
26 return FileOverlayStatus.Unversioned;
28 var pithosPath = Settings.PithosPath;
29 if (path.StartsWith(pithosPath, true, null))
31 var status = _overlayCache[path];
34 return FileOverlayStatus.Unversioned;
37 public IEnumerable<string> StoreUnversionedFiles(ParallelQuery<string> paths)
40 var newFiles = (from file in paths
41 where !_overlayCache.ContainsKey(file)
45 OverlayStatus = FileOverlayStatus.Unversioned,
46 FileStatus = FileStatus.Created,
47 Checksum = Signature.CalculateHash(file)
49 var files = new ConcurrentBag<string>();
50 newFiles.ForAll(state =>
52 _overlayCache[state.FilePath] = state.OverlayStatus;
53 _statusCache[state.FilePath] = state.FileStatus;
54 _checksums[state.FilePath] = state.Checksum;
55 files.Add(state.FilePath);
57 return files.ToList();
63 throw new NotImplementedException();
66 public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
68 if (String.IsNullOrWhiteSpace(path))
69 throw new ArgumentNullException("path", "path can't be empty");
70 SetFileStatus(path, fileStatus);
71 SetFileOverlayStatus(path, overlayStatus);
74 public void StoreInfo(string path, ObjectInfo objectInfo)
76 if (String.IsNullOrWhiteSpace(path))
77 throw new ArgumentNullException("path", "path can't be empty");
78 if (objectInfo == null)
79 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
81 _statusCache[path] = FileStatus.Unchanged;
82 _overlayCache[path] = FileOverlayStatus.Normal;
83 _checksums[path] = objectInfo.Hash;
88 public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
90 throw new NotImplementedException();
93 public void SetStatus(string path, Action<FileState> setter)
95 throw new NotImplementedException();
98 ConcurrentDictionary<string, NetworkState> _networkState = new ConcurrentDictionary<string, NetworkState>();
101 public void SetNetworkState(string path, NetworkState state)
103 _networkState[path.ToLower()] = state;
104 //Removing may fail so we store the "None" value anyway
105 if (state == NetworkState.None)
107 NetworkState oldState;
108 _networkState.TryRemove(path, out oldState);
112 public NetworkState GetNetworkState(string path)
115 if (_networkState.TryGetValue(path, out state))
117 return NetworkState.None;
120 public void StartProcessing(CancellationToken token)
126 private PithosStatus _pithosStatus = PithosStatus.InSynch;
127 public void SetPithosStatus(PithosStatus status)
129 _pithosStatus = status;
132 public PithosStatus GetPithosStatus()
134 return _pithosStatus;
137 public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
139 _overlayCache[path] = overlayStatus;
142 public void RemoveFileOverlayStatus(string path)
144 FileOverlayStatus value;
145 _overlayCache.TryRemove(path, out value);
148 public void RenameFileOverlayStatus(string oldPath, string newPath)
150 var status = _overlayCache[oldPath];
151 _overlayCache[newPath] = status;
152 FileOverlayStatus value;
153 _overlayCache.TryRemove(oldPath, out value);
156 public void UpdateFileChecksum(string path, string checksum)
158 _checksums[path] = checksum;
161 public void SetFileStatus(string path, FileStatus status)
163 _statusCache[path] = status;
166 public FileStatus GetFileStatus(string path)
168 if (!_statusCache.ContainsKey(path))
169 return FileStatus.Missing;
170 return _statusCache[path];
173 public void ClearFileStatus(string path)
176 _statusCache.TryRemove(path,out value);