Read a ledger: an instance's state, its history oldest first with sequence and author, a watch of its states, a declared index (issue 1966 T3)

This commit is contained in:
2026-11-03 13:42:33 +01:00
parent ad22c5e9fe
commit 63c1e3faa2
6 changed files with 390 additions and 2 deletions
+49
View File
@@ -1,9 +1,12 @@
package ledger
import (
"encoding/json"
"sort"
"strings"
"sync"
"git.novox.be/novox/mesh-sdk/go/buscalls"
)
// The memory stores keep what the bus keeps, as the bus answers it, for a test of a ledger without a bus: a stream whose
@@ -156,3 +159,49 @@ func (s *MemorySnapshots) Put(key string, value []byte, expect uint64) error {
s.kv[key] = memoryValue{value, s.at}
return nil
}
// MemoryIndex is an index's bucket in memory.
type MemoryIndex struct {
mu sync.Mutex
kv map[string][]byte
}
func (x *MemoryIndex) Entries(pattern string, limit int) ([]buscalls.IndexEntry, error) {
x.mu.Lock()
defer x.mu.Unlock()
if pattern == "" {
pattern = ">"
}
var keys []string
for k := range x.kv {
if Matches(pattern, k) {
keys = append(keys, k)
}
}
sort.Strings(keys)
var out []buscalls.IndexEntry
for _, k := range keys {
if limit > 0 && len(out) >= limit {
break
}
out = append(out, buscalls.IndexEntry{Key: k, Value: json.RawMessage(x.kv[k])})
}
return out, nil
}
func (x *MemoryIndex) Put(key string, value []byte) error {
x.mu.Lock()
defer x.mu.Unlock()
if x.kv == nil {
x.kv = map[string][]byte{}
}
x.kv[key] = value
return nil
}
func (x *MemoryIndex) Delete(key string) error {
x.mu.Lock()
defer x.mu.Unlock()
delete(x.kv, key)
return nil
}