暂且应该用不到处理这类东西, 所以在这里只是提到该接口, 不具体分析.
StatePersistor is used to store consensus state which should survive a process crash
type StatePersistor interface {
StoreState(key string, value []byte) error
ReadState(key string) ([]byte, error)
ReadStateSet(prefix string) (map[string][]byte, error)
DelState(key string)
}
// Helper provides an abstraction to access the Persist column family
// in the database.
type Helper struct{}
// StoreState stores a key,value pair
func (h *Helper) StoreState(key string, value []byte) error {
db := db.GetDBHandle()
return db.Put(db.PersistCF, []byte("consensus."+key), value)
}
// DelState removes a key,value pair
func (h *Helper) DelState(key string) {
db := db.GetDBHandle()
db.Delete(db.PersistCF, []byte("consensus."+key))
}
// ReadState retrieves a value to a key
func (h *Helper) ReadState(key string) ([]byte, error) {
db := db.GetDBHandle()
return db.Get(db.PersistCF, []byte("consensus."+key))
}
// ReadStateSet retrieves all key,value pairs where the key starts with prefix
func (h *Helper) ReadStateSet(prefix string) (map[string][]byte, error) {
db := db.GetDBHandle()
prefixRaw := []byte("consensus." + prefix)
ret := make(map[string][]byte)
it := db.GetIterator(db.PersistCF)
defer it.Close()
for it.Seek(prefixRaw); it.ValidForPrefix(prefixRaw); it.Next() {
key := string(it.Key().Data())
key = key[len("consensus."):]
// copy data from the slice!
ret[key] = append([]byte(nil), it.Value().Data()...)
}
return ret, nil
}