Skip to content

feat: add write coalescing with configurable batch flush#2

Open
jgwesterlund wants to merge 10 commits intomainfrom
feat/write-coalescing
Open

feat: add write coalescing with configurable batch flush#2
jgwesterlund wants to merge 10 commits intomainfrom
feat/write-coalescing

Conversation

@jgwesterlund
Copy link
Owner

Pull Request

What does this change do?

Adds write coalescing to buffer multiple appends and flush them in batches. New constructor params coalesce_max_records and coalesce_max_seconds control flush thresholds. Adds flush(), write_stats property, and include_pending param on iter_records().

Why is this needed?

Reduces I/O overhead for high-throughput workloads by batching writes instead of flushing per-record. Also fixes a bug where _last_flush_time was initialized to 0.0, which meant a config with only coalesce_max_seconds would never trigger time-based flush (the elapsed check required _last_flush_time > 0). Now initialized to time.monotonic().

Type of change

  • Feature
  • Bug fix

How was this tested?

7 new tests in tests/test_extended.py:

  • Batch flush on record count threshold
  • Batch flush on time threshold (covers the bug fix with time-only config)
  • close() flushes pending records
  • flush() explicit call
  • write_stats accuracy
  • iter_records() default excludes pending; include_pending=True includes them
  • Records survive reopen after flush

All 43 tests pass, 99% coverage, ruff clean, mypy clean.

Checklist

  • I tested this locally
  • I didn't reformat the entire repo
  • I wrote sane commit messages
  • I read the contributing guide
  • This probably won't summon new bugs

Notes for reviewers

iter_records() defaults to include_pending=False so it only reflects durable on-disk state. Pending records are opt-in to avoid silently changing on-disk semantics (addressing reviewer concern from original PR).

append() returns a projected sequence number for unflushed records since the actual sequence is assigned on flush.

Docs updated: usage.md, api.md.

jgwesterlund and others added 10 commits March 4, 2026 12:55
Merge pull request #1 from jgwesterlund/fix/ring-open-recovery-safety
Automatically generated by python-semantic-release
Automatically generated by python-semantic-release
lets hope, cause im tired....
Add coalesce_max_records and coalesce_max_seconds parameters to buffer
writes and flush in batches. Initialize _last_flush_time to
time.monotonic() (not 0.0) so time-based flush works from the start.
Add flush(), write_stats, and include_pending parameter to iter_records().

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants