|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.storage.common.implementation.structuredmessage; |
| 5 | + |
| 6 | +import com.azure.core.util.logging.ClientLogger; |
| 7 | +import com.azure.storage.common.implementation.BufferStagingArea; |
| 8 | +import reactor.core.publisher.Flux; |
| 9 | +import reactor.core.publisher.Mono; |
| 10 | + |
| 11 | +import java.nio.ByteBuffer; |
| 12 | +import java.util.concurrent.atomic.AtomicLong; |
| 13 | + |
| 14 | +/** |
| 15 | + * Stateful decoder for structured messages that supports mid-stream retries. |
| 16 | + * This decoder maintains state across network interruptions to ensure all data |
| 17 | + * is validated before retrying from the point of failure. |
| 18 | + */ |
| 19 | +public class StatefulStructuredMessageDecoder { |
| 20 | + private static final ClientLogger LOGGER = new ClientLogger(StatefulStructuredMessageDecoder.class); |
| 21 | + |
| 22 | + private final long expectedContentLength; |
| 23 | + private final StructuredMessageDecoder decoder; |
| 24 | + private final AtomicLong totalBytesDecoded; |
| 25 | + private final AtomicLong totalEncodedBytesProcessed; |
| 26 | + private ByteBuffer pendingData; |
| 27 | + private boolean finalized; |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a new stateful structured message decoder. |
| 31 | + * |
| 32 | + * @param expectedContentLength The expected length of the encoded content. |
| 33 | + */ |
| 34 | + public StatefulStructuredMessageDecoder(long expectedContentLength) { |
| 35 | + this.expectedContentLength = expectedContentLength; |
| 36 | + this.decoder = new StructuredMessageDecoder(expectedContentLength); |
| 37 | + this.totalBytesDecoded = new AtomicLong(0); |
| 38 | + this.totalEncodedBytesProcessed = new AtomicLong(0); |
| 39 | + this.pendingData = null; |
| 40 | + this.finalized = false; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Decodes a flux of byte buffers representing encoded structured message data. |
| 45 | + * |
| 46 | + * @param encodedFlux The flux of encoded byte buffers. |
| 47 | + * @return A flux of decoded byte buffers. |
| 48 | + */ |
| 49 | + public Flux<ByteBuffer> decode(Flux<ByteBuffer> encodedFlux) { |
| 50 | + if (finalized) { |
| 51 | + return Flux.error(new IllegalStateException("Decoder has already been finalized")); |
| 52 | + } |
| 53 | + |
| 54 | + // Collect all data first (structured message needs complete data to decode) |
| 55 | + return encodedFlux |
| 56 | + .collect(() -> new EncodedDataCollector(), EncodedDataCollector::addBuffer) |
| 57 | + .flatMapMany(collector -> { |
| 58 | + try { |
| 59 | + ByteBuffer allEncodedData = collector.getAllData(); |
| 60 | + |
| 61 | + if (allEncodedData.remaining() == 0) { |
| 62 | + return Flux.empty(); |
| 63 | + } |
| 64 | + |
| 65 | + // Update total encoded bytes processed |
| 66 | + totalEncodedBytesProcessed.addAndGet(allEncodedData.remaining()); |
| 67 | + |
| 68 | + // Decode the complete message |
| 69 | + ByteBuffer decodedData = decoder.decode(allEncodedData); |
| 70 | + |
| 71 | + // Update total bytes decoded |
| 72 | + totalBytesDecoded.addAndGet(decodedData.remaining()); |
| 73 | + |
| 74 | + // Finalize decoding |
| 75 | + decoder.finalizeDecoding(); |
| 76 | + finalized = true; |
| 77 | + |
| 78 | + return Flux.just(decodedData); |
| 79 | + } catch (Exception e) { |
| 80 | + LOGGER.error("Failed to decode structured message: " + e.getMessage(), e); |
| 81 | + return Flux.error(e); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Gets the total number of decoded bytes processed so far. |
| 88 | + * |
| 89 | + * @return The total decoded bytes. |
| 90 | + */ |
| 91 | + public long getTotalBytesDecoded() { |
| 92 | + return totalBytesDecoded.get(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Gets the total number of encoded bytes processed so far. |
| 97 | + * |
| 98 | + * @return The total encoded bytes processed. |
| 99 | + */ |
| 100 | + public long getTotalEncodedBytesProcessed() { |
| 101 | + return totalEncodedBytesProcessed.get(); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Checks if the decoder has been finalized. |
| 106 | + * |
| 107 | + * @return true if finalized, false otherwise. |
| 108 | + */ |
| 109 | + public boolean isFinalized() { |
| 110 | + return finalized; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Helper class to collect encoded data buffers. |
| 115 | + */ |
| 116 | + private static class EncodedDataCollector { |
| 117 | + private ByteBuffer accumulatedBuffer; |
| 118 | + |
| 119 | + EncodedDataCollector() { |
| 120 | + this.accumulatedBuffer = ByteBuffer.allocate(0); |
| 121 | + } |
| 122 | + |
| 123 | + void addBuffer(ByteBuffer buffer) { |
| 124 | + // Accumulate the buffer |
| 125 | + ByteBuffer newBuffer = ByteBuffer.allocate(accumulatedBuffer.remaining() + buffer.remaining()); |
| 126 | + newBuffer.put(accumulatedBuffer); |
| 127 | + newBuffer.put(buffer); |
| 128 | + newBuffer.flip(); |
| 129 | + accumulatedBuffer = newBuffer; |
| 130 | + } |
| 131 | + |
| 132 | + ByteBuffer getAllData() { |
| 133 | + return accumulatedBuffer; |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments