Files
mesh-sdk/go/stateobjects/bundle.go
T
jschoubben 5fa64be894 Revert the revert of G's mesh-sdk merge (#191): G's sdk stands again over reader release 3
Main reverted G's half merge (#189, reverted by 8afc3eb in #191) and then took the reader release's S1-S5, which
copied G's code. G's form holds where they differ (process_bus.go, seats.go, state.go and their tests); the
release's kept names stay so main's callers keep building: SeatViewPrefix beside SeatReadPrefix, and QueueInvoke's
empty strings on not-ok (with its test TestQueueInvoke). The tree differs from G's head f30dee54 only by those and
the release's two tests (objects cap option, seat states and stream kinds).

Builds: design 47 G (the milestone's sdk part, as at f30dee54)

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-13 12:49:56 +01:00

79 lines
2.3 KiB
Go

package stateobjects
import (
"encoding/json"
"errors"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/busstate"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// ForState is a state of this module, kept through its runtime: the state by its local name, and its object store
// `<state>-objects` (catalogue.StateObjectsName).
func ForState(state string) Values {
store := catalogue.StateObjectsName(state)
return Values{KV: BundleKV{State: stdio.State(state)}, Objects: BundleObjects{Store: busstate.ObjectsOf(store)}, Store: store}
}
// BundleKV is a state as a bundle reaches it.
type BundleKV struct{ State stdio.KeptState }
func (b BundleKV) Get(key string) (*Entry, error) {
e, err := b.State.Get(key)
if err != nil || e == nil {
return nil, err
}
return &Entry{Value: e.Value, Revision: e.Revision}, nil
}
func changed(err error) error {
if errors.Is(err, stdio.ErrChanged) {
return ErrChanged
}
return err
}
func (b BundleKV) Create(key string, value []byte) error {
_, err := b.State.Create(key, json.RawMessage(value))
return changed(err)
}
func (b BundleKV) Update(key string, value []byte, revision uint64) error {
_, err := b.State.Update(key, json.RawMessage(value), revision)
return changed(err)
}
func (b BundleKV) Delete(key string) error { return b.State.Delete(key) }
func (b BundleKV) Keys() ([]string, error) { return b.State.Keys() }
// BundleObjects is an object store as a bundle reaches it.
type BundleObjects struct{ Store busstate.Objects }
// MostBytes is zero: a bundle's value of any size is put by a session of chunks, bounded by its store's cap, which the tool
// runner refuses at the chunk that passes it (design 47 G §518).
func (BundleObjects) MostBytes() int { return 0 }
func (b BundleObjects) Put(name string, data []byte) error {
_, err := b.Store.Put(name, data, "a value kept beside its key")
return err
}
func (b BundleObjects) Get(name string) ([]byte, error) {
d, err := b.Store.Get(name)
return d.Data, err
}
func (b BundleObjects) Delete(name string) error { return b.Store.Delete(name) }
func (b BundleObjects) List() ([]Object, error) {
l, err := b.Store.List()
out := make([]Object, 0, len(l))
for _, o := range l {
if !o.Deleted {
out = append(out, Object{Name: o.Name, Size: int64(o.Size), Modified: o.Modified})
}
}
return out, err
}