Statistics
| Branch: | Revision:

root / trunk / Pithos.Core.Test / MockStatusKeeper.cs @ 5e31048f

History | View | Annotate | Download (6.1 kB)

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

    
135
        private PithosStatus _pithosStatus = PithosStatus.InSynch;
136
        public void SetPithosStatus(PithosStatus status)
137
        {
138
            _pithosStatus = status;
139
        }
140

    
141
        public PithosStatus GetPithosStatus()
142
        {
143
            return _pithosStatus;
144
        }
145

    
146
        public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
147
        {
148
            _overlayCache[path] = overlayStatus;
149
        }
150

    
151
        public void RemoveFileOverlayStatus(string path)
152
        {
153
            FileOverlayStatus value;
154
            _overlayCache.TryRemove(path, out value);
155
        }
156

    
157
        public void RenameFileOverlayStatus(string oldPath, string newPath)
158
        {
159
            var status = _overlayCache[oldPath];
160
            _overlayCache[newPath] = status;
161
            FileOverlayStatus value;
162
            _overlayCache.TryRemove(oldPath, out value);
163
        }
164

    
165
        public void UpdateFileChecksum(string path, string checksum)
166
        {
167
            _checksums[path] = checksum;
168
        }
169

    
170
        public void SetFileStatus(string path, FileStatus status)
171
        {
172
            _statusCache[path] = status;
173
        }
174

    
175
        public FileStatus GetFileStatus(string path)
176
        {
177
            if (!_statusCache.ContainsKey(path))
178
                return FileStatus.Missing;
179
            return _statusCache[path];
180
        }
181

    
182
        public void ClearFileStatus(string path)
183
        {
184
            FileStatus value;
185
            _statusCache.TryRemove(path,out value);
186
        }
187
    }
188
}