From 0050438ed698afdbb252e3f6784457537ece3643 Mon Sep 17 00:00:00 2001 From: Panagiotis Kanavos Date: Mon, 22 Aug 2011 19:43:44 +0300 Subject: [PATCH] Added Tags retrieval --- trunk/Pithos.Core/Pithos.Core.csproj | 3 + trunk/Pithos.Core/StatusChecker.cs | 173 ------------------------------ trunk/Pithos.Core/StatusKeeper.cs | 24 ++++- trunk/Pithos.Interfaces/ICloudClient.cs | 7 ++ trunk/Pithos.Network/CloudFilesClient.cs | 9 +- 5 files changed, 38 insertions(+), 178 deletions(-) delete mode 100644 trunk/Pithos.Core/StatusChecker.cs diff --git a/trunk/Pithos.Core/Pithos.Core.csproj b/trunk/Pithos.Core/Pithos.Core.csproj index 8b73aa7..88acfe1 100644 --- a/trunk/Pithos.Core/Pithos.Core.csproj +++ b/trunk/Pithos.Core/Pithos.Core.csproj @@ -97,6 +97,9 @@ False ..\Libraries\Microsoft.WindowsAPICodePack.dll + + ..\Libraries\Microsoft.WindowsAPICodePack.Shell.dll + ..\Libraries\NHibernate.dll diff --git a/trunk/Pithos.Core/StatusChecker.cs b/trunk/Pithos.Core/StatusChecker.cs deleted file mode 100644 index 5657d52..0000000 --- a/trunk/Pithos.Core/StatusChecker.cs +++ /dev/null @@ -1,173 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.ComponentModel.Composition; -using System.Diagnostics; -using System.Diagnostics.Contracts; -using System.IO; -using System.Linq; -using System.Security.Cryptography; -using System.Text; -using System.Threading.Tasks; -using Castle.ActiveRecord; -using Castle.ActiveRecord.Framework; -using Castle.ActiveRecord.Framework.Config; -using Pithos.Interfaces; - -namespace Pithos.Core -{ - [Export(typeof(IStatusChecker)),Export(typeof(IStatusKeeper))] - public class StatusChecker:IStatusChecker,IStatusKeeper - { - [System.ComponentModel.Composition.Import] - public IPithosSettings Settings { get; set; } - - - public StatusChecker() - { - var source = new XmlConfigurationSource("DbConfig.xml"); - ActiveRecordStarter.Initialize(source,typeof(FileState)); - - if (!File.Exists("pithos.db")) - ActiveRecordStarter.CreateSchema(); - - } - - public FileOverlayStatus GetFileOverlayStatus(string path) - { - try - { - var state = FileState.TryFind(path); - return state == null ? FileOverlayStatus.Unversioned : state.OverlayStatus; - } - catch (Exception exc) - { - Trace.TraceError(exc.ToString()); - return FileOverlayStatus.Unversioned; - } - } - - public IEnumerable StoreUnversionedFiles(ParallelQuery paths) - { - var existingFiles = FileState.FindAll().Select(state=>state.FilePath); - - var newFiles = (from file in paths.Except(existingFiles.AsParallel()) - select new FileState - { - FilePath = file, - OverlayStatus = FileOverlayStatus.Unversioned, - FileStatus=FileStatus.Created, - Checksum=Signature.CalculateHash(file) - } - ).AsParallel(); - - var files=new ConcurrentBag(); - newFiles.ForAll(state=> - { - state.Save(); - files.Add(state.FilePath); - }); - - return files.GetConsumingEnumerable(); - - } - -/* - private static Task CalculateHashAsync(string path) - { - - string hash; - using (var hasher = MD5.Create()) - { - return FileAsync.ReadAllBytes(path) - .ContinueWith(t => hasher.ComputeHash(t.Result)) - .ContinueWith(t => - { - //var hashBuilder = new StringBuilder(); - return (from byte b in t.Result.AsParallel() - select b.ToString("x2").ToLower()).Aggregate((s1, s2) => s1 + s2); - }); - } - /*using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true)) - { - - stream.ReadAllBytes() - .ContinueWith(result => hasher.ComputeHash(result.Result)) - .; - var hashBytes = hasher.ComputeHash(stream); - var hashBuilder = new StringBuilder(); - foreach (byte b in hasher.ComputeHash(stream)) - hashBuilder.Append(b.ToString("x2").ToLower()); - hash = hashBuilder.ToString(); - - } - return hash;#1# - } -*/ - - - private PithosStatus _pithosStatus=PithosStatus.InSynch; - public void SetPithosStatus(PithosStatus status) - { - _pithosStatus = status; - } - - public PithosStatus GetPithosStatus() - { - return _pithosStatus; - } - - public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus) - { - var state = FileState.TryFind(path); - if (state != null) - { - state.OverlayStatus = overlayStatus; - state.Update(); - } - else - { - state=new FileState{FilePath=path,OverlayStatus=overlayStatus}; - state.Save(); - } - } - - public void RemoveFileOverlayStatus(string path) - { - FileState.DeleteAll(new[] {path}); - } - - public void RenameFileOverlayStatus(string oldPath, string newPath) - { - var state = FileState.Find(oldPath); - //TODO: This will cause problems if path is used as a key in relationships - state.FilePath = newPath; - state.Update(); - } - - public void SetFileStatus(string path, FileStatus status) - { - var state = FileState.Find(path); - state.FileStatus = status; - } - - public FileStatus GetFileStatus(string path) - { - var state = FileState.TryFind(path); - return (state==null)?FileStatus.Missing:state.FileStatus ; - } - - public void ClearFileStatus(string path) - { - //TODO:SHOULDN'T need both clear file status and remove overlay status - FileState.DeleteAll(new[] { path }); - } - - public void UpdateFileChecksum(string path, string checksum) - { - var state = FileState.Find(path); - state.Checksum = checksum; - state.Update(); - } - } -} diff --git a/trunk/Pithos.Core/StatusKeeper.cs b/trunk/Pithos.Core/StatusKeeper.cs index 29548bc..aa68980 100644 --- a/trunk/Pithos.Core/StatusKeeper.cs +++ b/trunk/Pithos.Core/StatusKeeper.cs @@ -173,7 +173,12 @@ namespace Pithos.Core private static void InnerRenameFileOverlayStatus(string oldPath, string newPath) { - var state = FileState.Find(oldPath); + var state = FileState.TryFind(oldPath); + if (state == null) + { + Trace.TraceWarning("[NOFILE] Unable to set status for {0}.", oldPath); + return; + } //NOTE: This will cause problems if path is used as a key in relationships state.FilePath = newPath; state.Update(); @@ -187,7 +192,12 @@ namespace Pithos.Core private static void InnerSetFileStatus(string path, FileStatus status) { - var state = FileState.Find(path); + var state = FileState.TryFind(path.ToLower()); + if (state == null) + { + Trace.TraceWarning("[NOFILE] Unable to set status for {0}.", path); + return; + } state.FileStatus = status; } @@ -200,12 +210,18 @@ namespace Pithos.Core public void ClearFileStatus(string path) { //TODO:SHOULDN'T need both clear file status and remove overlay status - FileState.DeleteAll(new[] { path }); + _statusUpdateQueue.Add(()=> + FileState.DeleteAll(new[] { path.ToLower() })); } public void UpdateFileChecksum(string path, string checksum) { - var state = FileState.Find(path); + var state = FileState.TryFind(path); + if (state == null) + { + Trace.TraceWarning("[NOFILE] Unable to set checkesum for {0}.",path); + return; + } state.Checksum = checksum; state.Update(); } diff --git a/trunk/Pithos.Interfaces/ICloudClient.cs b/trunk/Pithos.Interfaces/ICloudClient.cs index 7b6b5bf..dd293cd 100644 --- a/trunk/Pithos.Interfaces/ICloudClient.cs +++ b/trunk/Pithos.Interfaces/ICloudClient.cs @@ -211,6 +211,13 @@ namespace Pithos.Interfaces public string Content_Type { get; set; } public DateTime Last_Modified { get; set; } + private Dictionary _tags=new Dictionary(); + public Dictionary Tags + { + get { return _tags; } + set { _tags = value; } + } + public static ObjectInfo Empty=new ObjectInfo {Name=String.Empty,Hash=String.Empty,Bytes=0,Content_Type=String.Empty,Last_Modified=DateTime.MinValue}; } } diff --git a/trunk/Pithos.Network/CloudFilesClient.cs b/trunk/Pithos.Network/CloudFilesClient.cs index b24937f..032b2c6 100644 --- a/trunk/Pithos.Network/CloudFilesClient.cs +++ b/trunk/Pithos.Network/CloudFilesClient.cs @@ -266,12 +266,18 @@ namespace Pithos.Network case HttpStatusCode.OK: case HttpStatusCode.NoContent: var keys = response.Headers.AllKeys.AsQueryable(); + var tags=(from key in keys + where key.StartsWith("X-Object-Meta-") + let name=key.Substring(14) + select new {Name=name,Value=response.Headers[name]}) + .ToDictionary(t=>t.Name,t=>t.Value); return new ObjectInfo { Name = objectName, Bytes = long.Parse(GetHeaderValue("Content-Length", response, keys)), Hash = GetHeaderValue("ETag", response, keys), - Content_Type = GetHeaderValue("Content-Type", response, keys) + Content_Type = GetHeaderValue("Content-Type", response, keys), + Tags=tags }; case HttpStatusCode.NotFound: return ObjectInfo.Empty; @@ -455,6 +461,7 @@ namespace Pithos.Network } } + /// /// Copies headers from a Hammock RestClient to a WebClient -- 1.7.10.4