Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / FileState.cs @ 0c02aa65

History | View | Annotate | Download (3.2 kB)

1
// -----------------------------------------------------------------------
2
// <copyright file="FileState.cs" company="Microsoft">
3
// TODO: Update copyright text.
4
// </copyright>
5
// -----------------------------------------------------------------------
6

    
7
using System.Threading.Tasks;
8
using Castle.ActiveRecord;
9
using Castle.ActiveRecord.Framework;
10
using Pithos.Interfaces;
11

    
12
namespace Pithos.Core
13
{
14
    using System;
15
    using System.Collections.Generic;
16
    using System.Linq;
17
    using System.Text;
18

    
19
    /// <summary>
20
    /// TODO: Update summary.
21
    /// </summary>
22
    [ActiveRecord]
23
    public class FileState:ActiveRecordLinqBase<FileState>
24
    {
25
        private string _filePath;
26
        private IList<FileTag> _tags=new List<FileTag>();
27

    
28

    
29
        [PrimaryKey]
30
        public string FilePath
31
        {
32
            get { return _filePath; }
33
            set { _filePath = value.ToLower(); }
34
        }
35

    
36
        [Property]
37
        public FileOverlayStatus OverlayStatus { get; set; }
38

    
39
        [Property]
40
        public FileStatus FileStatus { get; set; }
41

    
42
        [Property]
43
        public string Checksum { get; set; }
44

    
45
        [HasMany(Cascade=ManyRelationCascadeEnum.AllDeleteOrphan,Lazy=true)]
46
        public IList<FileTag> Tags
47
        {
48
            get { return _tags; }   
49
            set { _tags=value;}
50
        }
51

    
52
        [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true)]
53
        public IList<FileHash> Hashes { get; set; }
54

    
55
        public FileState()
56
        {
57
            
58
        }
59

    
60
        private static int BLOCK_SIZE = 4194304;
61

    
62
        public static Task<FileState> CreateForAsync(string filePath)
63
        {
64
            var fileState = new FileState
65
                                {
66
                                    FilePath = filePath, 
67
                                    OverlayStatus = FileOverlayStatus.Unversioned, 
68
                                    FileStatus = FileStatus.Created
69
                                };
70
            
71
            
72
            return Task.Factory.StartNew(()=> {
73
                    fileState.Checksum = Signature.CalculateHash(fileState.FilePath);
74
                })
75
                .ContinueWith(_=> Signature.CalculateBlockHashesAsync(filePath, BLOCK_SIZE)).Unwrap()
76
                .ContinueWith(t =>
77
                {
78
                    fileState.Hashes = t.Result;
79
                }).ContinueWith(t =>
80
                {
81
                    fileState.HashmapHash = Signature.CalculateHashmapHash(fileState.Hashes);
82
                }).ContinueWith(t=> fileState);
83
            //return fileState;
84
        }
85

    
86
        public byte[] HashmapHash { get; set; }
87
    }
88

    
89
    [ActiveRecord]
90
    public class FileTag : ActiveRecordLinqBase<FileTag>
91
    {
92
        [PrimaryKey]
93
        public string FilePath { get; set; }
94

    
95
        [Property]
96
        public string Value { get; set; }
97

    
98
        [BelongsTo("FilePath")]
99
        public FileState FileState { get; set; }
100

    
101
    }
102
    
103
    [ActiveRecord]
104
    public class FileHash : ActiveRecordLinqBase<FileHash>
105
    {
106
        [PrimaryKey]
107
        public string FilePath { get; set; }
108

    
109
        [Property]
110
        public byte[] Value { get; set; }        
111

    
112
        [BelongsTo("FilePath")]
113
        public FileState FileState { get; set; }
114

    
115
    }
116

    
117

    
118
}