Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / StatusAgent.cs @ bc27bb7e

History | View | Annotate | Download (36 kB)

1
#region
2
/* -----------------------------------------------------------------------
3
 * <copyright file="StatusAgent.cs" company="GRNet">
4
 * 
5
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or
8
 * without modification, are permitted provided that the following
9
 * conditions are met:
10
 *
11
 *   1. Redistributions of source code must retain the above
12
 *      copyright notice, this list of conditions and the following
13
 *      disclaimer.
14
 *
15
 *   2. Redistributions in binary form must reproduce the above
16
 *      copyright notice, this list of conditions and the following
17
 *      disclaimer in the documentation and/or other materials
18
 *      provided with the distribution.
19
 *
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 * The views and conclusions contained in the software and
35
 * documentation are those of the authors and should not be
36
 * interpreted as representing official policies, either expressed
37
 * or implied, of GRNET S.A.
38
 * </copyright>
39
 * -----------------------------------------------------------------------
40
 */
41
#endregion
42
using System;
43
using System.Collections.Generic;
44
using System.ComponentModel.Composition;
45
using System.Data.SQLite;
46
using System.Diagnostics;
47
using System.Diagnostics.Contracts;
48
using System.IO;
49
using System.Linq;
50
using System.Reflection;
51
using System.Security.Cryptography;
52
using System.Text;
53
using System.Threading;
54
using System.Threading.Tasks;
55
using Castle.ActiveRecord;
56
using Castle.ActiveRecord.Framework;
57
using Castle.ActiveRecord.Framework.Config;
58
using NHibernate.ByteCode.Castle;
59
using NHibernate.Cfg;
60
using NHibernate.Cfg.Loquacious;
61
using NHibernate.Dialect;
62
using Pithos.Interfaces;
63
using Pithos.Network;
64
using log4net;
65
using Environment = System.Environment;
66

    
67
namespace Pithos.Core.Agents
68
{
69
    [Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))]
70
    public class StatusAgent:IStatusChecker,IStatusKeeper
