Slotted Page
Slotted Page
Implement a slotted page that stores variable-length records in a fixed-size byte array.
Types
type SlottedPage struct {
// internal fields
}
func NewSlottedPage(size int) *SlottedPage
func (p *SlottedPage) Insert(record []byte) (slot int, ok bool)
func (p *SlottedPage) Get(slot int) ([]byte, bool)
func (p *SlottedPage) Delete(slot int) bool
func (p *SlottedPage) Compact()
func (p *SlottedPage) FreeSpace() int
Layout
- Data grows from the front.
- Slot directory grows from the back.
- Each slot entry is 4 bytes: offset (uint16) + length (uint16).
Rules
- If there is not enough space for
record+ a slot entry,Insertmust return ok=false. Deletemarks the slot as empty (length=0). It does not remove the slot.Compactrepacks live records to eliminate gaps and updates offsets.Getreturns a copy of the record.
Notes
FreeSpacereturns the contiguous free space between data and slot directory.- Page size in tests is <= 256.
Run tests to see results
No issues detected