2 using System.Collections.Generic;
3 using System.ComponentModel.Composition;
4 using System.Diagnostics;
5 using System.Diagnostics.Contracts;
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;
16 using log4net.Appender;
20 namespace Pithos.Core.Agents
22 [Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))]
23 public class StatusAgent:IStatusChecker,IStatusKeeper
25 [System.ComponentModel.Composition.Import]
26 public IPithosSettings Settings { get; set; }
28 //private Agent<Action> _persistenceAgent;
29 private ActionBlock<Action> _persistenceAgent;
32 private static readonly ILog Log = LogManager.GetLogger("StatusAgent");
36 var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
37 _pithosDataPath = Path.Combine(appDataPath , "Pithos");
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();
45 if (!File.Exists(Path.Combine(_pithosDataPath ,"pithos.db")))
46 ActiveRecordStarter.CreateSchema();
49 private static InPlaceConfigurationSource GetConfiguration(string pithosDbPath)
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();
57 var properties = new Dictionary<string, string>
59 {"connection.driver_class", "NHibernate.Driver.SQLite20Driver"},
60 {"dialect", "NHibernate.Dialect.SQLiteDialect"},
61 {"connection.provider", "NHibernate.Connection.DriverConnectionProvider"},
63 "proxyfactory.factory_class",
64 "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"
68 var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3", pithosDbPath);
69 properties.Add("connection.connection_string", connectionString);
71 var source = new InPlaceConfigurationSource();
72 source.Add(typeof (ActiveRecordBase), properties);
73 source.SetDebugFlag(false);
77 public void StartProcessing(CancellationToken token)
79 _persistenceAgent = new ActionBlock<Action>(async action=>
83 await TaskEx.Run(action);
87 Log.ErrorFormat("[ERROR] STATE \n{0}",ex);
97 _persistenceAgent.Complete();
101 public void ProcessExistingFiles(IEnumerable<FileInfo> existingFiles)
103 if(existingFiles ==null)
104 throw new ArgumentNullException("existingFiles");
105 Contract.EndContractBlock();
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};
114 //To get the deleted files we must get the states that have no corresponding
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();
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 };
128 var pairs = currentFiles.Union(deletedFiles);
130 Parallel.ForEach(pairs, pair =>
132 var fileState = pair.State;
133 var file = pair.File;
134 if (fileState == null)
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));
141 else if (file == null)
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);
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)
155 FileState.UpdateStatus(fileState.Id, FileStatus.Modified);
165 public string BlockHash { get; set; }
167 public int BlockSize { get; set; }
168 public void ChangeRoots(string oldPath, string newPath)
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();
180 FileState.ChangeRootPath(oldPath,newPath);
184 private PithosStatus _pithosStatus=PithosStatus.InSynch;
186 public void SetPithosStatus(PithosStatus status)
188 _pithosStatus = status;
191 public PithosStatus GetPithosStatus()
193 return _pithosStatus;
197 private string _pithosDataPath;
200 public FileOverlayStatus GetFileOverlayStatus(string path)
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();
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;
215 catch (Exception exc)
217 Log.ErrorFormat(exc.ToString());
218 return FileOverlayStatus.Unversioned;
222 public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
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();
230 _persistenceAgent.Post(() => FileState.StoreOverlayStatus(path.ToLower(),overlayStatus));
233 /*public void RemoveFileOverlayStatus(string path)
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();
241 _persistenceAgent.Post(() =>
242 InnerRemoveFileOverlayStatus(path));
245 private static void InnerRemoveFileOverlayStatus(string path)
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();
253 FileState.DeleteByFilePath(path);
256 public void RenameFileOverlayStatus(string oldPath, string newPath)
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();
268 _persistenceAgent.Post(() =>FileState.RenameState(oldPath, newPath));
271 public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
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();
279 Debug.Assert(!path.Contains(FolderConstants.CacheFolder));
280 Debug.Assert(!path.EndsWith(".ignore"));
282 _persistenceAgent.Post(() => FileState.UpdateStatus(path.ToLower(), fileStatus, overlayStatus));
285 public void StoreInfo(string path,ObjectInfo objectInfo)
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();
295 _persistenceAgent.Post(() =>
297 var filePath = path.ToLower();
298 //Load the existing files state and set its properties in one session
299 using (new SessionScope())
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);
305 //Create a new empty state object if this is a new file
306 state = state ?? new FileState();
308 state.FilePath = filePath;
309 state.Checksum = objectInfo.Hash;
310 state.Version = objectInfo.Version;
311 state.VersionTimeStamp = objectInfo.VersionTimestamp;
313 state.FileStatus = FileStatus.Unchanged;
314 state.OverlayStatus = FileOverlayStatus.Normal;
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
322 // FileState = state,
324 // Value = pair.Value
336 public void SetFileStatus(string path, FileStatus status)
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();
344 _persistenceAgent.Post(() => FileState.UpdateStatus(path.ToLower(), status));
347 public FileStatus GetFileStatus(string path)
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();
355 var status = from r in FileState.Queryable
356 where r.FilePath == path.ToLower()
358 return status.Any()?status.First(): FileStatus.Missing;
361 public void ClearFileStatus(string path)
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();
369 //TODO:SHOULDN'T need both clear file status and remove overlay status
370 _persistenceAgent.Post(() =>
372 using (new SessionScope())
374 FileState.DeleteByFilePath(path);
379 public void UpdateFileChecksum(string path, string checksum)
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();
387 _persistenceAgent.Post(() => FileState.UpdateChecksum(path.ToLower(), checksum));