|
| 1 | +// <copyright file="HashIndexDuplicateKeyBatchDeleteTests.cs" company="MPCoreDeveloper"> |
| 2 | +// Copyright (c) 2026 MPCoreDeveloper. All rights reserved. |
| 3 | +// Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 4 | +// </copyright> |
| 5 | +namespace SharpCoreDB.Tests; |
| 6 | + |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using SharpCoreDB.Interfaces; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Globalization; |
| 12 | +using System.IO; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Regression coverage for duplicate-key hash-index removal: batch-DELETEs that hit a non-unique |
| 17 | +/// indexed column with large position groups exercise HashIndex.RemoveBatchKeys's deferred |
| 18 | +/// duplicate-key compaction (previously one O(list) List.Remove shift per duplicate). |
| 19 | +/// </summary> |
| 20 | +public sealed class HashIndexDuplicateKeyBatchDeleteTests : IDisposable |
| 21 | +{ |
| 22 | + private readonly DatabaseFactory _factory; |
| 23 | + private readonly string _dirPath; |
| 24 | + |
| 25 | + public HashIndexDuplicateKeyBatchDeleteTests() |
| 26 | + { |
| 27 | + var services = new ServiceCollection(); |
| 28 | + services.AddSharpCoreDB(); |
| 29 | + _factory = services.BuildServiceProvider().GetRequiredService<DatabaseFactory>(); |
| 30 | + _dirPath = Path.Combine(Path.GetTempPath(), $"SCDB_HashDup_{Guid.NewGuid():N}"); |
| 31 | + } |
| 32 | + |
| 33 | + public void Dispose() |
| 34 | + { |
| 35 | + try { if (Directory.Exists(_dirPath)) Directory.Delete(_dirPath, true); } catch { } |
| 36 | + } |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [InlineData(false)] |
| 40 | + [InlineData(true)] |
| 41 | + public void BatchDelete_DuplicatedNameGroups_RemovesOnlyThoseKeys_AcrossReopen(bool useUnsafeEqualityIndex) |
| 42 | + { |
| 43 | + IDatabase? db = _factory.Create(_dirPath, "pw", isReadOnly: false, |
| 44 | + config: new DatabaseConfig |
| 45 | + { |
| 46 | + NoEncryptMode = true, |
| 47 | + AutoFixedWidthRecords = false, |
| 48 | + EnableUnsafeEqualityIndex = useUnsafeEqualityIndex, |
| 49 | + }); |
| 50 | + try |
| 51 | + { |
| 52 | + db.ExecuteSQL("CREATE TABLE docs (id INTEGER PRIMARY KEY, name TEXT, score REAL)"); |
| 53 | + db.ExecuteSQL("CREATE INDEX idx_docs_name ON docs(name)"); |
| 54 | + |
| 55 | + // 2000 rows, 200 rows per duplicated name group (dup0..dup9). |
| 56 | + var stmts = new List<string>(2000); |
| 57 | + for (int i = 1; i <= 2000; i++) |
| 58 | + { |
| 59 | + stmts.Add(string.Format(CultureInfo.InvariantCulture, |
| 60 | + "INSERT INTO docs VALUES ({0}, 'dup{1}', {2})", i, i % 10, i * 0.5)); |
| 61 | + } |
| 62 | + |
| 63 | + db.ExecuteBatchSQL(stmts); |
| 64 | + db.Flush(); |
| 65 | + |
| 66 | + Assert.Equal(2000, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 67 | + |
| 68 | + // Delete three full duplicate groups in one batch: dup1, dup5, dup9 (600 rows). |
| 69 | + db.ExecuteBatchSQL( |
| 70 | + [ |
| 71 | + "DELETE FROM docs WHERE name = 'dup1'", |
| 72 | + "DELETE FROM docs WHERE name = 'dup5'", |
| 73 | + "DELETE FROM docs WHERE name = 'dup9'", |
| 74 | + ]); |
| 75 | + db.Flush(); |
| 76 | + |
| 77 | + Assert.Equal(1400, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 78 | + Assert.Empty(db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup1'")); |
| 79 | + Assert.Empty(db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup5'")); |
| 80 | + Assert.Empty(db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup9'")); |
| 81 | + Assert.Equal(200, db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup2'").Count); |
| 82 | + Assert.Equal(200, db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup0'").Count); |
| 83 | + |
| 84 | + // Partial group delete: remove half of dup2 by id, keeping the rest reachable. |
| 85 | + var partial = new List<string>(100); |
| 86 | + for (int i = 2; i <= 2000; i += 20) |
| 87 | + { |
| 88 | + partial.Add($"DELETE FROM docs WHERE id = {i}"); |
| 89 | + } |
| 90 | + |
| 91 | + db.ExecuteBatchSQL(partial); |
| 92 | + db.Flush(); |
| 93 | + Assert.Equal(100, db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup2'").Count); |
| 94 | + } |
| 95 | + finally { (db as IDisposable)?.Dispose(); } |
| 96 | + |
| 97 | + // Reopen: tombstoned rows stay gone, live duplicated-key groups stay fully reachable. |
| 98 | + db = _factory.Create(_dirPath, "pw", isReadOnly: false, |
| 99 | + config: new DatabaseConfig |
| 100 | + { |
| 101 | + NoEncryptMode = true, |
| 102 | + AutoFixedWidthRecords = false, |
| 103 | + EnableUnsafeEqualityIndex = useUnsafeEqualityIndex, |
| 104 | + }); |
| 105 | + try |
| 106 | + { |
| 107 | + Assert.Equal(1300, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 108 | + Assert.Empty(db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup1'")); |
| 109 | + Assert.Empty(db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup5'")); |
| 110 | + Assert.Empty(db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup9'")); |
| 111 | + Assert.Equal(100, db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup2'").Count); |
| 112 | + Assert.Equal(200, db.ExecuteQuery("SELECT id FROM docs WHERE name = 'dup0'").Count); |
| 113 | + } |
| 114 | + finally { (db as IDisposable)?.Dispose(); } |
| 115 | + } |
| 116 | +} |
0 commit comments