root / trunk / Pithos.Core / Agents / SnapshotDifferencer.cs @ a0622735
History | View | Annotate | Download (3.4 kB)
1 |
// ----------------------------------------------------------------------- |
---|---|
2 |
// <copyright file="SnapshotDifferencer.cs" company="Microsoft"> |
3 |
// TODO: Update copyright text. |
4 |
// </copyright> |
5 |
// ----------------------------------------------------------------------- |
6 |
|
7 |
using Pithos.Interfaces; |
8 |
using Pithos.Network; |
9 |
|
10 |
namespace Pithos.Core.Agents |
11 |
{ |
12 |
using System; |
13 |
using System.Collections.Generic; |
14 |
using System.Linq; |
15 |
using System.Text; |
16 |
|
17 |
/// <summary> |
18 |
/// TODO: Update summary. |
19 |
/// </summary> |
20 |
public class SnapshotDifferencer |
21 |
{ |
22 |
private IEnumerable<ObjectInfo> _previous; |
23 |
private IEnumerable<ObjectInfo> _current; |
24 |
private static ObjectInfo[] _empty = new ObjectInfo[0]; |
25 |
private ObjectInfoComparer _comparer = new ObjectInfoComparer(); |
26 |
|
27 |
public SnapshotDifferencer() |
28 |
{ |
29 |
_previous = new List<ObjectInfo>(); |
30 |
_current= new List<ObjectInfo>(); |
31 |
} |
32 |
|
33 |
|
34 |
public SnapshotDifferencer(IEnumerable<ObjectInfo> previous,IEnumerable<ObjectInfo> current ) |
35 |
{ |
36 |
_previous = previous ?? new List<ObjectInfo>(); |
37 |
_current= current ?? new List<ObjectInfo>(); |
38 |
} |
39 |
public SnapshotDifferencer Post(IEnumerable<ObjectInfo> list) |
40 |
{ |
41 |
_previous = _current; |
42 |
_current = list ?? new List<ObjectInfo>(); |
43 |
return this; |
44 |
} |
45 |
|
46 |
public IEnumerable<ObjectInfo> Deleted |
47 |
{ |
48 |
get { return _previous.Except(_current,_comparer); } |
49 |
} |
50 |
public IEnumerable<ObjectInfo> Created |
51 |
{ |
52 |
get { return _current.Except(_previous,_comparer); } |
53 |
} |
54 |
public IEnumerable<ObjectInfo> Changed |
55 |
{ |
56 |
get |
57 |
{ |
58 |
var changes = from newItem in _current |
59 |
let oldItem=_previous.FirstOrDefault(old=>_comparer.Equals(old,newItem)) |
60 |
where oldItem !=null && |
61 |
newItem.Hash != oldItem.Hash |
62 |
select newItem; |
63 |
return changes; |
64 |
} |
65 |
} |
66 |
public IEnumerable<ObjectInfo> Unchanged |
67 |
{ |
68 |
get |
69 |
{ |
70 |
var unChanged = from newItem in _current |
71 |
let oldItem = _previous.FirstOrDefault(old => _comparer.Equals(old, newItem)) |
72 |
where oldItem != null && |
73 |
newItem.Hash == oldItem.Hash |
74 |
select newItem; |
75 |
return unChanged; |
76 |
} |
77 |
} |
78 |
} |
79 |
|
80 |
public class AccountsDifferencer |
81 |
{ |
82 |
Dictionary<string, SnapshotDifferencer> _differencers = new Dictionary<string, SnapshotDifferencer>(); |
83 |
|
84 |
public Dictionary<string, SnapshotDifferencer> Differencers { get { return _differencers; } } |
85 |
|
86 |
public SnapshotDifferencer PostSnapshot(AccountInfo accountInfo, List<ObjectInfo> cleanRemotes) |
87 |
{ |
88 |
SnapshotDifferencer differencer; |
89 |
if (!_differencers.TryGetValue(accountInfo.UserName, out differencer)) |
90 |
{ |
91 |
differencer = new SnapshotDifferencer(); |
92 |
_differencers[accountInfo.UserName] = differencer; |
93 |
} |
94 |
differencer.Post(cleanRemotes); |
95 |
return differencer; |
96 |
} |
97 |
|
98 |
} |
99 |
} |