New SQLite version
[pithos-ms-client] / trunk / Pithos.Core.Test / MockStatusKeeper.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 System.Threading.Tasks;
10 using Pithos.Interfaces;
11 using Pithos.Network;
12
13 namespace Pithos.Core.Test
14 {
15     public class MockStatusChecker : IStatusChecker, IStatusKeeper
16     {
17         public IPithosSettings Settings { get; set; }
18
19         private readonly string[] _states = { "Normal", "Modified", "Conflict", "Synch" };
20
21         readonly ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
22         readonly ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
23         readonly ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
24
25         public FileOverlayStatus GetFileOverlayStatus(string path)
26         {
27             
28             if (!_overlayCache.ContainsKey(path))
29                 return FileOverlayStatus.Unversioned;
30
31             var pithosPath = Settings.PithosPath;
32             if (path.StartsWith(pithosPath, true, null))
33             {
34                 var status = _overlayCache[path];
35                 return status;
36             }
37             return FileOverlayStatus.Unversioned;
38         }
39
40         public void ProcessExistingFiles(IEnumerable<FileInfo> paths)
41         {
42
43             var newFiles = (from file in paths
44                             where !_overlayCache.ContainsKey(file.FullName)
45                             select new
46                             {
47                                 FilePath = file.FullName.ToLower(),
48                                 OverlayStatus = FileOverlayStatus.Unversioned,
49                                 FileStatus = FileStatus.Created,
50                                 Checksum = Signature.CalculateMD5(file)
51                             });
52             var files = new ConcurrentBag<string>();
53             foreach (var state in newFiles)
54             {            
55                 _overlayCache[state.FilePath] = state.OverlayStatus;
56                 _statusCache[state.FilePath] = state.FileStatus;
57                 _checksums[state.FilePath] = state.Checksum;
58                 files.Add(state.FilePath);
59             }            
60             
61         }
62
63         public void Stop()
64         {
65             throw new NotImplementedException();
66         }
67
68         public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus, string localFileMissingFromServer)
69         {
70             if (String.IsNullOrWhiteSpace(path))
71                 throw new ArgumentNullException("path", "path can't be empty");
72             SetFileStatus(path, fileStatus);
73             SetFileOverlayStatus(path, overlayStatus);
74         }
75
76         public void StoreInfo(string path, ObjectInfo objectInfo)
77         {
78             if (String.IsNullOrWhiteSpace(path))
79                 throw new ArgumentNullException("path", "path can't be empty");
80             if (objectInfo == null)
81                 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
82
83             _statusCache[path] = FileStatus.Unchanged;
84             _overlayCache[path] = FileOverlayStatus.Normal;
85             _checksums[path] = objectInfo.Hash;
86
87
88         }
89
90         public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
91         {
92             throw new NotImplementedException();
93         }
94
95         public void SetStatus(string path, Action<FileState> setter)
96         {
97             throw new NotImplementedException();
98         }
99
100         ConcurrentDictionary<string, NetworkOperation> _networkState = new ConcurrentDictionary<string, NetworkOperation>();
101
102
103         public void SetNetworkState(string path, NetworkOperation operation)
104         {
105             _networkState[path.ToLower()] = operation;
106             //Removing may fail so we store the "None" value anyway
107             if (operation == NetworkOperation.None)
108             {
109                 NetworkOperation oldOperation;
110                 _networkState.TryRemove(path, out oldOperation);
111             }
112         }
113
114         public NetworkOperation GetNetworkState(string path)
115         {
116             NetworkOperation operation;
117             if (_networkState.TryGetValue(path, out operation))
118                 return operation;
119             return NetworkOperation.None;
120         }
121
122         public void StartProcessing(CancellationToken token)
123         {
124             
125         }
126
127         public string BlockHash { get; set; }
128
129         public int BlockSize { get; set; }
130         public void ChangeRoots(string oldPath, string newPath)
131         {
132             throw new NotImplementedException();
133         }
134
135         public FileState GetStateByFilePath(string path)
136         {
137             throw new NotImplementedException();
138         }
139
140         public void ClearFolderStatus(string path)
141         {
142             throw new NotImplementedException();
143         }
144
145         public IEnumerable<FileState> GetChildren(FileState fileState)
146         {
147             throw new NotImplementedException();
148         }
149
150         public void EnsureFileState(string path)
151         {
152             throw new NotImplementedException();
153         }
154
155
156         private PithosStatus _pithosStatus = PithosStatus.InSynch;
157         public void SetPithosStatus(PithosStatus status)
158         {
159             _pithosStatus = status;
160         }
161
162         public PithosStatus GetPithosStatus()
163         {
164             return _pithosStatus;
165         }
166
167         public Task SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus, string shortHash = null)
168         {
169             _overlayCache[path] = overlayStatus;
170             return Task.Factory.StartNew(()=>{});
171         }
172
173         public void RemoveFileOverlayStatus(string path)
174         {
175             FileOverlayStatus value;
176             _overlayCache.TryRemove(path, out value);
177         }
178
179         public void RenameFileOverlayStatus(string oldPath, string newPath)
180         {
181             var status = _overlayCache[oldPath];
182             _overlayCache[newPath] = status;
183             FileOverlayStatus value;
184             _overlayCache.TryRemove(oldPath, out value);
185         }
186
187         public void UpdateFileChecksum(string path, string shortHash, string checksum)
188         {
189             _checksums[path] = checksum;
190         }
191
192         public void SetFileStatus(string path, FileStatus status)
193         {
194             _statusCache[path] = status;
195         }
196
197         public FileStatus GetFileStatus(string path)
198         {
199             if (!_statusCache.ContainsKey(path))
200                 return FileStatus.Missing;
201             return _statusCache[path];
202         }
203
204         public void ClearFileStatus(string path)
205         {
206             FileStatus value;
207             _statusCache.TryRemove(path,out value);
208         }
209
210
211         public void CleanupStaleStates(AccountInfo accountInfo, List<ObjectInfo> objectInfos)
212         {
213             //throw new NotImplementedException();
214         }
215
216
217         public void CleanupOrphanStates()
218         {
219             throw new NotImplementedException();
220         }
221     }
222 }