|
| 1 | +/* |
| 2 | +* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +* contributor license agreements. See the NOTICE file distributed with |
| 4 | +* this work for additional information regarding copyright ownership. |
| 5 | +* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +* (the "License"); you may not use this file except in compliance with |
| 7 | +* the License. You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.IO; |
| 20 | +using System.Threading; |
| 21 | +using System.Threading.Tasks; |
| 22 | +using K4os.Compression.LZ4.Encoders; |
| 23 | +using K4os.Compression.LZ4.Streams; |
| 24 | + |
| 25 | +namespace Apache.Arrow.Adbc.Drivers.Databricks |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// Custom LZ4 decoder stream that uses CustomLZ4FrameReader for buffer pooling. |
| 29 | + /// This replaces K4os.Compression.LZ4.Streams.LZ4DecoderStream to use our custom reader |
| 30 | + /// that pools 4MB+ buffers. |
| 31 | + /// |
| 32 | + /// Why not inherit from LZ4DecoderStream or LZ4StreamOnStreamEssentials? |
| 33 | + /// - LZ4DecoderStream directly instantiates StreamLZ4FrameReader (no injection point) |
| 34 | + /// - LZ4StreamOnStreamEssentials has a 'private protected' constructor (inaccessible from external assemblies) |
| 35 | + /// |
| 36 | + /// What features from K4os base classes are intentionally omitted: |
| 37 | + /// - Timeout support: Not needed since inner stream (MemoryStream) doesn't support timeouts |
| 38 | + /// - Write operations: This is a read-only decompression stream |
| 39 | + /// - DisposeAsync: Optional - base Stream.DisposeAsync() calls our Dispose(bool) which is sufficient |
| 40 | + /// </summary> |
| 41 | + internal sealed class CustomLZ4DecoderStream : Stream |
| 42 | + { |
| 43 | + private readonly CustomLZ4FrameReader _reader; |
| 44 | + private readonly Stream _inner; |
| 45 | + private readonly bool _leaveOpen; |
| 46 | + private readonly bool _interactive; |
| 47 | + private bool _disposed; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Creates a new CustomLZ4DecoderStream instance. |
| 51 | + /// </summary> |
| 52 | + /// <param name="inner">The inner stream containing compressed LZ4 data.</param> |
| 53 | + /// <param name="decoderFactory">Factory function to create the LZ4 decoder.</param> |
| 54 | + /// <param name="bufferPool">The ArrayPool to use for buffer allocation (from DatabricksDatabase).</param> |
| 55 | + /// <param name="leaveOpen">Whether to leave the inner stream open when disposing.</param> |
| 56 | + /// <param name="interactive">Interactive mode - provide bytes as soon as available.</param> |
| 57 | + public CustomLZ4DecoderStream( |
| 58 | + Stream inner, |
| 59 | + Func<ILZ4Descriptor, ILZ4Decoder> decoderFactory, |
| 60 | + System.Buffers.ArrayPool<byte> bufferPool, |
| 61 | + bool leaveOpen = false, |
| 62 | + bool interactive = false) |
| 63 | + { |
| 64 | + _inner = inner ?? throw new ArgumentNullException(nameof(inner)); |
| 65 | + _reader = new CustomLZ4FrameReader(inner, true, decoderFactory, bufferPool); |
| 66 | + _leaveOpen = leaveOpen; |
| 67 | + _interactive = interactive; |
| 68 | + } |
| 69 | + |
| 70 | + public override bool CanRead => !_disposed && _inner.CanRead; |
| 71 | + public override bool CanSeek => false; |
| 72 | + public override bool CanWrite => false; |
| 73 | + |
| 74 | + // Timeout properties are not implemented since: |
| 75 | + // - The inner stream (MemoryStream in our use case) doesn't support timeouts |
| 76 | + // - LZ4 decompression is CPU-bound, not I/O-bound, so timeouts don't apply |
| 77 | + public override bool CanTimeout => false; |
| 78 | + public override int ReadTimeout |
| 79 | + { |
| 80 | + get => throw new InvalidOperationException("LZ4 decoder stream does not support timeouts"); |
| 81 | + set => throw new InvalidOperationException("LZ4 decoder stream does not support timeouts"); |
| 82 | + } |
| 83 | + public override int WriteTimeout |
| 84 | + { |
| 85 | + get => throw new InvalidOperationException("LZ4 decoder stream does not support timeouts"); |
| 86 | + set => throw new InvalidOperationException("LZ4 decoder stream does not support timeouts"); |
| 87 | + } |
| 88 | + |
| 89 | + public override long Length => _reader.GetFrameLength() ?? -1; |
| 90 | + public override long Position |
| 91 | + { |
| 92 | + get => _reader.GetBytesRead(); |
| 93 | + set => throw new NotSupportedException("LZ4 stream does not support setting position"); |
| 94 | + } |
| 95 | + |
| 96 | + public override long Seek(long offset, SeekOrigin origin) => |
| 97 | + throw new NotSupportedException("LZ4 stream does not support seeking"); |
| 98 | + |
| 99 | + public override void SetLength(long value) => |
| 100 | + throw new NotSupportedException("LZ4 stream does not support SetLength"); |
| 101 | + |
| 102 | + public override void Write(byte[] buffer, int offset, int count) => |
| 103 | + throw new NotSupportedException("LZ4 decoder stream does not support writing"); |
| 104 | + |
| 105 | + public override int ReadByte() => _reader.ReadOneByte(); |
| 106 | + |
| 107 | + public override int Read(byte[] buffer, int offset, int count) => |
| 108 | + _reader.ReadManyBytes(buffer.AsSpan(offset, count), _interactive); |
| 109 | + |
| 110 | + public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => |
| 111 | + _reader.ReadManyBytesAsync(cancellationToken, buffer.AsMemory(offset, count), _interactive); |
| 112 | + |
| 113 | +#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER |
| 114 | + public override int Read(Span<byte> buffer) => |
| 115 | + _reader.ReadManyBytes(buffer, _interactive); |
| 116 | + |
| 117 | + public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) => |
| 118 | + new(_reader.ReadManyBytesAsync(cancellationToken, buffer, _interactive)); |
| 119 | +#endif |
| 120 | + |
| 121 | + public override void Flush() |
| 122 | + { |
| 123 | + // No-op for read-only stream - nothing to flush since we only read |
| 124 | + } |
| 125 | + |
| 126 | + public override Task FlushAsync(CancellationToken cancellationToken) |
| 127 | + { |
| 128 | + // No-op for read-only stream - nothing to flush since we only read |
| 129 | + return Task.CompletedTask; |
| 130 | + } |
| 131 | + |
| 132 | + protected override void Dispose(bool disposing) |
| 133 | + { |
| 134 | + // Double-dispose protection: only dispose once |
| 135 | + if (!_disposed) |
| 136 | + { |
| 137 | + if (disposing) |
| 138 | + { |
| 139 | + // Dispose managed resources |
| 140 | + _reader.Dispose(); // Returns 4MB buffer to pool |
| 141 | + if (!_leaveOpen) |
| 142 | + { |
| 143 | + _inner?.Dispose(); // Dispose inner stream if we own it |
| 144 | + } |
| 145 | + } |
| 146 | + // No unmanaged resources to clean up (no finalizer needed) |
| 147 | + _disposed = true; |
| 148 | + } |
| 149 | + base.Dispose(disposing); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments