Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / SnapshotDifferencer.cs @ 422c9598

History | View | Annotate | Download (2.6 kB)

1
// -----------------------------------------------------------------------
2
// <copyright file="SnapshotDifferencer.cs" company="Microsoft">
3
// TODO: Update copyright text.
4
// </copyright>
5
// -----------------------------------------------------------------------
6

    
7
using Pithos.Interfaces;
8

    
9
namespace Pithos.Core.Agents
10
{
11
    using System;
12
    using System.Collections.Generic;
13
    using System.Linq;
14
    using System.Text;
15

    
16
    /// <summary>
17
    /// TODO: Update summary.
18
    /// </summary>
19
    public class SnapshotDifferencer
20
    {
21
        private IEnumerable<ObjectInfo> _previous;
22
        private IEnumerable<ObjectInfo> _current;
23
        private static ObjectInfo[] _empty = new ObjectInfo[0];
24
        private ObjectInfoComparer _comparer = new ObjectInfoComparer();
25

    
26
        public SnapshotDifferencer()
27
        {
28
            _previous = new List<ObjectInfo>();
29
            _current= new List<ObjectInfo>();
30
        }
31

    
32

    
33
        public SnapshotDifferencer(IEnumerable<ObjectInfo> previous,IEnumerable<ObjectInfo> current  )
34
        {
35
            _previous = previous ?? new List<ObjectInfo>();
36
            _current= current ?? new List<ObjectInfo>();
37
        }
38
        public SnapshotDifferencer Post(IEnumerable<ObjectInfo> list)
39
        {
40
            _previous = _current;
41
            _current = list ?? new List<ObjectInfo>();
42
            return this;
43
        }
44
        
45
        public IEnumerable<ObjectInfo> Deleted
46
        {
47
            get { return _previous.Except(_current,_comparer); }
48
        }
49
        public IEnumerable<ObjectInfo> Created
50
        {
51
            get { return _current.Except(_previous,_comparer); }
52
        }
53
        public IEnumerable<ObjectInfo> Changed
54
        {
55
            get
56
            {
57
                var changes = from newItem in _current 
58
                              let oldItem=_previous.FirstOrDefault(old=>_comparer.Equals(old,newItem))
59
                              where oldItem !=null &&
60
                                    newItem.Hash != oldItem.Hash
61
                              select newItem;
62
                return changes;
63
            }
64
        }
65
        public IEnumerable<ObjectInfo> Unchanged
66
        {
67
            get
68
            {
69
                var unChanged = from newItem in _current
70
                              let oldItem = _previous.FirstOrDefault(old => _comparer.Equals(old, newItem))
71
                              where oldItem != null &&
72
                                    newItem.Hash == oldItem.Hash
73
                              select newItem;
74
                return unChanged;
75
            }
76
        }
77
    }
78
}