Skip to content

Commit

Permalink
Merge pull request #70 from EventStore/sakno/fixed-regression
Browse files Browse the repository at this point in the history
[KDB-521]: Fixed checksum calculation for sync write when stream position is advanced
  • Loading branch information
timothycoleman authored Dec 12, 2024
2 parents e9f6080 + 05feb47 commit 6cdaa72
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/EventStore.Plugins/Transforms/ChunkDataWriteStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public class ChunkDataWriteStream(Stream chunkFileStream, IncrementalHash checks
public sealed override int Read(Span<byte> buffer) => throw new NotSupportedException();

public override void Write(ReadOnlySpan<byte> buffer) {
if (_positionToHash is { } count) {
ReadAndChecksum(count);

Debug.Assert(ChunkFileStream.Position == count);
_positionToHash = null;
}

ChunkFileStream.Write(buffer);
checksumAlgorithm.AppendData(buffer);
}
Expand Down Expand Up @@ -76,4 +83,16 @@ private async ValueTask ReadAndChecksumAsync(long count, CancellationToken token
ArrayPool<byte>.Shared.Return(buffer);
}
}

private void ReadAndChecksum(long count) {
Span<byte> buffer = stackalloc byte[1024];

for (int bytesRead; count > 0L; count -= bytesRead) {
bytesRead = ChunkFileStream.Read(buffer.Slice(0, (int)long.Min(count, buffer.Length)));
if (bytesRead is 0)
break;

checksumAlgorithm.AppendData(buffer.Slice(0, bytesRead));
}
}
}

0 comments on commit 6cdaa72

Please sign in to comment.