seaweedfs/weed/storage/super_block/super_block_read.go.go

47 lines
1.4 KiB
Go
Raw Normal View History

2019-12-24 04:48:20 +08:00
package super_block
import (
"fmt"
2022-08-18 03:05:07 +08:00
"google.golang.org/protobuf/proto"
2019-12-24 04:48:20 +08:00
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"github.com/seaweedfs/seaweedfs/weed/storage/backend"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/util"
2019-12-24 04:48:20 +08:00
)
// ReadSuperBlock reads from data file and load it into volume's super block
func ReadSuperBlock(datBackend backend.BackendStorageFile) (superBlock SuperBlock, err error) {
header := make([]byte, SuperBlockSize)
if n, e := datBackend.ReadAt(header, 0); e != nil {
if n != SuperBlockSize {
err = fmt.Errorf("cannot read volume %s super block: %v", datBackend.Name(), e)
return
}
2019-12-24 04:48:20 +08:00
}
superBlock.Version = needle.Version(header[0])
if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
err = fmt.Errorf("cannot read replica type: %s", err.Error())
return
}
superBlock.Ttl = needle.LoadTTLFromBytes(header[2:4])
superBlock.CompactionRevision = util.BytesToUint16(header[4:6])
superBlock.ExtraSize = util.BytesToUint16(header[6:8])
if superBlock.ExtraSize > 0 {
// read more
extraData := make([]byte, int(superBlock.ExtraSize))
superBlock.Extra = &master_pb.SuperBlockExtra{}
err = proto.Unmarshal(extraData, superBlock.Extra)
if err != nil {
err = fmt.Errorf("cannot read volume %s super block extra: %v", datBackend.Name(), err)
return
}
}
return
}