Revision b5061ac8 trunk/Pithos.Core/StatusChecker.cs

b/trunk/Pithos.Core/StatusChecker.cs
1 1
using System;
2
using System.Collections.Concurrent;
2 3
using System.Collections.Generic;
3 4
using System.ComponentModel.Composition;
4 5
using System.Diagnostics;
5 6
using System.Diagnostics.Contracts;
6 7
using System.IO;
8
using System.Linq;
9
using System.Security.Cryptography;
10
using System.Text;
11
using System.Threading.Tasks;
7 12
using Castle.ActiveRecord;
13
using Castle.ActiveRecord.Framework;
8 14
using Castle.ActiveRecord.Framework.Config;
9 15
using Pithos.Interfaces;
10 16

  
......
32 38
            try
33 39
            {
34 40
                var state = FileState.TryFind(path);
35
                return state == null ? FileOverlayStatus.NA : state.OverlayStatus;
41
                return state == null ? FileOverlayStatus.Unversioned : state.OverlayStatus;
36 42
            }
37 43
            catch (Exception exc)
38 44
            {
39 45
                Trace.TraceError(exc.ToString());
40
                return FileOverlayStatus.NA;
46
                return FileOverlayStatus.Unversioned;
41 47
            }
42 48
        }
43 49

  
50
        public IEnumerable<string> StoreUnversionedFiles(ParallelQuery<string> paths)
51
        {            
52
            var existingFiles =  FileState.FindAll().Select(state=>state.FilePath);
53

  
54
            var newFiles = (from file in paths.Except(existingFiles.AsParallel())
55
                            select new FileState
56
                                       {
57
                                           FilePath = file,
58
                                           OverlayStatus = FileOverlayStatus.Unversioned,
59
                                           FileStatus=FileStatus.Created,     
60
                                           Checksum=Signature.CalculateHash(file)
61
                                       }
62
                           ).AsParallel();
63
            
64
            var files=new ConcurrentBag<string>();
65
            newFiles.ForAll(state=>
66
                                {
67
                                    state.Save();
68
                                    files.Add(state.FilePath);
69
                                });
70
            
71
            return files.GetConsumingEnumerable();
72

  
73
        }
74

  
75
/*
76
        private static Task<string> CalculateHashAsync(string path)
77
        {
78

  
79
            string hash;
80
            using (var hasher = MD5.Create())
81
            {
82
                return FileAsync.ReadAllBytes(path)
83
                    .ContinueWith(t => hasher.ComputeHash(t.Result))
84
                    .ContinueWith(t =>
85
                                      {
86
                                          //var hashBuilder = new StringBuilder();
87
                                          return (from byte b in t.Result.AsParallel()
88
                                                  select b.ToString("x2").ToLower()).Aggregate((s1, s2) => s1 + s2);                                         
89
                                      });
90
            }
91
            /*using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true))
92
            {
93
                
94
                stream.ReadAllBytes()
95
                    .ContinueWith(result => hasher.ComputeHash(result.Result))
96
                    .;
97
                var hashBytes = hasher.ComputeHash(stream);
98
                var hashBuilder = new StringBuilder();
99
                foreach (byte b in hasher.ComputeHash(stream))
100
                    hashBuilder.Append(b.ToString("x2").ToLower());
101
                hash = hashBuilder.ToString();
102

  
103
            }
104
            return hash;#1#
105
        }
106
*/
107

  
44 108

  
45 109
        private PithosStatus _pithosStatus=PithosStatus.InSynch;
46 110
        public void SetPithosStatus(PithosStatus status)

Also available in: Unified diff