kv: add optional Snapshotter for consistent point-in-time reads (pebblekv) - #108
Merged
Conversation
Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
Add PebbleDB.Snapshot() backed by *pebble.Snapshot, satisfying the optional kv.Snapshotter capability. The pebbleSnapshot type delegates Get/Scan/ScanRange to the same shared read helpers as PebbleDB so their semantics stay byte-identical, guards Snapshot() on a closed DB, and has an idempotent Close(). NewSnapshot() is added to the pebbleStore seam so tests can substitute a fake. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Tests for the Snapshotter/Snapshot point-in-time read view (SDD tasks T3+T4): consistency vs concurrent writes, Scan/ScanRange isolation, the 0xff prefix bound through the snapshot, Get value-copy + not-found, stop-early, a -race concurrent-readers test (one snapshot, N readers while the DB is written), Close-before-DB-Close ordering (positive) and the negative case (open snapshot makes DB.Close error "leaked snapshots"), Close idempotency, Snapshot() on a closed store returning a literal-nil view, and — via an errSnapReader fake — the underlying-error propagation of Get/Scan/ScanRange/Close. Test-only. go1.24.2: ./kv/... + -race green; gofmt clean; go-cov zero CRITICAL (Snapshot + the 4 pebbleSnapshot methods 100%, pebblekv 92.9%). Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
codetrek
approved these changes
Jul 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds an optional consistent point-in-time read primitive at the
kvlayer:core/kv/kv.go— two NEW interfaces,kv.StoreUNCHANGED:Snapshotter { Snapshot() (Snapshot, error) }— an optional capability a backend implements alongsideStore; callers type-assert (sn, ok := store.(kv.Snapshotter)).Snapshot { Get; Scan; ScanRange; Close }— a read-only view frozen at the moment it was taken.core/kv/pebblekv— implements it viapebble.DB.NewSnapshot(); extracts the shared read helpers (getCopy/scanPrefix/scanRange) soPebbleDB.{Get,Scan,ScanRange}and the snapshot share one source of truth for the subtle0xffprefix bound.Why
Today every
Get/Scanreads at "now", so a multi-read operation has no point-in-time consistency. The motivating case is search: one query fans out into many independent index scans + docid→document reads over a single shared pebble store, and concurrent writes (indexing, deletes, the keyword merger + compaction) can tear that view.pebble.DB.NewSnapshot()fixes it, and because index + docs share one*pebble.DB, a single snapshot can cover an entire query.Scope (intentionally narrow)
This PR only exposes the primitive. It does not wire the snapshot into
search/engine/invertedindex/documents— that is a separate later change. No production caller adoptsSnapshotteryet.Compatibility
Non-breaking:
kv.Storeis untouched, so all existing implementers (pebblekv + the in-repo test fakes + any external backend) keep satisfying it.Snapshotter/Snapshotare purely additive; consumers opt in via type assertion. No on-disk format change, noStorageVersionbump.Design notes (verified against pebble v1.1.5)
Snapshot()guardsIsClosed()(pebble'sNewSnapshot()panics on a closed DB) and returns a literal-nil view on error.pebbleSnapshot.Closeis idempotent (nils its reader unconditionally on first close) — a rawpebble.Snapshot.Closepanics on double-close.Snapshot()is safe with concurrent writes/reads, and one snapshot serves concurrentGet/Scan/ScanRange(each opens its own reader/iterator).Closea snapshot before closing theStore(else pebble'sDB.Closeerrors "leaked snapshots").Verification (go1.24.2, CI parity)
./kv/...+-racegreen.Snapshot+ the 4pebbleSnapshotmethods at 100%, pebblekv 92.9%.gofmtclean; core builds. Behavior of the refactoredGet/Scan/ScanRangeguarded by the existing db_test suite.Built via the SDD flow (spec → 2 review rounds to a clean gate → task breakdown → review → workflow-driven TDD).
🤖 Generated with Claude Code
via Happy