-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCompact4.cs
More file actions
181 lines (139 loc) · 6.16 KB
/
TestCompact4.cs
File metadata and controls
181 lines (139 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using ArcOne;
using UnitTestOne;
namespace UnitTestFour
{
[TestClass]
public sealed class UnitThree
{
private bool IsDebugStress = TestSettings.CanDebugStress;
[TestMethod]
public void IntegrityCheckOne()
{
string path = "carrot.db";
File.Delete(path);
using (var tree = new BTree(path, order: 10))
{
// 1. Build a specific structure: Root with two children
// Inserting 10, 20, 30. With Order 4, this may stay in one node.
// We insert more to force a split.
int[] keys = { 10, 20, 30, 40, 50, 60 };
foreach (var k in keys) tree.Insert(k, k * 100);
// 2. Perform a deletion that triggers MergeChildren
// We delete keys until a node hits t-1 and its sibling is also thin.
tree.Delete(60, 6000);
tree.Delete(50, 5000);
int count = tree.CountKeys();
Assert.AreEqual(4, count, "Missing Keys.");
// 3. Run Integrity Check prior to compaction
// This ensures Case 3 didn't leave orphaned IDs in Kids[]
tree.ValidateIntegrity();
// 4. Verify physical space management
// Ensure deleted node IDs were pushed to FreeList.
tree.Compact();
Assert.IsTrue(tree.Header.NodeCount < 5, "Compaction failed.");
// Zombies
Assert.AreEqual(0, tree.CountZombies(), "Zombies");
}
File.Delete(path);
}
[TestMethod]
public void CompactTestOne()
{
string path = "delta.db";
File.Delete(path);
int count = 1000;
int deleteCount = 800;
using (var tree = new BTree(path, order: 10))
{
var random = new Random();
var keys = new HashSet<int>();
while (keys.Count < count)
{
int k = random.Next(1, 100000);
if (keys.Add(k)) tree.Insert(k, k * 2);
}
// Delete a significant portion (80%) to create major fragmentation holes in the file.
var keysToDelete = keys.Take(deleteCount).ToList();
foreach (var k in keysToDelete)
{
tree.Delete(k, k * 2);
keys.Remove(k);
}
tree.ValidateIntegrity();
long sizeBefore = new FileInfo(path).Length;
// COMPACT
tree.Compact();
long sizeAfter = new FileInfo(path).Length;
// 1. PHYSICAL ASSERT: File must be smaller.
Assert.IsTrue(sizeAfter < sizeBefore, $"Compaction failed. Before: {sizeBefore}, After: {sizeAfter}");
// 2. INTEGRITY ASSERT: Root must be valid.
Assert.IsFalse(tree.Header.RootId < 0, "Root lost");
// 3. DATA ASSERT: Every remaining key must still be searchable and correct.
foreach (var k in keys)
{
Element result;
Assert.IsTrue(tree.TrySearch(k, out result), $"Key {k} missing");
Assert.AreEqual(k * 2, result.Data, "Corrupted");
}
// 4. STRUCTURE ASSERT: Ensure the B-Tree logic still holds
tree.CheckGhost();
count = tree.CountKeys();
Assert.AreEqual(keys.Count, count, "Missing Keys");
Assert.AreEqual(0, tree.CountZombies(), "Zombies");
// 5. LEAF CHAIN ASSERT: Verify horizontal integrity
// If NextLeafId mapping failed, this will crash or return a wrong count.
int leafLinkCount = tree.GetLeafChainCount();
Assert.IsTrue(leafLinkCount > 0, "Leaf chain broken");
// 6. FREELIST ASSERT: Ensure the holes were truly welded shut.
Assert.AreEqual(0, tree.Header.FreeListCount, "FreeList not cleared");
Assert.AreEqual(0, tree.Header.FreeListOffset, "FreeListOffset should be 0.");
}
File.Delete(path);
}
[TestMethod]
public void CompactPreservesData()
{
if (!IsDebugStress)
{
Assert.Inconclusive("Skipped");
}
string path = "sam.db";
File.Delete(path);
int count = 1000;
int deleteCount = 100;
var rnd = new Random();
var keys = new HashSet<int>();
using (var tree = new BTree(path, order: 10))
{
while (keys.Count < count)
{
int k = rnd.Next(1, 1000000);
if (keys.Add(k)) tree.Insert(k, k * 2);
}
// Delete some keys
var toDelete = keys.Take(deleteCount).ToList();
foreach (var k in toDelete)
{
tree.Delete(k, k * 2);
keys.Remove(k);
}
tree.ValidateIntegrity();
long before = new FileInfo(path).Length;
tree.Compact();
long after = new FileInfo(path).Length;
// File should shrink and data preserved
Assert.IsTrue(after <= before, $"Compact did not shrink file: before={before} after={after}");
// All remaining keys must still be searchable
foreach (var k in keys)
{
Element e;
Assert.IsTrue(tree.TrySearch(k, out e), $"Missing key after compact: {k}");
Assert.AreEqual(k * 2, e.Data);
}
tree.ValidateIntegrity();
Assert.AreEqual(0, tree.CountZombies(), "Zombies present after compact");
}
File.Delete(path);
}
}
}