commit f968d80d5e87723702e9d64777f735747b4ba95e
parent f56356bdd8554ee8c9574d80cb3e363ad248307a
Author: lash <dev@holbrook.no>
Date: Mon, 2 Sep 2024 02:01:59 +0100
Add workaround for cache.Memory false nil results
Diffstat:
2 files changed, 48 insertions(+), 17 deletions(-)
diff --git a/engine/db.go b/engine/db.go
@@ -117,6 +117,7 @@ func(en *DbEngine) ensureState() {
if en.st.Language != nil {
en.st.SetFlag(state.FLAG_LANG)
}
+ logg.Debugf("new engine state added", "state", en.st)
} else {
if (en.cfg.Language != "") {
if en.st.Language == nil {
@@ -127,17 +128,22 @@ func(en *DbEngine) ensureState() {
}
}
}
-
}
-func(en *DbEngine) ensureMemory() {
- if en.ca == nil {
+func(en *DbEngine) ensureMemory() error {
+ cac, ok := en.ca.(*cache.Cache)
+ if cac == nil {
ca := cache.NewCache()
if en.cfg.CacheSize > 0 {
- ca.WithCacheSize(en.cfg.CacheSize)
+ ca = ca.WithCacheSize(en.cfg.CacheSize)
}
en.ca = ca
+ logg.Debugf("new engine memory added", "memory", en.ca)
+ } else if !ok {
+ return errors.New("memory MUST be *cache.Cache for now. sorry")
}
+
+ return nil
}
func(en *DbEngine) preparePersist() error {
@@ -157,17 +163,23 @@ func(en *DbEngine) preparePersist() error {
en.st = st
}
}
- ca := en.pe.GetMemory().(*cache.Cache)
- if ca != nil {
- if en.ca == nil {
+
+ ca := en.pe.GetMemory()
+ cac, ok := ca.(*cache.Cache)
+ if !ok {
+ return errors.New("memory MUST be *cache.Cache for now. sorry")
+ }
+ if cac != nil {
+ logg.Debugf("ca", "ca", cac)
+ if en.ca != nil {
return errors.New("cache cannot be explicitly set in both persister and engine.")
}
- en.ca = ca
+ en.ca = cac
} else {
if en.ca == nil {
- logg.Debugf("defer persist memory set until state set in engine")
+ logg.Debugf("defer persist memory set until memory set in engine")
} else {
- en.ca = ca
+ en.ca = cac
}
}
return nil
@@ -180,15 +192,18 @@ func(en *DbEngine) ensurePersist() error {
st := en.pe.GetState()
if st == nil {
st = en.st
+ logg.Debugf("using engine state for persister", "state", st)
}
ca := en.pe.GetMemory()
- if ca == nil {
- ca = en.ca
- }
cac, ok := ca.(*cache.Cache)
- if !ok {
- return errors.New("Memory is not a *cache.Cache. For the time being it has to be, because of the way persister serialization is implemented. This hopefully changes in the future.")
+ if cac == nil {
+ cac, ok = en.ca.(*cache.Cache)
+ if !ok {
+ return errors.New("memory MUST be *cache.Cache for now. sorry")
+ }
+ logg.Debugf("using engine memory for persister", "memory", cac)
}
+ logg.Tracef("set persister", "st", st, "cac", cac)
en.pe = en.pe.WithContent(st, cac)
return nil
}
@@ -207,7 +222,10 @@ func(en *DbEngine) prepare() error {
return err
}
en.ensureState()
- en.ensureMemory()
+ err = en.ensureMemory()
+ if err != nil {
+ return err
+ }
err = en.ensurePersist()
if err != nil {
return err
diff --git a/engine/db_test.go b/engine/db_test.go
@@ -8,8 +8,10 @@ import (
"testing"
"git.defalsify.org/vise.git/resource"
+ "git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/state"
"git.defalsify.org/vise.git/vm"
+ memdb "git.defalsify.org/vise.git/db/mem"
)
func getNull() io.WriteCloser {
@@ -52,6 +54,10 @@ func TestDbEngineMinimal(t *testing.T) {
if cont {
t.Fatalf("expected not continue")
}
+ err = en.Finish()
+ if err != nil {
+ t.Fatal(err)
+ }
}
func TestDbEngineRoot(t *testing.T) {
@@ -84,6 +90,10 @@ func TestDbEngineRoot(t *testing.T) {
if err == nil {
t.Fatalf("expected nocode")
}
+ err = en.Finish()
+ if err != nil {
+ t.Fatal(err)
+ }
}
func TestDbEnginePersist(t *testing.T) {
@@ -119,5 +129,8 @@ func TestDbEnginePersist(t *testing.T) {
if err != nil {
t.Fatal(err)
}
-
+ err = en.Finish()
+ if err != nil {
+ t.Fatal(err)
+ }
}