diff --git a/src/Microsoft.DocAsCode.Build.Engine/HashStreamHelper.cs b/src/Microsoft.DocAsCode.Build.Engine/HashStreamHelper.cs deleted file mode 100644 index 4fc982b0e8f..00000000000 --- a/src/Microsoft.DocAsCode.Build.Engine/HashStreamHelper.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using Microsoft.DocAsCode.Common; - -namespace Microsoft.DocAsCode.Build.Engine; - -internal static class HashStreamHelper -{ - public static Stream WithSha256Hash(this Stream stream, out Task hashTask) - { - var cs = new CircularStream(); - hashTask = Task.Run(() => - { - using var csr = cs.CreateReaderView(); - return HashUtility.GetSha256Hash(csr); - }); - return new CompositeStream(stream, cs.CreateWriterView()); - } -} diff --git a/src/Microsoft.DocAsCode.Build.Engine/TemplateProcessors/TemplateModelTransformer.cs b/src/Microsoft.DocAsCode.Build.Engine/TemplateProcessors/TemplateModelTransformer.cs index 0fb86d39731..03bfce83f51 100644 --- a/src/Microsoft.DocAsCode.Build.Engine/TemplateProcessors/TemplateModelTransformer.cs +++ b/src/Microsoft.DocAsCode.Build.Engine/TemplateProcessors/TemplateModelTransformer.cs @@ -273,9 +273,8 @@ private static string ExportModel(object model, string modelFileRelativePath, Ex private void TransformDocument(string result, string extension, IDocumentBuildContext context, string destFilePath, ManifestItem manifestItem, out List unresolvedXRefs) { - Task hashTask; unresolvedXRefs = new List(); - using (var stream = EnvironmentContext.FileAbstractLayer.Create(destFilePath).WithSha256Hash(out hashTask)) + using (var stream = EnvironmentContext.FileAbstractLayer.Create(destFilePath)) { using var sw = new StreamWriter(stream); if (extension.Equals(".html", StringComparison.OrdinalIgnoreCase)) @@ -292,7 +291,6 @@ private void TransformDocument(string result, string extension, IDocumentBuildCo { RelativePath = destFilePath, LinkToPath = GetLinkToPath(destFilePath), - Hash = Convert.ToBase64String(hashTask.Result) }; manifestItem.OutputFiles.Add(extension, ofi); } diff --git a/src/Microsoft.DocAsCode.Common/CircularBuffer.cs b/src/Microsoft.DocAsCode.Common/CircularBuffer.cs deleted file mode 100644 index 0e77ecaaec2..00000000000 --- a/src/Microsoft.DocAsCode.Common/CircularBuffer.cs +++ /dev/null @@ -1,105 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace Microsoft.DocAsCode.Common; - -public class CircularBuffer -{ - private T[] _buffer; - private int _index; - private int _count; - - public CircularBuffer() - : this(0x10) - { - } - - public CircularBuffer(int capacity) - { - _buffer = new T[capacity]; - } - - public void Write(T item) - { - EnsureCapacity(1); - _buffer[WriteIndex] = item; - _count++; - } - - public void Write(T[] items) - { - Write(items, 0, items.Length); - } - - public void Write(T[] items, int startIndex, int count) - { - EnsureCapacity(count); - var c = _buffer.Length - WriteIndex; - if (c >= count) - { - Array.Copy(items, startIndex, _buffer, WriteIndex, count); - } - else - { - Array.Copy(items, startIndex, _buffer, WriteIndex, c); - Array.Copy(items, startIndex + c, _buffer, 0, count - c); - } - _count += count; - } - - public T Read() - { - if (_count == 0) - { - throw new InvalidOperationException("No item to read."); - } - var result = _buffer[_index]; - _index = ++_index % _buffer.Length; - _count--; - return result; - } - - public int Read(T[] buffer, int startIndex, int count) - { - var read = Math.Min(count, _count); - var c = Math.Min(read, _buffer.Length - _index); - Array.Copy(_buffer, _index, buffer, startIndex, c); - if (c < read) - { - Array.Copy(_buffer, 0, buffer, startIndex + c, read - c); - } - _index = (_index + read) % _buffer.Length; - _count -= read; - return read; - } - - public int Count => _count; - - private int WriteIndex => (_index + _count) % _buffer.Length; - - private void EnsureCapacity(int count) - { - var c = _count + count; - if (c > _buffer.Length) - { - Resize(Math.Max(c, _buffer.Length * 2)); - } - } - - private void Resize(int capacity) - { - var buffer = new T[capacity]; - if (_index + _count <= _buffer.Length) - { - Array.Copy(_buffer, _index, buffer, 0, _count); - } - else - { - var c = _buffer.Length - _index; - Array.Copy(_buffer, _index, buffer, 0, c); - Array.Copy(_buffer, 0, buffer, c, _count - c); - } - _buffer = buffer; - _index = 0; - } -} diff --git a/src/Microsoft.DocAsCode.Common/CircularStream.cs b/src/Microsoft.DocAsCode.Common/CircularStream.cs deleted file mode 100644 index 114113b5b7e..00000000000 --- a/src/Microsoft.DocAsCode.Common/CircularStream.cs +++ /dev/null @@ -1,147 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace Microsoft.DocAsCode.Common; - -public class CircularStream -{ - private readonly CircularBuffer _buffer = new(4096); - private readonly object _syncRoot = new(); - private bool _eof; - - private int Read(byte[] buffer, int offset, int count) - { - lock (_syncRoot) - { - while (true) - { - var result = _buffer.Read(buffer, offset, count); - if (result == 0 && !_eof) - { - Monitor.Wait(_syncRoot); - } - else - { - return result; - } - } - } - } - - private void Write(byte[] buffer, int offset, int count) - { - lock (_syncRoot) - { - if (_buffer.Count == 0) - { - Monitor.PulseAll(_syncRoot); - } - _buffer.Write(buffer, offset, count); - } - } - - private void Eof() - { - lock (_syncRoot) - { - _eof = true; - Monitor.PulseAll(_syncRoot); - } - } - - public Stream CreateReaderView() - { - return new CircularStreamView(this, false); - } - - public Stream CreateWriterView() - { - return new CircularStreamView(this, true); - } - - private sealed class CircularStreamView : Stream - { - private readonly CircularStream _circularStream; - private readonly bool _writeMode; - - public CircularStreamView(CircularStream circularStream, bool writeMode) - { - _circularStream = circularStream; - _writeMode = writeMode; - } - - public override bool CanRead => !_writeMode; - - public override bool CanSeek => false; - - public override bool CanWrite => _writeMode; - - public override long Length - { - get - { - throw new NotSupportedException(); - } - } - - public override long Position - { - get - { - throw new NotSupportedException(); - } - - set - { - throw new NotSupportedException(); - } - } - - public override void Flush() - { - } - - public override int Read(byte[] buffer, int offset, int count) - { - if (_writeMode) - { - throw new NotSupportedException(); - } - else - { - return _circularStream.Read(buffer, offset, count); - } - } - - public override long Seek(long offset, SeekOrigin origin) - { - throw new NotSupportedException(); - } - - public override void SetLength(long value) - { - throw new NotSupportedException(); - } - - public override void Write(byte[] buffer, int offset, int count) - { - if (_writeMode) - { - _circularStream.Write(buffer, offset, count); - } - else - { - throw new NotSupportedException(); - } - } - - public override void Close() - { - if (_writeMode) - { - _circularStream.Eof(); - } - base.Close(); - } - } -} diff --git a/src/Microsoft.DocAsCode.HtmlToPdf/Constants.cs b/src/Microsoft.DocAsCode.HtmlToPdf/Constants.cs index 4c20a514347..8ab1ed63f1b 100644 --- a/src/Microsoft.DocAsCode.HtmlToPdf/Constants.cs +++ b/src/Microsoft.DocAsCode.HtmlToPdf/Constants.cs @@ -64,7 +64,6 @@ public static class BuildManifestItem public const string OutputRelativePath = "relative_path"; public const string OutputLinkToPath = "link_to_path"; - public const string OutputHash = "hash"; public const string IsRawPage = "is_raw_page"; public const string SkipPublish = "skip_publish"; diff --git a/src/Microsoft.DocAsCode.HtmlToPdf/FileOutput.cs b/src/Microsoft.DocAsCode.HtmlToPdf/FileOutput.cs index 9d7c4079e82..6764531025c 100644 --- a/src/Microsoft.DocAsCode.HtmlToPdf/FileOutput.cs +++ b/src/Microsoft.DocAsCode.HtmlToPdf/FileOutput.cs @@ -15,9 +15,6 @@ public class FileOutput [JsonProperty(ManifestConstants.BuildManifestItem.OutputLinkToPath)] public string LinkToPath { get; set; } - [JsonProperty(ManifestConstants.BuildManifestItem.OutputHash)] - public string Hash { get; set; } - [JsonProperty(ManifestConstants.BuildManifestItem.IsRawPage)] public bool IsRawPage { get; set; } diff --git a/src/Microsoft.DocAsCode.Plugins/OutputFileInfo.cs b/src/Microsoft.DocAsCode.Plugins/OutputFileInfo.cs index 5a74139f8a4..1fd13647c7a 100644 --- a/src/Microsoft.DocAsCode.Plugins/OutputFileInfo.cs +++ b/src/Microsoft.DocAsCode.Plugins/OutputFileInfo.cs @@ -37,18 +37,6 @@ public string LinkToPath } } - [JsonProperty("hash")] - public string Hash - { - get { return _hash; } - set - { - var o = _hash; - _hash = value; - OnPropertyChanged(nameof(Hash), o, value); - } - } - [JsonExtensionData] public Dictionary Metadata { get; set; } = new Dictionary(); diff --git a/test/Microsoft.DocAsCode.Common.Tests/CircularBufferTest.cs b/test/Microsoft.DocAsCode.Common.Tests/CircularBufferTest.cs deleted file mode 100644 index b38433f94e2..00000000000 --- a/test/Microsoft.DocAsCode.Common.Tests/CircularBufferTest.cs +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Security.Cryptography; -using Xunit; - -namespace Microsoft.DocAsCode.Common.Tests; - -[Trait("Related", "CircularBuffer")] -public class CircularBufferTest -{ - [Fact] - public void TestCircularBuffer() - { - var cb = new CircularBuffer(2); - Assert.Throws(() => cb.Read()); - cb.Write(1); - Assert.Equal(1, cb.Read()); - cb.Write(2); - cb.Write(3); - Assert.Equal(2, cb.Read()); - cb.Write(4); - Assert.Equal(new[] { 3, 4 }, Read(cb, 2)); - cb.Write(new[] { 5, 6, 7, 8, 9 }); - Assert.Equal(new[] { 5, 6, 7 }, Read(cb, 3)); - cb.Write(new[] { 10, 11 }); - Assert.Equal(8, cb.Read()); - cb.Write(new[] { 12, 13, 14, 15, 16, 17 }); - Assert.Equal(new[] { 9, 10, 11 }, Read(cb, 3)); - Assert.Equal(new[] { 12, 13, 14, 15, 16, 17 }, Read(cb, 6)); - } - - [Fact] - public void TestCircularBuffer_2() - { - var cb = new CircularBuffer(3); - Assert.Equal(0, cb.Read(new int[10], 0, 10)); - cb.Write(new[] { 1, 2 }); - Assert.Equal(new[] { 1, 2 }, Read(cb, 2)); - cb.Write(new[] { 3, 4 }); - Assert.Equal(new[] { 3, 4 }, Read(cb, 2)); - } - - private static int[] Read(CircularBuffer cb, int count) - { - var buffer = new int[count]; - int read = 0; - while (read < count) - { - read += cb.Read(buffer, read, count - read); - } - return buffer; - } - - [Fact] - public void TestCircularStream() - { - const int LineCount = 100; - var cs = new CircularStream(); - Task.Run(() => - { - using var writer = cs.CreateWriterView(); - using var sw = new StreamWriter(writer); - for (int i = 0; i < LineCount; i++) - { - sw.WriteLine($"Line {i}: test!"); - } - }); - using var reader = cs.CreateReaderView(); - using var sr = new StreamReader(reader); - for (int i = 0; i < LineCount; i++) - { - Assert.Equal($"Line {i}: test!", sr.ReadLine()); - } - Assert.Equal(string.Empty, sr.ReadToEnd()); - } - - [Fact] - public async Task TestCompositeStream() - { - const int LineCount = 100; - var ms = new MemoryStream(); - var cs = new CircularStream(); - var task = Task.Run(() => - { - using var stream = cs.CreateReaderView(); - return SHA256.Create().ComputeHash(stream); - }); - byte[] expected; - using (var ds = new CompositeStream(ms, cs.CreateWriterView())) - { - var sw = new StreamWriter(ds); - for (int i = 0; i < LineCount; i++) - { - sw.WriteLine($"Line {i}: test!"); - } - sw.Flush(); - expected = SHA256.Create().ComputeHash(ms.ToArray()); - } - Assert.Equal(expected, await task); - } -}