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 NHibernate.Engine;
13 using Pithos.Interfaces;
19 using System.Collections.Generic;
24 /// TODO: Update summary.
27 public class FileState:ActiveRecordLinqBase<FileState>
29 private string _filePath;
30 private IList<FileTag> _tags=new List<FileTag>();
32 [PrimaryKey(PrimaryKeyType.Guid)]
33 public Guid Id { get; set; }
35 [Property(Unique=true,UniqueKey="IX_FileState_FilePath")]
36 public string FilePath
38 get { return _filePath; }
39 set { _filePath = value.ToLower(); }
43 public FileOverlayStatus OverlayStatus { get; set; }
46 public FileStatus FileStatus { get; set; }
49 public string Checksum { get; set; }
52 public string TopHash { get; set; }
55 public long? Version { get; set; }
58 public DateTime? VersionTimeStamp { get; set; }
61 [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true,Inverse=true)]
62 public IList<FileTag> Tags
68 // [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true)]
69 // public IList<FileHash> Hashes { get; set; }
72 // public byte[] HashmapHash { get; set; }
74 public static FileState FindByFilePath(string absolutePath)
76 return Queryable.FirstOrDefault(s => s.FilePath == absolutePath.ToLower());
79 public static Task<FileState> CreateForAsync(string filePath,int blockSize,string algorithm)
82 throw new ArgumentOutOfRangeException("blockSize");
83 if (String.IsNullOrWhiteSpace(algorithm))
84 throw new ArgumentNullException("algorithm");
85 Contract.EndContractBlock();
88 var fileState = new FileState
91 OverlayStatus = FileOverlayStatus.Unversioned,
92 FileStatus = FileStatus.Created,
97 return fileState.UpdateHashesAsync(blockSize,algorithm);
100 public Task<FileState> UpdateHashesAsync(int blockSize,string algorithm)
103 throw new ArgumentOutOfRangeException("blockSize");
104 if (String.IsNullOrWhiteSpace(algorithm))
105 throw new ArgumentNullException("algorithm");
106 Contract.EndContractBlock();
108 //Skip updating the hash for folders
109 if (Directory.Exists(FilePath))
110 return Task.Factory.StartNew(() => this);
112 var results=Task.Factory.TrackedSequence(
113 () => Task.Factory.StartNew(() => Signature.CalculateMD5(FilePath)),
114 () => Signature.CalculateTreeHashAsync(FilePath, blockSize, algorithm)
117 results.ContinueWith(t =>
119 var hashes = t.Result;
120 Checksum = (hashes[0] as Task<string>).Result;
121 TopHash = (hashes[0] as Task<TreeHash>).Result.TopHash.ToHashString();
124 return results.ContinueWith(t => this);
128 [ActiveRecord("Tags")]
129 public class FileTag : ActiveRecordLinqBase<FileTag>
132 public int Id { get; set; }
135 public string Name { get; set; }
138 public string Value { get; set; }
140 [BelongsTo("FileStateId")]
141 public FileState FileState { get; set; }
145 /* [ActiveRecord("hashes")]
146 public class FileHash : ActiveRecordLinqBase<FileHash>
149 public int Id { get; set; }
152 public int Order { get; set; }
155 public byte[] Value { get; set; }
157 [BelongsTo("FileStateID")]
158 public FileState FileState { get; set; }