Function: iterate in write-ahead-log/wal.py

Date: 2026-05-28

Time: 18:50

WriteAheadLog.iterate

Purpose

iterate provides raw, unfiltered access to every record stored in the WAL — including uncommitted batch operations, COMMIT markers, and CHECKPOINT records. This contrasts with replay, which filters out non-data records and skips uncommitted batches. It exists for diagnostic, inspection, or tooling scenarios where the caller needs to see the full on-disk state of the log, not just the logically committed view.

Contract

Parameters

None beyond self. The method takes no arguments — it always scans the entire log from the beginning.

Return Value

Returns an Iterator[WALRecord]. Each WALRecord contains:

The caller must handle:

Algorithm

1. Acquire lock and flush: Grabs lock, checks if there's an open file descriptor (fd), and flushes it. This ensures any writes buffered in userspace by the current process are pushed to the OS before reading. The lock is released immediately after the flush — it is not held during iteration.

2. Delegate to readallrecords: Uses yield from to lazily stream records. readallrecords iterates over every .wal file in sorted filename order, opens each read-only, and calls readrecord in a loop until EOF (None return) or CRC failure (ValueError).

Side Effects

Error Handling

Usage Patterns

Typical callers use iterate when they need the raw log contents rather than the committed-only view:


wal = WriteAheadLog("/tmp/wal")
for record in wal.iterate():
    print(f"seq={record.seq_num} op={record.op_type} key={record.key}")

Caller obligations:

Dependencies