Modified UpdateStatusDirect to use NHibernate's session instead of a separate ADO...
authorpkanavos <pkanavos@gmail.com>
Mon, 2 Jul 2012 12:51:17 +0000 (15:51 +0300)
committerpkanavos <pkanavos@gmail.com>
Mon, 2 Jul 2012 12:51:17 +0000 (15:51 +0300)
trunk/Pithos.Client.WPF/Preferences/PreferencesView.xaml
trunk/Pithos.Core/Agents/StatusAgent.cs
trunk/Pithos.Core/FileState.cs

index afdfdcf..42b1860 100644 (file)
@@ -40,7 +40,7 @@
         </Grid.RowDefinitions>
 
        
-        <TabControl Grid.Row="0" x:Name="Tabs">  
+        <TabControl Grid.Row="0" x:Name="Tabs" SelectedItem="{Binding SelecteTab,Mode=TwoWay}">  
             <TabItem VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" x:Name="GeneralTab">
                 <TabItem.Header>
                     <StackPanel>
                     
                 </Grid>
             </TabItem>
-            <TabItem VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" x:Name="PluginTab" IsSelected="{Binding AccountTabSelected,Mode=OneWay}">
+            <TabItem VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" x:Name="PluginTab">
                 <TabItem.Header>
                     <StackPanel>
                         <Image Source="/PithosPlus;component/Images/Plugins.png" Stretch="Uniform" Height="32"/>
index 00f1cf7..86b158c 100644 (file)
@@ -42,6 +42,7 @@
 using System;
 using System.Collections.Generic;
 using System.ComponentModel.Composition;
+using System.Data;
 using System.Data.SQLite;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
@@ -55,10 +56,12 @@ using System.Threading.Tasks;
 using Castle.ActiveRecord;
 using Castle.ActiveRecord.Framework;
 using Castle.ActiveRecord.Framework.Config;
+using Castle.ActiveRecord.Queries;
 using NHibernate.ByteCode.Castle;
 using NHibernate.Cfg;
 using NHibernate.Cfg.Loquacious;
 using NHibernate.Dialect;
+using NHibernate.Exceptions;
 using Pithos.Interfaces;
 using Pithos.Network;
 using log4net;
