The runtime's channel carries 16 MiB a message, so a value over the 8 MiB a put takes is split over objects (Ref.Parts). Builds: design 47 G §57, §138 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
82 lines
2.4 KiB
Go
82 lines
2.4 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 }
|
|
|
|
// BundleObjectMost is the largest object a bundle puts in one call: the runtime's channel carries a message of 16 MiB, and
|
|
// the bytes travel in it as base64.
|
|
const BundleObjectMost = 8 << 20
|
|
|
|
// MostBytes is BundleObjectMost: a larger value is split over objects.
|
|
func (BundleObjects) MostBytes() int { return BundleObjectMost }
|
|
|
|
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
|
|
}
|