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();
60 private PithosStatus _pithosStatus = PithosStatus.InSynch;
61 public void SetPithosStatus(PithosStatus status)
63 _pithosStatus = status;
66 public PithosStatus GetPithosStatus()
71 public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
73 _overlayCache[path] = overlayStatus;
76 public void RemoveFileOverlayStatus(string path)
78 FileOverlayStatus value;
79 _overlayCache.TryRemove(path, out value);
82 public void RenameFileOverlayStatus(string oldPath, string newPath)
84 var status=_overlayCache[oldPath];
85 _overlayCache[newPath] = status;
86 FileOverlayStatus value;
87 _overlayCache.TryRemove(oldPath,out value);
90 public void SetFileStatus(string path, FileStatus status)
92 _statusCache[path] = status;
95 public FileStatus GetFileStatus(string path)
97 if (!_statusCache.ContainsKey(path))
98 return FileStatus.Missing;
99 return _statusCache[path];
102 public void ClearFileStatus(string path)
105 _statusCache.TryRemove(path,out value);
108 public void UpdateFileChecksum(string path, string checksum)
110 _checksums[path] = checksum;