public bool TryGet(TKey key, out TEntity view) { view = default(TEntity); try { var name = GetName(key); if (!File.Exists(name)) { return(false); } using (var stream = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (stream.Length == 0) { return(false); } view = _strategy.Deserialize <TEntity>(stream); return(true); } } catch (FileNotFoundException) { // if file happened to be deleted between the moment of check and actual read. return(false); } catch (DirectoryNotFoundException) { return(false); } }
public TEntity AddOrUpdate(TKey key, Func <TEntity> addViewFactory, Func <TEntity, TEntity> updateViewFactory, AddOrUpdateHint hint) { string etag = null; var blob = GetBlobReference(key); TEntity view; try { // atomic entities should be small, so we can use the simple method var bytes = blob.DownloadByteArray(); using (var stream = new MemoryStream(bytes)) { view = _strategy.Deserialize <TEntity>(stream); } view = updateViewFactory(view); etag = blob.Attributes.Properties.ETag; } catch (StorageClientException ex) { switch (ex.ErrorCode) { case StorageErrorCode.ContainerNotFound: var s = string.Format( "Container '{0}' does not exist. You need to initialize this atomic storage and ensure that '{1}' is known to '{2}'.", blob.Container.Name, typeof(TEntity).Name, _strategy.GetType().Name); throw new InvalidOperationException(s, ex); case StorageErrorCode.BlobNotFound: case StorageErrorCode.ResourceNotFound: view = addViewFactory(); break; default: throw; } } // atomic entities should be small, so we can use the simple method // http://toolheaven.net/post/Azure-and-blob-write-performance.aspx using (var memory = new MemoryStream()) { _strategy.Serialize(view, memory); // note that upload from stream does weird things var bro = etag != null ? new BlobRequestOptions { AccessCondition = AccessCondition.IfMatch(etag) } : new BlobRequestOptions { AccessCondition = AccessCondition.IfNoneMatch("*") }; // make sure that upload is not rejected due to cashed content MD5 // http://social.msdn.microsoft.com/Forums/hu-HU/windowsazuredata/thread/4764e38f-b200-4efe-ada2-7de442dc4452 blob.Properties.ContentMD5 = null; blob.UploadByteArray(memory.ToArray(), bro); } return(view); }
public bool TryGet(TKey key, out TEntity entity) { var name = GetName(key); byte[] bytes; if (_store.TryGetValue(name, out bytes)) { using (var mem = new MemoryStream(bytes)) { entity = _strategy.Deserialize <TEntity>(mem); return(true); } } entity = default(TEntity); return(false); }
public bool TryGet(TKey key, out TEntity entity) { var blob = GetBlobReference(key); try { // blob request options are cloned from the config // atomic entities should be small, so we can use the simple method var bytes = blob.DownloadByteArray(); using (var stream = new MemoryStream(bytes)) { entity = _strategy.Deserialize <TEntity>(stream); return(true); } } catch (StorageClientException ex) { switch (ex.ErrorCode) { case StorageErrorCode.ContainerNotFound: case StorageErrorCode.BlobNotFound: case StorageErrorCode.ResourceNotFound: entity = default(TEntity); return(false); default: throw; } } }
public bool TryGet(TKey key, out TEntity entity) { var name = GetName(key); byte[] bytes; if (_store.TryGetValue(name, out bytes)) // Get the byte array from the store { using (var mem = new MemoryStream(bytes)) { // Using a memory stream deserialise the stream to an entity entity = _strategy.Deserialize <TEntity>(mem); return(true); } } entity = default(TEntity); return(false); }
public bool TryGet(TKey key, out TEntity view) { view = default(TEntity); var name = GetName(key); try { if (!File.Exists(name)) { return(false); } using (var stream = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (stream.Length == 0) { return(false); } view = _strategy.Deserialize <TEntity>(stream); return(true); } } catch (FileNotFoundException) { return(false); } catch (DirectoryNotFoundException) { return(false); } catch (IOException) // fix for fast read / slow write concurency { _log.WriteLine("File read failed for {0} - {1} with key {2}. Will retry.", _folder, name, key); Thread.Sleep(2); TryGet(key, out view); return(false); } }