Simplified snapshot comparisons usind dictionaries
authorPanagiotis Kanavos <pkanavos@gmail.com>
Wed, 29 Feb 2012 13:18:26 +0000 (15:18 +0200)
committerPanagiotis Kanavos <pkanavos@gmail.com>
Wed, 29 Feb 2012 13:18:26 +0000 (15:18 +0200)
trunk/Pithos.Core/Agents/SnapshotDifferencer.cs

index 7bd83b9..2725e72 100644 (file)
@@ -43,7 +43,6 @@ using System.Collections.Concurrent;
 using System.Diagnostics.Contracts;\r
 using Pithos.Interfaces;\r
 using Pithos.Network;\r
-using System;\r
 using System.Collections.Generic;\r
 using System.Linq;\r
 \r
@@ -81,7 +80,7 @@ namespace Pithos.Core.Agents
             /// Common objects, lazily evalueated. \r
             /// The common objects are used to calculate both the Changed and Unchanged objects\r
             /// </summary>\r
-            public readonly Lazy<IEnumerable<ObjectInfo>> Common;\r
+            public readonly IEnumerable<ObjectInfo> Common;\r
 \r
             private readonly static ObjectInfo[] Empty = new ObjectInfo[0];\r
 \r
@@ -104,11 +103,15 @@ namespace Pithos.Core.Agents
                 CurrentDict = Current.ToDictionary(info => info.UUID);\r
                 PreviousDict = Previous.ToDictionary(info => info.UUID);\r
 \r
-                Common=new Lazy<IEnumerable<ObjectInfo>>(() =>\r
-                    Current.Join(Previous,\r
-                                outKey => outKey.UUID,\r
-                                inKey => inKey.UUID,\r
-                                (outer, inner) =>outer.SetPrevious(inner)));            \r
+                //Attach the previous version to the current listings\r
+                foreach (var info in Current)\r
+                {\r
+                    ObjectInfo prev;\r
+                    if (PreviousDict.TryGetValue(info.UUID, out prev))\r
+                        info.SetPrevious(prev);\r
+                }\r
+\r
+                Common=Current.Where(c=>c.Previous !=null);\r
             }\r
         }\r
 \r
@@ -116,12 +119,6 @@ namespace Pithos.Core.Agents
         private State _state;\r
 \r
         /// <summary>\r
-        /// The comparer used to identify common objects.\r
-        /// Objects are considered common when they have the same Account, Container and Name\r
-        /// </summary>\r
-        private readonly ObjectInfoComparer _comparer = new ObjectInfoComparer();\r
-\r
-        /// <summary>\r
         /// Default constructor. Initializes the Current and Previous listings to empty lists\r
         /// </summary>\r
         public SnapshotDifferencer()\r
@@ -156,52 +153,61 @@ namespace Pithos.Core.Agents
         \r
         /// <summary>\r
         /// Deleted objects are those that existed in the Previous listing\r
-        /// but are not found in the Current listing, and their UUIDs do not\r
-        /// appear in the Current listing (ie they were not renamed/moved)\r
+        /// but are not found in the Current listing\r
         /// </summary>\r
         public IEnumerable<ObjectInfo> Deleted\r
         {\r
-            get { return _state.Previous.Except(_state.Current,_comparer)\r
+            get { return _state.Previous\r
                 .Where(info=>!_state.CurrentDict.ContainsKey(info.UUID)); }\r
         }\r
 \r
         /// <summary>\r
         /// Created objects are those that exist in the Current listing\r
-        /// but are not found in the Previous listing, and their UUIDs do not\r
-        /// appear in the Previous listing (ie they were not renamed/moved)\r
+        /// but are not found in the Previous listing \r
         /// </summary>\r
         public IEnumerable<ObjectInfo> Created\r
         {\r
-            get { return _state.Current.Except(_state.Previous,_comparer)\r
+            get { return _state.Current\r
                 .Where(info=>!_state.PreviousDict.ContainsKey(info.UUID)); }\r
         }\r
                 \r
         public IEnumerable<ObjectInfo> Common\r
         {\r
-            get { return _state.Common.Value; }\r
+            get { return _state.Common; }\r
         }\r
 \r
         public IEnumerable<ObjectInfo> Changed\r
         {\r
             get\r
             {\r
-                return Common.Where(i => i.PreviousHash != i.Hash && (i.Previous == null ||  i.Uri == i.Previous.Uri));}\r
+                return Common.Where(info => \r
+                    //The hash is different\r
+                    info.PreviousHash != info.Hash \r
+                    //And the Uri is unchanged or there is no previous version\r
+                    && (info.Previous == null ||  info.Uri == info.Previous.Uri));\r
+            }\r
         }\r
+\r
+        /// <summary>\r
+        /// Unchanged objects have the same current and previous hash\r
+        /// </summary>\r
         public IEnumerable<ObjectInfo> Unchanged\r
         {\r
             get{ return Common.Where(i => i.PreviousHash == i.Hash);}\r
         }\r
 \r
+        /// <summary>\r
+        /// Moved objects have a previous version with a different name\r
+        /// </summary>\r
         public IEnumerable<ObjectInfo>  Moved\r
         {\r
             get\r
-            {\r
-                                \r
-                return _state.Current.Join(_state.Previous,\r
-                                    outer => outer.UUID,\r
-                                    inner => inner.UUID,\r
-                                    (outer, inner) => (outer.Name == inner.Name ? null : outer.SetPrevious(inner)))\r
-                    .Where(t => t != null);                \r
+            {                                \r
+                return Common.Where(info=>\r
+                    //A previous version exists\r
+                    info.Previous!= null \r
+                    //and the Uri is different\r
+                    && info.Uri!=info.Previous.Uri);                    \r
             }\r
         }\r
     }\r