2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Diagnostics.Contracts;
8 using System.Threading;
9 using System.Threading.Tasks;
10 using Pithos.Interfaces;
13 namespace Pithos.Core.Test
15 public class MockStatusChecker : IStatusChecker, IStatusKeeper
17 public IPithosSettings Settings { get; set; }
19 private readonly string[] _states = { "Normal", "Modified", "Conflict", "Synch" };
21 readonly ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
22 readonly ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
23 readonly ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
25 public FileOverlayStatus GetFileOverlayStatus(string path)
28 if (!_overlayCache.ContainsKey(path))
29 return FileOverlayStatus.Unversioned;
31 var pithosPath = Settings.PithosPath;
32 if (path.StartsWith(pithosPath, true, null))
34 var status = _overlayCache[path];
37 return FileOverlayStatus.Unversioned;
40 public void ProcessExistingFiles(IEnumerable<FileInfo> paths)
43 var newFiles = (from file in paths
44 where !_overlayCache.ContainsKey(file.FullName)
47 FilePath = file.FullName.ToLower(),
48 OverlayStatus = FileOverlayStatus.Unversioned,
49 FileStatus = FileStatus.Created,
50 Checksum = Signature.CalculateMD5(file)
52 var files = new ConcurrentBag<string>();
53 foreach (var state in newFiles)
55 _overlayCache[state.FilePath] = state.OverlayStatus;
56 _statusCache[state.FilePath] = state.FileStatus;
57 _checksums[state.FilePath] = state.Checksum;
58 files.Add(state.FilePath);
65 throw new NotImplementedException();
68 public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus, string localFileMissingFromServer)
70 if (String.IsNullOrWhiteSpace(path))
71 throw new ArgumentNullException("path", "path can't be empty");
72 SetFileStatus(path, fileStatus);
73 SetFileOverlayStatus(path, overlayStatus);
76 public void StoreInfo(string path, ObjectInfo objectInfo)
78 if (String.IsNullOrWhiteSpace(path))
79 throw new ArgumentNullException("path", "path can't be empty");
80 if (objectInfo == null)
81 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
83 _statusCache[path] = FileStatus.Unchanged;
84 _overlayCache[path] = FileOverlayStatus.Normal;
85 _checksums[path] = objectInfo.Hash;
90 public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
92 throw new NotImplementedException();
95 public void SetStatus(string path, Action<FileState> setter)
97 throw new NotImplementedException();
100 ConcurrentDictionary<string, NetworkOperation> _networkState = new ConcurrentDictionary<string, NetworkOperation>();
103 public void SetNetworkState(string path, NetworkOperation operation)
105 _networkState[path.ToLower()] = operation;
106 //Removing may fail so we store the "None" value anyway
107 if (operation == NetworkOperation.None)
109 NetworkOperation oldOperation;
110 _networkState.TryRemove(path, out oldOperation);
114 public NetworkOperation GetNetworkState(string path)
116 NetworkOperation operation;
117 if (_networkState.TryGetValue(path, out operation))
119 return NetworkOperation.None;
122 public void StartProcessing(CancellationToken token)
127 public string BlockHash { get; set; }
129 public int BlockSize { get; set; }
130 public void ChangeRoots(string oldPath, string newPath)
132 throw new NotImplementedException();
135 public FileState GetStateByFilePath(string path)
137 throw new NotImplementedException();
140 public void ClearFolderStatus(string path)
142 throw new NotImplementedException();
145 public IEnumerable<FileState> GetChildren(FileState fileState)
147 throw new NotImplementedException();
150 public void EnsureFileState(string path)
152 throw new NotImplementedException();
156 private PithosStatus _pithosStatus = PithosStatus.InSynch;
157 public void SetPithosStatus(PithosStatus status)
159 _pithosStatus = status;
162 public PithosStatus GetPithosStatus()
164 return _pithosStatus;
167 public Task SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus, string shortHash = null)
169 _overlayCache[path] = overlayStatus;
170 return Task.Factory.StartNew(()=>{});
173 public void RemoveFileOverlayStatus(string path)
175 FileOverlayStatus value;
176 _overlayCache.TryRemove(path, out value);
179 public void RenameFileOverlayStatus(string oldPath, string newPath)
181 var status = _overlayCache[oldPath];
182 _overlayCache[newPath] = status;
183 FileOverlayStatus value;
184 _overlayCache.TryRemove(oldPath, out value);
187 public void UpdateFileChecksum(string path, string shortHash, string checksum)
189 _checksums[path] = checksum;
192 public void SetFileStatus(string path, FileStatus status)
194 _statusCache[path] = status;
197 public FileStatus GetFileStatus(string path)
199 if (!_statusCache.ContainsKey(path))
200 return FileStatus.Missing;
201 return _statusCache[path];
204 public void ClearFileStatus(string path)
207 _statusCache.TryRemove(path,out value);
211 public void CleanupStaleStates(AccountInfo accountInfo, List<ObjectInfo> objectInfos)
213 //throw new NotImplementedException();
217 public void CleanupOrphanStates()
219 throw new NotImplementedException();