root / trunk / Pithos.Core.Test / MockStatusKeeper.cs @ 5ce54458
History | View | Annotate | Download (6 kB)
1 |
using System; |
---|---|
2 |
using System.Collections.Concurrent; |
3 |
using System.Collections.Generic; |
4 |
using System.Diagnostics.Contracts; |
5 |
using System.Linq; |
6 |
using System.Text; |
7 |
using System.Threading; |
8 |
using Pithos.Interfaces; |
9 |
using Pithos.Network; |
10 |
|
11 |
namespace Pithos.Core.Test |
12 |
{ |
13 |
public class MockStatusChecker : IStatusChecker, IStatusKeeper |
14 |
{ |
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 |
Contract.Requires(!String.IsNullOrWhiteSpace(path)); |
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 |
{ |
45 |
FilePath = file, |
46 |
OverlayStatus = FileOverlayStatus.Unversioned, |
47 |
FileStatus = FileStatus.Created, |
48 |
Checksum = Signature.CalculateMD5(file) |
49 |
}); |
50 |
var 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.ToList(); |
59 |
|
60 |
} |
61 |
|
62 |
public void Stop() |
63 |
{ |
64 |
throw new NotImplementedException(); |
65 |
} |
66 |
|
67 |
public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus) |
68 |
{ |
69 |
if (String.IsNullOrWhiteSpace(path)) |
70 |
throw new ArgumentNullException("path", "path can't be empty"); |
71 |
SetFileStatus(path, fileStatus); |
72 |
SetFileOverlayStatus(path, overlayStatus); |
73 |
} |
74 |
|
75 |
public void StoreInfo(string path, ObjectInfo objectInfo) |
76 |
{ |
77 |
if (String.IsNullOrWhiteSpace(path)) |
78 |
throw new ArgumentNullException("path", "path can't be empty"); |
79 |
if (objectInfo == null) |
80 |
throw new ArgumentNullException("objectInfo", "objectInfo can't be empty"); |
81 |
|
82 |
_statusCache[path] = FileStatus.Unchanged; |
83 |
_overlayCache[path] = FileOverlayStatus.Normal; |
84 |
_checksums[path] = objectInfo.Hash; |
85 |
|
86 |
|
87 |
} |
88 |
|
89 |
public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue) |
90 |
{ |
91 |
throw new NotImplementedException(); |
92 |
} |
93 |
|
94 |
public void SetStatus(string path, Action<FileState> setter) |
95 |
{ |
96 |
throw new NotImplementedException(); |
97 |
} |
98 |
|
99 |
ConcurrentDictionary<string, NetworkOperation> _networkState = new ConcurrentDictionary<string, NetworkOperation>(); |
100 |
|
101 |
|
102 |
public void SetNetworkState(string path, NetworkOperation operation) |
103 |
{ |
104 |
_networkState[path.ToLower()] = operation; |
105 |
//Removing may fail so we store the "None" value anyway |
106 |
if (operation == NetworkOperation.None) |
107 |
{ |
108 |
NetworkOperation oldOperation; |
109 |
_networkState.TryRemove(path, out oldOperation); |
110 |
} |
111 |
} |
112 |
|
113 |
public NetworkOperation GetNetworkState(string path) |
114 |
{ |
115 |
NetworkOperation operation; |
116 |
if (_networkState.TryGetValue(path, out operation)) |
117 |
return operation; |
118 |
return NetworkOperation.None; |
119 |
} |
120 |
|
121 |
public void StartProcessing(CancellationToken token) |
122 |
{ |
123 |
|
124 |
} |
125 |
|
126 |
public string BlockHash { get; set; } |
127 |
|
128 |
public int BlockSize { get; set; } |
129 |
|
130 |
|
131 |
private PithosStatus _pithosStatus = PithosStatus.InSynch; |
132 |
public void SetPithosStatus(PithosStatus status) |
133 |
{ |
134 |
_pithosStatus = status; |
135 |
} |
136 |
|
137 |
public PithosStatus GetPithosStatus() |
138 |
{ |
139 |
return _pithosStatus; |
140 |
} |
141 |
|
142 |
public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus) |
143 |
{ |
144 |
_overlayCache[path] = overlayStatus; |
145 |
} |
146 |
|
147 |
public void RemoveFileOverlayStatus(string path) |
148 |
{ |
149 |
FileOverlayStatus value; |
150 |
_overlayCache.TryRemove(path, out value); |
151 |
} |
152 |
|
153 |
public void RenameFileOverlayStatus(string oldPath, string newPath) |
154 |
{ |
155 |
var status = _overlayCache[oldPath]; |
156 |
_overlayCache[newPath] = status; |
157 |
FileOverlayStatus value; |
158 |
_overlayCache.TryRemove(oldPath, out value); |
159 |
} |
160 |
|
161 |
public void UpdateFileChecksum(string path, string checksum) |
162 |
{ |
163 |
_checksums[path] = checksum; |
164 |
} |
165 |
|
166 |
public void SetFileStatus(string path, FileStatus status) |
167 |
{ |
168 |
_statusCache[path] = status; |
169 |
} |
170 |
|
171 |
public FileStatus GetFileStatus(string path) |
172 |
{ |
173 |
if (!_statusCache.ContainsKey(path)) |
174 |
return FileStatus.Missing; |
175 |
return _statusCache[path]; |
176 |
} |
177 |
|
178 |
public void ClearFileStatus(string path) |
179 |
{ |
180 |
FileStatus value; |
181 |
_statusCache.TryRemove(path,out value); |
182 |
} |
183 |
} |
184 |
} |