Skip to content

Commit 3a18751

Browse files
authored
Fix loading bloom filter binary (#1379)
1 parent 5928733 commit 3a18751

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

src/Elastic.Documentation.LegacyDocs/BloomFilter.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,16 @@ public void Save(string filePath)
141141
}
142142

143143
/// <summary>
144-
/// Loads a Bloom Filter from a binary file.
144+
/// Loads a Bloom Filter from a stream.
145+
/// The stream is expected to contain data in the same format as Save() produces.
145146
/// </summary>
146-
/// <param name="filePath">The path to the file containing the filter data.</param>
147+
/// <param name="stream">The stream containing the filter data.</param>
147148
/// <returns>A new BloomFilter instance.</returns>
148-
public static BloomFilter Load(string filePath)
149+
public static BloomFilter Load(Stream stream)
149150
{
150-
using var stream = File.OpenRead(filePath);
151-
using var reader = new BinaryReader(stream);
151+
// Use a BinaryReader, but leave the stream open as it's managed externally.
152+
using var reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true);
153+
152154
// 1. Read metadata (Size and HashCount)
153155
var size = reader.ReadInt32();
154156
var hashCount = reader.ReadInt32();

src/Elastic.Documentation.LegacyDocs/LegacyPageChecker.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5-
using System.IO.Abstractions;
65
using Elastic.Documentation.Configuration;
76

87
namespace Elastic.Documentation.LegacyDocs;
98

10-
public class LegacyPageChecker(IFileSystem fs)
9+
public class LegacyPageChecker
1110
{
1211
private BloomFilter? _bloomFilter;
13-
private readonly string _bloomFilterBinaryPath = Path.Combine(Paths.WorkingDirectoryRoot.FullName, "src", "Elastic.Documentation.LegacyDocs", "legacy-pages.bloom.bin");
12+
private const string RootNamespace = "Elastic.Documentation.LegacyDocs";
13+
private const string FileName = "legacy-pages.bloom.bin";
14+
private const string ResourceName = $"{RootNamespace}.{FileName}";
15+
private readonly string _bloomFilterBinaryPath = Path.Combine(Paths.WorkingDirectoryRoot.FullName, "src", RootNamespace, FileName);
1416

1517

1618
public bool PathExists(string path)
@@ -19,11 +21,13 @@ public bool PathExists(string path)
1921
return _bloomFilter.Check(path);
2022
}
2123

22-
private BloomFilter LoadBloomFilter()
24+
private static BloomFilter LoadBloomFilter()
2325
{
24-
var bloomFilterBinaryInfo = fs.FileInfo.New(_bloomFilterBinaryPath);
25-
_bloomFilter ??= BloomFilter.Load(bloomFilterBinaryInfo.FullName);
26-
return _bloomFilter;
26+
var assembly = typeof(LegacyPageChecker).Assembly;
27+
using var stream = assembly.GetManifestResourceStream(ResourceName) ?? throw new FileNotFoundException(
28+
$"Embedded resource '{ResourceName}' not found in assembly '{assembly.FullName}'. " +
29+
"Ensure the Build Action for 'legacy-pages.bloom.bin' is 'Embedded Resource' and the path/name is correct.");
30+
return BloomFilter.Load(stream);
2731
}
2832

2933
public void GenerateBloomFilterBinary(IPagesProvider pagesProvider)

src/tooling/docs-assembler/Cli/LegacyDocsCommands.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public async Task<int> CreateBloomBin(string builtDocsDir, Cancel ctx = default)
3333
NoHints = true
3434
}.StartAsync(ctx);
3535
var pagesProvider = new LocalPagesProvider(builtDocsDir);
36-
var legacyPageLookup = new LegacyPageChecker(new FileSystem());
37-
legacyPageLookup.GenerateBloomFilterBinary(pagesProvider);
36+
var legacyPageChecker = new LegacyPageChecker();
37+
legacyPageChecker.GenerateBloomFilterBinary(pagesProvider);
3838
await collector.StopAsync(ctx);
3939
return collector.Errors;
4040
}
@@ -48,8 +48,8 @@ public async Task<int> PageExists(string path, Cancel ctx = default)
4848
{
4949
NoHints = true
5050
}.StartAsync(ctx);
51-
var legacyPageLookup = new LegacyPageChecker(new FileSystem());
52-
var result = legacyPageLookup.PathExists(path);
51+
var legacyPageChecker = new LegacyPageChecker();
52+
var result = legacyPageChecker.PathExists(path);
5353
Console.WriteLine(result ? "exists" : "does not exist");
5454
await collector.StopAsync(ctx);
5555
return collector.Errors;

src/tooling/docs-assembler/Cli/RepositoryCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public async Task<int> BuildAll(
128128

129129
var pathProvider = new GlobalNavigationPathProvider(navigationFile, assembleSources, assembleContext);
130130
var htmlWriter = new GlobalNavigationHtmlWriter(navigationFile, assembleContext, navigation, assembleSources);
131-
var legacyPageChecker = new LegacyPageChecker(assembleContext.ReadFileSystem);
131+
var legacyPageChecker = new LegacyPageChecker();
132132
var historyMapper = new PageLegacyUrlMapper(legacyPageChecker, assembleSources.HistoryMappings);
133133

134134
var builder = new AssemblerBuilder(logger, assembleContext, navigation, htmlWriter, pathProvider, historyMapper);

tests/Elastic.Documentation.LegacyDocs.Tests/LegacyPageLookupTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class LegacyPageCheckerTests
1212
[Fact]
1313
public void TestVersions()
1414
{
15-
var legacyPageChecker = new LegacyPageChecker(new FileSystem());
15+
var legacyPageChecker = new LegacyPageChecker();
1616
var expected = new Dictionary<string, bool>
1717
{
1818
["8.0"] = false,

0 commit comments

Comments
 (0)