71
    {
72
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
73

    
74
        [System.ComponentModel.Composition.Import]
75
        public IPithosSettings Settings { get; set; }
76

    
77
        private Agent<Action> _persistenceAgent;
78

    
79

    
80

    
81
        public StatusAgent()
82
        {            
83
            var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
84

    
85
            _pithosDataPath = Path.Combine(appDataPath , "GRNET\\PITHOS");
86
            if (!Directory.Exists(_pithosDataPath))
87
                Directory.CreateDirectory(_pithosDataPath);
88

    
89
            var dbPath = Path.Combine(_pithosDataPath, "pithos.db");
90

    
91
            MigrateOldDb(dbPath, appDataPath);
92

    
93

    
94
            var source = GetConfiguration(_pithosDataPath);
95
            ActiveRecordStarter.Initialize(source,typeof(FileState),typeof(FileTag));
96
            
97
            ActiveRecordStarter.UpdateSchema();
98

    
99

    
100
            if (!File.Exists(dbPath))
101
                ActiveRecordStarter.CreateSchema();
102

    
103
            CreateTrigger();
104
            
105
        }
106

    
107

    
108
        private static void MigrateOldDb(string dbPath, string appDataPath)
109
        {
110
            if(String.IsNullOrWhiteSpace(dbPath))
111
                throw new ArgumentNullException("dbPath");
112
            if(String.IsNullOrWhiteSpace(appDataPath))
113
                throw new ArgumentNullException("appDataPath");
114
            Contract.EndContractBlock();
115

    
116
            var oldDbPath = Path.Combine(appDataPath, "Pithos", "pithos.db");
117
            var oldDbInfo = new FileInfo(oldDbPath);
118
            if (oldDbInfo.Exists && !File.Exists(dbPath))
119
            {
120
                Log.InfoFormat("Moving database from {0} to {1}",oldDbInfo.FullName,dbPath);
121
                var oldDirectory = oldDbInfo.Directory;
122
                oldDbInfo.MoveTo(dbPath);
123
                
124
                if (Log.IsDebugEnabled)
125
                    Log.DebugFormat("Deleting {0}",oldDirectory.FullName);
126
                
127
                oldDirectory.Delete(true);
128
            }
129
        }
130

    
131
        private void CreateTrigger()
132
        {
133
            using (var connection = GetConnection())
134
            using (var triggerCommand = connection.CreateCommand())
135
            {
136
                var cmdText = new StringBuilder()
137
                    .AppendLine("CREATE TRIGGER IF NOT EXISTS update_last_modified UPDATE ON FileState FOR EACH ROW")
138
                    .AppendLine("BEGIN")
139
                    .AppendLine("UPDATE FileState SET Modified=datetime('now')  WHERE Id=old.Id;")
140
                    .AppendLine("END;")
141
                    .AppendLine("CREATE TRIGGER IF NOT EXISTS insert_last_modified INSERT ON FileState FOR EACH ROW")
142
                    .AppendLine("BEGIN")
143
                    .AppendLine("UPDATE FileState SET Modified=datetime('now')  WHERE Id=new.Id;")
144
                    .AppendLine("END;")
145
                    .ToString();
146
                triggerCommand.CommandText = cmdText;                
147
                triggerCommand.ExecuteNonQuery();
148
            }
149
        }
150

    
151

    
152
        private static InPlaceConfigurationSource GetConfiguration(string pithosDbPath)
153
        {
154
            if (String.IsNullOrWhiteSpace(pithosDbPath))
155
                throw new ArgumentNullException("pithosDbPath");
156
            if (!Path.IsPathRooted(pithosDbPath))
157
                throw new ArgumentException("path must be a rooted path", "pithosDbPath");
158
            Contract.EndContractBlock();
159

    
160
            var properties = new Dictionary<string, string>
161
                                 {
162
                                     {"connection.driver_class", "NHibernate.Driver.SQLite20Driver"},
163
                                     {"dialect", "NHibernate.Dialect.SQLiteDialect"},
164
                                     {"connection.provider", "NHibernate.Connection.DriverConnectionProvider"},
165
                                     {
166
                                         "proxyfactory.factory_class",
167
                                         "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"
168
                                         },
169
                                 };
170

    
171
            var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3;Enlist=N", pithosDbPath);
172
            properties.Add("connection.connection_string", connectionString);
173

    
174
            var source = new InPlaceConfigurationSource();                        
175
            source.Add(typeof (ActiveRecordBase), properties);
176
            source.SetDebugFlag(false);            
177
            return source;
178
        }
179

    
180
        public void StartProcessing(CancellationToken token)
181
        {
182
            _persistenceAgent = Agent<Action>.Start(queue =>
183
            {
184
                Action loop = null;
185
                loop = () =>
186
                {
187
                    var job = queue.Receive();
188
                    job.ContinueWith(t =>
189
                    {
190
                        var action = job.Result;
191
                        try
192
                        {
193
                            action();
194
                        }
195
                        catch (SQLiteException ex)
196
                        {
197
                            Log.ErrorFormat("[ERROR] SQL \n{0}", ex);
198
                        }
199
                        catch (Exception ex)
200
                        {
201
                            Log.ErrorFormat("[ERROR] STATE \n{0}", ex);
202
                        }
203
                        queue.NotifyComplete(action);
204
// ReSharper disable AccessToModifiedClosure
205
                        queue.DoAsync(loop);
206
// ReSharper restore AccessToModifiedClosure
207
                    });
208
                };
209
                loop();
210
            });
211
            
212
        }
213

    
214
       
215

    
216
        public void Stop()
217
        {
218
            _persistenceAgent.Stop();            
219
        }
220
               
221

    
222
        public void ProcessExistingFiles(IEnumerable<FileInfo> existingFiles)
223
        {
224
            if(existingFiles  ==null)
225
                throw new ArgumentNullException("existingFiles");
226
            Contract.EndContractBlock();
227
            
228
            //Find new or matching files with a left join to the stored states
229
            var fileStates = FileState.Queryable;
230
            var currentFiles=from file in existingFiles
231
                      join state in fileStates on file.FullName.ToLower() equals state.FilePath.ToLower() into gs
232
                      from substate in gs.DefaultIfEmpty()
233
                               select new {File = file, State = substate};
234

    
235
            //To get the deleted files we must get the states that have no corresponding
236
            //files. 
237
            //We can't use the File.Exists method inside a query, so we get all file paths from the states
238
            var statePaths = (from state in fileStates
239
                             select new {state.Id, state.FilePath}).ToList();
240
            //and check each one
241
            var missingStates= (from path in statePaths
242
                                where !File.Exists(path.FilePath) && !Directory.Exists(path.FilePath)
243
                               select path.Id).ToList();
244
            //Finally, retrieve the states that correspond to the deleted files            
245
            var deletedFiles = from state in fileStates 
246
                        where missingStates.Contains(state.Id)
247
                        select new { File = default(FileInfo), State = state };
248

    
249
            var pairs = currentFiles.Union(deletedFiles).ToList();
250

    
251
            using (var shortHasher = HashAlgorithm.Create("sha1"))
252
            {
253
                foreach (var pair in pairs)
254
                {
255
                    var fileState = pair.State;
256
                    var file = pair.File;
257
                    if (fileState == null)
258
                    {
259
                        //This is a new file                        
260
                        var createState = FileState.CreateFor(file);
261
                        _persistenceAgent.Post(createState.Create);                        
262
                    }
263
                    else if (file == null)
264
                    {
265
                        //This file was deleted while we were down. We should mark it as deleted
266
                        //We have to go through UpdateStatus here because the state object we are using
267
                        //was created by a different ORM session.
268
                        _persistenceAgent.Post(() => UpdateStatusDirect(fileState.Id, FileStatus.Deleted));
269
                    }
270
                    else
271
                    {
272
                        //This file has a matching state. Need to check for possible changes
273
                        //To check for changes, we use the cheap (in CPU terms) SHA1 algorithm
274
                        //on the entire file.
275
                        
276
                        var hashString = file.ComputeShortHash(shortHasher);                        
277
                        //TODO: Need a way to attach the hashes to the filestate so we don't
278
                        //recalculate them each time a call to calculate has is made
279
                        //We can either store them to the filestate or add them to a 
280
                        //dictionary
281

    
282
                        //If the hashes don't match the file was changed
283
                        if (fileState.ShortHash != hashString)
284
                        {
285
                            _persistenceAgent.Post(() => UpdateStatusDirect(fileState.Id, FileStatus.Modified));
286
                        }
287
                    }
288
                }
289
            }
290
                        
291
         
292
        }
293
        
294

    
295

    
296
        private int UpdateStatusDirect(Guid id, FileStatus status)
297
        {
298
            using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect"))
299
            {
300

    
301
                try
302
                {
303
                    
304
                    using (var connection = GetConnection())
305
                    using (
306
                        var command = new SQLiteCommand("update FileState set FileStatus= :fileStatus where Id = :id  ",
307
                                                        connection))
308
                    {                                                
309
                        command.Parameters.AddWithValue("fileStatus", status);
310

    
311
                        command.Parameters.AddWithValue("id", id);
312
                        
313
                        var affected = command.ExecuteNonQuery();
314
                        
315
                        return affected;
316
                    }
317

    
318
                }
319
                catch (Exception exc)
320
                {
321
                    Log.Error(exc.ToString());
322
                    throw;
323
                }
324
            }
325
        }
326
        
327
        private int UpdateStatusDirect(string path, FileStatus status)
328
        {
329
            using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect"))
330
            {
331

    
332
                try
333
                {
334

    
335
                    
336
                    using (var connection = GetConnection())
337
                    using (
338
                        var command =
339
                            new SQLiteCommand("update FileState set FileStatus= :fileStatus where FilePath = :path COLLATE NOCASE",
340
                                              connection))
341
                    {
342

    
343

    
344
                        command.Parameters.AddWithValue("fileStatus", status);
345

    
346
                        command.Parameters.AddWithValue("path", path);
347
                        
348
                        var affected = command.ExecuteNonQuery();
349
                        return affected;
350
                    }
351
                }
352
                catch (Exception exc)
353
                {
354
                    Log.Error(exc.ToString());
355
                    throw;
356
                }
357
            }
358
        }
359

    
360
        private int UpdateStatusDirect(string absolutePath, FileStatus fileStatus, FileOverlayStatus overlayStatus)
361
        {
362
            using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect"))
363
            {
364

    
365
                try
366
                {
367

    
368
                    
369
                    using (var connection = GetConnection())
370
                    using (
371
                        var command =
372
                            new SQLiteCommand(
373
                                "update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus where FilePath = :path COLLATE NOCASE ",
374
                                connection))
375
                    {
376

    
377
                        command.Parameters.AddWithValue("path", absolutePath);
378
                        command.Parameters.AddWithValue("fileStatus", fileStatus);
379
                        command.Parameters.AddWithValue("overlayStatus", overlayStatus);
380
                        
381
                        var affected = command.ExecuteNonQuery();
382
                        return affected;
383
                    }
384
                }
385
                catch (Exception exc)
386
                {
387
                    Log.Error(exc.ToString());
388
                    throw;
389
                }
390
            }
391
        }
392
        
393

    
394

    
395
        public string BlockHash { get; set; }
396

    
397
        public int BlockSize { get; set; }
398
        public void ChangeRoots(string oldPath, string newPath)
399
        {
400
            if (String.IsNullOrWhiteSpace(oldPath))
401
                throw new ArgumentNullException("oldPath");
402
            if (!Path.IsPathRooted(oldPath))
403
                throw new ArgumentException("oldPath must be an absolute path", "oldPath");
404
            if (string.IsNullOrWhiteSpace(newPath))
405
                throw new ArgumentNullException("newPath");
406
            if (!Path.IsPathRooted(newPath))
407
                throw new ArgumentException("newPath must be an absolute path", "newPath");
408
            Contract.EndContractBlock();
409

    
410
            FileState.ChangeRootPath(oldPath,newPath);
411

    
412
        }
413

    
414

    
415

    
416
        private readonly string _pithosDataPath;
417

    
418

    
419
        public FileState GetStateByFilePath(string path)
420
        {
421
            if (String.IsNullOrWhiteSpace(path))
422
                throw new ArgumentNullException("path");
423
            if (!Path.IsPathRooted(path))
424
                throw new ArgumentException("The path must be rooted", "path");
425
            Contract.EndContractBlock();
426

    
427
            try
428
            {
429
                
430
                using (var connection = GetConnection())
431
                using (var command = new SQLiteCommand("select Id, FilePath, OverlayStatus,FileStatus ,Checksum ,ShortHash,Version    ,VersionTimeStamp,IsShared   ,SharedBy   ,ShareWrite  from FileState where FilePath=:path COLLATE NOCASE", connection))
432
                {
433
                    
434
                    command.Parameters.AddWithValue("path", path);
435
                    
436
                    using (var reader = command.ExecuteReader())
437
                    {
438
                        if (reader.Read())
439
                        {
440
                            //var values = new object[reader.FieldCount];
441
                            //reader.GetValues(values);
442
                            var state = new FileState
443
                                            {
444
                                                Id = reader.GetGuid(0),
445
                                                FilePath = reader.IsDBNull(1)?"":reader.GetString(1),
446
                                                OverlayStatus =reader.IsDBNull(2)?FileOverlayStatus.Unversioned: (FileOverlayStatus) reader.GetInt64(2),
447
                                                FileStatus = reader.IsDBNull(3)?FileStatus.Missing:(FileStatus) reader.GetInt64(3),
448
                                                Checksum = reader.IsDBNull(4)?"":reader.GetString(4),
449
                                                ShortHash= reader.IsDBNull(5)?"":reader.GetString(5),
450
                                                Version = reader.IsDBNull(6)?default(long):reader.GetInt64(6),
451
                                                VersionTimeStamp = reader.IsDBNull(7)?default(DateTime):reader.GetDateTime(7),
452
                                                IsShared = !reader.IsDBNull(8) && reader.GetBoolean(8),
453
                                                SharedBy = reader.IsDBNull(9)?"":reader.GetString(9),
454
                                                ShareWrite = !reader.IsDBNull(10) && reader.GetBoolean(10)
455
                                            };
456
/*
457
                            var state = new FileState
458
                                            {
459
                                                Id = (Guid) values[0],
460
                                                FilePath = (string) values[1],
461
                                                OverlayStatus = (FileOverlayStatus) (long)values[2],
462
                                                FileStatus = (FileStatus) (long)values[3],
463
                                                Checksum = (string) values[4],
464
                                                Version = (long?) values[5],
465
                                                VersionTimeStamp = (DateTime?) values[6],
466
                                                IsShared = (long)values[7] == 1,
467
                                                SharedBy = (string) values[8],
468
                                                ShareWrite = (long)values[9] == 1
469
                                            };
470
*/
471
                            return state;
472
                        }
473
                        else
474
                        {
475
                            return null;
476
                        }
477

    
478
                    }                    
479
                }
480
            }
481
            catch (Exception exc)
482
            {
483
                Log.ErrorFormat(exc.ToString());
484
                throw;
485
            }            
486
        }
487

    
488
        public FileOverlayStatus GetFileOverlayStatus(string path)
489
        {
490
            if (String.IsNullOrWhiteSpace(path))
491
                throw new ArgumentNullException("path");
492
            if (!Path.IsPathRooted(path))
493
                throw new ArgumentException("The path must be rooted", "path");
494
            Contract.EndContractBlock();
495

    
496
            try
497
            {
498
                
499
                using (var connection = GetConnection())
500
                using (var command = new SQLiteCommand("select OverlayStatus from FileState where FilePath=:path  COLLATE NOCASE", connection))
501
                {
502
                    
503
                    command.Parameters.AddWithValue("path", path);
504
                    
505
                    var s = command.ExecuteScalar();
506
                    return (FileOverlayStatus) Convert.ToInt32(s);
507
                }
508
            }
509
            catch (Exception exc)
510
            {
511
                Log.ErrorFormat(exc.ToString());
512
                return FileOverlayStatus.Unversioned;
513
            }
514
        }
515

    
516
        private string GetConnectionString()
517
        {
518
            var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3;Enlist=N;Pooling=True", _pithosDataPath);
519
            return connectionString;
520
        }
521

    
522
        private SQLiteConnection GetConnection()
523
        {
524
            var connectionString = GetConnectionString();
525
            var connection = new SQLiteConnection(connectionString);
526
            connection.Open();
527
            using(var cmd =connection.CreateCommand())
528
            {
529
                cmd.CommandText = "PRAGMA journal_mode=WAL";
530
                cmd.ExecuteNonQuery();
531
            }
532
            return connection;
533
        }
534

    
535
       /* public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus)
536
        {
537
            if (String.IsNullOrWhiteSpace(path))
538
                throw new ArgumentNullException("path");
539
            if (!Path.IsPathRooted(path))
540
                throw new ArgumentException("The path must be rooted","path");
541
            Contract.EndContractBlock();
542

    
543
            _persistenceAgent.Post(() => FileState.StoreOverlayStatus(path,overlayStatus));
544
        }*/
545

    
546
        public Task SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus, string shortHash = null)
