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