@@ -346,15 +349,14 @@ namespace Pithos.Core.Agents
                 try
                 {
 
-                    
                     using (var connection = GetConnection())
                     using (
                         var command =
                             new SQLiteCommand("update FileState set FileStatus= :fileStatus where FilePath = :path COLLATE NOCASE",
                                               connection))
                     {
-
-
+                        //var query=new HqlBasedQuery(typeof(FileState),)
+                        //FileState.ExecuteQuery()
                         command.Parameters.AddWithValue("fileStatus", status);
 
                         command.Parameters.AddWithValue("path", path);
@@ -385,21 +387,21 @@ namespace Pithos.Core.Agents
                 try
                 {
 
-                    
-                    using (var connection = GetConnection())
-                    using (
-                        var command =
-                            new SQLiteCommand(
-                                "update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus,ConflictReason= :conflictReason where FilePath = :path COLLATE NOCASE ",
-                                connection))
+                    using (var session = ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(FileState)))
+                    using (session.BeginTransaction(IsolationLevel.ReadCommitted))
                     {
 
-                        command.Parameters.AddWithValue("path", absolutePath);
-                        command.Parameters.AddWithValue("fileStatus", fileStatus);
-                        command.Parameters.AddWithValue("overlayStatus", overlayStatus);
-                        command.Parameters.AddWithValue("conflictReason", conflictReason);
-                        
-                        var affected = command.ExecuteNonQuery();
+                        var walquery = session.CreateSQLQuery("PRAGMA journal_mode=WAL");
+                        walquery.List();
+
+
+                        var updatecmd = session.CreateSQLQuery("update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus,ConflictReason= :conflictReason where FilePath = :path COLLATE NOCASE")
+                                                .SetString("path", absolutePath)
+                                                .SetEnum("fileStatus", fileStatus)
+                                                .SetEnum("overlayStatus", overlayStatus)
+                                                .SetString("conflictReason", conflictReason);
+                        var affected = updatecmd.ExecuteUpdate();
+
                         if (affected == 0)
                         {
                             var createdState = FileState.CreateFor(FileInfoExtensions.FromPath(absolutePath), StatusNotification);
@@ -407,7 +409,8 @@ namespace Pithos.Core.Agents
                             createdState.OverlayStatus = overlayStatus;
                             createdState.ConflictReason = conflictReason;
                             createdState.LastMD5 = String.Empty;
-                            createdState.Create();  
+                            session.Save(createdState);
+                            //createdState.Create();
                         }
                         return affected;
                     }
@@ -458,7 +461,7 @@ namespace Pithos.Core.Agents
             {
                 
                 using (var connection = GetConnection())
-                using (var command = new SQLiteCommand("select Id, FilePath, OverlayStatus,FileStatus ,Checksum ,ETag,Version    ,VersionTimeStamp,IsShared   ,SharedBy   ,ShareWrite  from FileState where FilePath=:path COLLATE NOCASE", connection))
+                using (var command = new SQLiteCommand("select Id, FilePath, OverlayStatus,FileStatus ,Checksum ,ETag,Version    ,VersionTimeStamp,IsShared   ,SharedBy   ,ShareWrite, LastMD5,LastLength,LastWriteDate  from FileState where FilePath=:path COLLATE NOCASE", connection))
                 {
                     
                     command.Parameters.AddWithValue("path", path);
@@ -481,23 +484,12 @@ namespace Pithos.Core.Agents
                                                 VersionTimeStamp = reader.IsDBNull(7)?default(DateTime):reader.GetDateTime(7),
                                                 IsShared = !reader.IsDBNull(8) && reader.GetBoolean(8),
                                                 SharedBy = reader.IsDBNull(9)?"":reader.GetString(9),
-                                                ShareWrite = !reader.IsDBNull(10) && reader.GetBoolean(10)
+                                                ShareWrite = !reader.IsDBNull(10) && reader.GetBoolean(10),
+                                                LastMD5=reader.GetString(11),
+                                                LastLength=reader.IsDBNull(12)? default(long):reader.GetInt64(12),
+                                                LastWriteDate=reader.IsDBNull(13)?default(DateTime):reader.GetDateTime(13)
                                             };
-/*
-                            var state = new FileState
-                                            {
-                                                Id = (Guid) values[0],
-                                                FilePath = (string) values[1],
-                                                OverlayStatus = (FileOverlayStatus) (long)values[2],
-                                                FileStatus = (FileStatus) (long)values[3],
-                                                Checksum = (string) values[4],
-                                                Version = (long?) values[5],
-                                                VersionTimeStamp = (DateTime?) values[6],
-                                                IsShared = (long)values[7] == 1,
-                                                SharedBy = (string) values[8],
-                                                ShareWrite = (long)values[9] == 1
-                                            };
-*/
+
                             return state;
                         }
                         else
@@ -613,46 +605,6 @@ namespace Pithos.Core.Agents
             _persistenceAgent.Post(() => UpdateStatusDirect(path, fileStatus, overlayStatus, conflictReason));
         }
 
-/*
-        public void StoreInfo(string path,ObjectInfo objectInfo)
-        {
-            if (String.IsNullOrWhiteSpace(path))
-                throw new ArgumentNullException("path");
-            if (!Path.IsPathRooted(path))
-                throw new ArgumentException("The path must be rooted", "path");            
-            if (objectInfo == null)
-                throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
-            Contract.EndContractBlock();
-
-            _persistenceAgent.Post(() =>
-            {
-                var filePath = path.ToLower();
-                //Load the existing files state and set its properties in one session            
-                using (new SessionScope())
-                {
-                    //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();
-
-                    state.FilePath = filePath;
-                    state.Checksum = objectInfo.Hash;
-                    state.Version = objectInfo.Version;
-                    state.VersionTimeStamp = objectInfo.VersionTimestamp;
-
-                    state.FileStatus = FileStatus.Unchanged;
-                    state.OverlayStatus = FileOverlayStatus.Normal;
-                    
-                  
-                    //Do the save
-                    state.Save();
-                }
-            });
-
-        }
-*/
         
         public void StoreInfo(string path, ObjectInfo objectInfo)
         {
index 3561b10..b9a3dfb 100644 (file)
@@ -214,6 +214,7 @@ namespace Pithos.Core
 
         }*/
 
+/*
         public static void StoreFileStatus(string absolutePath, FileStatus newStatus)
         {
             if (string.IsNullOrWhiteSpace(absolutePath))
@@ -221,59 +222,33 @@ namespace Pithos.Core
             Contract.EndContractBlock();
 
             ExecuteWithRetry((session, instance) =>
-                        {
-                            const string hqlUpdate = "update FileState set FileStatus= :status where FilePath = :path ";
-                            var updatedEntities = session.CreateQuery(hqlUpdate)
-                                .SetString("path", absolutePath)
-                                .SetEnum("status", newStatus)
-                                .ExecuteUpdate();
-                            if (updatedEntities == 0)
-                            {
-                                var newState = new FileState
-                                                   {
-                                                       FilePath = absolutePath,
-                                                       Id = Guid.NewGuid(),
-                                                       FileStatus = newStatus,
-                                                       IsFolder=Directory.Exists(absolutePath)
-                                                   };
-                                newState.CreateAndFlush();
-                            }
-                            return null;
-                        }, null);
-
-        }
-
-        /*public static void StoreOverlayStatus(string absolutePath, FileOverlayStatus newStatus)
-        {
-            if (string.IsNullOrWhiteSpace(absolutePath))
-                throw new ArgumentNullException("absolutePath");
-            Contract.EndContractBlock();
-
-            ExecuteWithRetry((session, instance) =>
-                        {
-                            const string hqlUpdate =
-                                "update FileState set OverlayStatus= :status where FilePath = :path ";
-                            var updatedEntities = session.CreateQuery(hqlUpdate)                                
-                                .SetString("path", absolutePath)
-                                .SetEnum("status", newStatus)                                
-                                .ExecuteUpdate();
-                            if (updatedEntities == 0)
-                            {
-                                var newState = new FileState
-                                                   {
-                                                       FilePath = absolutePath,
-                                                       Id = Guid.NewGuid(),
-                                                       OverlayStatus = newStatus,
-                                                       ETag = String.Empty,
-                                                       IsFolder=Directory.Exists(absolutePath)
-                                                   };
-                                newState.CreateAndFlush();
-                            }
-                            return null;
-                        }, null);
+                                 {
+                                     const string hqlUpdate =
+                                         "update FileState set FileStatus= :status where FilePath = :path ";
+                                     using (session.BeginTransaction())
+                                     {
+                                         var updatedEntities = session.CreateQuery(hqlUpdate)
+                                             .SetString("path", absolutePath)
+                                             .SetEnum("status", newStatus)
+                                             .ExecuteUpdate();
+                                         if (updatedEntities == 0)
+                                         {
+                                             var newState = new FileState
+                                                                {
+                                                                    FilePath = absolutePath,
+                                                                    Id = Guid.NewGuid(),
+                                                                    FileStatus = newStatus,
+                                                                    IsFolder = Directory.Exists(absolutePath)
+                                                                };
+                                             newState.CreateAndFlush();
+                                         }
+                                         return null;
+                                     }
+                                 }, null);
 
         }
 */
+
         public static void StoreOverlayStatus(string absolutePath, FileOverlayStatus newStatus,string etag)
         {
             if (string.IsNullOrWhiteSpace(absolutePath))
@@ -284,23 +259,27 @@ namespace Pithos.Core
                         {
                             const string hqlUpdate =
                                 "update FileState set OverlayStatus= :status where FilePath = :path ";
-                            var updatedEntities = session.CreateQuery(hqlUpdate)                                
-                                .SetString("path", absolutePath)
-                                .SetEnum("status", newStatus)                                
-                                .ExecuteUpdate();
-                            if (updatedEntities == 0)
+                            using (session.BeginTransaction())
                             {
-                                var newState = new FileState
-                                                   {
-                                                       FilePath = absolutePath,
-                                                       Id = Guid.NewGuid(),
-                                                       OverlayStatus = newStatus,
-                                                       ETag = etag??String.Empty,
-                                                       IsFolder=Directory.Exists(absolutePath)
-                                                   };
-                                newState.CreateAndFlush();
+                                var updatedEntities = session.CreateQuery(hqlUpdate)
+                                    .SetString("path", absolutePath)
+                                    .SetEnum("status", newStatus)
+                                    .ExecuteUpdate();
+                                if (updatedEntities == 0)
+                                {
+                                    var newState = new FileState
+                                                       {
+                                                           FilePath = absolutePath,
+                                                           Id = Guid.NewGuid(),
+                                                           OverlayStatus = newStatus,
+                                                           ETag = etag ?? String.Empty,
+                                                           LastMD5=String.Empty,
+                                                           IsFolder = Directory.Exists(absolutePath)
+                                                       };
+                                    newState.CreateAndFlush();
+                                }
+                                return null;
                             }
-                            return null;
                         }, null);
 
         }
