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