From: Panagiotis Kanavos Date: Wed, 29 Feb 2012 13:18:26 +0000 (+0200) Subject: Simplified snapshot comparisons usind dictionaries X-Git-Url: https://code.grnet.gr/git/pithos-ms-client/commitdiff_plain/89472316a52c0289eca9b84c0061bdc33e4828bb?ds=sidebyside Simplified snapshot comparisons usind dictionaries --- diff --git a/trunk/Pithos.Core/Agents/SnapshotDifferencer.cs b/trunk/Pithos.Core/Agents/SnapshotDifferencer.cs index 7bd83b9..2725e72 100644 --- a/trunk/Pithos.Core/Agents/SnapshotDifferencer.cs +++ b/trunk/Pithos.Core/Agents/SnapshotDifferencer.cs @@ -43,7 +43,6 @@ using System.Collections.Concurrent; using System.Diagnostics.Contracts; using Pithos.Interfaces; using Pithos.Network; -using System; using System.Collections.Generic; using System.Linq; @@ -81,7 +80,7 @@ namespace Pithos.Core.Agents /// Common objects, lazily evalueated. /// The common objects are used to calculate both the Changed and Unchanged objects /// - public readonly Lazy> Common; + public readonly IEnumerable Common; private readonly static ObjectInfo[] Empty = new ObjectInfo[0]; @@ -104,11 +103,15 @@ namespace Pithos.Core.Agents CurrentDict = Current.ToDictionary(info => info.UUID); PreviousDict = Previous.ToDictionary(info => info.UUID); - Common=new Lazy>(() => - Current.Join(Previous, - outKey => outKey.UUID, - inKey => inKey.UUID, - (outer, inner) =>outer.SetPrevious(inner))); + //Attach the previous version to the current listings + foreach (var info in Current) + { + ObjectInfo prev; + if (PreviousDict.TryGetValue(info.UUID, out prev)) + info.SetPrevious(prev); + } + + Common=Current.Where(c=>c.Previous !=null); } } @@ -116,12 +119,6 @@ namespace Pithos.Core.Agents private State _state; /// - /// The comparer used to identify common objects. - /// Objects are considered common when they have the same Account, Container and Name - /// - private readonly ObjectInfoComparer _comparer = new ObjectInfoComparer(); - - /// /// Default constructor. Initializes the Current and Previous listings to empty lists /// public SnapshotDifferencer() @@ -156,52 +153,61 @@ namespace Pithos.Core.Agents /// /// Deleted objects are those that existed in the Previous listing - /// but are not found in the Current listing, and their UUIDs do not - /// appear in the Current listing (ie they were not renamed/moved) + /// but are not found in the Current listing /// public IEnumerable Deleted { - get { return _state.Previous.Except(_state.Current,_comparer) + get { return _state.Previous .Where(info=>!_state.CurrentDict.ContainsKey(info.UUID)); } } /// /// Created objects are those that exist in the Current listing - /// but are not found in the Previous listing, and their UUIDs do not - /// appear in the Previous listing (ie they were not renamed/moved) + /// but are not found in the Previous listing /// public IEnumerable Created { - get { return _state.Current.Except(_state.Previous,_comparer) + get { return _state.Current .Where(info=>!_state.PreviousDict.ContainsKey(info.UUID)); } } public IEnumerable Common { - get { return _state.Common.Value; } + get { return _state.Common; } } public IEnumerable Changed { get { - return Common.Where(i => i.PreviousHash != i.Hash && (i.Previous == null || i.Uri == i.Previous.Uri));} + return Common.Where(info => + //The hash is different + info.PreviousHash != info.Hash + //And the Uri is unchanged or there is no previous version + && (info.Previous == null || info.Uri == info.Previous.Uri)); + } } + + /// + /// Unchanged objects have the same current and previous hash + /// public IEnumerable Unchanged { get{ return Common.Where(i => i.PreviousHash == i.Hash);} } + /// + /// Moved objects have a previous version with a different name + /// public IEnumerable Moved { get - { - - return _state.Current.Join(_state.Previous, - outer => outer.UUID, - inner => inner.UUID, - (outer, inner) => (outer.Name == inner.Name ? null : outer.SetPrevious(inner))) - .Where(t => t != null); + { + return Common.Where(info=> + //A previous version exists + info.Previous!= null + //and the Uri is different + && info.Uri!=info.Previous.Uri); } } }