diff --git a/go/storage/collection_settings.go b/go/storage/collection_settings.go index 5ea0d11b9..84653f054 100644 --- a/go/storage/collection_settings.go +++ b/go/storage/collection_settings.go @@ -43,11 +43,11 @@ func (cs *CollectionSettings) ToPbMessage() []*weedpb.CollectionSetting { setting := &weedpb.CollectionSetting{ Collection: collection, } - if v, ok := m[keyReplicatePlacement]; ok { + if v, ok := m[keyReplicatePlacement]; ok && v != nil { setting.ReplicaPlacement = v.(*ReplicaPlacement).String() } - if v, ok := m[keyGarbageThreshold]; ok { - setting.ReplicaPlacement = v.(string) + if v, ok := m[keyGarbageThreshold]; ok && v != nil { + setting.VacuumGarbageThreshold = v.(string) } msg = append(msg, setting) } @@ -87,7 +87,12 @@ func (cs *CollectionSettings) GetGarbageThreshold(collection string) string { } func (cs *CollectionSettings) SetGarbageThreshold(collection string, gt string) { - cs.set(collection, keyGarbageThreshold, gt) + if gt == "" { + cs.set(collection, keyGarbageThreshold, nil) + } else { + cs.set(collection, keyGarbageThreshold, gt) + + } } func (cs *CollectionSettings) GetReplicaPlacement(collection string) *ReplicaPlacement { @@ -100,6 +105,10 @@ func (cs *CollectionSettings) GetReplicaPlacement(collection string) *ReplicaPla } func (cs *CollectionSettings) SetReplicaPlacement(collection, t string) error { + if t == "" { + cs.set(collection, keyReplicatePlacement, nil) + return nil + } rp, e := NewReplicaPlacementFromString(t) if e == nil { cs.set(collection, keyReplicatePlacement, rp) diff --git a/go/storage/collection_settings_test.go b/go/storage/collection_settings_test.go new file mode 100644 index 000000000..88e35f737 --- /dev/null +++ b/go/storage/collection_settings_test.go @@ -0,0 +1,32 @@ +package storage + +import ( + "testing" + "reflect" + "encoding/json" +) + +func TestCollectionSettings(t *testing.T) { + cs1 := NewCollectionSettings("000", "0.3") + cs1.SetReplicaPlacement("col1", "001") + cs1.SetGarbageThreshold("col2", "0.5") + + if cs1.GetGarbageThreshold("col1") != "0.3" || + cs1.GetGarbageThreshold("col2") != "0.5" || + cs1.GetGarbageThreshold("") != "0.3" || + cs1.GetReplicaPlacement("").String() != "000" || + cs1.GetReplicaPlacement("col1").String() != "001" || + cs1.GetReplicaPlacement("col2").String() != "000" { + t.Fatal("Value incorrect.") + } + pb := cs1.ToPbMessage() + if buf, e := json.MarshalIndent(pb, "", "\t");e == nil { + t.Log(string(buf)) + }else{ + t.Fatal(e) + } + cs2 := NewCollectionSettingsFromPbMessage(pb) + if !reflect.DeepEqual(cs1, cs2) { + t.Fatal("PbMessage convert incorrect.") + } +} diff --git a/go/storage/volume_super_block_test.go b/go/storage/volume_super_block_test.go index 13db4b194..859f35110 100644 --- a/go/storage/volume_super_block_test.go +++ b/go/storage/volume_super_block_test.go @@ -5,12 +5,10 @@ import ( ) func TestSuperBlockReadWrite(t *testing.T) { - rp, _ := NewReplicaPlacementFromByte(byte(001)) ttl, _ := ReadTTL("15d") s := &SuperBlock{ - version: CurrentVersion, - ReplicaPlacement: rp, - Ttl: ttl, + version: CurrentVersion, + Ttl: ttl, } bytes := s.Bytes()