Changes to NHibernate/Activerecord storage code
authorPanagiotis Kanavos <pkanavos@gmail.com>
Thu, 5 Jan 2012 15:20:27 +0000 (17:20 +0200)
committerPanagiotis Kanavos <pkanavos@gmail.com>
Thu, 5 Jan 2012 15:20:27 +0000 (17:20 +0200)
trunk/Pithos.Core/Agents/StatusAgent.cs
trunk/Pithos.Core/FileState.cs
trunk/Pithos.Core/IStatusKeeper.cs
trunk/Pithos.sln

index 1880df0..1639e76 100644 (file)
@@ -143,7 +143,7 @@ namespace Pithos.Core.Agents
                     //This file was deleted while we were down. We should mark it as deleted
                     //We have to go through UpdateStatus here because the state object we are using
                     //was created by a different ORM session.
-                    UpdateStatus(fileState.Id,state=> state.FileStatus = FileStatus.Deleted);                    
+                    FileState.UpdateStatus(fileState.Id,FileStatus.Deleted);                    
                 }
                 else
                 {
@@ -152,7 +152,7 @@ namespace Pithos.Core.Agents
                     //If the hashes don't match the file was changed
                     if (fileState.Checksum != hashString)
                     {
-                        UpdateStatus(fileState.Id, state => state.FileStatus = FileStatus.Modified);
+                        FileState.UpdateStatus(fileState.Id, FileStatus.Modified);
                     }                    
                 }
             });            
@@ -195,135 +195,7 @@ namespace Pithos.Core.Agents
 
 
         private string _pithosDataPath;
