2017-02-27 16:28:41 +00:00
|
|
|
package mapstate
|
|
|
|
|
2018-01-05 04:07:52 +00:00
|
|
|
// To add a new state format
|
|
|
|
// - implement the previous format's "next" function to the new format
|
|
|
|
// - implement the new format's unmarshal function
|
|
|
|
// - add a case to the switch statement for the previous format version
|
|
|
|
// - update the code copying the from mapStateVx to mapState
|
2017-02-27 16:28:41 +00:00
|
|
|
import (
|
2017-11-28 22:45:10 +00:00
|
|
|
"bytes"
|
2017-02-27 16:28:41 +00:00
|
|
|
"errors"
|
|
|
|
|
2017-11-28 22:45:10 +00:00
|
|
|
msgpack "github.com/multiformats/go-multicodec/msgpack"
|
|
|
|
|
2017-02-27 16:28:41 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
)
|
|
|
|
|
2018-01-05 04:07:52 +00:00
|
|
|
// Instances of migrateable can be read from a serialized format and migrated
|
|
|
|
// to other state formats
|
|
|
|
type migrateable interface {
|
|
|
|
next() migrateable
|
|
|
|
unmarshal([]byte) error
|
|
|
|
}
|
|
|
|
|
2017-02-27 16:28:41 +00:00
|
|
|
type mapStateV1 struct {
|
|
|
|
Version int
|
|
|
|
PinMap map[string]struct{}
|
|
|
|
}
|
|
|
|
|
2018-01-05 04:07:52 +00:00
|
|
|
// Unmarshal the serialization of a v1 state
|
|
|
|
func (st *mapStateV1) unmarshal(bs []byte) error {
|
|
|
|
buf := bytes.NewBuffer(bs)
|
|
|
|
dec := msgpack.Multicodec(msgpack.DefaultMsgpackHandle()).Decoder(buf)
|
|
|
|
return dec.Decode(st)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate from v1 to v2
|
|
|
|
func (st *mapStateV1) next() migrateable {
|
|
|
|
var mst2 mapStateV2
|
|
|
|
mst2.PinMap = make(map[string]api.PinSerial)
|
|
|
|
for k := range st.PinMap {
|
|
|
|
mst2.PinMap[k] = api.PinSerial{
|
|
|
|
Cid: k,
|
|
|
|
Allocations: []string{},
|
|
|
|
ReplicationFactor: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &mst2
|
|
|
|
}
|
|
|
|
|
|
|
|
type mapStateV2 struct {
|
|
|
|
PinMap map[string]api.PinSerial
|
|
|
|
Version int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *mapStateV2) unmarshal(bs []byte) error {
|
|
|
|
buf := bytes.NewBuffer(bs)
|
|
|
|
dec := msgpack.Multicodec(msgpack.DefaultMsgpackHandle()).Decoder(buf)
|
|
|
|
return dec.Decode(st)
|
|
|
|
}
|
|
|
|
|
|
|
|
// No migration possible, v2 is the latest state
|
|
|
|
func (st *mapStateV2) next() migrateable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func finalCopy(st *MapState, internal *mapStateV2) {
|
|
|
|
for k := range internal.PinMap {
|
|
|
|
st.PinMap[k] = internal.PinMap[k]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-27 16:28:41 +00:00
|
|
|
func (st *MapState) migrateFrom(version int, snap []byte) error {
|
2018-01-05 04:07:52 +00:00
|
|
|
var m, next migrateable
|
2017-02-27 16:28:41 +00:00
|
|
|
switch version {
|
|
|
|
case 1:
|
2018-01-05 04:07:52 +00:00
|
|
|
var mst1 mapStateV1
|
|
|
|
m = &mst1
|
|
|
|
break
|
2017-11-28 22:45:10 +00:00
|
|
|
|
2017-02-27 16:28:41 +00:00
|
|
|
default:
|
|
|
|
return errors.New("version migration not supported")
|
|
|
|
}
|
2018-01-05 04:07:52 +00:00
|
|
|
|
|
|
|
err := m.unmarshal(snap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
next = m.next()
|
|
|
|
if next == nil {
|
|
|
|
mst2, ok := m.(*mapStateV2)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("migration ended prematurely")
|
|
|
|
}
|
|
|
|
finalCopy(st, mst2)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
m = next
|
|
|
|
}
|
2017-02-27 16:28:41 +00:00
|
|
|
}
|