package process import ( "context" "github.com/nats-io/nats.go/jetstream" stdio "git.novox.be/novox/mesh-sdk/go" ) // pruneKV is a fakeKV with a cap: its bytes are what its current values put (stdio.PutBytes), its Status says them // with the cap, WatchAll hands every current value, and Purge removes a key. type pruneKV struct { *fakeKV cap int64 statuss int } type pruneStatus struct { jetstream.KeyValueStatus bytes uint64 cap int64 } func (s pruneStatus) Bytes() uint64 { return s.bytes } func (s pruneStatus) Config() jetstream.KeyValueConfig { return jetstream.KeyValueConfig{MaxBytes: s.cap} } func (f *pruneKV) held() uint64 { f.mu.Lock() defer f.mu.Unlock() var n uint64 for k, v := range f.values { n += stdio.PutBytes(k, v) } return n } func (f *pruneKV) Status(context.Context) (jetstream.KeyValueStatus, error) { f.statuss++ return pruneStatus{bytes: f.held(), cap: f.cap}, nil } func (f *pruneKV) WatchAll(context.Context, ...jetstream.WatchOpt) (jetstream.KeyWatcher, error) { f.mu.Lock() defer f.mu.Unlock() w := &fakeWatcher{updates: make(chan jetstream.KeyValueEntry, len(f.values)+1)} for k, v := range f.values { w.updates <- putEntry{fakeEntry{key: k, value: v, rev: f.revs[k]}} } w.updates <- nil return w, nil } func (f *pruneKV) Purge(ctx context.Context, key string, _ ...jetstream.KVDeleteOpt) error { return f.Delete(ctx, key) } type putEntry struct{ fakeEntry } func (putEntry) Operation() jetstream.KeyValueOp { return jetstream.KeyValuePut }