| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Azure.Core.Pipeline; |
| | | 9 | | using Azure.Storage.Internal.Avro; |
| | | 10 | | using Azure.Storage.Blobs.Models; |
| | | 11 | | using System.Buffers; |
| | | 12 | | |
| | | 13 | | namespace Azure.Storage.Blobs |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// QuickQueryStream. |
| | | 17 | | /// </summary> |
| | | 18 | | internal class BlobQuickQueryStream : Stream |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Underlying stream. |
| | | 22 | | /// </summary> |
| | | 23 | | internal Stream _avroStream; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Avro Reader. |
| | | 27 | | /// </summary> |
| | | 28 | | internal AvroReader _avroReader; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Buffer to hold bytes we haven't processed yet. |
| | | 32 | | /// </summary> |
| | | 33 | | internal byte[] _buffer; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Current buffer offset. |
| | | 37 | | /// </summary> |
| | | 38 | | internal int _bufferOffset; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The current length of the buffer. |
| | | 42 | | /// </summary> |
| | | 43 | | internal int _bufferLength; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Progress handler. |
| | | 47 | | /// </summary> |
| | | 48 | | internal IProgress<long> _progressHandler; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Error handler. |
| | | 52 | | /// </summary> |
| | | 53 | | internal Action<BlobQueryError> _errorHandler; |
| | | 54 | | |
| | 90 | 55 | | public BlobQuickQueryStream( |
| | 90 | 56 | | Stream avroStream, |
| | 90 | 57 | | IProgress<long> progressHandler = default, |
| | 90 | 58 | | Action<BlobQueryError> errorHandler = default) |
| | | 59 | | { |
| | 90 | 60 | | _avroStream = avroStream; |
| | 90 | 61 | | _avroReader = new AvroReader(_avroStream); |
| | 90 | 62 | | _bufferOffset = 0; |
| | 90 | 63 | | _bufferLength = 0; |
| | 90 | 64 | | _progressHandler = progressHandler; |
| | 90 | 65 | | _errorHandler = errorHandler; |
| | 90 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc/> |
| | | 69 | | public override int Read(byte[] buffer, int offset, int count) |
| | 80 | 70 | | => ReadInternal(async: false, buffer, offset, count).EnsureCompleted(); |
| | | 71 | | |
| | | 72 | | /// <inheritdoc/> |
| | | 73 | | public new async Task<int> ReadAsync(byte[] buffer, int offset, int count) |
| | 0 | 74 | | => await ReadInternal(async: true, buffer, offset, count).ConfigureAwait(false); |
| | | 75 | | |
| | | 76 | | |
| | | 77 | | // Note - offset is with respect to buffer. |
| | | 78 | | private async Task<int> ReadInternal(bool async, byte[] buffer, int offset, int count) |
| | | 79 | | { |
| | 80 | 80 | | ValidateReadParameters(buffer, offset, count); |
| | | 81 | | |
| | 80 | 82 | | int remainingBytes = _bufferLength - _bufferOffset; |
| | | 83 | | |
| | | 84 | | // We have enough bytes in the buffer and don't need to read the next Record. |
| | 80 | 85 | | if (count <= remainingBytes) |
| | | 86 | | { |
| | 0 | 87 | | Array.Copy( |
| | 0 | 88 | | sourceArray: _buffer, |
| | 0 | 89 | | sourceIndex: _bufferOffset, |
| | 0 | 90 | | destinationArray: buffer, |
| | 0 | 91 | | destinationIndex: offset, |
| | 0 | 92 | | length: count); |
| | 0 | 93 | | _bufferOffset += count; |
| | 0 | 94 | | return count; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | // Copy remaining buffer |
| | 80 | 98 | | if (remainingBytes > 0) |
| | | 99 | | { |
| | 0 | 100 | | Array.Copy( |
| | 0 | 101 | | sourceArray: _buffer, |
| | 0 | 102 | | sourceIndex: _bufferOffset, |
| | 0 | 103 | | destinationArray: buffer, |
| | 0 | 104 | | destinationIndex: offset, |
| | 0 | 105 | | length: remainingBytes); |
| | 0 | 106 | | _bufferOffset += remainingBytes; |
| | 0 | 107 | | return remainingBytes; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | // Reset _bufferOffset, _bufferLength, and remainingBytes |
| | 80 | 111 | | _bufferOffset = 0; |
| | 80 | 112 | | _bufferLength = 0; |
| | 80 | 113 | | remainingBytes = 0; |
| | | 114 | | |
| | | 115 | | // We've caught up to the end of the _avroStream, but it isn't necessarly the end of the stream. |
| | 80 | 116 | | if (!_avroReader.HasNext()) |
| | | 117 | | { |
| | 0 | 118 | | return 0; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | // We need to keep getting the next record until we get a data record. |
| | 176 | 122 | | while (remainingBytes == 0) |
| | | 123 | | { |
| | | 124 | | // Get next Record. |
| | 136 | 125 | | Dictionary<string, object> record = (Dictionary<string, object>)await _avroReader.Next(async).ConfigureA |
| | | 126 | | |
| | 136 | 127 | | switch (record["$schema"]) |
| | | 128 | | { |
| | | 129 | | // Data Record |
| | | 130 | | case Constants.QuickQuery.DataRecordName: |
| | 40 | 131 | | record.TryGetValue(Constants.QuickQuery.Data, out object byteObject); |
| | | 132 | | |
| | 40 | 133 | | if (byteObject == null) |
| | | 134 | | { |
| | 0 | 135 | | throw new InvalidOperationException($"Avro data record is missing {Constants.QuickQuery.Data |
| | | 136 | | } |
| | | 137 | | |
| | 40 | 138 | | byte[] bytes = (byte[])byteObject; |
| | | 139 | | |
| | | 140 | | // Return the buffer if it is not null and not big enough. |
| | 40 | 141 | | if (_buffer != null && _buffer.Length < bytes.Length) |
| | | 142 | | { |
| | 0 | 143 | | ArrayPool<byte>.Shared.Return(_buffer, clearArray: true); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | // Rent a new buffer if it is null or not big enough. |
| | 40 | 147 | | if (_buffer == null || _buffer.Length < bytes.Length) |
| | | 148 | | { |
| | 40 | 149 | | _buffer = ArrayPool<byte>.Shared.Rent(Math.Max(4 * Constants.MB, bytes.Length)); |
| | | 150 | | } |
| | | 151 | | |
| | 40 | 152 | | Array.Copy( |
| | 40 | 153 | | sourceArray: bytes, |
| | 40 | 154 | | sourceIndex: 0, |
| | 40 | 155 | | destinationArray: _buffer, |
| | 40 | 156 | | destinationIndex: 0, |
| | 40 | 157 | | length: bytes.Length); |
| | | 158 | | |
| | 40 | 159 | | _bufferLength = bytes.Length; |
| | | 160 | | |
| | | 161 | | // Don't remove this reset, it is used in the final array copy below. |
| | 40 | 162 | | remainingBytes = bytes.Length; |
| | 40 | 163 | | break; |
| | | 164 | | |
| | | 165 | | // Progress Record |
| | | 166 | | case Constants.QuickQuery.ProgressRecordName: |
| | 40 | 167 | | if (_progressHandler != default) |
| | | 168 | | { |
| | 8 | 169 | | record.TryGetValue(Constants.QuickQuery.BytesScanned, out object progress); |
| | | 170 | | |
| | 8 | 171 | | if (progress == null) |
| | | 172 | | { |
| | 0 | 173 | | throw new InvalidOperationException($"Avro progress record is mssing {Constants.QuickQue |
| | | 174 | | } |
| | | 175 | | |
| | 8 | 176 | | _progressHandler.Report((long)progress); |
| | | 177 | | } |
| | 8 | 178 | | break; |
| | | 179 | | |
| | | 180 | | // Error Record |
| | | 181 | | case Constants.QuickQuery.ErrorRecordName: |
| | 16 | 182 | | ProcessErrorRecord(record); |
| | 16 | 183 | | break; |
| | | 184 | | |
| | | 185 | | // End Record |
| | | 186 | | case Constants.QuickQuery.EndRecordName: |
| | 40 | 187 | | if (_progressHandler != default) |
| | | 188 | | { |
| | 8 | 189 | | record.TryGetValue(Constants.QuickQuery.TotalBytes, out object progress); |
| | | 190 | | |
| | 8 | 191 | | if (progress == null) |
| | | 192 | | { |
| | 0 | 193 | | throw new InvalidOperationException($"Avro end record is missing {Constants.QuickQuery.T |
| | | 194 | | } |
| | | 195 | | |
| | 8 | 196 | | _progressHandler.Report((long)progress); |
| | | 197 | | } |
| | 40 | 198 | | return 0; |
| | | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | 40 | 202 | | int length = Math.Min(count, remainingBytes); |
| | 40 | 203 | | Array.Copy( |
| | 40 | 204 | | sourceArray: _buffer, |
| | 40 | 205 | | sourceIndex: _bufferOffset, |
| | 40 | 206 | | destinationArray: buffer, |
| | 40 | 207 | | destinationIndex: offset, |
| | 40 | 208 | | length: length); |
| | | 209 | | |
| | 40 | 210 | | _bufferOffset += length; |
| | 40 | 211 | | return length; |
| | 80 | 212 | | } |
| | | 213 | | |
| | | 214 | | |
| | | 215 | | internal static void ValidateReadParameters(byte[] buffer, int offset, int count) |
| | | 216 | | { |
| | 88 | 217 | | if (buffer == null) |
| | | 218 | | { |
| | 2 | 219 | | throw new ArgumentNullException($"{nameof(buffer)}", "Parameter cannot be null."); |
| | | 220 | | } |
| | | 221 | | |
| | 86 | 222 | | if (offset < 0) |
| | | 223 | | { |
| | 2 | 224 | | throw new ArgumentOutOfRangeException($"{nameof(offset)}", "Parameter cannot be negative."); |
| | | 225 | | } |
| | | 226 | | |
| | 84 | 227 | | if (count < 0) |
| | | 228 | | { |
| | 2 | 229 | | throw new ArgumentOutOfRangeException($"{nameof(count)}", "Parameter cannot be negative."); |
| | | 230 | | } |
| | | 231 | | |
| | 82 | 232 | | if (offset + count > buffer.Length) |
| | | 233 | | { |
| | 2 | 234 | | throw new ArgumentException($"The sum of {nameof(offset)} and {nameof(count)} cannot be greater than {na |
| | | 235 | | } |
| | 80 | 236 | | } |
| | | 237 | | |
| | | 238 | | internal void ProcessErrorRecord(Dictionary<string, object> record) |
| | | 239 | | { |
| | 18 | 240 | | record.TryGetValue(Constants.QuickQuery.Fatal, out object fatal); |
| | 18 | 241 | | record.TryGetValue(Constants.QuickQuery.Name, out object name); |
| | 18 | 242 | | record.TryGetValue(Constants.QuickQuery.Description, out object description); |
| | 18 | 243 | | record.TryGetValue(Constants.QuickQuery.Position, out object position); |
| | | 244 | | |
| | 18 | 245 | | if (fatal == null) |
| | | 246 | | { |
| | 0 | 247 | | throw new InvalidOperationException($"Avro error record is missing {nameof(fatal)} property"); |
| | | 248 | | } |
| | | 249 | | |
| | 18 | 250 | | if (name == null) |
| | | 251 | | { |
| | 0 | 252 | | throw new InvalidOperationException($"Avro error record is missing {nameof(name)} property"); |
| | | 253 | | } |
| | | 254 | | |
| | 18 | 255 | | if (description == null) |
| | | 256 | | { |
| | 0 | 257 | | throw new InvalidOperationException($"Avro error record is missing {nameof(description)} property"); |
| | | 258 | | } |
| | | 259 | | |
| | 18 | 260 | | if (position == null) |
| | | 261 | | { |
| | 0 | 262 | | throw new InvalidOperationException($"Avro error record is missing {nameof(position)} property"); |
| | | 263 | | } |
| | | 264 | | |
| | 18 | 265 | | if (_errorHandler != null) |
| | | 266 | | { |
| | 10 | 267 | | BlobQueryError blobQueryError = new BlobQueryError |
| | 10 | 268 | | { |
| | 10 | 269 | | IsFatal = (bool)fatal, |
| | 10 | 270 | | Name = (string)name, |
| | 10 | 271 | | Description = (string)description, |
| | 10 | 272 | | Position = (long)position |
| | 10 | 273 | | }; |
| | 10 | 274 | | _errorHandler(blobQueryError); |
| | | 275 | | } |
| | 18 | 276 | | } |
| | | 277 | | |
| | | 278 | | /// <inheritdoc/> |
| | 120 | 279 | | public override bool CanRead => true; |
| | | 280 | | |
| | | 281 | | /// <inheritdoc/> |
| | 0 | 282 | | public override bool CanSeek => false; |
| | | 283 | | |
| | | 284 | | /// <inheritdoc/> |
| | 0 | 285 | | public override bool CanWrite => false; |
| | | 286 | | |
| | | 287 | | /// <inheritdoc/> |
| | 0 | 288 | | public override long Length => throw new NotSupportedException(); |
| | | 289 | | |
| | | 290 | | /// <inheritdoc/> |
| | | 291 | | public override long Position |
| | | 292 | | { |
| | 0 | 293 | | get => throw new NotSupportedException(); |
| | 0 | 294 | | set => throw new NotSupportedException(); |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | /// <inheritdoc/> |
| | 0 | 298 | | public override void Flush() => throw new NotSupportedException(); |
| | | 299 | | |
| | | 300 | | /// <inheritdoc/> |
| | 0 | 301 | | public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| | | 302 | | |
| | | 303 | | /// <inheritdoc/> |
| | 0 | 304 | | public override void SetLength(long value) => throw new NotSupportedException(); |
| | | 305 | | |
| | | 306 | | /// <inheritdoc/> |
| | 0 | 307 | | public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); |
| | | 308 | | |
| | | 309 | | /// <inheritdoc/> |
| | | 310 | | protected override void Dispose(bool disposing) |
| | | 311 | | { |
| | | 312 | | // Return the buffer to the pool if we're called from Dispose or a finalizer |
| | 40 | 313 | | if (_buffer != null) |
| | | 314 | | { |
| | 40 | 315 | | ArrayPool<byte>.Shared.Return(_buffer, clearArray: true); |
| | 40 | 316 | | _buffer = null; |
| | | 317 | | } |
| | | 318 | | |
| | 40 | 319 | | _avroStream.Dispose(); |
| | 40 | 320 | | if (_buffer != null) |
| | | 321 | | { |
| | 0 | 322 | | ArrayPool<byte>.Shared.Return(_buffer, clearArray: true); |
| | 0 | 323 | | _buffer = null; |
| | | 324 | | } |
| | 40 | 325 | | } |
| | | 326 | | } |
| | | 327 | | } |