| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Buffers; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Diagnostics; |
| | 8 | | using System.IO; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Runtime.CompilerServices; |
| | 11 | | using System.Runtime.InteropServices; |
| | 12 | | using System.Threading; |
| | 13 | | using System.Threading.Tasks; |
| | 14 | |
|
| | 15 | | namespace Azure.Storage |
| | 16 | | { |
| | 17 | | internal sealed class StreamPartition : Stream |
| | 18 | | { |
| | 19 | | private Action _disposeAction; |
| | 20 | | private ReadOnlyMemory<byte> _memory; |
| | 21 | |
|
| 3702 | 22 | | public override bool CanRead => true; |
| | 23 | |
|
| 3702 | 24 | | public override bool CanSeek => true; |
| | 25 | |
|
| 0 | 26 | | public override bool CanWrite => false; |
| | 27 | |
|
| 9872 | 28 | | public override long Length { get; } |
| | 29 | |
|
| | 30 | | private long _position; |
| | 31 | |
|
| 0 | 32 | | public override long Position { get => _position; set => _position = value; } |
| | 33 | |
|
| 1234 | 34 | | public long ParentPosition { get; } |
| | 35 | |
|
| 0 | 36 | | public Task DisposalTask { get; } |
| | 37 | |
|
| | 38 | | #pragma warning disable IDE0069 // Disposable fields should be disposed // disposed in DisposalTask |
| | 39 | | //ManualResetEventSlim disposalTaskCompletionSource; |
| | 40 | | private SemaphoreSlim _disposalTaskCompletionSource; |
| | 41 | | #pragma warning restore IDE0069 // Disposable fields should be disposed |
| | 42 | |
|
| 1234 | 43 | | public StreamPartition(ReadOnlyMemory<byte> buffer, long parentPosition, int count, Action disposeAction, Cancel |
| | 44 | | { |
| 1234 | 45 | | _memory = buffer; |
| 1234 | 46 | | ParentPosition = parentPosition; |
| 1234 | 47 | | Length = count; |
| 1234 | 48 | | _disposeAction = disposeAction; |
| | 49 | | //this.disposalTaskCompletionSource = new ManualResetEventSlim(false); |
| 1234 | 50 | | _disposalTaskCompletionSource = new SemaphoreSlim(0); |
| 1234 | 51 | | DisposalTask = DisposalTaskCore(ct); |
| 1234 | 52 | | } |
| | 53 | |
|
| | 54 | | #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously |
| | 55 | | private async Task DisposalTaskCore(CancellationToken ct) |
| | 56 | | { |
| | 57 | | //Console.WriteLine($"Waiting for partition {this.ParentPosition}"); |
| | 58 | |
|
| | 59 | | //this.disposalTaskCompletionSource.Wait(ct); |
| 1234 | 60 | | await _disposalTaskCompletionSource.WaitAsync(ct).ConfigureAwait(false); |
| | 61 | |
|
| | 62 | | //Console.WriteLine($"Completed partition {this.ParentPosition}"); |
| | 63 | |
|
| 0 | 64 | | _disposalTaskCompletionSource.Dispose(); |
| 0 | 65 | | _disposalTaskCompletionSource = default; |
| 0 | 66 | | } |
| | 67 | | #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously |
| | 68 | |
|
| | 69 | | protected override void Dispose(bool disposing) |
| | 70 | | { |
| 0 | 71 | | if (!_disposedValue) |
| | 72 | | { |
| 0 | 73 | | if (disposing) |
| | 74 | | { |
| 0 | 75 | | base.Dispose(disposing); |
| 0 | 76 | | _disposeAction(); |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | _memory = default; |
| 0 | 80 | | _disposeAction = default; |
| | 81 | |
|
| 0 | 82 | | _disposedValue = true; |
| | 83 | |
|
| | 84 | | //this.disposalTaskCompletionSource.Set(); |
| 0 | 85 | | _disposalTaskCompletionSource.Release(); |
| | 86 | | } |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private bool _disposedValue = false; // To detect redundant calls |
| | 90 | |
|
| | 91 | | public override void Flush() |
| | 92 | | { |
| | 93 | | // Flush is allowed on read-only stream |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public int Read(out ReadOnlyMemory<byte> buffer, int count) |
| | 97 | | { |
| 0 | 98 | | var n = Math.Min(count, (int)(Length - _position)); |
| | 99 | |
|
| 0 | 100 | | buffer = _memory.Slice((int)_position, n); |
| | 101 | |
|
| 0 | 102 | | Interlocked.Add(ref _position, n); |
| | 103 | |
|
| 0 | 104 | | return n; |
| | 105 | | } |
| | 106 | |
|
| | 107 | | public override int Read(byte[] buffer, int offset, int count) |
| | 108 | | { |
| 4936 | 109 | | var n = Math.Min(count, (int)(Length - _position)); |
| | 110 | |
|
| 4936 | 111 | | _memory.Slice((int)_position, n).CopyTo(new Memory<byte>(buffer, offset, count)); |
| | 112 | |
|
| 4936 | 113 | | Interlocked.Add(ref _position, n); |
| | 114 | |
|
| 4936 | 115 | | return n; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | public override long Seek(long offset, SeekOrigin origin) |
| | 119 | | { |
| | 120 | | switch (origin) |
| | 121 | | { |
| | 122 | | case SeekOrigin.Begin: |
| 2468 | 123 | | Interlocked.Exchange(ref _position, offset); |
| 2468 | 124 | | break; |
| | 125 | | case SeekOrigin.Current: |
| 0 | 126 | | Interlocked.Add(ref _position, offset); |
| 0 | 127 | | break; |
| | 128 | | case SeekOrigin.End: |
| 0 | 129 | | Interlocked.Exchange(ref _position, Length - offset); |
| | 130 | | break; |
| | 131 | | } |
| | 132 | |
|
| 2468 | 133 | | return Position; |
| | 134 | | } |
| | 135 | |
|
| 0 | 136 | | public override void SetLength(long value) => throw Errors.NotImplemented(); |
| | 137 | |
|
| 0 | 138 | | public override void Write(byte[] buffer, int offset, int count) => throw Errors.NotImplemented(); |
| | 139 | | } |
| | 140 | | } |