547
        {
548
            if (String.IsNullOrWhiteSpace(path))
549
                throw new ArgumentNullException("path");
550
            if (!Path.IsPathRooted(path))
551
                throw new ArgumentException("The path must be rooted","path");
552
            Contract.EndContractBlock();
553

    
554
            return _persistenceAgent.PostAndAwait(() => FileState.StoreOverlayStatus(path,overlayStatus,shortHash));
555
        }
556

    
557
       /* public void RenameFileOverlayStatus(string oldPath, string newPath)
558
        {
559
            if (String.IsNullOrWhiteSpace(oldPath))
560
                throw new ArgumentNullException("oldPath");
561
            if (!Path.IsPathRooted(oldPath))
562
                throw new ArgumentException("The oldPath must be rooted", "oldPath");
563
            if (String.IsNullOrWhiteSpace(newPath))
564
                throw new ArgumentNullException("newPath");
565
            if (!Path.IsPathRooted(newPath))
566
                throw new ArgumentException("The newPath must be rooted", "newPath");
567
            Contract.EndContractBlock();
568

    
569
            _persistenceAgent.Post(() =>FileState.RenameState(oldPath, newPath));
570
        }*/
571

    
572
        public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
573
        {
574
            if (String.IsNullOrWhiteSpace(path))
575
                throw new ArgumentNullException("path");
576
            if (!Path.IsPathRooted(path))
577
                throw new ArgumentException("The path must be rooted", "path");
578
            Contract.EndContractBlock();
579

    
580
            Debug.Assert(!path.Contains(FolderConstants.CacheFolder));
581
            Debug.Assert(!path.EndsWith(".ignore"));
582

    
583
            _persistenceAgent.Post(() => UpdateStatusDirect(path, fileStatus, overlayStatus));
584
        }