-
-        public T GetStatus<T>(string path,Func<FileState,T> getter,T defaultValue )
-        {
-            if (String.IsNullOrWhiteSpace(path))
-                throw new ArgumentNullException("path");
-            if (!Path.IsPathRooted(path))
-                throw new ArgumentException("path must be a rooted path", "path");
-            if (getter == null)
-                throw new ArgumentNullException("getter");
-            Contract.EndContractBlock();
-
-
-            try
-            {                
-                var state = FileState.FindByFilePath(path);
-                return state == null ? defaultValue : getter(state);
-            }
-            catch (Exception exc)
-            {
-                Log.ErrorFormat(exc.ToString());
-                return defaultValue;
-            }
-        }
-
-        /// <summary>
-        /// Sets the status of a file, creating a new FileState entry if one doesn't already exist.
-        /// </summary>
-        /// <param name="path"></param>
-        /// <param name="setter"></param>
-        public void SetStatus(string path,Action<FileState> setter)
-        {
-            if (String.IsNullOrWhiteSpace(path))
-                throw new ArgumentNullException("path", "path can't be empty");
-            if (setter==null)
-                throw new ArgumentNullException("setter", "setter can't be empty");
-            Contract.EndContractBlock();
-
-            _persistenceAgent.Post(() =>
-            {
-                using (new SessionScope())
-                {
-                    var filePath = path.ToLower();
-                    var state = FileState.FindByFilePath(filePath);
-                    if (state != null)
-                    {
-                        setter(state);
-                        state.Save();
-                    }
-                    else
-                    {
-                        state = new FileState {FilePath = filePath};
-                        setter(state);
-                        state.Save();
-                    }                    
-                }
-            });
-        }
-
-        /// <summary>
-        /// Sets the status of a file only if the file already exists
-        /// </summary>
-        /// <param name="path"></param>
-        /// <param name="setter"></param>
-        private void UpdateStatus(string path, Action<FileState> setter)
-        {
-            if (String.IsNullOrWhiteSpace(path))
-                throw new ArgumentNullException("path");
-            if (!Path.IsPathRooted(path))
-                throw new ArgumentException("The path must be rooted", "path");
-            if (setter == null)
-                throw new ArgumentNullException("setter");
-            Contract.EndContractBlock();
-
-            Debug.Assert(!path.Contains(FolderConstants.CacheFolder));
-            Debug.Assert(!path.EndsWith(".ignore"));
-
-            if (String.IsNullOrWhiteSpace(path))
-                throw new ArgumentNullException("path", "path can't be empty");
-
-            if (setter == null)
-                throw new ArgumentNullException("setter", "setter can't be empty");
-
-            _persistenceAgent.Post(() =>
-            {
-                using (new SessionScope())
-                {
-                    var filePath = path.ToLower();
-
-                    var state = FileState.FindByFilePath(filePath);
-                    if (state == null)
-                    {
-                        Log.WarnFormat("[NOFILE] Unable to set status for {0}.", filePath);
-                        return;
-                    }
-                    setter(state);
-                    state.Save();
-                }
-                
-            });
-        }
-        
-        /// <summary>
-        /// Sets the status of a specific state
-        /// </summary>
-        /// <param name="path"></param>
-        /// <param name="setter"></param>
-        private void UpdateStatus(Guid stateID, Action<FileState> setter)
-        {
-            if (setter == null)
-                throw new ArgumentNullException("setter");
-            Contract.EndContractBlock();
-
-
-            _persistenceAgent.Post(() =>
-            {
-                using (new SessionScope())
-                {
-                    var state = FileState.Find(stateID);
-                    if (state == null)
-                    {
-                        Log.WarnFormat("[NOFILE] Unable to set status for {0}.", stateID);
-                        return;
-                    }
-                    setter(state);
-                    state.Save();
-                }
-                
-            });
-        }
+       
 
         public FileOverlayStatus GetFileOverlayStatus(string path)
         {
@@ -335,8 +207,10 @@ namespace Pithos.Core.Agents
 
             try
             {
-                var state = FileState.FindByFilePath(path);
-                return state == null ? FileOverlayStatus.Unversioned : state.OverlayStatus;
+                var status = from state in  FileState.Queryable 
+                                 where state.FilePath ==path.ToLower()
+                                 select state.OverlayStatus;
+                return status.Any()? status.First():FileOverlayStatus.Unversioned;
             }
             catch (Exception exc)
             {
@@ -353,7 +227,7 @@ namespace Pithos.Core.Agents
                 throw new ArgumentException("The path must be rooted","path");
             Contract.EndContractBlock();
 
-            SetStatus(path.ToLower(),s=>s.OverlayStatus=overlayStatus);
+            _persistenceAgent.Post(() => FileState.StoreOverlayStatus(path.ToLower(),overlayStatus));
         }
 
         /*public void RemoveFileOverlayStatus(string path)
@@ -391,32 +265,7 @@ namespace Pithos.Core.Agents
                 throw new ArgumentException("The newPath must be rooted", "newPath");
             Contract.EndContractBlock();
 
-            _persistenceAgent.Post(() =>
-                InnerRenameFileOverlayStatus(oldPath, newPath));
-        }
-
-        private static void InnerRenameFileOverlayStatus(string oldPath, string newPath)
-        {
-            if (String.IsNullOrWhiteSpace(oldPath))
-                throw new ArgumentNullException("oldPath");
-            if (!Path.IsPathRooted(oldPath))
-                throw new ArgumentException("The oldPath must be rooted", "oldPath");
-            if (String.IsNullOrWhiteSpace(newPath))
-                throw new ArgumentNullException("newPath");
-            if (!Path.IsPathRooted(newPath))
-                throw new ArgumentException("The newPath must be rooted", "newPath");
-            Contract.EndContractBlock();
-
-            var state = FileState.FindByFilePath(oldPath);
-
-            if (state == null)
-            {
-                Log.WarnFormat("[NOFILE] Unable to set status for {0}.", oldPath);
-                return;
-            }
-            //NOTE: This will cause problems if path is used as a key in relationships
-            state.FilePath = newPath;
-            state.Update();
+            _persistenceAgent.Post(() =>FileState.RenameState(oldPath, newPath));
         }
 
         public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
@@ -427,11 +276,10 @@ namespace Pithos.Core.Agents
                 throw new ArgumentException("The path must be rooted", "path");
             Contract.EndContractBlock();
 
-            UpdateStatus(path.ToLower(),state=>
-                                  {
-                                      state.FileStatus = fileStatus;
-                                      state.OverlayStatus = overlayStatus;
-                                  });            
+            Debug.Assert(!path.Contains(FolderConstants.CacheFolder));
+            Debug.Assert(!path.EndsWith(".ignore"));
+
+            _persistenceAgent.Post(() => FileState.UpdateStatus(path.ToLower(), fileStatus, overlayStatus));
         }
 
         public void StoreInfo(string path,ObjectInfo objectInfo)
@@ -453,6 +301,7 @@ namespace Pithos.Core.Agents
                     //Forgetting to use a sessionscope results in two sessions being created, one by 
                     //FirstOrDefault and one by Save()
                     var state =FileState.FindByFilePath(filePath);
+                    
                     //Create a new empty state object if this is a new file
                     state = state ?? new FileState();
 
@@ -485,8 +334,14 @@ namespace Pithos.Core.Agents
 
         
         public void SetFileStatus(string path, FileStatus status)
-        {            
-            UpdateStatus(path.ToLower(), state=>state.FileStatus = status);
+        {
+            if (String.IsNullOrWhiteSpace(path))
+                throw new ArgumentNullException("path");
+            if (!Path.IsPathRooted(path))
+                throw new ArgumentException("The path must be rooted", "path");
+            Contract.EndContractBlock();
+
+            _persistenceAgent.Post(() => FileState.UpdateStatus(path.ToLower(), status));
         }
 
         public FileStatus GetFileStatus(string path)
@@ -497,8 +352,10 @@ namespace Pithos.Core.Agents
                 throw new ArgumentException("The path must be rooted", "path");
             Contract.EndContractBlock();
 
-            var state = FileState.FindByFilePath(path);
-            return (state==null)?FileStatus.Missing:state.FileStatus ;
+            var status = from r in FileState.Queryable
+                     where r.FilePath == path.ToLower()
+                     select r.FileStatus;                        
+            return status.Any()?status.First(): FileStatus.Missing;
         }
 
         public void ClearFileStatus(string path)
@@ -527,20 +384,7 @@ namespace Pithos.Core.Agents
                 throw new ArgumentException("The path must be rooted", "path");            
             Contract.EndContractBlock();
 
-            _persistenceAgent.Post(() =>
-            {
-                using (new SessionScope())
-                {
-                    var state = FileState.FindByFilePath(path);
-                    if (state == null)
-                    {
-                        Log.WarnFormat("[NOFILE] Unable to set checkesum for {0}.", path);
-                        return;
-                    }
-                    state.Checksum = checksum;
-                    state.Update();
-                }
-            });
+            _persistenceAgent.Post(() => FileState.UpdateChecksum(path.ToLower(), checksum));
         }
 
     }
index c1be0fd..54e44a3 100644 (file)
@@ -110,6 +110,140 @@ namespace Pithos.Core
             
         }
 
+        public static void StoreFileStatus(string absolutePath, FileStatus newStatus)
+        {
+            if (string.IsNullOrWhiteSpace(absolutePath))
+                throw new ArgumentNullException("absolutePath");
+            Contract.EndContractBlock();
+
+            Execute((session, instance) =>
+            {
+                const string hqlUpdate = "update FileState set FileStatus= :status where FilePath = :path  ";
+                var updatedEntities = session.CreateQuery(hqlUpdate)
+                        .SetString("path", absolutePath.ToLower())
+                        .SetEnum("status", newStatus)
+                        .ExecuteUpdate();
+                if (updatedEntities == 0)
+                {
+                    var newState = new FileState { FilePath = absolutePath, Id = Guid.NewGuid(), FileStatus = newStatus };
+                    newState.CreateAndFlush();
+                }
+                return null;
+            }, null);
+
+        }
+
+        public static void StoreOverlayStatus(string absolutePath, FileOverlayStatus newStatus)
+        {
+            if (string.IsNullOrWhiteSpace(absolutePath))
+                throw new ArgumentNullException("absolutePath");
+            Contract.EndContractBlock();
+
+            Execute((session, instance) =>
+            {
+                const string hqlUpdate = "update FileState set OverlayStatus= :status where FilePath = :path  ";
+                var updatedEntities = session.CreateQuery(hqlUpdate)
+                        .SetString("path", absolutePath.ToLower())
+                        .SetEnum("status", newStatus)
+                        .ExecuteUpdate();
+                if (updatedEntities == 0)
+                {
+                    var newState = new FileState { FilePath = absolutePath, Id = Guid.NewGuid(), OverlayStatus = newStatus };
+                    newState.CreateAndFlush();
+                }
+                return null;
+            }, null);
+
+        }
+
+        public static void UpdateStatus(string absolutePath, FileStatus fileStatus, FileOverlayStatus overlayStatus)
+        {
+            if (string.IsNullOrWhiteSpace(absolutePath))
+                throw new ArgumentNullException("absolutePath");
+            Contract.EndContractBlock();
+
+            Execute((session, instance) =>
+            {
+                const string hqlUpdate = "update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus where FilePath = :path  ";
+                var updatedEntities = session.CreateQuery(hqlUpdate)
+                        .SetString("path", absolutePath.ToLower())
+                        .SetEnum("fileStatus", fileStatus)
+                        .SetEnum("overlayStatus", overlayStatus)
+                        .ExecuteUpdate();
+                return null;
+            }, null);
+
+        }
+        public static void UpdateStatus(string absolutePath, FileStatus fileStatus)
+        {
+            if (string.IsNullOrWhiteSpace(absolutePath))
+                throw new ArgumentNullException("absolutePath");
+            Contract.EndContractBlock();
+
+            Execute((session, instance) =>
+            {
+                const string hqlUpdate = "update FileState set FileStatus= :fileStatus where FilePath = :path  ";
+                var updatedEntities = session.CreateQuery(hqlUpdate)
+                        .SetString("path", absolutePath.ToLower())
+                        .SetEnum("fileStatus", fileStatus)                        
+                        .ExecuteUpdate();
+                return updatedEntities;
+            }, null);
+
+        }
+
+        public static void RenameState(string oldPath, string newPath)
+        {
+            if (string.IsNullOrWhiteSpace(oldPath))
+                throw new ArgumentNullException("oldPath");
+            Contract.EndContractBlock();
+
+            Execute((session, instance) =>
+            {
+                const string hqlUpdate = "update FileState set FilePath= :newPath where FilePath = :oldPath  ";
+                var updatedEntities = session.CreateQuery(hqlUpdate)
+                        .SetString("oldPath", oldPath.ToLower())
+                        .SetString("newPath", newPath.ToLower())                                          
+                        .ExecuteUpdate();
+                return updatedEntities;
+            }, null);
+
+        }
+
+        public static void UpdateStatus(Guid id, FileStatus fileStatus)
+        {
+            Contract.EndContractBlock();
+
+            Execute((session, instance) =>
+            {
+                const string hqlUpdate = "update FileState set FileStatus= :fileStatus where Id = :id  ";
+                var updatedEntities = session.CreateQuery(hqlUpdate)
+                        .SetGuid("id", id)
+                        .SetEnum("fileStatus", fileStatus)                        
+                        .ExecuteUpdate();
+                return null;
+            }, null);
+
+        }
+
+        public static void UpdateChecksum(string absolutePath, string checksum)
+        {
+            if (string.IsNullOrWhiteSpace(absolutePath))
+                throw new ArgumentNullException("absolutePath");
+            Contract.EndContractBlock();
+
+            Execute((session, instance) =>
+            {
+                const string hqlUpdate = "update FileState set Checksum= :checksum where FilePath = :path  ";
+                var updatedEntities = session.CreateQuery(hqlUpdate)
+                        .SetString("path", absolutePath.ToLower())
+                        .SetString("checksum", checksum)                        
+                        .ExecuteUpdate();
+                return null;
+            }, null);
+
+        }
+
         public static void ChangeRootPath(string oldPath,string newPath)
         {
             if (String.IsNullOrWhiteSpace(oldPath))
index b4f2163..56f3bba 100644 (file)
@@ -22,8 +22,8 @@ namespace Pithos.Core
         void Stop();
         void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus);
         void StoreInfo(string path, ObjectInfo objectInfo);
-        T GetStatus<T>(string path,Func<FileState,T> getter,T defaultValue );
-        void SetStatus(string path, Action<FileState> setter);        
+        //T GetStatus<T>(string path,Func<FileState,T> getter,T defaultValue );
+        //void SetStatus(string path, Action<FileState> setter);        
 
         void StartProcessing(CancellationToken token);
 
index 6474704..d0d8612 100644 (file)
@@ -36,8 +36,6 @@ Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "Pithos.Setup.x64", "Pithos.
 EndProject
 Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "Pithos.Setup.x86", "Pithos.Setup.x86\Pithos.Setup.x86.vdproj", "{0D7E50F2-D7B4-4458-AA01-2CAC0F386737}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkTests", "Tests\NetworkTests\NetworkTests.csproj", "{052D04DA-28FE-471F-96FD-BC1E92BF2A54}"
-EndProject
 Global
        GlobalSection(SolutionConfigurationPlatforms) = preSolution
                Debug All|Any CPU = Debug All|Any CPU
@@ -534,36 +532,6 @@ Global
                {0D7E50F2-D7B4-4458-AA01-2CAC0F386737}.Test|Mixed Platforms.ActiveCfg = Release
                {0D7E50F2-D7B4-4458-AA01-2CAC0F386737}.Test|x64.ActiveCfg = Release
                {0D7E50F2-D7B4-4458-AA01-2CAC0F386737}.Test|x86.ActiveCfg = Release
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug All|Any CPU.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug All|Mixed Platforms.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug All|Mixed Platforms.Build.0 = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug All|x64.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug All|x86.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug All|x86.Build.0 = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug|Any CPU.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug|Mixed Platforms.Build.0 = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug|x64.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug|x86.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Debug|x86.Build.0 = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Premium Debug|Any CPU.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Premium Debug|Mixed Platforms.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Premium Debug|Mixed Platforms.Build.0 = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Premium Debug|x64.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Premium Debug|x86.ActiveCfg = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Premium Debug|x86.Build.0 = Debug|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Release|Any CPU.ActiveCfg = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Release|Mixed Platforms.ActiveCfg = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Release|Mixed Platforms.Build.0 = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Release|x64.ActiveCfg = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Release|x86.ActiveCfg = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Release|x86.Build.0 = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Test|Any CPU.ActiveCfg = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Test|Mixed Platforms.ActiveCfg = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Test|Mixed Platforms.Build.0 = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Test|x64.ActiveCfg = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Test|x86.ActiveCfg = Release|x86
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54}.Test|x86.Build.0 = Release|x86
        EndGlobalSection
        GlobalSection(SolutionProperties) = preSolution
                HideSolutionNode = FALSE
@@ -573,6 +541,5 @@ Global
                {2CFE2DF1-20AE-47E2-B1BB-36B974600BE1} = {B5DD7C4D-D396-4C55-A8D5-DCFE865AA095}
                {E027200B-C26A-4877-BFD9-1A18CF5DF2F4} = {B5DD7C4D-D396-4C55-A8D5-DCFE865AA095}
                {F9AF3E97-BCB7-46B7-8014-7FC858AEE9BA} = {B5DD7C4D-D396-4C55-A8D5-DCFE865AA095}
-               {052D04DA-28FE-471F-96FD-BC1E92BF2A54} = {B5DD7C4D-D396-4C55-A8D5-DCFE865AA095}
        EndGlobalSection
 EndGlobal