WAL Record Encoding

medium · database, wal, serialization

WAL Record Encoding

Implement encoding/decoding for WAL log records with integrity checks.

Types

type LogRecord struct {
    LSN     uint64
    Txn     uint32
    PrevLSN uint64
    Type    uint8 // 1=begin, 2=update, 3=commit, 4=abort

    PageID uint32
    Offset uint16
    Before []byte
    After  []byte
}

func EncodeLog(rec LogRecord) []byte
func DecodeLog(data []byte) (LogRecord, bool)

Format (little-endian)

Magic[2] = "WL"
Type[1]
Reserved[1] = 0
LSN[8]
Txn[4]
PrevLSN[8]
PageID[4]
Offset[2]
BeforeLen[2]
AfterLen[2]
Before bytes
After bytes
CRC32[4]  // IEEE, over all bytes except CRC

Rules

  • Non-update records use PageID=0, Offset=0, BeforeLen=0, AfterLen=0.
  • If decoding fails (bad magic, lengths overflow, CRC mismatch), return ok=false.
  • Lengths are <= 256 in tests.

Notes

  • Use hash/crc32 with the IEEE table.
Run tests to see results
No issues detected