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.

113 lines
2.9 KiB

  1. package erasure_coding
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "strconv"
  7. "github.com/chrislusf/seaweedfs/weed/storage/idx"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/seaweedfs/weed/storage/types"
  10. )
  11. type ShardId uint8
  12. type EcVolumeShard struct {
  13. VolumeId needle.VolumeId
  14. ShardId ShardId
  15. Collection string
  16. dir string
  17. ecdFile *os.File
  18. ecdFileSize int64
  19. ecxFile *os.File
  20. ecxFileSize int64
  21. }
  22. func NewEcVolumeShard(dirname string, collection string, id needle.VolumeId, shardId ShardId) (v *EcVolumeShard, e error) {
  23. v = &EcVolumeShard{dir: dirname, Collection: collection, VolumeId: id, ShardId: shardId}
  24. baseFileName := v.FileName()
  25. // open ecx file
  26. if v.ecxFile, e = os.OpenFile(baseFileName+".ecx", os.O_RDONLY, 0644); e != nil {
  27. return nil, fmt.Errorf("cannot read ec volume index %s.ecx: %v", baseFileName, e)
  28. }
  29. ecxFi, statErr := v.ecxFile.Stat()
  30. if statErr != nil {
  31. return nil, fmt.Errorf("can not stat ec volume index %s.ecx: %v", baseFileName, statErr)
  32. }
  33. v.ecxFileSize = ecxFi.Size()
  34. // open ecd file
  35. if v.ecdFile, e = os.OpenFile(baseFileName+ToExt(int(shardId)), os.O_RDONLY, 0644); e != nil {
  36. return nil, fmt.Errorf("cannot read ec volume shard %s.%s: %v", baseFileName, ToExt(int(shardId)), e)
  37. }
  38. ecdFi, statErr := v.ecdFile.Stat()
  39. if statErr != nil {
  40. return nil, fmt.Errorf("can not stat ec volume shard %s.%s: %v", baseFileName, ToExt(int(shardId)), statErr)
  41. }
  42. v.ecdFileSize = ecdFi.Size()
  43. return
  44. }
  45. func (shard *EcVolumeShard) String() string {
  46. return fmt.Sprintf("ec shard %v:%v, dir:%s, Collection:%s", shard.VolumeId, shard.ShardId, shard.dir, shard.Collection)
  47. }
  48. func (shard *EcVolumeShard) FileName() (fileName string) {
  49. return EcShardFileName(shard.Collection, shard.dir, int(shard.VolumeId))
  50. }
  51. func EcShardFileName(collection string, dir string, id int) (fileName string) {
  52. idString := strconv.Itoa(id)
  53. if collection == "" {
  54. fileName = path.Join(dir, idString)
  55. } else {
  56. fileName = path.Join(dir, collection+"_"+idString)
  57. }
  58. return
  59. }
  60. func (shard *EcVolumeShard) Close() {
  61. if shard.ecdFile != nil {
  62. _ = shard.ecdFile.Close()
  63. shard.ecdFile = nil
  64. }
  65. if shard.ecxFile != nil {
  66. _ = shard.ecxFile.Close()
  67. shard.ecxFile = nil
  68. }
  69. }
  70. func (shard *EcVolumeShard) findNeedleFromEcx(needleId types.NeedleId) (offset types.Offset, size uint32, err error) {
  71. var key types.NeedleId
  72. buf := make([]byte, types.NeedleMapEntrySize)
  73. l, h := int64(0), shard.ecxFileSize/types.NeedleMapEntrySize
  74. for l < h {
  75. m := (l + h) / 2
  76. if _, err := shard.ecxFile.ReadAt(buf, m*types.NeedleMapEntrySize); err != nil {
  77. return types.Offset{}, 0, err
  78. }
  79. key, offset, size = idx.IdxFileEntry(buf)
  80. if key == needleId {
  81. return
  82. }
  83. if key < needleId {
  84. l = m + 1
  85. } else {
  86. h = m
  87. }
  88. }
  89. err = fmt.Errorf("needle id %d not found", needleId)
  90. return
  91. }
  92. func (shard *EcVolumeShard) ReadAt(buf []byte, offset int64) (int, error) {
  93. return shard.ecdFile.ReadAt(buf, offset)
  94. }