1 // -----------------------------------------------------------------------
2 // <copyright file="FileState.cs" company="Microsoft">
3 // TODO: Update copyright text.
5 // -----------------------------------------------------------------------
7 using System.Diagnostics.Contracts;
9 using System.Threading.Tasks;
10 using Castle.ActiveRecord;
11 using Castle.ActiveRecord.Framework;
12 using Castle.ActiveRecord.Queries;
13 using NHibernate.Engine;
14 using Pithos.Core.Agents;
15 using Pithos.Interfaces;
21 using System.Collections.Generic;
26 /// TODO: Update summary.
29 public class FileState:ActiveRecordLinqBase<FileState>
31 private string _filePath;
32 private IList<FileTag> _tags=new List<FileTag>();
34 [PrimaryKey(PrimaryKeyType.Guid)]
35 public Guid Id { get; set; }
37 [Property(Unique=true,UniqueKey="IX_FileState_FilePath")]
38 public string FilePath
40 get { return _filePath; }
41 set { _filePath = value.ToLower(); }
45 public FileOverlayStatus OverlayStatus { get; set; }
48 public FileStatus FileStatus { get; set; }
51 public string Checksum { get; set; }
55 public string TopHash { get; set; }
59 public long? Version { get; set; }
62 public DateTime? VersionTimeStamp { get; set; }
66 public bool IsShared { get; set; }
69 public string SharedBy { get; set; }
72 public bool ShareWrite { get; set; }
75 [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true,Inverse=true)]
76 public IList<FileTag> Tags
82 // [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true)]
83 // public IList<FileHash> Hashes { get; set; }
86 // public byte[] HashmapHash { get; set; }
88 public static FileState FindByFilePath(string absolutePath)
90 if (string.IsNullOrWhiteSpace(absolutePath))
91 throw new ArgumentNullException("absolutePath");
92 Contract.EndContractBlock();
93 return Queryable.FirstOrDefault(s => s.FilePath == absolutePath.ToLower());
96 public static void DeleteByFilePath(string absolutePath)
98 if(string.IsNullOrWhiteSpace(absolutePath))
99 throw new ArgumentNullException("absolutePath");
100 Contract.EndContractBlock();
102 FileState.Execute((session, instance) =>
104 const string hqlDelete = "delete FileState where FilePath = :path";
105 var deletedEntities = session.CreateQuery(hqlDelete)
106 .SetString("path", absolutePath.ToLower())
113 public static Task<FileState> CreateForAsync(string filePath,int blockSize,string algorithm)
116 throw new ArgumentOutOfRangeException("blockSize");
117 if (String.IsNullOrWhiteSpace(algorithm))
118 throw new ArgumentNullException("algorithm");
119 Contract.EndContractBlock();
122 var fileState = new FileState
125 OverlayStatus = FileOverlayStatus.Unversioned,
126 FileStatus = FileStatus.Created,
131 return fileState.UpdateHashesAsync(blockSize,algorithm);
134 public Task<FileState> UpdateHashesAsync(int blockSize,string algorithm)
137 throw new ArgumentOutOfRangeException("blockSize");
138 if (String.IsNullOrWhiteSpace(algorithm))
139 throw new ArgumentNullException("algorithm");
140 Contract.EndContractBlock();
142 //Skip updating the hash for folders
143 if (Directory.Exists(FilePath))
144 return Task.Factory.FromResult(this);
146 var results = Task.Factory.StartNew(() =>
148 var info = new FileInfo(FilePath);
149 return info.CalculateHash(blockSize, algorithm);
152 var state=results.Then(hash =>
155 return Task.Factory.FromResult(this);
162 [ActiveRecord("Tags")]
163 public class FileTag : ActiveRecordLinqBase<FileTag>
166 public int Id { get; set; }
169 public string Name { get; set; }
172 public string Value { get; set; }
174 [BelongsTo("FileStateId")]
175 public FileState FileState { get; set; }
179 /* [ActiveRecord("hashes")]
180 public class FileHash : ActiveRecordLinqBase<FileHash>
183 public int Id { get; set; }
186 public int Order { get; set; }
189 public byte[] Value { get; set; }
191 [BelongsTo("FileStateID")]
192 public FileState FileState { get; set; }