public async ValueTask BeginOperationAsync(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); _semaphoreReleaser = await _state.AsyncSemaphore.EnterAsync(cancellationToken); _state.Reset(); _state.SourceCacheContext = new SourceCacheContext(); }
public ValueTask EndOperationAsync(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); using (AsyncSemaphore.Releaser? semaphoreReleaser = _semaphoreReleaser) { _state.Reset(); _semaphoreReleaser = null; } return(new ValueTask()); }
public async Task RemoveRow(string key) { if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentNullException(nameof(key)); } string keyNormalized = new Value(key).ToString(); string keyDelimited = string.Concat(keyNormalized, CommaDelimitedFileAdapter.Comma); AsyncSemaphore.Releaser?releaser = null; try { releaser = await this.semaphore.EnterAsync(); string fileNameTemporary = Path.GetTempFileName(); TextReader reader = null; try { reader = File.OpenText(this.FilePath); FileStream output = null; try { output = File.OpenWrite(fileNameTemporary); TextWriter writer = null; try { writer = new StreamWriter(output); output = null; string line = await reader.ReadLineAsync(); await writer.WriteLineAsync(line); while (true) { line = await reader.ReadLineAsync(); if (null == line) { break; } if (line.StartsWith(keyDelimited, StringComparison.OrdinalIgnoreCase)) { continue; } await writer.WriteLineAsync(line); } } finally { if (writer != null) { writer.Close(); writer = null; } } } finally { if (output != null) { output.Close(); output = null; } } } finally { if (reader != null) { reader.Close(); reader = null; } } File.Delete(this.FilePath); File.Move(fileNameTemporary, this.FilePath); } finally { if (releaser.HasValue) { releaser.Value.Dispose(); releaser = null; } } }
private async Task <IRow> InsertRow(string key, IReadOnlyDictionary <string, string> columns) { if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentNullException(nameof(key)); } if (null == columns) { throw new ArgumentNullException(nameof(columns)); } IDictionary <string, string> valuesByNormalizedNames = columns .ToDictionary( (KeyValuePair <string, string> item) => new Value(item.Key).ToString().ToUpperInvariant(), (KeyValuePair <string, string> item) => item.Value); IList <string> values = new List <string>(this.headers.Count); string keyNormalized = new Value(key).ToString(); values.Add(keyNormalized); IEnumerable <string> normalizedHeaders = this.headersNormalized.Skip(1); foreach (string normalizedHeader in normalizedHeaders) { string value = null; if (!valuesByNormalizedNames.TryGetValue(normalizedHeader, out value)) { values.Add(string.Empty); continue; } string normalizedValue = new Value(value).ToString(); values.Add(normalizedValue); } string row = string.Join(CommaDelimitedFileAdapter.Comma, values); AsyncSemaphore.Releaser?releaser = null; try { releaser = await this.semaphore.EnterAsync(); StreamWriter writer = null; try { writer = File.AppendText(this.FilePath); await writer.WriteLineAsync(row); } finally { if (writer != null) { writer.Close(); writer = null; } } } finally { if (releaser.HasValue) { releaser.Value.Dispose(); releaser = null; } } IRow result = new Row(keyNormalized, columns); return(result); }