nodemgr: save and load nm.nextUpdates

This commit is contained in:
Tzu-Jung Lee 2018-08-14 19:38:49 -07:00
parent 385b5df6c8
commit 85631bd0b9
2 changed files with 26 additions and 0 deletions

View file

@ -63,6 +63,9 @@ func New() (*ClaimTrie, error) {
tr: tr, tr: tr,
cleanup: func() error { cleanup: func() error {
if err := nm.Save(); err != nil {
return errors.Wrapf(err, "nm.Save()")
}
if err := cm.Save(); err != nil { if err := cm.Save(); err != nil {
return errors.Wrapf(err, "cm.Save()") return errors.Wrapf(err, "cm.Save()")
} }

View file

@ -1,6 +1,8 @@
package nodemgr package nodemgr
import ( import (
"bytes"
"encoding/gob"
"fmt" "fmt"
"sort" "sort"
@ -39,6 +41,27 @@ func (nm *NodeMgr) Load(ht claim.Height) {
name := string(iter.Key()) name := string(iter.Key())
nm.cache[name] = nm.load(name, ht) nm.cache[name] = nm.load(name, ht)
} }
data, err := nm.db.Get([]byte("nextUpdates"), nil)
if err == leveldb.ErrNotFound {
return
} else if err != nil {
panic(err)
}
if err = gob.NewDecoder(bytes.NewBuffer(data)).Decode(&nm.nextUpdates); err != nil {
panic(err)
}
}
// Save saves the states to the database.
func (nm *NodeMgr) Save() error {
buf := bytes.NewBuffer(nil)
if err := gob.NewEncoder(buf).Encode(nm.nextUpdates); err != nil {
return errors.Wrapf(err, "gob.Encode()")
}
if err := nm.db.Put([]byte("nextUpdates"), buf.Bytes(), nil); err != nil {
return errors.Wrapf(err, "db.Put()")
}
return nil
} }
// Get returns the latest node with name specified by key. // Get returns the latest node with name specified by key.