Skip to content

kv: forward-port EncodedSizeForSingleWrite + its test to main (closes #146) - #147

Merged
0g-peterzhb merged 2 commits into
mainfrom
kv-encoded-size-single-write
May 27, 2026
Merged

kv: forward-port EncodedSizeForSingleWrite + its test to main (closes #146)#147
0g-peterzhb merged 2 commits into
mainfrom
kv-encoded-size-single-write

Conversation

@0g-peterzhb

Copy link
Copy Markdown
Contributor

Closes #146.

What

Cherry-picks two commits that sit on `feat/s3-gateway-additions` but never reached `main` after #138 was squash-merged:

  • `c932f5a kv: add EncodedSizeForSingleWrite helper for preflight pricing`
  • `039b5b1 kv: test EncodedSizeForSingleWrite matches Size() and Encode()`

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

  • `go build ./...` clean against `origin/main` + these two commits
  • `go vet ./...` clean
  • `go test ./kv/...` green (`TestEncodedSizeForSingleWriteMatchesSize` constructs a real one-write `StreamData`, calls `.Encode()`, asserts `len(encoded) == EncodedSizeForSingleWrite(keyLen, dataLen)` — so the formula can't drift from `Size()` without the test breaking)

🤖 Generated with Claude Code

0g-peterzhb and others added 2 commits May 27, 2026 13:51
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>
@0g-peterzhb
0g-peterzhb merged commit 3e799e4 into main May 27, 2026
1 of 2 checks passed
@0g-peterzhb
0g-peterzhb deleted the kv-encoded-size-single-write branch May 27, 2026 05:58
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.

kv: EncodedSizeForSingleWrite + its test exist only on feat/s3-gateway-additions, missing from main

1 participant