Slotted Page

hard · database, storage, pages

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, Insert must return ok=false.
  • Delete marks the slot as empty (length=0). It does not remove the slot.
  • Compact repacks live records to eliminate gaps and updates offsets.
  • Get returns a copy of the record.

Notes

  • FreeSpace returns the contiguous free space between data and slot directory.
  • Page size in tests is <= 256.
Run tests to see results
No issues detected