Skip to content

Commit 928a22c

Browse files
s26-p2: Insert only 5 keys in OptimisticInsertTest (#874)
* s26-p2: Insert only 5 keys in OptimisticInsertTest Inserting 5 keys ensures there is at least one leaf page with at most 2 keys. This allows reliably testing for optimistic insertions across any combination of design decisions such as when a leaf page is considered to have overflowed, how keys are distributed on splits, etc. * s26-p2: Comment why OptimisticInsertTest inserts 5 keys
1 parent 60aa96f commit 928a22c

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

test/storage/b_plus_tree_insert_test.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,41 @@ TEST(BPlusTreeTests, DISABLED_OptimisticInsertTest) {
7070
GenericKey<8> index_key;
7171
RID rid;
7272

73-
size_t num_keys = 25;
73+
// Inserting 5 keys ensures there is at least one leaf page with at most 2 keys. This allows reliably testing for
74+
// optimistic insertions across any combination of design decisions such as when a leaf page is considered to have
75+
// overflowed, how keys are distributed on splits, etc.
76+
//
77+
// Previously, 25 keys were being inserted. This was problematic for a particular combination of design decisions
78+
// where a leaf page is considered to have overflowed only when it exceeds the maximum size, and if the number of keys
79+
// is odd, more keys are distributed to the page left of the spilt. This resulted in leaf pages of size 3 and one leaf
80+
// page of size 4, making it impossible to guarantee an optimistic insertion on any of the leaves.
81+
//
82+
// For Spring 2026, this test is disabled on Gradescope.
83+
size_t num_keys = 5;
7484
for (size_t i = 0; i < num_keys; i++) {
7585
int64_t value = i & 0xFFFFFFFF;
7686
rid.Set(static_cast<int32_t>(i >> 32), value);
7787
index_key.SetFromInteger(2 * i);
7888
tree.Insert(index_key, rid);
7989
}
8090

81-
size_t to_insert = num_keys + 1;
91+
size_t to_insert = 2 * num_keys;
8292
auto leaf = IndexLeaves<GenericKey<8>, RID, GenericComparator<8>>(tree.GetRootPageId(), bpm);
8393
while (leaf.Valid()) {
8494
if (((*leaf)->GetSize() + 1) < (*leaf)->GetMaxSize()) {
8595
to_insert = (*leaf)->KeyAt(0).GetAsInteger() + 1;
8696
}
8797
++leaf;
8898
}
89-
EXPECT_NE(to_insert, num_keys + 1);
99+
EXPECT_NE(to_insert, 2 * num_keys);
90100

91101
auto base_reads = tree.bpm_->GetReads();
92102
auto base_writes = tree.bpm_->GetWrites();
93103

94104
index_key.SetFromInteger(to_insert);
95105
int64_t value = to_insert & 0xFFFFFFFF;
96106
rid.Set(static_cast<int32_t>(to_insert >> 32), value);
97-
tree.Insert(index_key, rid);
107+
EXPECT_TRUE(tree.Insert(index_key, rid));
98108

99109
auto new_reads = tree.bpm_->GetReads();
100110
auto new_writes = tree.bpm_->GetWrites();

0 commit comments

Comments
 (0)