Using MD5 to quickly check for local modifications before calculating the expensive...
[pithos-ms-client] / trunk / Pithos.Core.Test / MockStatusKeeper.cs
index 6f6ba14..f45c14b 100644 (file)
@@ -2,9 +2,13 @@
 using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Diagnostics.Contracts;
+using System.IO;
 using System.Linq;
 using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
 using Pithos.Interfaces;
+using Pithos.Network;
 
 namespace Pithos.Core.Test
 {
@@ -14,13 +18,13 @@ namespace Pithos.Core.Test
 
         private readonly string[] _states = { "Normal", "Modified", "Conflict", "Synch" };
 
-        ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
-        ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
-        ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
+        readonly ConcurrentDictionary<string, FileOverlayStatus> _overlayCache = new ConcurrentDictionary<string, FileOverlayStatus>();
+        readonly ConcurrentDictionary<string, FileStatus> _statusCache = new ConcurrentDictionary<string, FileStatus>();
+        readonly ConcurrentDictionary<string, string> _checksums = new ConcurrentDictionary<string, string>();
 
         public FileOverlayStatus GetFileOverlayStatus(string path)
         {
-            Contract.Requires(!String.IsNullOrWhiteSpace(path));
+            
             if (!_overlayCache.ContainsKey(path))
                 return FileOverlayStatus.Unversioned;
 
@@ -33,30 +37,121 @@ namespace Pithos.Core.Test
             return FileOverlayStatus.Unversioned;
         }
 
-        public IEnumerable<string> StoreUnversionedFiles(ParallelQuery<string> paths)
+        public void ProcessExistingFiles(IEnumerable<FileInfo> paths)
         {
 
             var newFiles = (from file in paths
-                            where !_overlayCache.ContainsKey(file)
+                            where !_overlayCache.ContainsKey(file.FullName)
                             select new
                             {
-                                FilePath = file,
+                                FilePath = file.FullName.ToLower(),
                                 OverlayStatus = FileOverlayStatus.Unversioned,
                                 FileStatus = FileStatus.Created,
-                                Checksum = Signature.CalculateHash(file)
+                                Checksum = Signature.CalculateMD5(file)
                             });
             var files = new ConcurrentBag<string>();
-            newFiles.ForAll(state =>
-            {
+            foreach (var state in newFiles)
+            {            
                 _overlayCache[state.FilePath] = state.OverlayStatus;
                 _statusCache[state.FilePath] = state.FileStatus;
                 _checksums[state.FilePath] = state.Checksum;
                 files.Add(state.FilePath);
-            });
-            return files.ToList();
+            }            
+            
+        }
+
+        public void Stop()
+        {
+            throw new NotImplementedException();
+        }
+
+        public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus, string localFileMissingFromServer)
+        {
+            if (String.IsNullOrWhiteSpace(path))
+                throw new ArgumentNullException("path", "path can't be empty");
+            SetFileStatus(path, fileStatus);
+            SetFileOverlayStatus(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.X_Object_Hash;
+
+
+        }
+
+        public T GetStatus<T>(string path, Func<FileState, T> getter, T defaultValue)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void SetStatus(string path, Action<FileState> setter)
+        {
+            throw new NotImplementedException();
+        }
+
+        ConcurrentDictionary<string, NetworkOperation> _networkState = new ConcurrentDictionary<string, NetworkOperation>();
+
+
+        public void SetNetworkState(string path, NetworkOperation operation)
+        {
+            _networkState[path.ToLower()] = operation;
+            //Removing may fail so we store the "None" value anyway
+            if (operation == NetworkOperation.None)
+            {
+                NetworkOperation oldOperation;
+                _networkState.TryRemove(path, out oldOperation);
+            }
+        }
+
+        public NetworkOperation GetNetworkState(string path)
+        {
+            NetworkOperation operation;
+            if (_networkState.TryGetValue(path, out operation))
+                return operation;
+            return NetworkOperation.None;
+        }
+
+        public void StartProcessing(CancellationToken token)
+        {
             
         }
 
+        public string BlockHash { get; set; }
+
+        public int BlockSize { get; set; }
+        public void ChangeRoots(string oldPath, string newPath)
+        {
+            throw new NotImplementedException();
+        }
+
+        public FileState GetStateByFilePath(string path)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void ClearFolderStatus(string path)
+        {
+            throw new NotImplementedException();
+        }
+
+        public IEnumerable<FileState> GetChildren(FileState fileState)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void EnsureFileState(string path)
+        {
+            throw new NotImplementedException();
+        }
+
 
         private PithosStatus _pithosStatus = PithosStatus.InSynch;
         public void SetPithosStatus(PithosStatus status)
@@ -69,9 +164,10 @@ namespace Pithos.Core.Test
             return _pithosStatus;
         }
 
-        public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
+        public Task SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus, string shortHash = null)
         {
             _overlayCache[path] = overlayStatus;
+            return Task.Factory.StartNew(()=>{});
         }
 
         public void RemoveFileOverlayStatus(string path)
@@ -88,7 +184,7 @@ namespace Pithos.Core.Test
             _overlayCache.TryRemove(oldPath, out value);
         }
 
-        public void UpdateFileChecksum(string path, string checksum)
+        public void UpdateFileChecksum(string path, string shortHash, string checksum)
         {
             _checksums[path] = checksum;
         }
@@ -110,5 +206,17 @@ namespace Pithos.Core.Test
             FileStatus value;
             _statusCache.TryRemove(path,out value);
         }
+
+
+        public void CleanupStaleStates(AccountInfo accountInfo, List<ObjectInfo> objectInfos)
+        {
+            //throw new NotImplementedException();
+        }
+
+
+        public void CleanupOrphanStates()
+        {
+            throw new NotImplementedException();
+        }
     }
 }