585

    
586
/*
587
        public void StoreInfo(string path,ObjectInfo objectInfo)
588
        {
589
            if (String.IsNullOrWhiteSpace(path))
590
                throw new ArgumentNullException("path");
591
            if (!Path.IsPathRooted(path))
592
                throw new ArgumentException("The path must be rooted", "path");            
593
            if (objectInfo == null)
594
                throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
595
            Contract.EndContractBlock();
596

    
597
            _persistenceAgent.Post(() =>
598
            {
599
                var filePath = path.ToLower();
600
                //Load the existing files state and set its properties in one session            
601
                using (new SessionScope())
602
                {
603
                    //Forgetting to use a sessionscope results in two sessions being created, one by 
604
                    //FirstOrDefault and one by Save()
605
                    var state =FileState.FindByFilePath(filePath);
606
                    
607
                    //Create a new empty state object if this is a new file
608
                    state = state ?? new FileState();
609

    
610
                    state.FilePath = filePath;
611
                    state.Checksum = objectInfo.Hash;
612
                    state.Version = objectInfo.Version;
613
                    state.VersionTimeStamp = objectInfo.VersionTimestamp;
614

    
615
                    state.FileStatus = FileStatus.Unchanged;
616
                    state.OverlayStatus = FileOverlayStatus.Normal;
617
                    
618
                  
619
                    //Do the save
620
                    state.Save();
621
                }
622
            });
623

    
624
        }
625
*/
626
        
