WAL Recovery

hard · database, recovery, wal

WAL Recovery (Redo + Undo)

Implement recovery for a toy page store using WAL records.

Types

type Page struct {
    Data []byte // fixed size 32 bytes
    LSN  uint64
}

type LogRecord struct {
    LSN    uint64
    Txn    uint32
    Type   string // "begin", "update", "commit", "abort"
    PageID uint32
    Offset uint16
    Before []byte
    After  []byte
}

func Recover(pages map[uint32]Page, log []LogRecord) map[uint32]Page

Rules

  • Page size is 32 bytes. If a page is missing, treat it as zeroed with LSN=0.
  • Analysis: a transaction is committed if it has a "commit" record.
  • Redo: scan log in increasing LSN; for each update record, apply After if record.LSN > page.LSN.
  • Undo: for each uncommitted transaction, scan log backward and apply Before for its updates.
  • After applying an update (redo or undo), set page.LSN to the record.LSN.

Notes

  • Ignore non-update records in redo/undo.
  • Assume all update offsets and lengths are valid within a 32-byte page.
Run tests to see results
No issues detected