Added Tag storage
[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 System.Threading;
8 using Pithos.Interfaces;
9
10 namespace Pithos.Core
11 {
12     //[Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))]
13     public class InMemStatusChecker:IStatusChecker,IStatusKeeper
14     {
15         [Import]
16         public IPithosSettings Settings { get; set; }
17
18         private readonly string[] _states = {"Normal", "Modified", "Conflict","Synch"};
19
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>();
23
24         public FileOverlayStatus GetFileOverlayStatus(string path)
25         {
26             if (!_overlayCache.ContainsKey(path))
27                 return FileOverlayStatus.Unversioned;
28
29             var pithosPath = Settings.PithosPath;
30             if (path.StartsWith(pithosPath,true,null))
31             {
32                 var status = _overlayCache[path];
33                 return status;
34             }
35             return FileOverlayStatus.Unversioned;
36         }
37
38         public IEnumerable<string> StoreUnversionedFiles(ParallelQuery<string> paths)
39         {
40
41             var newFiles = (from file in paths
42                             where !_overlayCache.ContainsKey(file)
43                             select new {
44                                            FilePath = file,
45                                            OverlayStatus = FileOverlayStatus.Unversioned,
46                                            FileStatus = FileStatus.Created,
47                                            Checksum = Signature.CalculateHash(file)
48                                        });
49             ConcurrentBag<string> files = new ConcurrentBag<string>();           
50             newFiles.ForAll(state =>
51                                 {
52                                     _overlayCache[state.FilePath] = state.OverlayStatus;
53                                     _statusCache[state.FilePath] = state.FileStatus;
54                                     _checksums[state.FilePath] = state.Checksum;
55                                     files.Add(state.FilePath);
56                                 });
57             return files.GetConsumingEnumerable();
58         }
59
60         public void Stop()
61         {
62             
63         }
64
65
66         private PithosStatus _pithosStatus = PithosStatus.InSynch;
67         public void SetPithosStatus(PithosStatus status)
68         {
69             _pithosStatus = status;
70         }
71
72         public PithosStatus GetPithosStatus()
73         {
74             return _pithosStatus;
75         }
76
77     public void SetStatus(string path, Action<FileState> setter)
78         {
79             throw new NotImplementedException();
80         }
81
82         ConcurrentDictionary<string, NetworkState> _networkState = new ConcurrentDictionary<string, NetworkState>();
83
84     
85         public void SetNetworkState(string path, NetworkState state)
86         {
87             _networkState[path.ToLower()] = state;
88             //Removing may fail so we store the "None" value anyway
89             if (state == NetworkState.None)
90             {
91                 NetworkState oldState;
92                 _networkState.TryRemove(path, out oldState);
93             }
94         }
95
96         public NetworkState GetNetworkState(string path)
97         {
98             NetworkState state;
99             if (_networkState.TryGetValue(path, out state))
100                 return state;
101             return NetworkState.None;
102         }
103
104         public void StartProcessing(CancellationToken token)
105         {
106             
107         }
108
109         public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
110         {
111             _overlayCache[path] = overlayStatus;
112         }
113
114         public void RemoveFileOverlayStatus(string path)
115         {
116             FileOverlayStatus value;
117             _overlayCache.TryRemove(path, out value);
118         }
119
120         public void RenameFileOverlayStatus(string oldPath, string newPath)
121         {
122             var status=_overlayCache[oldPath];
123             _overlayCache[newPath] = status;
124             FileOverlayStatus value;
125             _overlayCache.TryRemove(oldPath,out value);
126         }
127
128         public void SetFileStatus(string path, FileStatus status)
129         {
130             _statusCache[path] = status;
131         }
132
133         public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
134         {
135             if (String.IsNullOrWhiteSpace(path))
136                 throw new ArgumentNullException("path","path can't be empty");
137             _statusCache[path] = fileStatus;
138             _overlayCache[path] = overlayStatus;
139         }
140
141         public void StoreInfo(string path, ObjectInfo objectInfo)
142         {
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");
147
148             _statusCache[path] = FileStatus.Unchanged;
149             _overlayCache[path] = FileOverlayStatus.Normal;
150             _checksums[path] = objectInfo.Hash;
151
152
153         }
154
155         public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
156         {
157             throw new NotImplementedException();
158         }
159
160         public FileStatus GetFileStatus(string path)
161         {
162             if (!_statusCache.ContainsKey(path))
163                 return FileStatus.Missing;
164             return _statusCache[path];
165         }
166
167         public void ClearFileStatus(string path)
168         {
169             FileStatus value;
170             _statusCache.TryRemove(path,out value);
171         }
172
173         public void UpdateFileChecksum(string path, string checksum)
174         {
175             _checksums[path] = checksum;
176         }
177     }
178 }