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.

73 lines
1.8 KiB

  1. package storage
  2. import (
  3. "io/ioutil"
  4. "strings"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. )
  7. type DiskLocation struct {
  8. Directory string
  9. MaxVolumeCount int
  10. volumes map[VolumeId]*Volume
  11. }
  12. func NewDiskLocation(dir string, maxVolumeCount int) *DiskLocation {
  13. location := &DiskLocation{Directory: dir, MaxVolumeCount: maxVolumeCount}
  14. location.volumes = make(map[VolumeId]*Volume)
  15. return location
  16. }
  17. func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapType) {
  18. if dirs, err := ioutil.ReadDir(l.Directory); err == nil {
  19. for _, dir := range dirs {
  20. name := dir.Name()
  21. if !dir.IsDir() && strings.HasSuffix(name, ".dat") {
  22. collection := ""
  23. base := name[:len(name)-len(".dat")]
  24. i := strings.LastIndex(base, "_")
  25. if i > 0 {
  26. collection, base = base[0:i], base[i+1:]
  27. }
  28. if vid, err := NewVolumeId(base); err == nil {
  29. if l.volumes[vid] == nil {
  30. if v, e := NewVolume(l.Directory, collection, vid, needleMapKind, nil, nil); e == nil {
  31. l.volumes[vid] = v
  32. 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())
  33. } else {
  34. glog.V(0).Infof("new volume %s error %s", name, e)
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. glog.V(0).Infoln("Store started on dir:", l.Directory, "with", len(l.volumes), "volumes", "max", l.MaxVolumeCount)
  42. }
  43. func (l *DiskLocation) DeleteCollectionFromDiskLocation(collection string) (e error) {
  44. for k, v := range l.volumes {
  45. if v.Collection == collection {
  46. e = l.deleteVolumeById(k)
  47. if e != nil {
  48. return
  49. }
  50. }
  51. }
  52. return
  53. }
  54. func (l *DiskLocation) deleteVolumeById(vid VolumeId) (e error) {
  55. v, ok := l.volumes[vid]
  56. if !ok {
  57. return
  58. }
  59. e = v.Destroy()
  60. if e != nil {
  61. return
  62. }
  63. delete(l.volumes, vid)
  64. return
  65. }