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;
13 //[Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))]
14 public class InMemStatusChecker:IStatusChecker,IStatusKeeper
17 public IPithosSettings Settings { get; set; }
19 private readonly string[] _states = {"Normal", "Modified", "Conflict","Synch"};
21 ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
22 ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
23 ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
25 public FileOverlayStatus GetFileOverlayStatus(string 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 StoreUnversionedFiles(IEnumerable<FileInfo> paths)
42 var newFiles = (from file in paths
43 where !_overlayCache.ContainsKey(file)
46 OverlayStatus = FileOverlayStatus.Unversioned,
47 FileStatus = FileStatus.Created,
48 Checksum = Signature.CalculateMD5(file)
50 ConcurrentBag<string> files = new ConcurrentBag<string>();
51 newFiles.ForAll(state =>
53 _overlayCache[state.FilePath] = state.OverlayStatus;
54 _statusCache[state.FilePath] = state.FileStatus;
55 _checksums[state.FilePath] = state.Checksum;
56 files.Add(state.FilePath);
58 return files.GetConsumingEnumerable();
67 private PithosStatus _pithosStatus = PithosStatus.InSynch;
68 public void SetPithosStatus(PithosStatus status)
70 _pithosStatus = status;
73 public PithosStatus GetPithosStatus()
78 public void SetStatus(string path, Action<FileState> setter)
80 throw new NotImplementedException();
83 ConcurrentDictionary<string, NetworkOperation> _networkState = new ConcurrentDictionary<string, NetworkOperation>();
86 public void SetNetworkState(string path, NetworkOperation operation)
88 _networkState[path.ToLower()] = operation;
89 //Removing may fail so we store the "None" value anyway
90 if (operation == NetworkOperation.None)
92 NetworkOperation oldOperation;
93 _networkState.TryRemove(path, out oldOperation);
97 public NetworkOperation GetNetworkState(string path)
99 NetworkOperation operation;
100 if (_networkState.TryGetValue(path, out operation))
102 return NetworkOperation.None;
105 public void StartProcessing(CancellationToken token)
110 public string BlockHash { get; set; }
112 public int BlockSize { get; set; }
115 public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
117 _overlayCache[path] = overlayStatus;
120 public void RemoveFileOverlayStatus(string path)
122 FileOverlayStatus value;
123 _overlayCache.TryRemove(path, out value);
126 public void RenameFileOverlayStatus(string oldPath, string newPath)
128 var status=_overlayCache[oldPath];
129 _overlayCache[newPath] = status;
130 FileOverlayStatus value;
131 _overlayCache.TryRemove(oldPath,out value);
134 public void SetFileStatus(string path, FileStatus status)
136 _statusCache[path] = status;
139 public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
141 if (String.IsNullOrWhiteSpace(path))
142 throw new ArgumentNullException("path","path can't be empty");
143 _statusCache[path] = fileStatus;
144 _overlayCache[path] = overlayStatus;
147 public void StoreInfo(string path, ObjectInfo objectInfo)
149 if (String.IsNullOrWhiteSpace(path))
150 throw new ArgumentNullException("path", "path can't be empty");
151 if (objectInfo == null)
152 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
154 _statusCache[path] = FileStatus.Unchanged;
155 _overlayCache[path] = FileOverlayStatus.Normal;
156 _checksums[path] = objectInfo.Hash;
161 public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
163 throw new NotImplementedException();
166 public FileStatus GetFileStatus(string path)
168 if (!_statusCache.ContainsKey(path))
169 return FileStatus.Missing;
170 return _statusCache[path];
173 public void ClearFileStatus(string path)
176 _statusCache.TryRemove(path,out value);
179 public void UpdateFileChecksum(string path, string checksum)
181 _checksums[path] = checksum;