627
        public void StoreInfo(string path, ObjectInfo objectInfo)
628
        {
629
            if (String.IsNullOrWhiteSpace(path))
630
                throw new ArgumentNullException("path");
631
            if (!Path.IsPathRooted(path))
632
                throw new ArgumentException("The path must be rooted", "path");
633
            if (objectInfo == null)
634
                throw new ArgumentNullException("objectInfo", "objectInfo can't be empty");
635
            Contract.EndContractBlock();
636

    
637
            _persistenceAgent.Post(() => StoreInfoDirect(path, objectInfo));
638

    
639
        }
640

    
641
        private void StoreInfoDirect(string path, ObjectInfo objectInfo)
642
        {
643
            try
644
            {
645
                
646
                using (var connection = GetConnection())
647
                using (var command = new SQLiteCommand(connection))
648
                {
649
                    if (StateExists(path, connection))
650
                        command.CommandText =
651
                            "update FileState set FileStatus= :fileStatus where FilePath = :path  COLLATE NOCASE ";
652
                    else
653
                    {
654
                        command.CommandText =
655
                            "INSERT INTO FileState (Id,FilePath,Checksum,Version,VersionTimeStamp,ShortHash,FileStatus,OverlayStatus) VALUES (:id,:path,:checksum,:version,:versionTimeStamp,:shortHash,:fileStatus,:overlayStatus)";
656
                        command.Parameters.AddWithValue("id", Guid.NewGuid());
657
                    }
658

    
659
                    command.Parameters.AddWithValue("path", path);
660
                    command.Parameters.AddWithValue("checksum", objectInfo.Hash);
661
                    command.Parameters.AddWithValue("shortHash", "");
662
                    command.Parameters.AddWithValue("version", objectInfo.Version);
663
                    command.Parameters.AddWithValue("versionTimeStamp",
664
                                                    objectInfo.VersionTimestamp);
665
                    command.Parameters.AddWithValue("fileStatus", FileStatus.Unchanged);
666
                    command.Parameters.AddWithValue("overlayStatus",
667
                                                    FileOverlayStatus.Normal);
668

    
669
                    var affected = command.ExecuteNonQuery();
670
                    return;
671
                }
672
            }
673
            catch (Exception exc)
674
            {
675
                Log.Error(exc.ToString());
676
                throw;
677
            }
678
        }
