public async Task<StorageValue> ReadAsync(StorageKey key) { for (int i = 0; i < _retries; ++i) { if (_cancellation.IsCancellationRequested) throw new ObjectDisposedException(nameof(SaveFile)); lock (_mutex) { /* If we find the value in the write queue, return the queued value. (It's more recent than what's on disk.) */ StorageValue value; if (_queuedValues.TryGetValue(key, out value)) return value; } var filename = GetFilename(key); if (!File.Exists(filename)) return null; try { using (var stream = File.Open(GetFilename(key), FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var memoryStream = new MemoryStream((int)stream.Length)) { await stream.CopyToAsync(memoryStream); return new StorageValue(memoryStream.ToArray()); } } } catch (IOException) { } } /* Ignore read errors (for now). */ return null; }
public void Write(StorageKey key, StorageValue value) { if (key == null) throw new ArgumentNullException(nameof(key)); if (value == null) throw new ArgumentNullException(nameof(value)); lock (_mutex) { _queuedValues[key] = value; } _queuedKeys.Add(key); }
private string GetFilename(StorageKey key) { using (var sha1 = new SHA1Managed()) { var hashBytes = sha1.ComputeHash(key.Bytes); return Path.Combine( _path, HexConverter.ToHex(hashBytes, 0, 1), HexConverter.ToHex(hashBytes, 1, hashBytes.Length - 2)) + ".cubedata"; } }
private void SaveValue(StorageKey key) { StorageValue value; lock (_mutex) { if (!_queuedValues.TryGetValue(key, out value)) return; } string filename = GetFilename(key); string directoryName = Path.GetDirectoryName(filename); for (int i = 0; i < _retries; ++i) { try { Directory.CreateDirectory(directoryName); using (var stream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None)) { stream.Write(value.Bytes, 0, value.Bytes.Length); } break; } catch (IOException) { } } lock (_mutex) { StorageValue newValue; if (_queuedValues.TryGetValue(key, out newValue) && object.ReferenceEquals(value, newValue)) { /* Value is still current; remove it from the queue. */ _queuedValues.Remove(key); } } }
public void Write(StorageKey key, StorageValue value) { }
public Task<StorageValue> ReadAsync(StorageKey key) { return null; }
public Task <StorageValue> ReadAsync(StorageKey key) { return(null); }