You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.4 KiB

  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 n, e := datBackend.ReadAt(header, 0); e != nil {
  14. if n != SuperBlockSize {
  15. err = fmt.Errorf("cannot read volume %s super block: %v", datBackend.Name(), e)
  16. return
  17. }
  18. }
  19. superBlock.Version = needle.Version(header[0])
  20. if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
  21. err = fmt.Errorf("cannot read replica type: %s", err.Error())
  22. return
  23. }
  24. superBlock.Ttl = needle.LoadTTLFromBytes(header[2:4])
  25. superBlock.CompactionRevision = util.BytesToUint16(header[4:6])
  26. superBlock.ExtraSize = util.BytesToUint16(header[6:8])
  27. if superBlock.ExtraSize > 0 {
  28. // read more
  29. extraData := make([]byte, int(superBlock.ExtraSize))
  30. superBlock.Extra = &master_pb.SuperBlockExtra{}
  31. err = proto.Unmarshal(extraData, superBlock.Extra)
  32. if err != nil {
  33. err = fmt.Errorf("cannot read volume %s super block extra: %v", datBackend.Name(), err)
  34. return
  35. }
  36. }
  37. return
  38. }