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.

71 lines
1.7 KiB

  1. package erasure_coding
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "strconv"
  7. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  8. )
  9. type ShardId uint8
  10. type EcVolumeShard struct {
  11. VolumeId needle.VolumeId
  12. ShardId ShardId
  13. Collection string
  14. dir string
  15. ecdFile *os.File
  16. ecdFileSize int64
  17. }
  18. func NewEcVolumeShard(dirname string, collection string, id needle.VolumeId, shardId ShardId) (v *EcVolumeShard, e error) {
  19. v = &EcVolumeShard{dir: dirname, Collection: collection, VolumeId: id, ShardId: shardId}
  20. baseFileName := v.FileName()
  21. // open ecd file
  22. if v.ecdFile, e = os.OpenFile(baseFileName+ToExt(int(shardId)), os.O_RDONLY, 0644); e != nil {
  23. return nil, fmt.Errorf("cannot read ec volume shard %s.%s: %v", baseFileName, ToExt(int(shardId)), e)
  24. }
  25. ecdFi, statErr := v.ecdFile.Stat()
  26. if statErr != nil {
  27. return nil, fmt.Errorf("can not stat ec volume shard %s.%s: %v", baseFileName, ToExt(int(shardId)), statErr)
  28. }
  29. v.ecdFileSize = ecdFi.Size()
  30. return
  31. }
  32. func (shard *EcVolumeShard) String() string {
  33. return fmt.Sprintf("ec shard %v:%v, dir:%s, Collection:%s", shard.VolumeId, shard.ShardId, shard.dir, shard.Collection)
  34. }
  35. func (shard *EcVolumeShard) FileName() (fileName string) {
  36. return EcShardFileName(shard.Collection, shard.dir, int(shard.VolumeId))
  37. }
  38. func EcShardFileName(collection string, dir string, id int) (fileName string) {
  39. idString := strconv.Itoa(id)
  40. if collection == "" {
  41. fileName = path.Join(dir, idString)
  42. } else {
  43. fileName = path.Join(dir, collection+"_"+idString)
  44. }
  45. return
  46. }
  47. func (shard *EcVolumeShard) Close() {
  48. if shard.ecdFile != nil {
  49. _ = shard.ecdFile.Close()
  50. shard.ecdFile = nil
  51. }
  52. }
  53. func (shard *EcVolumeShard) ReadAt(buf []byte, offset int64) (int, error) {
  54. return shard.ecdFile.ReadAt(buf, offset)
  55. }