root / trunk / Pithos.Core.Test / MockStatusKeeper.cs @ 283809f3
History | View | Annotate | Download (5.7 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 Pithos.Interfaces; |
8 |
|
9 |
namespace Pithos.Core.Test |
10 |
{ |
11 |
public class MockStatusChecker : IStatusChecker, IStatusKeeper |
12 |
{ |
13 |
public IPithosSettings Settings { get; set; } |
14 |
|
15 |
private readonly string[] _states = { "Normal", "Modified", "Conflict", "Synch" }; |
16 |
|
17 |
ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>(); |
18 |
ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>(); |
19 |
ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>(); |
20 |
|
21 |
public FileOverlayStatus GetFileOverlayStatus(string path) |
22 |
{ |
23 |
Contract.Requires(!String.IsNullOrWhiteSpace(path)); |
24 |
if (!_overlayCache.ContainsKey(path)) |
25 |
return FileOverlayStatus.Unversioned; |
26 |
|
27 |
var pithosPath = Settings.PithosPath; |
28 |
if (path.StartsWith(pithosPath, true, null)) |
29 |
{ |
30 |
var status = _overlayCache[path]; |
31 |
return status; |
32 |
} |
33 |
return FileOverlayStatus.Unversioned; |
34 |
} |
35 |
|
36 |
public IEnumerable<string> StoreUnversionedFiles(ParallelQuery<string> paths) |
37 |
{ |
38 |
|
39 |
var newFiles = (from file in paths |
40 |
where !_overlayCache.ContainsKey(file) |
41 |
select new |
42 |
{ |
43 |
FilePath = file, |
44 |
OverlayStatus = FileOverlayStatus.Unversioned, |
45 |
FileStatus = FileStatus.Created, |
46 |
Checksum = Signature.CalculateHash(file) |
47 |
}); |
48 |
var 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.ToList(); |
57 |
|
58 |
} |
59 |
|
60 |
public void Stop() |
61 |
{ |
62 |
throw new NotImplementedException(); |
63 |
} |
64 |
|
65 |
public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus) |
66 |
{ |
67 |
if (String.IsNullOrWhiteSpace(path)) |
68 |
throw new ArgumentNullException("path", "path can't be empty"); |
69 |
SetFileStatus(path, fileStatus); |
70 |
SetFileOverlayStatus(path, overlayStatus); |
71 |
} |
72 |
|
73 |
public void StoreInfo(string path, ObjectInfo objectInfo) |
74 |
{ |
75 |
if (String.IsNullOrWhiteSpace(path)) |
76 |
throw new ArgumentNullException("path", "path can't be empty"); |
77 |
if (objectInfo == null) |
78 |
throw new ArgumentNullException("objectInfo", "objectInfo can't be empty"); |
79 |
|
80 |
_statusCache[path] = FileStatus.Unchanged; |
81 |
_overlayCache[path] = FileOverlayStatus.Normal; |
82 |
_checksums[path] = objectInfo.Hash; |
83 |
|
84 |
|
85 |
} |
86 |
|
87 |
public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue) |
88 |
{ |
89 |
throw new NotImplementedException(); |
90 |
} |
91 |
|
92 |
public void SetStatus(string path, Action<FileState> setter) |
93 |
{ |
94 |
throw new NotImplementedException(); |
95 |
} |
96 |
|
97 |
ConcurrentDictionary<string, NetworkState> _networkState = new ConcurrentDictionary<string, NetworkState>(); |
98 |
|
99 |
|
100 |
public void SetNetworkState(string path, NetworkState state) |
101 |
{ |
102 |
_networkState[path.ToLower()] = state; |
103 |
//Removing may fail so we store the "None" value anyway |
104 |
if (state == NetworkState.None) |
105 |
{ |
106 |
NetworkState oldState; |
107 |
_networkState.TryRemove(path, out oldState); |
108 |
} |
109 |
} |
110 |
|
111 |
public NetworkState GetNetworkState(string path) |
112 |
{ |
113 |
NetworkState state; |
114 |
if (_networkState.TryGetValue(path, out state)) |
115 |
return state; |
116 |
return NetworkState.None; |
117 |
} |
118 |
|
119 |
|
120 |
private PithosStatus _pithosStatus = PithosStatus.InSynch; |
121 |
public void SetPithosStatus(PithosStatus status) |
122 |
{ |
123 |
_pithosStatus = status; |
124 |
} |
125 |
|
126 |
public PithosStatus GetPithosStatus() |
127 |
{ |
128 |
return _pithosStatus; |
129 |
} |
130 |
|
131 |
public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus) |
132 |
{ |
133 |
_overlayCache[path] = overlayStatus; |
134 |
} |
135 |
|
136 |
public void RemoveFileOverlayStatus(string path) |
137 |
{ |
138 |
FileOverlayStatus value; |
139 |
_overlayCache.TryRemove(path, out value); |
140 |
} |
141 |
|
142 |
public void RenameFileOverlayStatus(string oldPath, string newPath) |
143 |
{ |
144 |
var status = _overlayCache[oldPath]; |
145 |
_overlayCache[newPath] = status; |
146 |
FileOverlayStatus value; |
147 |
_overlayCache.TryRemove(oldPath, out value); |
148 |
} |
149 |
|
150 |
public void UpdateFileChecksum(string path, string checksum) |
151 |
{ |
152 |
_checksums[path] = checksum; |
153 |
} |
154 |
|
155 |
public void SetFileStatus(string path, FileStatus status) |
156 |
{ |
157 |
_statusCache[path] = status; |
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 |
} |