Changed the retry function in PithosClient to use the TPL
[pithos-ms-client] / trunk / Pithos.Core / InMemStatusChecker.cs
index 0a35e81..8bae384 100644 (file)
@@ -1,7 +1,10 @@
 using System;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.ComponentModel.Composition;
 using System.Diagnostics.Contracts;
+using System.Linq;
+using System.Threading;
 using Pithos.Interfaces;
 
 namespace Pithos.Core
@@ -14,14 +17,14 @@ namespace Pithos.Core
 
         private readonly string[] _states = {"Normal", "Modified", "Conflict","Synch"};
 
-        Dictionary<string,FileOverlayStatus> _overlayCache=new Dictionary<string,FileOverlayStatus>();
-        Dictionary<string, FileStatus> _statusCache= new Dictionary<string, FileStatus>();
-        Dictionary<string, string> _checksums = new Dictionary<string, string>();
+        ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
+        ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
+        ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
 
         public FileOverlayStatus GetFileOverlayStatus(string path)
         {
             if (!_overlayCache.ContainsKey(path))
-                return FileOverlayStatus.NA;
+                return FileOverlayStatus.Unversioned;
 
             var pithosPath = Settings.PithosPath;
             if (path.StartsWith(pithosPath,true,null))
@@ -29,12 +32,78 @@ namespace Pithos.Core
                 var status = _overlayCache[path];
                 return status;
             }
-            return FileOverlayStatus.NA;
+            return FileOverlayStatus.Unversioned;
+        }
+
+        public IEnumerable<string> StoreUnversionedFiles(ParallelQuery<string> paths)
+        {
+
+            var newFiles = (from file in paths
+                            where !_overlayCache.ContainsKey(file)
+                            select new {
+                                           FilePath = file,
+                                           OverlayStatus = FileOverlayStatus.Unversioned,
+                                           FileStatus = FileStatus.Created,
+                                           Checksum = Signature.CalculateHash(file)
+                                       });
+            ConcurrentBag<string> files = new ConcurrentBag<string>();           
+            newFiles.ForAll(state =>
+                                {
+                                    _overlayCache[state.FilePath] = state.OverlayStatus;
+                                    _statusCache[state.FilePath] = state.FileStatus;
+                                    _checksums[state.FilePath] = state.Checksum;
+                                    files.Add(state.FilePath);
+                                });
+            return files.GetConsumingEnumerable();
+        }
+
+        public void Stop()
+        {
+            
+        }
+
+
+        private PithosStatus _pithosStatus = PithosStatus.InSynch;
+        public void SetPithosStatus(PithosStatus status)
+        {
+            _pithosStatus = status;
         }
 
         public PithosStatus GetPithosStatus()
         {
-            return PithosStatus.InSynch;
+            return _pithosStatus;
+        }
+
+    public void SetStatus(string path, Action<FileState> setter)
+        {
+            throw new NotImplementedException();
+        }
+
+        ConcurrentDictionary<string, NetworkState> _networkState = new ConcurrentDictionary<string, NetworkState>();
+
+    
+        public void SetNetworkState(string path, NetworkState state)
+        {
+            _networkState[path.ToLower()] = state;
+            //Removing may fail so we store the "None" value anyway
+            if (state == NetworkState.None)
+            {
+                NetworkState oldState;
+                _networkState.TryRemove(path, out oldState);
+            }
+        }
+
+        public NetworkState GetNetworkState(string path)
+        {
+            NetworkState state;
+            if (_networkState.TryGetValue(path, out state))
+                return state;
+            return NetworkState.None;
+        }
+
+        public void StartProcessing(CancellationToken token)
+        {
+            
         }
 
         public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
@@ -44,14 +113,16 @@ namespace Pithos.Core
 
         public void RemoveFileOverlayStatus(string path)
         {
-            _overlayCache.Remove(path);
+            FileOverlayStatus value;
+            _overlayCache.TryRemove(path, out value);
         }
 
         public void RenameFileOverlayStatus(string oldPath, string newPath)
         {
             var status=_overlayCache[oldPath];
-            _overlayCache[newPath] = status;            
-            _overlayCache.Remove(oldPath);
+            _overlayCache[newPath] = status;
+            FileOverlayStatus value;
+            _overlayCache.TryRemove(oldPath,out value);
         }
 
         public void SetFileStatus(string path, FileStatus status)
@@ -59,6 +130,33 @@ namespace Pithos.Core
             _statusCache[path] = status;
         }
 
+        public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
+        {
+            if (String.IsNullOrWhiteSpace(path))
+                throw new ArgumentNullException("path","path can't be empty");
+            _statusCache[path] = fileStatus;
+            _overlayCache[path] = overlayStatus;
+        }
+
+        public void StoreInfo(string path, ObjectInfo objectInfo)
+        {
+            if (String.IsNullOrWhiteSpace(path))
+                throw new ArgumentNullException("path", "path can't be empty");
+            if (objectInfo == null)
+                throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
+
+            _statusCache[path] = FileStatus.Unchanged;
+            _overlayCache[path] = FileOverlayStatus.Normal;
+            _checksums[path] = objectInfo.Hash;
+
+
+        }
+
+        public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
+        {
+            throw new NotImplementedException();
+        }
+
         public FileStatus GetFileStatus(string path)
         {
             if (!_statusCache.ContainsKey(path))
@@ -68,7 +166,8 @@ namespace Pithos.Core
 
         public void ClearFileStatus(string path)
         {
-            _statusCache.Remove(path);
+            FileStatus value;
+            _statusCache.TryRemove(path,out value);
         }
 
         public void UpdateFileChecksum(string path, string checksum)