679

    
680
        private bool StateExists(string filePath,SQLiteConnection connection)
681
        {
682
            using (var command = new SQLiteCommand("Select count(*) from FileState where FilePath=:path  COLLATE NOCASE", connection))
683
            {
684
                command.Parameters.AddWithValue("path", filePath);
685
                var result = command.ExecuteScalar();
686
                return ((long)result >= 1);
687
            }
688

    
689
        }
690

    
691
        public void SetFileStatus(string path, FileStatus status)
692
        {
693
            if (String.IsNullOrWhiteSpace(path))
694
                throw new ArgumentNullException("path");
695
            if (!Path.IsPathRooted(path))
696
                throw new ArgumentException("The path must be rooted", "path");
697
            Contract.EndContractBlock();
698
            
699
            _persistenceAgent.Post(() => UpdateStatusDirect(path, status));
700
        }
701

    
702
        public FileStatus GetFileStatus(string path)
703
        {
704
            if (String.IsNullOrWhiteSpace(path))
705
                throw new ArgumentNullException("path");
706
            if (!Path.IsPathRooted(path))
707
                throw new ArgumentException("The path must be rooted", "path");
708
            Contract.EndContractBlock();
709

    
710
            
711
            using (var connection = GetConnection())
712
            {
713
                var command = new SQLiteCommand("select FileStatus from FileState where FilePath=:path  COLLATE NOCASE", connection);
714
                command.Parameters.AddWithValue("path", path);
715
                
716
                var statusValue = command.ExecuteScalar();
717
                if (statusValue==null)
718
                    return FileStatus.Missing;
719
                return (FileStatus)Convert.ToInt32(statusValue);
720
            }
721
        }
