Added header to all files. Closes #2064
[pithos-ms-client] / trunk / Pithos.Core / Agents / SnapshotDifferencer.cs
1 #region\r
2 /* -----------------------------------------------------------------------\r
3  * <copyright file="SnapshotDifferencer.cs" company="GRNet">\r
4  * \r
5  * Copyright 2011-2012 GRNET S.A. All rights reserved.\r
6  *\r
7  * Redistribution and use in source and binary forms, with or\r
8  * without modification, are permitted provided that the following\r
9  * conditions are met:\r
10  *\r
11  *   1. Redistributions of source code must retain the above\r
12  *      copyright notice, this list of conditions and the following\r
13  *      disclaimer.\r
14  *\r
15  *   2. Redistributions in binary form must reproduce the above\r
16  *      copyright notice, this list of conditions and the following\r
17  *      disclaimer in the documentation and/or other materials\r
18  *      provided with the distribution.\r
19  *\r
20  *\r
21  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS\r
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR\r
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF\r
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\r
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
32  * POSSIBILITY OF SUCH DAMAGE.\r
33  *\r
34  * The views and conclusions contained in the software and\r
35  * documentation are those of the authors and should not be\r
36  * interpreted as representing official policies, either expressed\r
37  * or implied, of GRNET S.A.\r
38  * </copyright>\r
39  * -----------------------------------------------------------------------\r
40  */\r
41 #endregion\r
42 using System.Collections.Concurrent;\r
43 using Pithos.Interfaces;\r
44 using Pithos.Network;\r
45 \r
46 namespace Pithos.Core.Agents\r
47 {\r
48     using System;\r
49     using System.Collections.Generic;\r
50     using System.Linq;\r
51     using System.Text;\r
52 \r
53     /// <summary>\r
54     /// TODO: Update summary.\r
55     /// </summary>\r
56     public class SnapshotDifferencer\r
57     {\r
58         private IEnumerable<ObjectInfo> _previous;\r
59         private IEnumerable<ObjectInfo> _current;\r
60         private static ObjectInfo[] _empty = new ObjectInfo[0];\r
61         private ObjectInfoComparer _comparer = new ObjectInfoComparer();\r
62 \r
63         public SnapshotDifferencer()\r
64         {\r
65             _previous = new List<ObjectInfo>();\r
66             _current= new List<ObjectInfo>();\r
67         }\r
68 \r
69 \r
70         public SnapshotDifferencer(IEnumerable<ObjectInfo> previous,IEnumerable<ObjectInfo> current  )\r
71         {\r
72             _previous = previous ?? new List<ObjectInfo>();\r
73             _current= current ?? new List<ObjectInfo>();\r
74         }\r
75         public SnapshotDifferencer Post(IEnumerable<ObjectInfo> list)\r
76         {\r
77             _previous = _current;\r
78             _current = list ?? new List<ObjectInfo>();\r
79             return this;\r
80         }\r
81         \r
82         public IEnumerable<ObjectInfo> Deleted\r
83         {\r
84             get { return _previous.Except(_current,_comparer); }\r
85         }\r
86         public IEnumerable<ObjectInfo> Created\r
87         {\r
88             get { return _current.Except(_previous,_comparer); }\r
89         }\r
90         public IEnumerable<ObjectInfo> Changed\r
91         {\r
92             get\r
93             {\r
94                 var changes = from newItem in _current \r
95                               let oldItem=_previous.FirstOrDefault(old=>_comparer.Equals(old,newItem))\r
96                               where oldItem !=null &&\r
97                                     newItem.Hash != oldItem.Hash\r
98                               select newItem;\r
99                 return changes;\r
100             }\r
101         }\r
102         public IEnumerable<ObjectInfo> Unchanged\r
103         {\r
104             get\r
105             {\r
106                 var unChanged = from newItem in _current\r
107                               let oldItem = _previous.FirstOrDefault(old => _comparer.Equals(old, newItem))\r
108                               where oldItem != null &&\r
109                                     newItem.Hash == oldItem.Hash\r
110                               select newItem;\r
111                 return unChanged;\r
112             }\r
113         }\r
114     }\r
115 \r
116     public class AccountsDifferencer\r
117     {\r
118         ConcurrentDictionary<string, SnapshotDifferencer> _differencers = new ConcurrentDictionary<string, SnapshotDifferencer>();\r
119 \r
120         public ConcurrentDictionary<string, SnapshotDifferencer> Differencers { get { return _differencers; } }\r
121 \r
122         public SnapshotDifferencer PostSnapshot(AccountInfo accountInfo, List<ObjectInfo> cleanRemotes)\r
123         {\r
124             SnapshotDifferencer differencer;\r
125             if (!_differencers.TryGetValue(accountInfo.UserName, out differencer))\r
126             {\r
127                 differencer = new SnapshotDifferencer();\r
128                 _differencers[accountInfo.UserName] = differencer;\r
129             }\r
130             differencer.Post(cleanRemotes);\r
131             return differencer;\r
132         }\r
133 \r
134     }\r
135 }\r