Replaced object load and update with direct HQL execution to resolve database locks...
[pithos-ms-client] / trunk / Pithos.Core / FileState.cs
index 41393dd..59d5e70 100644 (file)
@@ -9,11 +9,8 @@ using System.IO;
 using System.Threading.Tasks;
 using Castle.ActiveRecord;
 using Castle.ActiveRecord.Framework;
-using Castle.ActiveRecord.Queries;
-using NHibernate.Engine;
 using Pithos.Core.Agents;
 using Pithos.Interfaces;
-using Pithos.Network;
 using log4net;
 
 namespace Pithos.Core
@@ -21,7 +18,6 @@ namespace Pithos.Core
     using System;
     using System.Collections.Generic;
     using System.Linq;
-    using System.Text;
 
     /// <summary>
     /// TODO: Update summary.
@@ -53,10 +49,6 @@ namespace Pithos.Core
         [Property]
         public string Checksum { get; set; }
 
-/*
-        [Property]
-        public string TopHash { get; set; }
-*/
 
         [Property]
         public long? Version { get; set; }
@@ -82,12 +74,7 @@ namespace Pithos.Core
             set { _tags=value;}
         }
 
-//        [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true)]
-//        public IList<FileHash> Hashes { get; set; }
-
-//        [Property]
-//        public byte[] HashmapHash { get; set; }
-       
+   
         public static FileState FindByFilePath(string absolutePath)
         {
             if (string.IsNullOrWhiteSpace(absolutePath))
@@ -112,17 +99,151 @@ namespace Pithos.Core
                 throw new ArgumentNullException("absolutePath");
             Contract.EndContractBlock();
             
-            FileState.Execute((session, instance) =>
+            Execute((session, instance) =>
                              {
                                  const string hqlDelete = "delete FileState where FilePath = :path";                                 
                                  var deletedEntities = session.CreateQuery(hqlDelete)
                                          .SetString("path", absolutePath.ToLower())
                                          .ExecuteUpdate();
-                                 return null;
+                                 return deletedEntities;
                              },null);
             
         }
 
+        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 updatedEntities;
+            }, 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 updatedEntities;
+            }, 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 updatedEntities;
+            }, null);
+
+        }
+
         public static void ChangeRootPath(string oldPath,string newPath)
         {
             if (String.IsNullOrWhiteSpace(oldPath))
@@ -147,11 +268,11 @@ namespace Pithos.Core
                             {
                                 const string hqlUpdate =
                                     "update FileState set FilePath = replace(FilePath,:oldPath,:newPath) where FilePath like :oldPath || '%' ";
-                                var result=session.CreateQuery(hqlUpdate)
+                                var renames=session.CreateQuery(hqlUpdate)
                                     .SetString("oldPath", oldPath.ToLower())
                                     .SetString("newPath", newPath.ToLower())
                                     .ExecuteUpdate();
-                                return null;
+                                return renames;
                             }, null);
             }
         }
@@ -221,23 +342,5 @@ namespace Pithos.Core
         public FileState FileState { get; set; }
 
     }
-    
-  /*  [ActiveRecord("hashes")]
-    public class FileHash : ActiveRecordLinqBase<FileHash>
-    {
-        [PrimaryKey]
-        public int Id { get; set; }
-
-        [Property]
-        public int Order { get; set; }
-
-        [Property]
-        public byte[] Value { get; set; }        
-
-        [BelongsTo("FileStateID")]
-        public FileState FileState { get; set; }
-
-    }*/
-
-
+   
 }