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

  • Types are 1=begin, 2=update, 3=commit, 4=abort. EncodeLog returns nil for any other type.
  • For non-update records EncodeLog ignores supplied PageID, Offset, Before and After and emits zero/empty fields.
  • Update images may each contain 0..65535 bytes; EncodeLog returns nil if either is larger.
  • DecodeLog requires valid magic/type, reserved=0, exact size 38+BeforeLen+AfterLen, and the matching IEEE CRC32. Reject extra bytes as well as truncated records.
  • Wire non-update records must have PageID=0, Offset=0 and empty images; otherwise decoding fails even with a matching CRC.
  • Return ok=false on malformed data without panicking. A successful decode owns copies of both images.
  • The codec does not validate LSN history, page capacity, transaction completion or durability.

Notes

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