Migration to state specifying recursive attribute
License: MIT Signed-off-by: Wyatt Daviau <wdaviau@cs.stanford.edu>
This commit is contained in:
parent
1b7b9185e2
commit
bdc373de61
|
@ -19,7 +19,7 @@ import (
|
|||
|
||||
// Version is the map state Version. States with old versions should
|
||||
// perform an upgrade before.
|
||||
const Version = 3
|
||||
const Version = 4
|
||||
|
||||
var logger = logging.Logger("mapstate")
|
||||
|
||||
|
|
|
@ -21,6 +21,8 @@ type migrateable interface {
|
|||
unmarshal([]byte) error
|
||||
}
|
||||
|
||||
/* V1 */
|
||||
|
||||
type mapStateV1 struct {
|
||||
Version int
|
||||
PinMap map[string]struct{}
|
||||
|
@ -47,6 +49,8 @@ func (st *mapStateV1) next() migrateable {
|
|||
return &mst2
|
||||
}
|
||||
|
||||
/* V2 */
|
||||
|
||||
type pinSerialV2 struct {
|
||||
Cid string `json:"cid"`
|
||||
Name string `json:"name"`
|
||||
|
@ -67,9 +71,9 @@ func (st *mapStateV2) unmarshal(bs []byte) error {
|
|||
|
||||
func (st *mapStateV2) next() migrateable {
|
||||
var mst3 mapStateV3
|
||||
mst3.PinMap = make(map[string]api.PinSerial)
|
||||
mst3.PinMap = make(map[string]pinSerialV3)
|
||||
for k, v := range st.PinMap {
|
||||
mst3.PinMap[k] = api.PinSerial{
|
||||
mst3.PinMap[k] = pinSerialV3{
|
||||
Cid: v.Cid,
|
||||
Name: v.Name,
|
||||
Allocations: v.Allocations,
|
||||
|
@ -80,8 +84,18 @@ func (st *mapStateV2) next() migrateable {
|
|||
return &mst3
|
||||
}
|
||||
|
||||
/* V3 */
|
||||
|
||||
type pinSerialV3 struct {
|
||||
Cid string `json:"cid"`
|
||||
Name string `json:"name"`
|
||||
Allocations []string `json:"allocations"`
|
||||
ReplicationFactorMin int `json:"replication_factor_min"`
|
||||
ReplicationFactorMax int `json:"replication_factor_max"`
|
||||
}
|
||||
|
||||
type mapStateV3 struct {
|
||||
PinMap map[string]api.PinSerial
|
||||
PinMap map[string]pinSerialV3
|
||||
Version int
|
||||
}
|
||||
|
||||
|
@ -92,10 +106,39 @@ func (st *mapStateV3) unmarshal(bs []byte) error {
|
|||
}
|
||||
|
||||
func (st *mapStateV3) next() migrateable {
|
||||
var mst4 mapStateV4
|
||||
mst4.PinMap = make(map[string]api.PinSerial)
|
||||
for k, v := range st.PinMap {
|
||||
mst4.PinMap[k] = api.PinSerial{
|
||||
Cid: v.Cid,
|
||||
Name: v.Name,
|
||||
Allocations: v.Allocations,
|
||||
ReplicationFactorMin: v.ReplicationFactorMin,
|
||||
ReplicationFactorMax: v.ReplicationFactorMax,
|
||||
Recursive: true,
|
||||
}
|
||||
}
|
||||
return &mst4
|
||||
}
|
||||
|
||||
/* V4 */
|
||||
|
||||
type mapStateV4 struct {
|
||||
PinMap map[string]api.PinSerial
|
||||
Version int
|
||||
}
|
||||
|
||||
func (st *mapStateV4) unmarshal(bs []byte) error {
|
||||
buf := bytes.NewBuffer(bs)
|
||||
dec := msgpack.Multicodec(msgpack.DefaultMsgpackHandle()).Decoder(buf)
|
||||
return dec.Decode(st)
|
||||
}
|
||||
|
||||
func (st *mapStateV4) next() migrateable {
|
||||
return nil
|
||||
}
|
||||
|
||||
func finalCopy(st *MapState, internal *mapStateV3) {
|
||||
func finalCopy(st *MapState, internal *mapStateV4) {
|
||||
for k, v := range internal.PinMap {
|
||||
st.PinMap[k] = v
|
||||
}
|
||||
|
@ -110,6 +153,9 @@ func (st *MapState) migrateFrom(version int, snap []byte) error {
|
|||
case 2:
|
||||
var mst2 mapStateV2
|
||||
m = &mst2
|
||||
case 3:
|
||||
var mst3 mapStateV3
|
||||
m = &mst3
|
||||
default:
|
||||
return errors.New("version migration not supported")
|
||||
}
|
||||
|
@ -122,11 +168,11 @@ func (st *MapState) migrateFrom(version int, snap []byte) error {
|
|||
for {
|
||||
next = m.next()
|
||||
if next == nil {
|
||||
mst3, ok := m.(*mapStateV3)
|
||||
mst4, ok := m.(*mapStateV4)
|
||||
if !ok {
|
||||
return errors.New("migration ended prematurely")
|
||||
}
|
||||
finalCopy(st, mst3)
|
||||
finalCopy(st, mst4)
|
||||
return nil
|
||||
}
|
||||
m = next
|
||||
|
|
Loading…
Reference in New Issue
Block a user