kv: forward-port EncodedSizeForSingleWrite + its test to main (closes #146) - #147
Merged
Conversation
Gateways wrapping the KV API need the exact serialized byte count of a stream-data blob to compute storage cost before constructing the write payload. (*StreamData).Size() works once the data is built; this closed-form helper lets callers compute the size from just the key/data lengths. Pinned against Size() and Encode() across keyLen/dataLen ranges in TestEncodedSizeForSingleWriteMatchesSize. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Cross-check the closed-form helper against both Size() (what it mirrors) and Encode() (what Size() promises), across the key/data length ranges a real KV-backed gateway will hit (single-byte keys, JSON entries up to ~64KiB, plus the 0xFFFF cap). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Closes #146.
What
Cherry-picks two commits that sit on `feat/s3-gateway-additions` but never reached `main` after #138 was squash-merged:
Why
`EncodedSizeForSingleWrite(keyLen, dataLen)` returns the exact byte count that `(*StreamData).Encode()` would produce for a stream-data blob with exactly one `streamWrite` (no reads, no ACLs) — the shape every gateway-side KV submit uses. Lets a caller price storage cost from the (key, data) lengths it will submit, before building the bytes.
`0g-storage-s3-server` imports this helper in `services/preflight_kv.go` for PUT / COPY / DELETE storage-fee reservation. Without it on main, that build fails:
```
services/preflight_kv.go:75:18: undefined: kv.EncodedSizeForSingleWrite
services/preflight_kv.go:90:18: undefined: kv.EncodedSizeForSingleWrite
```
Today the failure is masked by a local `replace` directive pointing the s3-server's `go.mod` at a feat-branch checkout. Tagged-release builds of the gateway against SDK main would not compile.
Test plan
🤖 Generated with Claude Code