|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator.Storage; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Guards <c>DBCC SHRINKDATABASE</c> / <c>DBCC SHRINKFILE</c> trailing-trim: the |
| 7 | +/// reclamation work bounds <see cref="Heap.Pages"/> / <see cref="Heap.LobPages"/> |
| 8 | +/// by the peak working set but never lets the lists fall below their high-water |
| 9 | +/// mark on their own (stable <c>(page, slot)</c> addresses forbid mid-list |
| 10 | +/// removal). A shrink drops the fully-dead / freed pages off the *tail* of those |
| 11 | +/// lists, lowering the high-water mark — but only the trailing run, so interior |
| 12 | +/// dead pages and any version- or lock-pinned tail page stay put. |
| 13 | +/// </summary> |
| 14 | +[TestClass] |
| 15 | +public sealed class ShrinkTests |
| 16 | +{ |
| 17 | + private static Heap HeapFor(SimulatedDbConnection conn, string table) => |
| 18 | + conn.CurrentDatabase.Schemas[Database.DefaultSchemaName].HeapTables[table].Heap; |
| 19 | + |
| 20 | + private static void Exec(SimulatedDbConnection conn, string sql) |
| 21 | + { |
| 22 | + using var cmd = conn.CreateCommand(); |
| 23 | + cmd.CommandText = sql; |
| 24 | + _ = cmd.ExecuteNonQuery(); |
| 25 | + } |
| 26 | + |
| 27 | + // Fill many pages, delete every row (each autocommit DELETE commits dead |
| 28 | + // bytes), then shrink. With the whole heap dead, the trailing run is the |
| 29 | + // entire list, so the page list collapses. |
| 30 | + [TestMethod] |
| 31 | + public void ShrinkDatabase_DropsFullyDeadTrailingDataPages() |
| 32 | + { |
| 33 | + var conn = new Simulation().CreateDbConnection(); |
| 34 | + conn.Open(); |
| 35 | + Exec(conn, "create table t (id int not null primary key, v varchar(7000) not null)"); |
| 36 | + for (var id = 1; id <= 30; id++) |
| 37 | + Exec(conn, $"insert t values ({id}, replicate('a', 7000))"); |
| 38 | + |
| 39 | + var grown = HeapFor(conn, "t").Pages.Count; |
| 40 | + IsGreaterThan(5, grown, "setup should have allocated many page-sized rows"); |
| 41 | + |
| 42 | + Exec(conn, "delete from t"); |
| 43 | + Exec(conn, "dbcc shrinkdatabase (simulated)"); |
| 44 | + |
| 45 | + IsLessThanOrEqualTo(1, HeapFor(conn, "t").Pages.Count, "an all-dead heap should shrink its page list to the trailing-live remainder (here, empty)."); |
| 46 | + } |
| 47 | + |
| 48 | + // Free a whole heap's worth of LOB chains, then shrink: the trailing free |
| 49 | + // run is every LOB page, so the list collapses to near zero. |
| 50 | + [TestMethod] |
| 51 | + public void ShrinkDatabase_DropsFreedTrailingLobPages() |
| 52 | + { |
| 53 | + var conn = new Simulation().CreateDbConnection(); |
| 54 | + conn.Open(); |
| 55 | + Exec(conn, "create table t (id int not null primary key, v nvarchar(max) not null)"); |
| 56 | + for (var id = 1; id <= 20; id++) |
| 57 | + Exec(conn, $"insert t values ({id}, replicate(N'a', 200))"); |
| 58 | + |
| 59 | + IsGreaterThan(0, HeapFor(conn, "t").LobPages.Count, "MAX values should have pushed off-row LOB pages"); |
| 60 | + |
| 61 | + Exec(conn, "delete from t"); |
| 62 | + Exec(conn, "dbcc shrinkdatabase (simulated)"); |
| 63 | + |
| 64 | + IsLessThanOrEqualTo(1, HeapFor(conn, "t").LobPages.Count, "freeing every chain then shrinking should drop the trailing free LOB pages."); |
| 65 | + } |
| 66 | + |
| 67 | + // Delete the *leading* rows but keep a live row on the last page: the tail |
| 68 | + // page isn't dead, so the trailing run is empty and nothing is dropped — |
| 69 | + // even though interior pages are reclaimable. |
| 70 | + [TestMethod] |
| 71 | + public void ShrinkDatabase_KeepsInteriorDeadPagesWhenTailIsLive() |
| 72 | + { |
| 73 | + var conn = new Simulation().CreateDbConnection(); |
| 74 | + conn.Open(); |
| 75 | + Exec(conn, "create table t (id int not null primary key, v varchar(7000) not null)"); |
| 76 | + for (var id = 1; id <= 10; id++) |
| 77 | + Exec(conn, $"insert t values ({id}, replicate('a', 7000))"); |
| 78 | + |
| 79 | + var grown = HeapFor(conn, "t").Pages.Count; |
| 80 | + |
| 81 | + // Kill everything except the row on the last page. |
| 82 | + Exec(conn, "delete from t where id < 10"); |
| 83 | + Exec(conn, "dbcc shrinkdatabase (simulated)"); |
| 84 | + |
| 85 | + HasCount(grown, HeapFor(conn, "t").Pages, "a live row on the tail page blocks the trailing trim; interior dead pages can't be removed without renumbering."); |
| 86 | + } |
| 87 | + |
| 88 | + // A surviving row must read back intact after a shrink rearranges the lists |
| 89 | + // around it. |
| 90 | + [TestMethod] |
| 91 | + public void ShrinkDatabase_PreservesLiveValues() |
| 92 | + { |
| 93 | + var conn = new Simulation().CreateDbConnection(); |
| 94 | + conn.Open(); |
| 95 | + Exec(conn, "create table t (id int not null primary key, v nvarchar(max) not null)"); |
| 96 | + Exec(conn, "insert t values (1, N'keep-me')"); |
| 97 | + for (var id = 2; id <= 20; id++) |
| 98 | + Exec(conn, $"insert t values ({id}, replicate(N'a', 200))"); |
| 99 | + Exec(conn, "delete from t where id >= 2"); |
| 100 | + |
| 101 | + Exec(conn, "dbcc shrinkdatabase (simulated)"); |
| 102 | + |
| 103 | + using var cmd = conn.CreateCommand(); |
| 104 | + cmd.CommandText = "select v from t where id = 1"; |
| 105 | + AreEqual("keep-me", (string)cmd.ExecuteScalar()!); |
| 106 | + } |
| 107 | +} |
0 commit comments