2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Diagnostics.Contracts;
7 using Pithos.Interfaces;
9 namespace Pithos.Core.Test
11 public class MockStatusChecker : IStatusChecker, IStatusKeeper
13 public IPithosSettings Settings { get; set; }
15 private readonly string[] _states = { "Normal", "Modified", "Conflict", "Synch" };
17 ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
18 ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
19 ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
21 public FileOverlayStatus GetFileOverlayStatus(string path)
23 Contract.Requires(!String.IsNullOrWhiteSpace(path));
24 if (!_overlayCache.ContainsKey(path))
25 return FileOverlayStatus.Unversioned;
27 var pithosPath = Settings.PithosPath;
28 if (path.StartsWith(pithosPath, true, null))
30 var status = _overlayCache[path];
33 return FileOverlayStatus.Unversioned;
36 public IEnumerable<string> StoreUnversionedFiles(ParallelQuery<string> paths)
39 var newFiles = (from file in paths
40 where !_overlayCache.ContainsKey(file)
44 OverlayStatus = FileOverlayStatus.Unversioned,
45 FileStatus = FileStatus.Created,
46 Checksum = Signature.CalculateHash(file)
48 var files = new ConcurrentBag<string>();
49 newFiles.ForAll(state =>
51 _overlayCache[state.FilePath] = state.OverlayStatus;
52 _statusCache[state.FilePath] = state.FileStatus;
53 _checksums[state.FilePath] = state.Checksum;
54 files.Add(state.FilePath);
56 return files.ToList();
62 throw new NotImplementedException();
65 public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
67 if (String.IsNullOrWhiteSpace(path))
68 throw new ArgumentNullException("path", "path can't be empty");
69 SetFileStatus(path, fileStatus);
70 SetFileOverlayStatus(path, overlayStatus);
73 public void StoreInfo(string path, ObjectInfo objectInfo)
75 if (String.IsNullOrWhiteSpace(path))
76 throw new ArgumentNullException("path", "path can't be empty");
77 if (objectInfo == null)
78 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
80 _statusCache[path] = FileStatus.Unchanged;
81 _overlayCache[path] = FileOverlayStatus.Normal;
82 _checksums[path] = objectInfo.Hash;
87 public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
89 throw new NotImplementedException();
92 public void SetStatus(string path, Action<FileState> setter)
94 throw new NotImplementedException();
97 ConcurrentDictionary<string, NetworkState> _networkState = new ConcurrentDictionary<string, NetworkState>();
100 public void SetNetworkState(string path, NetworkState state)
102 _networkState[path.ToLower()] = state;
103 //Removing may fail so we store the "None" value anyway
104 if (state == NetworkState.None)
106 NetworkState oldState;
107 _networkState.TryRemove(path, out oldState);
111 public NetworkState GetNetworkState(string path)
114 if (_networkState.TryGetValue(path, out state))
116 return NetworkState.None;
120 private PithosStatus _pithosStatus = PithosStatus.InSynch;
121 public void SetPithosStatus(PithosStatus status)
123 _pithosStatus = status;
126 public PithosStatus GetPithosStatus()
128 return _pithosStatus;
131 public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
133 _overlayCache[path] = overlayStatus;
136 public void RemoveFileOverlayStatus(string path)
138 FileOverlayStatus value;
139 _overlayCache.TryRemove(path, out value);
142 public void RenameFileOverlayStatus(string oldPath, string newPath)
144 var status = _overlayCache[oldPath];
145 _overlayCache[newPath] = status;
146 FileOverlayStatus value;
147 _overlayCache.TryRemove(oldPath, out value);
150 public void UpdateFileChecksum(string path, string checksum)
152 _checksums[path] = checksum;
155 public void SetFileStatus(string path, FileStatus status)
157 _statusCache[path] = status;
160 public FileStatus GetFileStatus(string path)
162 if (!_statusCache.ContainsKey(path))
163 return FileStatus.Missing;
164 return _statusCache[path];
167 public void ClearFileStatus(string path)
170 _statusCache.TryRemove(path,out value);