2019-10-31 21:01:44 +00:00
|
|
|
package statestore
|
|
|
|
|
|
|
|
import (
|
2019-11-01 11:07:05 +00:00
|
|
|
"bytes"
|
2019-11-01 11:14:32 +00:00
|
|
|
"fmt"
|
2019-10-31 21:01:44 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/cborrpc"
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/query"
|
|
|
|
"golang.org/x/xerrors"
|
2019-11-01 11:07:05 +00:00
|
|
|
"reflect"
|
2019-10-31 21:01:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StateStore struct {
|
|
|
|
ds datastore.Datastore
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ds datastore.Datastore) *StateStore {
|
2019-11-01 10:05:48 +00:00
|
|
|
return &StateStore{ds: ds}
|
2019-10-31 21:01:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 11:14:32 +00:00
|
|
|
func toKey(k interface{}) datastore.Key {
|
|
|
|
switch t := k.(type) {
|
|
|
|
case uint64:
|
|
|
|
return datastore.NewKey(fmt.Sprint(t))
|
|
|
|
case fmt.Stringer:
|
|
|
|
return datastore.NewKey(t.String())
|
|
|
|
default:
|
|
|
|
panic("unexpected key type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateStore) Begin(i interface{}, state interface{}) error {
|
|
|
|
k := toKey(i)
|
2019-10-31 21:01:44 +00:00
|
|
|
has, err := st.ds.Has(k)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
return xerrors.Errorf("Already tracking state for %s", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := cborrpc.Dump(state)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return st.ds.Put(k, b)
|
|
|
|
}
|
|
|
|
|
2019-11-01 11:14:32 +00:00
|
|
|
func (st *StateStore) End(i interface{}) error {
|
|
|
|
k := toKey(i)
|
2019-10-31 21:01:44 +00:00
|
|
|
has, err := st.ds.Has(k)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return xerrors.Errorf("No state for %s", i)
|
|
|
|
}
|
|
|
|
return st.ds.Delete(k)
|
|
|
|
}
|
|
|
|
|
2019-11-01 11:07:05 +00:00
|
|
|
func cborMutator(mutator interface{}) func([]byte) ([]byte, error) {
|
|
|
|
rmut := reflect.ValueOf(mutator)
|
|
|
|
|
|
|
|
return func(in []byte) ([]byte, error) {
|
|
|
|
state := reflect.New(rmut.Type().In(0).Elem())
|
|
|
|
|
|
|
|
err := cborrpc.ReadCborRPC(bytes.NewReader(in), state.Interface())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := rmut.Call([]reflect.Value{state})
|
|
|
|
|
|
|
|
if err := out[0].Interface().(error); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cborrpc.Dump(state.Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mutator func(*T) error
|
2019-11-01 11:14:32 +00:00
|
|
|
func (st *StateStore) Mutate(i fmt.Stringer, mutator interface{}) error {
|
2019-11-01 11:07:05 +00:00
|
|
|
return st.mutate(i, cborMutator(mutator))
|
|
|
|
}
|
|
|
|
|
2019-11-01 11:14:32 +00:00
|
|
|
func (st *StateStore) mutate(i interface{}, mutator func([]byte) ([]byte, error)) error {
|
|
|
|
k := toKey(i)
|
2019-10-31 21:01:44 +00:00
|
|
|
has, err := st.ds.Has(k)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return xerrors.Errorf("No state for %s", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
cur, err := st.ds.Get(k)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mutated, err := mutator(cur)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return st.ds.Put(k, mutated)
|
|
|
|
}
|
|
|
|
|
2019-11-01 11:07:05 +00:00
|
|
|
// out: *[]T
|
|
|
|
func (st *StateStore) List(out interface{}) error {
|
2019-10-31 21:01:44 +00:00
|
|
|
res, err := st.ds.Query(query.Query{})
|
|
|
|
if err != nil {
|
2019-11-01 11:07:05 +00:00
|
|
|
return err
|
2019-10-31 21:01:44 +00:00
|
|
|
}
|
|
|
|
defer res.Close()
|
|
|
|
|
2019-11-01 11:07:05 +00:00
|
|
|
outT := reflect.TypeOf(out).Elem().Elem()
|
|
|
|
rout := reflect.ValueOf(out)
|
|
|
|
|
2019-10-31 21:01:44 +00:00
|
|
|
for {
|
|
|
|
res, ok := res.NextSync()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if res.Error != nil {
|
2019-11-01 11:07:05 +00:00
|
|
|
return res.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
elem := reflect.New(outT)
|
|
|
|
err := cborrpc.ReadCborRPC(bytes.NewReader(res.Value), elem.Interface())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-31 21:01:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 11:07:05 +00:00
|
|
|
rout.Set(reflect.Append(rout.Elem(), elem.Elem()))
|
2019-10-31 21:01:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 11:07:05 +00:00
|
|
|
return nil
|
2019-10-31 21:01:44 +00:00
|
|
|
}
|