@@ -401,6 +380,7 @@ namespace Pithos.Core
 
         }
 
+
         public static void UpdateLastMD5(FileInfo file, string md5)
         {
             if (file==null)
@@ -412,29 +392,32 @@ namespace Pithos.Core
                             const string hqlUpdate = "update FileState set LastMD5=:md5, LastWriteDate=:date,LastLength=:length where FilePath = :path ";
                             var fullName = file.WithProperCapitalization().FullName;
 
-                            var updatedEntities = session.CreateQuery(hqlUpdate)
-                                .SetDateTime("date", file.LastWriteTime)
-                                .SetInt64("length", file.Length)
-                                .SetString("md5", md5)
-                                .SetString("path", fullName)
-                                .ExecuteUpdate();
-                            if (updatedEntities == 0)
+                            using (session.BeginTransaction())
                             {
-                                var newState = new FileState
+                                var updatedEntities = session.CreateQuery(hqlUpdate)
+                                    .SetDateTime("date", file.LastWriteTime)
+                                    .SetInt64("length", file.Length)
+                                    .SetString("md5", md5)
+                                    .SetString("path", fullName)
+                                    .ExecuteUpdate();
+                                if (updatedEntities == 0)
                                 {
-                                    FilePath = fullName,
-                                    Id = Guid.NewGuid(),
-                                    OverlayStatus = FileOverlayStatus.Normal,
-                                    FileStatus=FileStatus.Unchanged,
-                                    IsFolder = false,
-                                    LastLength=file.Length,
-                                    LastWriteDate=file.LastWriteTime,
-                                    LastMD5 = md5 ?? String.Empty,
-                                    ETag=String.Empty
-                                };
-                                newState.CreateAndFlush();
+                                    var newState = new FileState
+                                                       {
+                                                           FilePath = fullName,
+                                                           Id = Guid.NewGuid(),
+                                                           OverlayStatus = FileOverlayStatus.Normal,
+                                                           FileStatus = FileStatus.Unchanged,
+                                                           IsFolder = false,
+                                                           LastLength = file.Length,
+                                                           LastWriteDate = file.LastWriteTime,
+                                                           LastMD5 = md5 ?? String.Empty,
+                                                           ETag = String.Empty
+                                                       };
+                                    newState.CreateAndFlush();
+                                }
+                                return updatedEntities;
                             }
-                            return updatedEntities;
                         }, null);
 
         }