Modified loggers to use their enclosing class
[pithos-ms-client] / trunk / Pithos.Core / FileState.cs
index 4a764c5..5fbacbd 100644 (file)
@@ -41,6 +41,7 @@
 #endregion
 using System.Diagnostics.Contracts;
 using System.IO;
+using System.Reflection;
 using System.Threading.Tasks;
 using Castle.ActiveRecord;
 using Castle.ActiveRecord.Framework;
@@ -54,8 +55,7 @@ using log4net;
 namespace Pithos.Core
 {
     using System;
-    using System.Collections.Generic;
-    using System.Linq;
+    using System.Collections.Generic;    
 
     /// <summary>
     /// TODO: Update summary.
@@ -63,7 +63,8 @@ namespace Pithos.Core
     [ActiveRecord]
     public class FileState : ActiveRecordLinqBase<FileState>
     {
-        private static readonly ILog Log = LogManager.GetLogger("FileState");
+        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
+
 
         private IList<FileTag> _tags = new List<FileTag>();
 
@@ -420,25 +421,47 @@ namespace Pithos.Core
                 }
         }
 
-        public static void RemovePaths(IEnumerable<string> removed)
+        /// <summary>
+        /// Mark Unversioned all FileState rows from the database whose path
+        /// starts with one of the removed paths
+        /// </summary>
+        /// <param name="removed"></param>
+        public static void UnversionPaths(List<string> removed)
         {
-            
-            var disjunction = new Disjunction();
+            if (removed == null)
+                return;
+            if (removed.Count == 0)
+                return;
 
+            //Create a disjunction (list of OR statements
+            var disjunction = new Disjunction();            
             foreach (var path in removed)
             {
-                disjunction.Add(Restrictions.On<FileState>(s => s.FilePath).IsLike(path, MatchMode.Start));
+                //with the restriction FileState.FilePath like '@path%'
+                disjunction.Add(Restrictions.On<FileState>(s => s.FilePath)
+                    .IsLike(path, MatchMode.Start));
             }
 
-            
-            
+            //Generate a query from the disjunction
             var query=QueryOver.Of<FileState>().Where(disjunction);
-            var aq = new ProjectionQuery<FileState,Guid>(query.DetachedCriteria,
-                        Projections.ProjectionList().Add(Projections.Id()));
-            var ids=aq.Execute();
-            FileState.DeleteAll(ids);
-                
+                        
+            ExecuteWithRetry((session,instance)=>
+                                 {
+                                     using (var t=session.BeginTransaction())
+                                     {
+                                         var states = query.GetExecutableQueryOver(session).List();
+                                         foreach (var state in states)
+                                         {
+                                             state.FileStatus = FileStatus.Unversioned;
+                                             state.OverlayStatus = FileOverlayStatus.Unversioned;
+                                             state.Update();
+                                         }
+                                         t.Commit();
+                                     }
+                                     return null;
+                                 },null);
         }
+
     }
 
     [ActiveRecord("Tags")]