2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.ComponentModel.Composition;
5 using System.Diagnostics.Contracts;
7 using System.Threading;
8 using Pithos.Interfaces;
12 //[Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))]
13 public class InMemStatusChecker: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 if (!_overlayCache.ContainsKey(path))
27 return FileOverlayStatus.Unversioned;
29 var pithosPath = Settings.PithosPath;
30 if (path.StartsWith(pithosPath,true,null))
32 var status = _overlayCache[path];
35 return FileOverlayStatus.Unversioned;
38 public IEnumerable<string> StoreUnversionedFiles(ParallelQuery<string> paths)
41 var newFiles = (from file in paths
42 where !_overlayCache.ContainsKey(file)
45 OverlayStatus = FileOverlayStatus.Unversioned,
46 FileStatus = FileStatus.Created,
47 Checksum = Signature.CalculateHash(file)
49 ConcurrentBag<string> 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.GetConsumingEnumerable();
66 private PithosStatus _pithosStatus = PithosStatus.InSynch;
67 public void SetPithosStatus(PithosStatus status)
69 _pithosStatus = status;
72 public PithosStatus GetPithosStatus()
77 public void SetStatus(string path, Action<FileState> setter)
79 throw new NotImplementedException();
82 ConcurrentDictionary<string, NetworkState> _networkState = new ConcurrentDictionary<string, NetworkState>();
85 public void SetNetworkState(string path, NetworkState state)
87 _networkState[path.ToLower()] = state;
88 //Removing may fail so we store the "None" value anyway
89 if (state == NetworkState.None)
91 NetworkState oldState;
92 _networkState.TryRemove(path, out oldState);
96 public NetworkState GetNetworkState(string path)
99 if (_networkState.TryGetValue(path, out state))
101 return NetworkState.None;
104 public void StartProcessing(CancellationToken token)
109 public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
111 _overlayCache[path] = overlayStatus;
114 public void RemoveFileOverlayStatus(string path)
116 FileOverlayStatus value;
117 _overlayCache.TryRemove(path, out value);
120 public void RenameFileOverlayStatus(string oldPath, string newPath)
122 var status=_overlayCache[oldPath];
123 _overlayCache[newPath] = status;
124 FileOverlayStatus value;
125 _overlayCache.TryRemove(oldPath,out value);
128 public void SetFileStatus(string path, FileStatus status)
130 _statusCache[path] = status;
133 public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
135 if (String.IsNullOrWhiteSpace(path))
136 throw new ArgumentNullException("path","path can't be empty");
137 _statusCache[path] = fileStatus;
138 _overlayCache[path] = overlayStatus;
141 public void StoreInfo(string path, ObjectInfo objectInfo)
143 if (String.IsNullOrWhiteSpace(path))
144 throw new ArgumentNullException("path", "path can't be empty");
145 if (objectInfo == null)
146 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
148 _statusCache[path] = FileStatus.Unchanged;
149 _overlayCache[path] = FileOverlayStatus.Normal;
150 _checksums[path] = objectInfo.Hash;
155 public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
157 throw new NotImplementedException();
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);
173 public void UpdateFileChecksum(string path, string checksum)
175 _checksums[path] = checksum;