722

    
723
        /// <summary>
724
        /// Deletes the status of the specified file
725
        /// </summary>
726
        /// <param name="path"></param>
727
        public void ClearFileStatus(string path)
728
        {
729
            if (String.IsNullOrWhiteSpace(path))
730
                throw new ArgumentNullException("path");
731
            if (!Path.IsPathRooted(path))
732
                throw new ArgumentException("The path must be rooted", "path");
733
            Contract.EndContractBlock();
734

    
735
            _persistenceAgent.Post(() => DeleteDirect(path));   
736
        }
737

    
738
        /// <summary>
739
        /// Deletes the status of the specified folder and all its contents
740
        /// </summary>
741
        /// <param name="path"></param>
742
        public void ClearFolderStatus(string path)
743
        {
744
            if (String.IsNullOrWhiteSpace(path))
745
                throw new ArgumentNullException("path");
746
            if (!Path.IsPathRooted(path))
747
                throw new ArgumentException("The path must be rooted", "path");
748
            Contract.EndContractBlock();
749

    
750
            _persistenceAgent.Post(() => DeleteFolderDirect(path));   
751
        }
752

    
753
        public IEnumerable<FileState> GetChildren(FileState fileState)
754
        {
755
            if (fileState == null)
756
                throw new ArgumentNullException("fileState");
757
            Contract.EndContractBlock();
758

    
759
            var children = from state in FileState.Queryable
760
                           where state.FilePath.StartsWith(fileState.FilePath + "\\")
761
                           select state;
762
            return children;
763
        }
764

    
765
        public void EnsureFileState(string path)
766
        {
767
            var existingState = GetStateByFilePath(path);
768
            if (existingState != null)
769
                return;
770
            var fileInfo = FileInfoExtensions.FromPath(path);
771
            using (new SessionScope())
772
            {
773
                var newState = FileState.CreateFor(fileInfo);
774
                newState.FileStatus=FileStatus.Missing;
775
                _persistenceAgent.PostAndAwait(newState.CreateAndFlush).Wait();
776
            }
777

    
778
        }
779

    
780
        private int DeleteDirect(string filePath)
