Skip to content

feat: let a callback read its key's value undecoded - #504

Open
abligh wants to merge 1 commit into
lovoo:masterfrom
abligh:fix-raw-value
Open

abligh wants to merge 1 commit into
lovoo:masterfrom
abligh:fix-raw-value

Conversation

@abligh

@abligh abligh commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Why

A callback that only needs to know whether a key's value has changed since it last looked has to decode the value through the table's codec to see it. A periodic VisitAll pass over a large group table is the usual example. With a costly codec such as encoding/gob, that decode is most of the pass. Comparing the stored bytes, or a hash of them, would avoid it, but Context gives no way to read them.

The change

The Context a callback is given, including a visit callback's, now also implements a new optional interface:

type RawValuer interface {
	RawValue() []byte
}

RawValue returns the key's value as the table stores it, still encoded, or nil if the key has none. A storage error fails the context, as it does for Value. The slice must be neither modified nor kept after the callback returns. The interface is reached by type assertion (ctx.(goka.RawValuer)), so Context and its other implementations, mocks included, do not change. Value now reads through the same internal lookup, so the two cannot disagree about what is stored.

Tests

  • TestContext_RawValue checks three things: a codec.String table returns the stored bytes, which Value would decode; an absent key returns nil; and a storage error fails the context.
  • TestContext_RawValueStateless checks the refusal on a processor with no group table, the same refusal Value gives.

go test -race ./... passes.

A callback that only needs to know whether a value has changed since it
last looked, such as a periodic pass over a whole group table, had to
decode every value through the table's codec to see it. For a codec as
costly as encoding/gob, that decode is most of the pass.

The Context a callback is given, visit callbacks included, now also
implements RawValuer. RawValue returns the key's value as the table
stores it, encoded, or nil for an absent key, and fails the context on
a storage error as Value does. The slice must be neither modified nor
kept after the callback. It is an optional interface reached by type
assertion, so Context and its other implementations are unchanged.
Value reads through the same rawValueForKey, so the two cannot disagree
about what is stored.

TestContext_RawValue pins the bytes as stored (a codec.String table,
which Value would decode), nil for an absent key, and a storage error
failing the context. TestContext_RawValueStateless pins the stateless
processor's refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant

⌂ 换网址