-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve compaction logic for AddObject
Previously, an add operation would simply append the data object to the end of the data section. If one or more object(s) had previously been deleted from the image, this could result in wasted space. Now, we trim the data section based on in-use objects before adding the new object. Update tests to reflect this new behaviour.
- Loading branch information
Showing
3 changed files
with
17 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved. | ||
// Copyright (c) 2018-2024, Sylabs Inc. All rights reserved. | ||
// Copyright (c) 2017, SingularityWare, LLC. All rights reserved. | ||
// Copyright (c) 2017, Yannick Cote <[email protected]> All rights reserved. | ||
// This software is licensed under a 3-clause BSD license. Please consult the | ||
|
@@ -55,6 +55,20 @@ func writeDataObjectAt(ws io.WriteSeeker, offsetUnaligned int64, di DescriptorIn | |
return nil | ||
} | ||
|
||
// calculatedDataSize calculates the size of the data section based on the in-use descriptors. | ||
func (f *FileImage) calculatedDataSize() int64 { | ||
dataEnd := f.DataOffset() | ||
|
||
f.WithDescriptors(func(d Descriptor) bool { | ||
if objectEnd := d.Offset() + d.Size(); dataEnd < objectEnd { | ||
dataEnd = objectEnd | ||
} | ||
return false | ||
}) | ||
|
||
return dataEnd - f.DataOffset() | ||
} | ||
|
||
var ( | ||
errInsufficientCapacity = errors.New("insufficient descriptor capacity to add data object(s) to image") | ||
errPrimaryPartition = errors.New("image already contains a primary partition") | ||
|
@@ -80,6 +94,8 @@ func (f *FileImage) writeDataObject(i int, di DescriptorInput, t time.Time) erro | |
d := &f.rds[i] | ||
d.ID = uint32(i) + 1 | ||
|
||
f.h.DataSize = f.calculatedDataSize() | ||
|
||
if err := writeDataObjectAt(f.rw, f.h.DataOffset+f.h.DataSize, di, t, d); err != nil { | ||
return err | ||
} | ||
|
Binary file modified
BIN
-3 Bytes
(100%)
pkg/sif/testdata/TestDeleteObjectAndAddObject/NoCompact.golden
Binary file not shown.
Binary file not shown.