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.X_Object_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; }
131 public IStatusNotification StatusNotification { get; set; }
133 public void ChangeRoots(string oldPath, string newPath)
135 throw new NotImplementedException();
138 public FileState GetStateByFilePath(string path)
140 throw new NotImplementedException();
143 public void ClearFolderStatus(string path)
145 throw new NotImplementedException();
148 public IEnumerable<FileState> GetChildren(FileState fileState)
150 throw new NotImplementedException();
153 public void EnsureFileState(string path)
155 throw new NotImplementedException();
159 private PithosStatus _pithosStatus = PithosStatus.InSynch;
160 public void SetPithosStatus(PithosStatus status)
162 _pithosStatus = status;
165 public PithosStatus GetPithosStatus()
167 return _pithosStatus;
170 public Task SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus, string etag = null)
172 _overlayCache[path] = overlayStatus;
173 return Task.Factory.StartNew(()=>{});
176 public void RemoveFileOverlayStatus(string path)
178 FileOverlayStatus value;
179 _overlayCache.TryRemove(path, out value);
182 public void RenameFileOverlayStatus(string oldPath, string newPath)
184 var status = _overlayCache[oldPath];
185 _overlayCache[newPath] = status;
186 FileOverlayStatus value;
187 _overlayCache.TryRemove(oldPath, out value);
190 public void UpdateFileChecksum(string path, string etag, string checksum)
192 _checksums[path] = checksum;
195 public void SetFileStatus(string path, FileStatus status)
197 _statusCache[path] = status;
200 public FileStatus GetFileStatus(string path)
202 if (!_statusCache.ContainsKey(path))
203 return FileStatus.Missing;
204 return _statusCache[path];
207 public void ClearFileStatus(string path)
210 _statusCache.TryRemove(path,out value);
214 public void CleanupStaleStates(AccountInfo accountInfo, List<ObjectInfo> objectInfos)
216 //throw new NotImplementedException();
220 public void CleanupOrphanStates()
222 throw new NotImplementedException();
225 public void UpdateLastMD5(FileInfo path, string etag)
227 throw new NotImplementedException();