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 // `-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 }