-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbtree_rebalancing_manual_test.go
More file actions
361 lines (303 loc) · 10.3 KB
/
btree_rebalancing_manual_test.go
File metadata and controls
361 lines (303 loc) · 10.3 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (c) 2025 SciGo HDF5 Library Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
package hdf5_test
import (
"fmt"
"os"
"testing"
"github.com/scigolib/hdf5"
)
// TestDatasetWriter_RebalanceAttributeBTree_CompactStorage tests rebalancing on compact storage.
//
// Compact storage (0-7 attributes) doesn't use B-trees, so rebalancing should be a no-op.
func TestDatasetWriter_RebalanceAttributeBTree_CompactStorage(t *testing.T) {
filename := "testdata/rebalance_compact.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("CreateForWrite failed: %v", err)
}
defer fw.Close()
ds, err := fw.CreateDataset("/data", hdf5.Float64, []uint64{10})
if err != nil {
t.Fatalf("CreateDataset failed: %v", err)
}
// Add 5 attributes (compact storage)
for i := 0; i < 5; i++ {
if err := ds.WriteAttribute(fmt.Sprintf("attr_%d", i), int32(i)); err != nil {
t.Fatalf("WriteAttribute failed: %v", err)
}
}
// Rebalance should work (no-op for compact storage)
if err := ds.RebalanceAttributeBTree(); err != nil {
t.Errorf("RebalanceAttributeBTree failed: %v", err)
}
}
// TestDatasetWriter_RebalanceAttributeBTree_DenseStorage tests rebalancing on dense storage.
//
// Dense storage (8+ attributes) uses B-trees. For MVP (single-leaf), rebalancing is a no-op
// but should execute without errors.
func TestDatasetWriter_RebalanceAttributeBTree_DenseStorage(t *testing.T) {
filename := "testdata/rebalance_dense.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("CreateForWrite failed: %v", err)
}
defer fw.Close()
ds, err := fw.CreateDataset("/data", hdf5.Float64, []uint64{10})
if err != nil {
t.Fatalf("CreateDataset failed: %v", err)
}
// Add 10 attributes (triggers dense storage at 8)
for i := 0; i < 10; i++ {
if err := ds.WriteAttribute(fmt.Sprintf("attr_%d", i), int32(i)); err != nil {
t.Fatalf("WriteAttribute failed: %v", err)
}
}
// Rebalance should work
if err := ds.RebalanceAttributeBTree(); err != nil {
t.Errorf("RebalanceAttributeBTree failed: %v", err)
}
}
// TestDatasetWriter_RebalanceAttributeBTree_AfterDeletion tests rebalancing after batch deletions.
//
// This is the primary use case: disable rebalancing, delete many attributes, then rebalance manually.
func TestDatasetWriter_RebalanceAttributeBTree_AfterDeletion(t *testing.T) {
filename := "testdata/rebalance_after_deletion.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate,
hdf5.WithBTreeRebalancing(false), // Disable auto-rebalancing
)
if err != nil {
t.Fatalf("CreateForWrite failed: %v", err)
}
defer fw.Close()
ds, err := fw.CreateDataset("/data", hdf5.Float64, []uint64{10})
if err != nil {
t.Fatalf("CreateDataset failed: %v", err)
}
// Add 20 attributes
for i := 0; i < 20; i++ {
if err := ds.WriteAttribute(fmt.Sprintf("attr_%d", i), int32(i)); err != nil {
t.Fatalf("WriteAttribute failed: %v", err)
}
}
// Delete 10 attributes (fast, no rebalancing)
for i := 0; i < 10; i++ {
if err := ds.DeleteAttribute(fmt.Sprintf("attr_%d", i)); err != nil {
t.Fatalf("DeleteAttribute failed: %v", err)
}
}
// Manual rebalance
if err := ds.RebalanceAttributeBTree(); err != nil {
t.Errorf("RebalanceAttributeBTree failed: %v", err)
}
// Verify remaining attributes still accessible (validate B-tree integrity)
// Note: This would require Read API, which is not in write-only package
// For now, just verify no errors during rebalancing
}
// TestDatasetWriter_RebalanceAttributeBTree_NoAttributes tests rebalancing on dataset with no attributes.
//
// Should be a no-op without errors.
func TestDatasetWriter_RebalanceAttributeBTree_NoAttributes(t *testing.T) {
filename := "testdata/rebalance_no_attrs.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("CreateForWrite failed: %v", err)
}
defer fw.Close()
ds, err := fw.CreateDataset("/data", hdf5.Float64, []uint64{10})
if err != nil {
t.Fatalf("CreateDataset failed: %v", err)
}
// No attributes added
// Rebalance should work (no-op)
if err := ds.RebalanceAttributeBTree(); err != nil {
t.Errorf("RebalanceAttributeBTree failed: %v", err)
}
}
// TestDatasetWriter_RebalanceAttributeBTree_OpenForWrite tests rebalancing on reopened file.
//
// Tests the RMW (read-modify-write) path with cached object header.
func TestDatasetWriter_RebalanceAttributeBTree_OpenForWrite(t *testing.T) {
filename := "tmp/rebalance_open_for_write.h5"
defer os.Remove(filename)
// Create file with dense attributes
{
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("CreateForWrite failed: %v", err)
}
ds, err := fw.CreateDataset("/data", hdf5.Float64, []uint64{10})
if err != nil {
t.Fatalf("CreateDataset failed: %v", err)
}
// Add 10 attributes (triggers dense storage)
for i := 0; i < 10; i++ {
if err := ds.WriteAttribute(fmt.Sprintf("attr_%d", i), int32(i)); err != nil {
t.Fatalf("WriteAttribute failed: %v", err)
}
}
fw.Close()
}
// Reopen and test rebalancing
{
fw, err := hdf5.OpenForWrite(filename, hdf5.OpenReadWrite)
if err != nil {
t.Fatalf("OpenForWrite failed: %v", err)
}
defer fw.Close()
ds, err := fw.OpenDataset("/data")
if err != nil {
t.Fatalf("OpenDataset failed: %v", err)
}
// Rebalance should work with cached object header
if err := ds.RebalanceAttributeBTree(); err != nil {
t.Errorf("RebalanceAttributeBTree failed: %v", err)
}
}
}
// TestFileWriter_RebalanceAllBTrees tests global rebalancing.
//
// This tests the FileWriter.RebalanceAllBTrees() method that rebalances
// all datasets in the file.
func TestFileWriter_RebalanceAllBTrees(t *testing.T) {
filename := "testdata/rebalance_all_btrees.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate,
hdf5.WithBTreeRebalancing(false),
)
if err != nil {
t.Fatalf("CreateForWrite failed: %v", err)
}
defer fw.Close()
// Create multiple datasets with attributes
for i := 0; i < 3; i++ {
ds, err := fw.CreateDataset(fmt.Sprintf("/dataset_%d", i), hdf5.Float64, []uint64{10})
if err != nil {
t.Fatalf("CreateDataset failed: %v", err)
}
// Add 15 attributes to each dataset
for j := 0; j < 15; j++ {
if err := ds.WriteAttribute(fmt.Sprintf("attr_%d", j), int32(j)); err != nil {
t.Fatalf("WriteAttribute failed: %v", err)
}
}
// Delete 7 attributes
for j := 0; j < 7; j++ {
if err := ds.DeleteAttribute(fmt.Sprintf("attr_%d", j)); err != nil {
t.Fatalf("DeleteAttribute failed: %v", err)
}
}
}
// Rebalance all B-trees
if err := fw.RebalanceAllBTrees(); err != nil {
t.Errorf("RebalanceAllBTrees failed: %v", err)
}
}
// TestFileWriter_RebalanceAllBTrees_EmptyFile tests rebalancing on empty file.
//
// Should be a no-op without errors.
func TestFileWriter_RebalanceAllBTrees_EmptyFile(t *testing.T) {
filename := "testdata/rebalance_empty_file.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("CreateForWrite failed: %v", err)
}
defer fw.Close()
// No datasets created
// Rebalance should work (no-op)
if err := fw.RebalanceAllBTrees(); err != nil {
t.Errorf("RebalanceAllBTrees failed: %v", err)
}
}
// TestBatchDeletionWorkflow tests the recommended batch deletion workflow.
//
// This demonstrates the optimal pattern for batch deletions:
// 1. Disable rebalancing
// 2. Delete many attributes (fast)
// 3. Manual rebalance once
// 4. Re-enable rebalancing
func TestBatchDeletionWorkflow(t *testing.T) {
filename := "testdata/batch_deletion_workflow.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("CreateForWrite failed: %v", err)
}
defer fw.Close()
ds, err := fw.CreateDataset("/data", hdf5.Float64, []uint64{10})
if err != nil {
t.Fatalf("CreateDataset failed: %v", err)
}
// Add 100 attributes
for i := 0; i < 100; i++ {
if err := ds.WriteAttribute(fmt.Sprintf("attr_%d", i), int32(i)); err != nil {
t.Fatalf("WriteAttribute failed: %v", err)
}
}
// Step 1: Disable rebalancing
fw.DisableRebalancing()
if fw.RebalancingEnabled() {
t.Error("Expected rebalancing to be disabled")
}
// Step 2: Delete 50 attributes (fast, no rebalancing)
for i := 0; i < 50; i++ {
if err := ds.DeleteAttribute(fmt.Sprintf("attr_%d", i)); err != nil {
t.Fatalf("DeleteAttribute failed: %v", err)
}
}
// Step 3: Manual rebalance once
if err := ds.RebalanceAttributeBTree(); err != nil {
t.Errorf("RebalanceAttributeBTree failed: %v", err)
}
// Step 4: Re-enable rebalancing
fw.EnableRebalancing()
if !fw.RebalancingEnabled() {
t.Error("Expected rebalancing to be enabled")
}
// Continue with normal operations (auto-rebalancing now active)
// Delete a few more (with rebalancing)
for i := 50; i < 55; i++ {
if err := ds.DeleteAttribute(fmt.Sprintf("attr_%d", i)); err != nil {
t.Fatalf("DeleteAttribute failed: %v", err)
}
}
}
// TestRebalancing_MultipleInvocations tests that rebalancing can be called multiple times.
//
// Calling RebalanceAttributeBTree() multiple times should be safe (idempotent for MVP).
func TestRebalancing_MultipleInvocations(t *testing.T) {
filename := "testdata/rebalance_multiple.h5"
defer os.Remove(filename)
fw, err := hdf5.CreateForWrite(filename, hdf5.CreateTruncate)
if err != nil {
t.Fatalf("CreateForWrite failed: %v", err)
}
defer fw.Close()
ds, err := fw.CreateDataset("/data", hdf5.Float64, []uint64{10})
if err != nil {
t.Fatalf("CreateDataset failed: %v", err)
}
// Add 10 attributes
for i := 0; i < 10; i++ {
if err := ds.WriteAttribute(fmt.Sprintf("attr_%d", i), int32(i)); err != nil {
t.Fatalf("WriteAttribute failed: %v", err)
}
}
// Call rebalance multiple times (should be safe)
for i := 0; i < 5; i++ {
if err := ds.RebalanceAttributeBTree(); err != nil {
t.Errorf("RebalanceAttributeBTree invocation %d failed: %v", i+1, err)
}
}
// Also test FileWriter global rebalancing
for i := 0; i < 3; i++ {
if err := fw.RebalanceAllBTrees(); err != nil {
t.Errorf("RebalanceAllBTrees invocation %d failed: %v", i+1, err)
}
}
}