781
        {
782
            using (log4net.ThreadContext.Stacks["StatusAgent"].Push("DeleteDirect"))
783
            {
784

    
785
                try
786
                {
787

    
788
                    
789
                    using (var connection = GetConnection())
790
                    {
791
                        var command = new SQLiteCommand("delete from FileState where FilePath = :path  COLLATE NOCASE",
792
                                                        connection);
793

    
794
                        command.Parameters.AddWithValue("path", filePath);
795
                        
796
                        var affected = command.ExecuteNonQuery();
797
                        return affected;
798
                    }
799
                }
800
                catch (Exception exc)
801
                {
802
                    Log.Error(exc.ToString());
803
                    throw;
804
                }
805
            }
806
        }
807

    
808
        private int DeleteFolderDirect(string filePath)
809
        {
810
            using (log4net.ThreadContext.Stacks["StatusAgent"].Push("DeleteDirect"))
811
            {
812

    
813
                try
814
                {
815

    
816
                    
817
                    using (var connection = GetConnection())
818
                    {
819
                        var command = new SQLiteCommand(@"delete from FileState where FilePath = :path or FilePath like :path || '\%'  COLLATE NOCASE",
820
                                                        connection);
821

    
822
                        command.Parameters.AddWithValue("path", filePath);
823
                        
824
                        var affected = command.ExecuteNonQuery();
825
                        return affected;
826
                    }
827
                }
828
                catch (Exception exc)
829
                {
830
                    Log.Error(exc.ToString());
831
                    throw;
832
                }
833
            }
834
        }
835

    
836
        public void UpdateFileChecksum(string path, string shortHash, string checksum)
837
        {
838
            if (String.IsNullOrWhiteSpace(path))
839
                throw new ArgumentNullException("path");
840
            if (!Path.IsPathRooted(path))
841
                throw new ArgumentException("The path must be rooted", "path");            
842
            Contract.EndContractBlock();
843

    
844
            _persistenceAgent.Post(() => FileState.UpdateChecksum(path, shortHash,checksum));
845
        }
846

    
847

    
848
        public void CleanupOrphanStates()
849
        {
850
            //Orphan states are those that do not correspond to an account, ie. their paths
851
            //do not start with the root path of any registered account
852

    
853
            var roots=(from account in Settings.Accounts
854
                      select account.RootPath).ToList();
855
            
856
            var allStates = from state in FileState.Queryable
857
                select state.FilePath;
858

    
859
            foreach (var statePath in allStates)
860
            {
861
                if (!roots.Any(root=>statePath.StartsWith(root,StringComparison.InvariantCultureIgnoreCase)))
862
                    this.DeleteDirect(statePath);
863
            }
864
        }
865

    
866
        public void CleanupStaleStates(AccountInfo accountInfo, List<ObjectInfo> objectInfos)
867
        {
868
            if (accountInfo == null)
869
                throw new ArgumentNullException("accountInfo");
870
            if (objectInfos == null)
871
                throw new ArgumentNullException("objectInfos");
872
            Contract.EndContractBlock();
873
            
874

    
875

    
876
            //Stale states are those that have no corresponding local or server file
877

    
878

    
879
            var agent=FileAgent.GetFileAgent(accountInfo);
880

    
881
            var localFiles=agent.EnumerateFiles();
882
            var localSet = new HashSet<string>(localFiles);
883

    
884
            var serverFiles = from info in objectInfos
885
                              select Path.Combine(accountInfo.AccountPath,info.RelativeUrlToFilePath(accountInfo.UserName));
886
            var serverSet = new HashSet<string>(serverFiles);
887

    
888
            var allStates = from state in FileState.Queryable
889
                            where state.FilePath.StartsWith(agent.RootPath)
890
                            select state.FilePath;
891
            var stateSet = new HashSet<string>(allStates);
892
            stateSet.ExceptWith(serverSet);
893
            stateSet.ExceptWith(localSet);
894

    
895
            foreach (var remainder in stateSet)
896
            {
897
                DeleteDirect(remainder);
898
            }
899

    
900
            
901
        }
902
    }
903

    
904
   
905
}