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 ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
21 ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
22 ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
24 public FileOverlayStatus GetFileOverlayStatus(string path)
26 Contract.Requires(!String.IsNullOrWhiteSpace(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();
135 private PithosStatus _pithosStatus = PithosStatus.InSynch;
136 public void SetPithosStatus(PithosStatus status)
138 _pithosStatus = status;
141 public PithosStatus GetPithosStatus()
143 return _pithosStatus;
146 public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
148 _overlayCache[path] = overlayStatus;
151 public void RemoveFileOverlayStatus(string path)
153 FileOverlayStatus value;
154 _overlayCache.TryRemove(path, out value);
157 public void RenameFileOverlayStatus(string oldPath, string newPath)
159 var status = _overlayCache[oldPath];
160 _overlayCache[newPath] = status;
161 FileOverlayStatus value;
162 _overlayCache.TryRemove(oldPath, out value);
165 public void UpdateFileChecksum(string path, string checksum)
167 _checksums[path] = checksum;
170 public void SetFileStatus(string path, FileStatus status)
172 _statusCache[path] = status;
175 public FileStatus GetFileStatus(string path)
177 if (!_statusCache.ContainsKey(path))
178 return FileStatus.Missing;
179 return _statusCache[path];
182 public void ClearFileStatus(string path)
185 _statusCache.TryRemove(path,out value);