2834922f7321494ed9ccd31db4fb9d2439caa8b8
[pithos-ms-client] / trunk%2FPithos.Core.Test%2FMockStatusKeeper.cs
1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Diagnostics.Contracts;
5 using System.IO;
6 using System.Linq;
7 using System.Text;
8 using System.Threading;
9 using Pithos.Interfaces;
10 using Pithos.Network;
11
12 namespace Pithos.Core.Test
13 {
14     public class MockStatusChecker : IStatusChecker, IStatusKeeper
15     {
16         public IPithosSettings Settings { get; set; }
17
18         private readonly string[] _states = { "Normal", "Modified", "Conflict", "Synch" };
19
20         readonly ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
21         readonly ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
22         readonly ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
23
24         public FileOverlayStatus GetFileOverlayStatus(string path)
25         {
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 ProcessExistingFiles(IEnumerable<FileInfo> paths)
40         {
41
42             var newFiles = (from file in paths
43                             where !_overlayCache.ContainsKey(file.FullName)
44                             select new
45                             {
46                                 FilePath = file.FullName.ToLower(),
47                                 OverlayStatus = FileOverlayStatus.Unversioned,
48                                 FileStatus = FileStatus.Created,
49                                 Checksum = Signature.CalculateMD5(file)
50                             });
51             var files = new ConcurrentBag<string>();
52             foreach (var state in newFiles)
53             {            
54                 _overlayCache[state.FilePath] = state.OverlayStatus;
55                 _statusCache[state.FilePath] = state.FileStatus;
56                 _checksums[state.FilePath] = state.Checksum;
57                 files.Add(state.FilePath);
58             }            
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         public void ChangeRoots(string oldPath, string newPath)
130         {
131             throw new NotImplementedException();
132         }
133
134         public FileState GetStateByFilePath(string path)
135         {
136             throw new NotImplementedException();
137         }
138
139         public void ClearFolderStatus(string path)
140         {
141             throw new NotImplementedException();
142         }
143
144         public IEnumerable<FileState> GetChildren(FileState fileState)
145         {
146             throw new NotImplementedException();
147         }
148
149
150         private PithosStatus _pithosStatus = PithosStatus.InSynch;
151         public void SetPithosStatus(PithosStatus status)
152         {
153             _pithosStatus = status;
154         }
155
156         public PithosStatus GetPithosStatus()
157         {
158             return _pithosStatus;
159         }
160
161         public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
162         {
163             _overlayCache[path] = overlayStatus;
164         }
165
166         public void RemoveFileOverlayStatus(string path)
167         {
168             FileOverlayStatus value;
169             _overlayCache.TryRemove(path, out value);
170         }
171
172         public void RenameFileOverlayStatus(string oldPath, string newPath)
173         {
174             var status = _overlayCache[oldPath];
175             _overlayCache[newPath] = status;
176             FileOverlayStatus value;
177             _overlayCache.TryRemove(oldPath, out value);
178         }
179
180         public void UpdateFileChecksum(string path, string checksum)
181         {
182             _checksums[path] = checksum;
183         }
184
185         public void SetFileStatus(string path, FileStatus status)
186         {
187             _statusCache[path] = status;
188         }
189
190         public FileStatus GetFileStatus(string path)
191         {
192             if (!_statusCache.ContainsKey(path))
193                 return FileStatus.Missing;
194             return _statusCache[path];
195         }
196
197         public void ClearFileStatus(string path)
198         {
199             FileStatus value;
200             _statusCache.TryRemove(path,out value);
201         }
202     }
203 }