78593e1d5b44a16cc0a5edbb20460febbc8fc441
[pithos-ms-client] / trunk / Pithos.Core / InMemStatusChecker.cs
1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.ComponentModel.Composition;
5 using System.Diagnostics.Contracts;
6 using System.Linq;
7 using Pithos.Interfaces;
8
9 namespace Pithos.Core
10 {
11     //[Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))]
12     public class InMemStatusChecker:IStatusChecker,IStatusKeeper
13     {
14         [Import]
15         public IPithosSettings Settings { get; set; }
16
17         private readonly string[] _states = {"Normal", "Modified", "Conflict","Synch"};
18
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>();
22
23         public FileOverlayStatus GetFileOverlayStatus(string path)
24         {
25             if (!_overlayCache.ContainsKey(path))
26                 return FileOverlayStatus.Unversioned;
27
28             var pithosPath = Settings.PithosPath;
29             if (path.StartsWith(pithosPath,true,null))
30             {
31                 var status = _overlayCache[path];
32                 return status;
33             }
34             return FileOverlayStatus.Unversioned;
35         }
36
37         public IEnumerable<string> StoreUnversionedFiles(ParallelQuery<string> paths)
38         {
39
40             var newFiles = (from file in paths
41                             where !_overlayCache.ContainsKey(file)
42                             select new {
43                                            FilePath = file,
44                                            OverlayStatus = FileOverlayStatus.Unversioned,
45                                            FileStatus = FileStatus.Created,
46                                            Checksum = Signature.CalculateHash(file)
47                                        });
48             ConcurrentBag<string> files = new ConcurrentBag<string>();           
49             newFiles.ForAll(state =>
50                                 {
51                                     _overlayCache[state.FilePath] = state.OverlayStatus;
52                                     _statusCache[state.FilePath] = state.FileStatus;
53                                     _checksums[state.FilePath] = state.Checksum;
54                                     files.Add(state.FilePath);
55                                 });
56             return files.GetConsumingEnumerable();
57         }
58
59
60         private PithosStatus _pithosStatus = PithosStatus.InSynch;
61         public void SetPithosStatus(PithosStatus status)
62         {
63             _pithosStatus = status;
64         }
65
66         public PithosStatus GetPithosStatus()
67         {
68             return _pithosStatus;
69         }
70
71         public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
72         {
73             _overlayCache[path] = overlayStatus;
74         }
75
76         public void RemoveFileOverlayStatus(string path)
77         {
78             FileOverlayStatus value;
79             _overlayCache.TryRemove(path, out value);
80         }
81
82         public void RenameFileOverlayStatus(string oldPath, string newPath)
83         {
84             var status=_overlayCache[oldPath];
85             _overlayCache[newPath] = status;
86             FileOverlayStatus value;
87             _overlayCache.TryRemove(oldPath,out value);
88         }
89
90         public void SetFileStatus(string path, FileStatus status)
91         {
92             _statusCache[path] = status;
93         }
94
95         public FileStatus GetFileStatus(string path)
96         {
97             if (!_statusCache.ContainsKey(path))
98                 return FileStatus.Missing;
99             return _statusCache[path];
100         }
101
102         public void ClearFileStatus(string path)
103         {
104             FileStatus value;
105             _statusCache.TryRemove(path,out value);
106         }
107
108         public void UpdateFileChecksum(string path, string checksum)
109         {
110             _checksums[path] = checksum;
111         }
112     }
113 }