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.

112 lines
2.3 KiB

  1. package storage
  2. import (
  3. "io/ioutil"
  4. "strings"
  5. "sync"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. )
  8. type DiskLocation struct {
  9. Directory string
  10. MaxVolumeCount int
  11. volumes map[VolumeId]*Volume
  12. sync.RWMutex
  13. }
  14. func NewDiskLocation(dir string, maxVolumeCount int) *DiskLocation {
  15. location := &DiskLocation{Directory: dir, MaxVolumeCount: maxVolumeCount}
  16. location.volumes = make(map[VolumeId]*Volume)
  17. return location
  18. }
  19. func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapType) {
  20. l.Lock()
  21. defer l.Unlock()
  22. if dirs, err := ioutil.ReadDir(l.Directory); err == nil {
  23. for _, dir := range dirs {
  24. name := dir.Name()
  25. if !dir.IsDir() && strings.HasSuffix(name, ".dat") {
  26. collection := ""
  27. base := name[:len(name)-len(".dat")]
  28. i := strings.LastIndex(base, "_")
  29. if i > 0 {
  30. collection, base = base[0:i], base[i+1:]
  31. }
  32. if vid, err := NewVolumeId(base); err == nil {
  33. if l.volumes[vid] == nil {
  34. if v, e := NewVolume(l.Directory, collection, vid, needleMapKind, nil, nil); e == nil {
  35. l.volumes[vid] = v
  36. glog.V(0).Infof("data file %s, replicaPlacement=%s v=%d size=%d ttl=%s", l.Directory+"/"+name, v.ReplicaPlacement, v.Version(), v.Size(), v.Ttl.String())
  37. } else {
  38. glog.V(0).Infof("new volume %s error %s", name, e)
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. glog.V(0).Infoln("Store started on dir:", l.Directory, "with", len(l.volumes), "volumes", "max", l.MaxVolumeCount)
  46. }
  47. func (l *DiskLocation) DeleteCollectionFromDiskLocation(collection string) (e error) {
  48. l.Lock()
  49. defer l.Unlock()
  50. for k, v := range l.volumes {
  51. if v.Collection == collection {
  52. e = l.deleteVolumeById(k)
  53. if e != nil {
  54. return
  55. }
  56. }
  57. }
  58. return
  59. }
  60. func (l *DiskLocation) deleteVolumeById(vid VolumeId) (e error) {
  61. v, ok := l.volumes[vid]
  62. if !ok {
  63. return
  64. }
  65. e = v.Destroy()
  66. if e != nil {
  67. return
  68. }
  69. delete(l.volumes, vid)
  70. return
  71. }
  72. func (l *DiskLocation) SetVolume(vid VolumeId, volume *Volume) {
  73. l.Lock()
  74. defer l.Unlock()
  75. l.volumes[vid] = volume
  76. }
  77. func (l *DiskLocation) FindVolume(vid VolumeId) (*Volume, bool) {
  78. l.RLock()
  79. defer l.RUnlock()
  80. v, ok := l.volumes[vid]
  81. return v, ok
  82. }
  83. func (l *DiskLocation) VolumesLen() int {
  84. l.RLock()
  85. defer l.RUnlock()
  86. return len(l.volumes)
  87. }
  88. func (l *DiskLocation) Close() {
  89. l.Lock()
  90. defer l.Unlock()
  91. for _, v := range l.volumes {
  92. v.Close()
  93. }
  94. return
  95. }