super_block_read.go.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package super_block
  2. import (
  3. "fmt"
  4. "google.golang.org/protobuf/proto"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. )
  10. // ReadSuperBlock reads from data file and load it into volume's super block
  11. func ReadSuperBlock(datBackend backend.BackendStorageFile) (superBlock SuperBlock, err error) {
  12. header := make([]byte, SuperBlockSize)
  13. if _, e := datBackend.ReadAt(header, 0); e != nil {
  14. err = fmt.Errorf("cannot read volume %s super block: %v", datBackend.Name(), e)
  15. return
  16. }
  17. superBlock.Version = needle.Version(header[0])
  18. if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
  19. err = fmt.Errorf("cannot read replica type: %s", err.Error())
  20. return
  21. }
  22. superBlock.Ttl = needle.LoadTTLFromBytes(header[2:4])
  23. superBlock.CompactionRevision = util.BytesToUint16(header[4:6])
  24. superBlock.ExtraSize = util.BytesToUint16(header[6:8])
  25. if superBlock.ExtraSize > 0 {
  26. // read more
  27. extraData := make([]byte, int(superBlock.ExtraSize))
  28. superBlock.Extra = &master_pb.SuperBlockExtra{}
  29. err = proto.Unmarshal(extraData, superBlock.Extra)
  30. if err != nil {
  31. err = fmt.Errorf("cannot read volume %s super block extra: %v", datBackend.Name(), err)
  32. return
  33. }
  34. }
  35. return
  36. }