2023-04-13 00:42:36 +02:00
|
|
|
package persist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2023-04-13 01:38:33 +02:00
|
|
|
"log"
|
2023-04-13 00:42:36 +02:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"github.com/fxamacker/cbor/v2"
|
|
|
|
|
|
|
|
"git.defalsify.org/festive/cache"
|
|
|
|
"git.defalsify.org/festive/state"
|
|
|
|
)
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// FsPersister is an implementation of Persister that saves state to the file system.
|
2023-04-13 00:42:36 +02:00
|
|
|
type FsPersister struct {
|
|
|
|
State *state.State
|
|
|
|
Memory *cache.Cache
|
|
|
|
dir string
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// NewFsPersister creates a new FsPersister.
|
|
|
|
//
|
|
|
|
// The filesystem store will be at the given directory. The directory must exist.
|
2023-04-13 00:42:36 +02:00
|
|
|
func NewFsPersister(dir string) *FsPersister {
|
|
|
|
fp, err := filepath.Abs(dir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &FsPersister{
|
|
|
|
dir: fp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// WithContent sets a current State and Cache object.
|
|
|
|
//
|
|
|
|
// This method is normally called before Serialize / Save.
|
2023-04-13 00:42:36 +02:00
|
|
|
func(p *FsPersister) WithContent(st *state.State, ca *cache.Cache) *FsPersister {
|
|
|
|
p.State = st
|
|
|
|
p.Memory = ca
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// GetState implements the Persister interface.
|
2023-04-13 01:38:33 +02:00
|
|
|
func(p *FsPersister) GetState() *state.State {
|
|
|
|
return p.State
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// GetState implements the Persister interface.
|
2023-04-13 01:38:33 +02:00
|
|
|
func(p *FsPersister) GetMemory() cache.Memory {
|
|
|
|
return p.Memory
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// GetState implements the Persister interface.
|
2023-04-13 00:42:36 +02:00
|
|
|
func(p *FsPersister) Serialize() ([]byte, error) {
|
|
|
|
return cbor.Marshal(p)
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// GetState implements the Persister interface.
|
2023-04-13 00:42:36 +02:00
|
|
|
func(p *FsPersister) Deserialize(b []byte) error {
|
|
|
|
err := cbor.Unmarshal(b, p)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// GetState implements the Persister interface.
|
2023-04-13 00:42:36 +02:00
|
|
|
func(p *FsPersister) Save(key string) error {
|
|
|
|
b, err := p.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fp := path.Join(p.dir, key)
|
2023-04-13 01:38:33 +02:00
|
|
|
log.Printf("saved key %v", key)
|
2023-04-13 00:42:36 +02:00
|
|
|
return ioutil.WriteFile(fp, b, 0600)
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// GetState implements the Persister interface.
|
2023-04-13 00:42:36 +02:00
|
|
|
func(p *FsPersister) Load(key string) error {
|
|
|
|
fp := path.Join(p.dir, key)
|
|
|
|
b, err := ioutil.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = p.Deserialize(b)
|
2023-04-13 01:38:33 +02:00
|
|
|
log.Printf("loaded key %v", key)
|
2023-04-13 00:42:36 +02:00
|
|
|
return err
|
|
|
|
}
|