go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit a4198d48b46a1e8f4d0917a1de1c3a2972e28a47
parent 03876d1a78bd42333f1cab527776dc6a9f274cea
Author: lash <dev@holbrook.no>
Date:   Wed, 11 Sep 2024 01:47:45 +0100

Pop top level cache in persister flush, test usesizes cache

Diffstat:
Mcache/cache.go | 7++++++-
Mpersist/persist.go | 1+
Mpersist/persist_test.go | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 112 insertions(+), 1 deletion(-)

diff --git a/cache/cache.go b/cache/cache.go @@ -132,13 +132,18 @@ func(ca *Cache) Get(key string) (string, error) { // Reset implements the Memory interface. func(ca *Cache) Reset() { + var v string if len(ca.Cache) == 0 { return } ca.Cache = ca.Cache[:1] ca.CacheUseSize = 0 + for _, v = range ca.Cache[0] { + ca.CacheUseSize += uint32(len(v)) + } return } + // Push implements the Memory interface. func (ca *Cache) Push() error { m := make(map[string]string) @@ -157,13 +162,13 @@ func (ca *Cache) Pop() error { for k, v := range m { sz := len(v) ca.CacheUseSize -= uint32(sz) + delete(ca.Sizes, k) logg.Debugf("Cache free", "frame", l, "key", k, "size", sz) } ca.Cache = ca.Cache[:l] if l == 0 { ca.Push() } - //ca.resetCurrent() return nil } diff --git a/persist/persist.go b/persist/persist.go @@ -105,6 +105,7 @@ func(p *Persister) Save(key string) error { if p.flush { logg.Tracef("state and cache flushed from persister") p.Memory.Reset() + p.Memory.Pop() p.State = p.State.CloneEmpty() } return nil diff --git a/persist/persist_test.go b/persist/persist_test.go @@ -9,6 +9,45 @@ import ( "git.defalsify.org/vise.git/cache" ) +func TestCreateCache(t *testing.T) { + ca := cache.NewCache() + if ca.Levels() != 1 { + t.Fatalf("expected level 1, got: %d", ca.Levels()) + } + ca.Push() + ca.Push() + if ca.Levels() != 3 { + t.Fatalf("expected level 3, got: %d", ca.Levels()) + } + ca.Reset() + if ca.Levels() != 1 { + t.Fatalf("expected level 1, got: %d", ca.Levels()) + } +} + +func TestCacheUseSize(t *testing.T) { + ca := cache.NewCache() + v := ca.CacheUseSize + if v != 0 { + t.Fatalf("expected cache use size 0, got: %v", v) + } + ca.Add("foo", "barbarbar", 12) + v = ca.CacheUseSize + if v != 9 { + t.Fatalf("expected cache use size 9, got: %v", v) + } + ca.Reset() + v = ca.CacheUseSize + if v != 9 { + t.Fatalf("expected cache use size 9, got: %v", v) + } + ca.Pop() + v = ca.CacheUseSize + if v != 0 { + t.Fatalf("expected cache use size 0, got: %v", v) + } +} + func TestInvalidateState(t *testing.T) { st := state.NewState(0) ca := cache.NewCache() @@ -75,3 +114,69 @@ func TestInvalidateAll(t *testing.T) { }() _ = pr.Save("foo") } + +func TestFlush(t *testing.T) { + ctx := context.Background() + st := state.NewState(15) + ca := cache.NewCache() + store := mem.NewMemDb() + store.Connect(ctx, "") + + ca.Add("foo", "bar", 0) + ca.Push() + ca.Add("inky", "pinky", 42) + ca.Push() + ca.Add("blinky", "clyde", 13) + ca.WithCacheSize(666) + + st.Down("xyzzy") + st.Down("plugh") + st.SetFlag(3) + st.SetFlag(10) + st.SetFlag(19) + + pe := NewPersister(store).WithContent(st, ca).WithFlush() + err := pe.Save("baz") + if err != nil { + t.Fatal(err) + } + expectBitSize := uint32(15 + 8) + if st.FlagBitSize() != expectBitSize { + t.Fatalf("expected bitsize %d, got %d", expectBitSize, st.FlagBitSize()) + } + st = pe.GetState() + node, lvl := st.Where() + if lvl != 0 { + t.Fatalf("expected level 0, got: %d", lvl) + } + if node != "" { + t.Fatalf("expected node '', got '%s'", node) + } + cm := pe.GetMemory() + if cm.Levels() != 1 { + t.Fatalf("expected level 1, got: %d", cm.Levels()) + } + _, err = cm.Get("foo") + if err == nil { + t.Fatal("expected error") + } + _, err = cm.Get("blinky") + if err == nil { + t.Fatal("expected error") + } + _, err = cm.ReservedSize("foo") + if err == nil { + t.Fatal("expected error") + } + ks := cm.Keys(0) + if len(ks) > 0 { + t.Fatalf("expected keys list length 0, got: %v", ks) + } + o, ok := cm.(*cache.Cache) + if !ok { + panic("not cache") + } + if o.CacheUseSize != 0 { + t.Fatalf("expected cache use size 0, got: %v", o.CacheUseSize) + } +}