2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.ComponentModel.Composition;
5 using System.Diagnostics.Contracts;
7 using Pithos.Interfaces;
11 //[Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))]
12 public class InMemStatusChecker:IStatusChecker,IStatusKeeper
15 public IPithosSettings Settings { get; set; }
17 private readonly string[] _states = {"Normal", "Modified", "Conflict","Synch"};
19 ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
20 ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
21 ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
23 public FileOverlayStatus GetFileOverlayStatus(string path)
25 if (!_overlayCache.ContainsKey(path))
26 return FileOverlayStatus.Unversioned;
28 var pithosPath = Settings.PithosPath;
29 if (path.StartsWith(pithosPath,true,null))
31 var status = _overlayCache[path];
34 return FileOverlayStatus.Unversioned;
37 public IEnumerable<string> StoreUnversionedFiles(ParallelQuery<string> paths)
40 var newFiles = (from file in paths
41 where !_overlayCache.ContainsKey(file)
44 OverlayStatus = FileOverlayStatus.Unversioned,
45 FileStatus = FileStatus.Created,
46 Checksum = Signature.CalculateHash(file)
48 ConcurrentBag<string> 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.GetConsumingEnumerable();
65 private PithosStatus _pithosStatus = PithosStatus.InSynch;
66 public void SetPithosStatus(PithosStatus status)
68 _pithosStatus = status;
71 public PithosStatus GetPithosStatus()
76 public void SetStatus(string path, Action<FileState> setter)
78 throw new NotImplementedException();
81 ConcurrentDictionary<string, NetworkState> _networkState = new ConcurrentDictionary<string, NetworkState>();
84 public void SetNetworkState(string path, NetworkState state)
86 _networkState[path.ToLower()] = state;
87 //Removing may fail so we store the "None" value anyway
88 if (state == NetworkState.None)
90 NetworkState oldState;
91 _networkState.TryRemove(path, out oldState);
95 public NetworkState GetNetworkState(string path)
98 if (_networkState.TryGetValue(path, out state))
100 return NetworkState.None;
103 public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
105 _overlayCache[path] = overlayStatus;
108 public void RemoveFileOverlayStatus(string path)
110 FileOverlayStatus value;
111 _overlayCache.TryRemove(path, out value);
114 public void RenameFileOverlayStatus(string oldPath, string newPath)
116 var status=_overlayCache[oldPath];
117 _overlayCache[newPath] = status;
118 FileOverlayStatus value;
119 _overlayCache.TryRemove(oldPath,out value);
122 public void SetFileStatus(string path, FileStatus status)
124 _statusCache[path] = status;
127 public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
129 if (String.IsNullOrWhiteSpace(path))
130 throw new ArgumentNullException("path","path can't be empty");
131 _statusCache[path] = fileStatus;
132 _overlayCache[path] = overlayStatus;
135 public void StoreInfo(string path, ObjectInfo objectInfo)
137 if (String.IsNullOrWhiteSpace(path))
138 throw new ArgumentNullException("path", "path can't be empty");
139 if (objectInfo == null)
140 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
142 _statusCache[path] = FileStatus.Unchanged;
143 _overlayCache[path] = FileOverlayStatus.Normal;
144 _checksums[path] = objectInfo.Hash;
149 public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
151 throw new NotImplementedException();
154 public FileStatus GetFileStatus(string path)
156 if (!_statusCache.ContainsKey(path))
157 return FileStatus.Missing;
158 return _statusCache[path];
161 public void ClearFileStatus(string path)
164 _statusCache.TryRemove(path,out value);
167 public void UpdateFileChecksum(string path, string checksum)
169 _checksums[path] = checksum;