|
| 1 | +// tests/SharpCoreDB.Tests/Storage/MetadataSizingTests.cs |
| 2 | +// Copyright (c) 2025-2026 MPCoreDeveloper and GitHub Copilot. All rights reserved. |
| 3 | +// Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +namespace SharpCoreDB.Tests.Storage; |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.IO; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using SharpCoreDB; |
| 11 | +using SharpCoreDB.Storage; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// REGRESSION TESTS: SingleFile metadata region sizing (issue #345). |
| 16 | +/// Verifies the FSM / Block Registry / Table Directory regions are no longer |
| 17 | +/// hard-coded to 4 pages and that the minimum file extension is byte-based |
| 18 | +/// (stays ~10 MB regardless of PageSize). |
| 19 | +/// </summary> |
| 20 | +public sealed class MetadataSizingTests : IDisposable |
| 21 | +{ |
| 22 | + private readonly string _testDbPath; |
| 23 | + |
| 24 | + public MetadataSizingTests() |
| 25 | + { |
| 26 | + _testDbPath = Path.Combine(Path.GetTempPath(), $"metadata_sizing_{Guid.NewGuid():N}.scdb"); |
| 27 | + } |
| 28 | + |
| 29 | + public void Dispose() |
| 30 | + { |
| 31 | + try |
| 32 | + { |
| 33 | + if (File.Exists(_testDbPath)) File.Delete(_testDbPath); |
| 34 | + if (File.Exists(_testDbPath + ".wal")) File.Delete(_testDbPath + ".wal"); |
| 35 | + if (File.Exists(_testDbPath + ".vacuum.tmp")) File.Delete(_testDbPath + ".vacuum.tmp"); |
| 36 | + if (File.Exists(_testDbPath + ".backup")) File.Delete(_testDbPath + ".backup"); |
| 37 | + } |
| 38 | + catch |
| 39 | + { |
| 40 | + // Ignore cleanup errors |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void ConfiguredRegionSizes_AreWrittenToHeader() |
| 46 | + { |
| 47 | + var options = DatabaseOptions.CreateSingleFileDefault(); |
| 48 | + options.PageSize = 4096; |
| 49 | + options.WalBufferSizePages = 64; |
| 50 | + options.EnableMemoryMapping = false; |
| 51 | + options.BlockRegistrySizePages = 8; |
| 52 | + options.FsmSizePages = 16; |
| 53 | + options.TableDirectorySizePages = 12; |
| 54 | + |
| 55 | + using (SingleFileStorageProvider.Open(_testDbPath, options)) |
| 56 | + { |
| 57 | + // Provider writes the header immediately on create; dispose closes the file. |
| 58 | + } |
| 59 | + |
| 60 | + using var fs = new FileStream(_testDbPath, FileMode.Open, FileAccess.Read); |
| 61 | + using var reader = new BinaryReader(fs); |
| 62 | + |
| 63 | + fs.Position = 0x28; // BlockRegistryLength |
| 64 | + var registryLength = reader.ReadUInt64(); |
| 65 | + fs.Position = 0x38; // FsmLength |
| 66 | + var fsmLength = reader.ReadUInt64(); |
| 67 | + fs.Position = 0x58; // TableDirLength |
| 68 | + var tableDirLength = reader.ReadUInt64(); |
| 69 | + |
| 70 | + Assert.Equal((ulong)(4096 * 8), registryLength); |
| 71 | + Assert.Equal((ulong)(4096 * 16), fsmLength); |
| 72 | + Assert.Equal((ulong)(4096 * 12), tableDirLength); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public async Task ManyBlocks_WithLargerRegistry_ShouldRoundtrip() |
| 77 | + { |
| 78 | + var options = DatabaseOptions.CreateSingleFileDefault(); |
| 79 | + options.PageSize = 4096; |
| 80 | + options.WalBufferSizePages = 64; |
| 81 | + options.EnableMemoryMapping = false; |
| 82 | + options.BlockRegistrySizePages = 16; // 64 KB registry ≈ 680 entries |
| 83 | + |
| 84 | + const int blockCount = 300; // exceeds the default 4-page (~170) capacity |
| 85 | + var payload = new byte[64]; |
| 86 | + for (var i = 0; i < payload.Length; i++) |
| 87 | + { |
| 88 | + payload[i] = (byte)(i % 251); |
| 89 | + } |
| 90 | + |
| 91 | + using (var provider = SingleFileStorageProvider.Open(_testDbPath, options)) |
| 92 | + { |
| 93 | + for (var i = 0; i < blockCount; i++) |
| 94 | + { |
| 95 | + await provider.WriteBlockAsync($"blk_{i}", payload); |
| 96 | + } |
| 97 | + |
| 98 | + // ForceFlushAsync = registry force flush + WAL checkpoint (full durability), |
| 99 | + // avoiding a race between the periodic registry flusher and the reopen below. |
| 100 | + await provider.ForceFlushAsync(); |
| 101 | + } |
| 102 | + |
| 103 | + using var reopened = SingleFileStorageProvider.Open(_testDbPath, options); |
| 104 | + for (var i = 0; i < blockCount; i++) |
| 105 | + { |
| 106 | + var data = await reopened.ReadBlockAsync($"blk_{i}"); |
| 107 | + Assert.NotNull(data); |
| 108 | + Assert.Equal(payload, data); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public async Task MinExtension_IsByteBased_NotPageBased() |
| 114 | + { |
| 115 | + var options = DatabaseOptions.CreateSingleFileDefault(); |
| 116 | + options.PageSize = 16384; // 16 KB pages |
| 117 | + options.WalBufferSizePages = 64; // keep the WAL small for the assertion |
| 118 | + options.EnableMemoryMapping = false; |
| 119 | + |
| 120 | + long fileSize; |
| 121 | + using (var provider = SingleFileStorageProvider.Open(_testDbPath, options)) |
| 122 | + { |
| 123 | + await provider.WriteBlockAsync("blob", new byte[8192]); |
| 124 | + await provider.FlushAsync(); |
| 125 | + fileSize = new FileInfo(_testDbPath).Length; |
| 126 | + } |
| 127 | + |
| 128 | + // Old behavior: MIN_EXTENSION_PAGES=2560 * 16 KB = 40 MB -> file ~41 MB. |
| 129 | + // Byte-based behavior: ~10 MB extension -> file ~11-12 MB. |
| 130 | + Assert.True( |
| 131 | + fileSize < 20L * 1024 * 1024, |
| 132 | + $"File grew to {fileSize} bytes; expected a ~10 MB byte-based extension, not a page-count-based one."); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public void InvalidRegionSizes_ShouldThrowOnValidate() |
| 137 | + { |
| 138 | + var fsmZero = DatabaseOptions.CreateSingleFileDefault(); |
| 139 | + fsmZero.FsmSizePages = 0; |
| 140 | + Assert.Throws<ArgumentException>(() => fsmZero.Validate()); |
| 141 | + |
| 142 | + var registryNegative = DatabaseOptions.CreateSingleFileDefault(); |
| 143 | + registryNegative.BlockRegistrySizePages = -1; |
| 144 | + Assert.Throws<ArgumentException>(() => registryNegative.Validate()); |
| 145 | + |
| 146 | + var tableDirTooBig = DatabaseOptions.CreateSingleFileDefault(); |
| 147 | + tableDirTooBig.TableDirectorySizePages = 70000; |
| 148 | + Assert.Throws<ArgumentException>(() => tableDirTooBig.Validate()); |
| 149 | + } |
| 150 | +} |
0 commit comments