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.

87 lines
2.1 KiB

6 years ago
  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) Size() int64 {
  33. return shard.ecdFileSize
  34. }
  35. func (shard *EcVolumeShard) String() string {
  36. return fmt.Sprintf("ec shard %v:%v, dir:%s, Collection:%s", shard.VolumeId, shard.ShardId, shard.dir, shard.Collection)
  37. }
  38. func (shard *EcVolumeShard) FileName() (fileName string) {
  39. return EcShardFileName(shard.Collection, shard.dir, int(shard.VolumeId))
  40. }
  41. func EcShardFileName(collection string, dir string, id int) (fileName string) {
  42. idString := strconv.Itoa(id)
  43. if collection == "" {
  44. fileName = path.Join(dir, idString)
  45. } else {
  46. fileName = path.Join(dir, collection+"_"+idString)
  47. }
  48. return
  49. }
  50. func EcShardBaseFileName(collection string, id int) (baseFileName string) {
  51. baseFileName = strconv.Itoa(id)
  52. if collection != "" {
  53. baseFileName = collection + "_" + baseFileName
  54. }
  55. return
  56. }
  57. func (shard *EcVolumeShard) Close() {
  58. if shard.ecdFile != nil {
  59. _ = shard.ecdFile.Close()
  60. shard.ecdFile = nil
  61. }
  62. }
  63. func (shard *EcVolumeShard) Destroy() {
  64. os.Remove(shard.FileName() + ToExt(int(shard.ShardId)))
  65. }
  66. func (shard *EcVolumeShard) ReadAt(buf []byte, offset int64) (int, error) {
  67. return shard.ecdFile.ReadAt(buf, offset)
  68. }