Changes to NHibernate/Activerecord storage code
[pithos-ms-client] / trunk / Pithos.Core / Agents / StatusAgent.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel.Composition;
4 using System.Diagnostics;
5 using System.Diagnostics.Contracts;
6 using System.IO;
7 using System.Linq;
8 using System.Threading;
9 using System.Threading.Tasks;
10 using System.Threading.Tasks.Dataflow;
11 using Castle.ActiveRecord;
12 using Castle.ActiveRecord.Framework.Config;
13 using Pithos.Interfaces;
14 using Pithos.Network;
15 using log4net;
16 using log4net.Appender;
17 using log4net.Config;
18 using log4net.Layout;
19
20 namespace Pithos.Core.Agents
21 {
22     [Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))]
23     public class StatusAgent:IStatusChecker,IStatusKeeper
24     {
25         [System.ComponentModel.Composition.Import]
26         public IPithosSettings Settings { get; set; }
27
28         //private Agent<Action> _persistenceAgent;
29         private ActionBlock<Action> _persistenceAgent;
30
31
32         private static readonly ILog Log = LogManager.GetLogger("StatusAgent");
33
34         public StatusAgent()
35         {            
36             var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
37             _pithosDataPath = Path.Combine(appDataPath , "Pithos");
38
39             if (!Directory.Exists(_pithosDataPath))
40                 Directory.CreateDirectory(_pithosDataPath);
41             var source = GetConfiguration(_pithosDataPath);
42             ActiveRecordStarter.Initialize(source,typeof(FileState),typeof(FileTag));
43             ActiveRecordStarter.UpdateSchema();
44
45             if (!File.Exists(Path.Combine(_pithosDataPath ,"pithos.db")))
46                 ActiveRecordStarter.CreateSchema();
47         }        
48
49         private static InPlaceConfigurationSource GetConfiguration(string pithosDbPath)
50         {
51             if (String.IsNullOrWhiteSpace(pithosDbPath))
52                 throw new ArgumentNullException("pithosDbPath");
53             if (!Path.IsPathRooted(pithosDbPath))
54                 throw new ArgumentException("path must be a rooted path", "pithosDbPath");
55             Contract.EndContractBlock();
56
57             var properties = new Dictionary<string, string>
58                                  {
59                                      {"connection.driver_class", "NHibernate.Driver.SQLite20Driver"},
60                                      {"dialect", "NHibernate.Dialect.SQLiteDialect"},
61                                      {"connection.provider", "NHibernate.Connection.DriverConnectionProvider"},
62                                      {
63                                          "proxyfactory.factory_class",
64                                          "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"
65                                          },
66                                  };
67
68             var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3", pithosDbPath);
69             properties.Add("connection.connection_string", connectionString);
70
71             var source = new InPlaceConfigurationSource();                        
72             source.Add(typeof (ActiveRecordBase), properties);
73             source.SetDebugFlag(false);            
74             return source;
75         }
76
77         public void StartProcessing(CancellationToken token)
78         {
79             _persistenceAgent = new ActionBlock<Action>(async action=>
80             {
81                     try
82                     {
83                         await TaskEx.Run(action);
84                     }
85                     catch (Exception ex)
86                     {
87                         Log.ErrorFormat("[ERROR] STATE \n{0}",ex);
88                     }
89             });
90             
91         }
92
93        
94
95         public void Stop()
96         {
97             _persistenceAgent.Complete();            
98         }
99        
100
101         public void ProcessExistingFiles(IEnumerable<FileInfo> existingFiles)
102         {
103             if(existingFiles  ==null)
104                 throw new ArgumentNullException("existingFiles");
105             Contract.EndContractBlock();
106             
107             //Find new or matching files with a left join to the stored states
108             var fileStates = FileState.Queryable;
109             var currentFiles=from file in existingFiles
110                       join state in fileStates on file.FullName.ToLower() equals state.FilePath.ToLower() into gs
111                       from substate in gs.DefaultIfEmpty()
112                                select new {File = file, State = substate};
113
114             //To get the deleted files we must get the states that have no corresponding
115             //files. 
116             //We can't use the File.Exists method inside a query, so we get all file paths from the states
117             var statePaths = (from state in fileStates
118                              select new {state.Id, state.FilePath}).ToList();
119             //and check each one
120             var missingStates= (from path in statePaths
121                                where !File.Exists(path.FilePath)
122                                select path.Id).ToList();
123             //Finally, retrieve the states that correspond to the deleted files            
124             var deletedFiles = from state in fileStates 
125                         where missingStates.Contains(state.Id)
126                         select new { File = default(FileInfo), State = state };
127
128             var pairs = currentFiles.Union(deletedFiles);
129
130             Parallel.ForEach(pairs, pair =>
131             {
132                 var fileState = pair.State;
133                 var file = pair.File;
134                 if (fileState == null)
135                 {
136                     //This is a new file
137                     var fullPath = pair.File.FullName.ToLower();
138                     var createState = FileState.CreateForAsync(fullPath, this.BlockSize, this.BlockHash);
139                     createState.ContinueWith(state => _persistenceAgent.Post(state.Result.Create));
140                 }                
141                 else if (file == null)
142                 {
143                     //This file was deleted while we were down. We should mark it as deleted
144                     //We have to go through UpdateStatus here because the state object we are using
145                     //was created by a different ORM session.
146                     FileState.UpdateStatus(fileState.Id,FileStatus.Deleted);                    
147                 }
148                 else
149                 {
150                     //This file has a matching state. Need to check for possible changes
151                     var hashString = file.CalculateHash(BlockSize,BlockHash);
152                     //If the hashes don't match the file was changed
153                     if (fileState.Checksum != hashString)
154                     {
155                         FileState.UpdateStatus(fileState.Id, FileStatus.Modified);
156                     }                    
157                 }
158             });            
159          
160         }
161
162        
163
164
165         public string BlockHash { get; set; }
166
167         public int BlockSize { get; set; }
168         public void ChangeRoots(string oldPath, string newPath)
169         {
170             if (String.IsNullOrWhiteSpace(oldPath))
171                 throw new ArgumentNullException("oldPath");
172             if (!Path.IsPathRooted(oldPath))
173                 throw new ArgumentException("oldPath must be an absolute path", "oldPath");
174             if (string.IsNullOrWhiteSpace(newPath))
175                 throw new ArgumentNullException("newPath");
176             if (!Path.IsPathRooted(newPath))
177                 throw new ArgumentException("newPath must be an absolute path", "newPath");
178             Contract.EndContractBlock();
179
180             FileState.ChangeRootPath(oldPath,newPath);
181
182         }
183
184         private PithosStatus _pithosStatus=PithosStatus.InSynch;       
185
186         public void SetPithosStatus(PithosStatus status)
187         {
188             _pithosStatus = status;
189         }
190
191         public PithosStatus GetPithosStatus()
192         {
193             return _pithosStatus;
194         }
195
196
197         private string _pithosDataPath;
198        
199
200         public FileOverlayStatus GetFileOverlayStatus(string path)
201         {
202             if (String.IsNullOrWhiteSpace(path))
203                 throw new ArgumentNullException("path");
204             if (!Path.IsPathRooted(path))
205                 throw new ArgumentException("The path must be rooted", "path");
206             Contract.EndContractBlock();
207
208             try
209             {
210                 var status = from state in  FileState.Queryable 
211                                  where state.FilePath ==path.ToLower()
212                                  select state.OverlayStatus;
213                 return status.Any()? status.First():FileOverlayStatus.Unversioned;
214             }
215             catch (Exception exc)
216             {
217                 Log.ErrorFormat(exc.ToString());
218                 return FileOverlayStatus.Unversioned;
219             }
220         }
221
222         public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
223         {
224             if (String.IsNullOrWhiteSpace(path))
225                 throw new ArgumentNullException("path");
226             if (!Path.IsPathRooted(path))
227                 throw new ArgumentException("The path must be rooted","path");
228             Contract.EndContractBlock();
229
230             _persistenceAgent.Post(() => FileState.StoreOverlayStatus(path.ToLower(),overlayStatus));
231         }
232
233         /*public void RemoveFileOverlayStatus(string path)
234         {
235             if (String.IsNullOrWhiteSpace(path))
236                 throw new ArgumentNullException("path");
237             if (!Path.IsPathRooted(path))
238                 throw new ArgumentException("The path must be rooted", "path");
239             Contract.EndContractBlock();
240
241             _persistenceAgent.Post(() =>
242                 InnerRemoveFileOverlayStatus(path));
243         }
244
245         private static void InnerRemoveFileOverlayStatus(string path)
246         {
247             if (String.IsNullOrWhiteSpace(path))
248                 throw new ArgumentNullException("path");
249             if (!Path.IsPathRooted(path))
250                 throw new ArgumentException("The path must be rooted", "path");
251             Contract.EndContractBlock();
252
253             FileState.DeleteByFilePath(path);            
254         }*/
255
256         public void RenameFileOverlayStatus(string oldPath, string newPath)
257         {
258             if (String.IsNullOrWhiteSpace(oldPath))
259                 throw new ArgumentNullException("oldPath");
260             if (!Path.IsPathRooted(oldPath))
261                 throw new ArgumentException("The oldPath must be rooted", "oldPath");
262             if (String.IsNullOrWhiteSpace(newPath))
263                 throw new ArgumentNullException("newPath");
264             if (!Path.IsPathRooted(newPath))
265                 throw new ArgumentException("The newPath must be rooted", "newPath");
266             Contract.EndContractBlock();
267
268             _persistenceAgent.Post(() =>FileState.RenameState(oldPath, newPath));
269         }
270
271         public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
272         {
273             if (String.IsNullOrWhiteSpace(path))
274                 throw new ArgumentNullException("path");
275             if (!Path.IsPathRooted(path))
276                 throw new ArgumentException("The path must be rooted", "path");
277             Contract.EndContractBlock();
278
279             Debug.Assert(!path.Contains(FolderConstants.CacheFolder));
280             Debug.Assert(!path.EndsWith(".ignore"));
281
282             _persistenceAgent.Post(() => FileState.UpdateStatus(path.ToLower(), fileStatus, overlayStatus));
283         }
284
285         public void StoreInfo(string path,ObjectInfo objectInfo)
286         {
287             if (String.IsNullOrWhiteSpace(path))
288                 throw new ArgumentNullException("path");
289             if (!Path.IsPathRooted(path))
290                 throw new ArgumentException("The path must be rooted", "path");            
291             if (objectInfo == null)
292                 throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
293             Contract.EndContractBlock();
294
295             _persistenceAgent.Post(() =>
296             {
297                 var filePath = path.ToLower();
298                 //Load the existing files state and set its properties in one session            
299                 using (new SessionScope())
300                 {
301                     //Forgetting to use a sessionscope results in two sessions being created, one by 
302                     //FirstOrDefault and one by Save()
303                     var state =FileState.FindByFilePath(filePath);
304                     
305                     //Create a new empty state object if this is a new file
306                     state = state ?? new FileState();
307
308                     state.FilePath = filePath;
309                     state.Checksum = objectInfo.Hash;
310                     state.Version = objectInfo.Version;
311                     state.VersionTimeStamp = objectInfo.VersionTimestamp;
312
313                     state.FileStatus = FileStatus.Unchanged;
314                     state.OverlayStatus = FileOverlayStatus.Normal;
315                     
316                     //Create a list of tags from the ObjectInfo's tag dictionary
317                     //Make sure to bind each tag to its parent state so we don't have to save each tag separately
318                     //state.Tags = (from pair in objectInfo.Tags
319                     //                select
320                     //                    new FileTag
321                     //                        {
322                     //                            FileState = state,
323                     //                            Name = pair.Key,
324                     //                            Value = pair.Value
325                     //                        }
326                     //            ).ToList();
327
328                     //Do the save
329                     state.Save();
330                 }
331             });
332
333         }
334
335         
336         public void SetFileStatus(string path, FileStatus status)
337         {
338             if (String.IsNullOrWhiteSpace(path))
339                 throw new ArgumentNullException("path");
340             if (!Path.IsPathRooted(path))
341                 throw new ArgumentException("The path must be rooted", "path");
342             Contract.EndContractBlock();
343
344             _persistenceAgent.Post(() => FileState.UpdateStatus(path.ToLower(), status));
345         }
346
347         public FileStatus GetFileStatus(string path)
348         {
349             if (String.IsNullOrWhiteSpace(path))
350                 throw new ArgumentNullException("path");
351             if (!Path.IsPathRooted(path))
352                 throw new ArgumentException("The path must be rooted", "path");
353             Contract.EndContractBlock();
354
355             var status = from r in FileState.Queryable
356                      where r.FilePath == path.ToLower()
357                      select r.FileStatus;                        
358             return status.Any()?status.First(): FileStatus.Missing;
359         }
360
361         public void ClearFileStatus(string path)
362         {
363             if (String.IsNullOrWhiteSpace(path))
364                 throw new ArgumentNullException("path");
365             if (!Path.IsPathRooted(path))
366                 throw new ArgumentException("The path must be rooted", "path");
367             Contract.EndContractBlock();
368
369             //TODO:SHOULDN'T need both clear file status and remove overlay status
370             _persistenceAgent.Post(() =>
371             {
372                 using (new SessionScope())
373                 {
374                     FileState.DeleteByFilePath(path);
375                 }
376             });   
377         }
378
379         public void UpdateFileChecksum(string path, string checksum)
380         {
381             if (String.IsNullOrWhiteSpace(path))
382                 throw new ArgumentNullException("path");
383             if (!Path.IsPathRooted(path))
384                 throw new ArgumentException("The path must be rooted", "path");            
385             Contract.EndContractBlock();
386
387             _persistenceAgent.Post(() => FileState.UpdateChecksum(path.ToLower(), checksum));
388         }
389
390     }
391
392    
393 }