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.

107 lines
3.1 KiB

9 years ago
9 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. )
  8. func loadVolumeWithoutIndex(dirname string, collection string, id VolumeId, needleMapKind NeedleMapType) (v *Volume, e error) {
  9. v = &Volume{dir: dirname, Collection: collection, Id: id}
  10. v.SuperBlock = SuperBlock{}
  11. v.needleMapKind = needleMapKind
  12. e = v.load(false, false, needleMapKind)
  13. return
  14. }
  15. func (v *Volume) load(alsoLoadIndex bool, createDatIfMissing bool, needleMapKind NeedleMapType) error {
  16. var e error
  17. fileName := v.FileName()
  18. if exists, canRead, canWrite, modifiedTime := checkFile(fileName + ".dat"); exists {
  19. if !canRead {
  20. return fmt.Errorf("cannot read Volume Data file %s.dat", fileName)
  21. }
  22. if canWrite {
  23. v.dataFile, e = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
  24. v.lastModifiedTime = uint64(modifiedTime.Unix())
  25. } else {
  26. glog.V(0).Infoln("opening " + fileName + ".dat in READONLY mode")
  27. v.dataFile, e = os.Open(fileName + ".dat")
  28. v.readOnly = true
  29. }
  30. } else {
  31. if createDatIfMissing {
  32. v.dataFile, e = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
  33. } else {
  34. return fmt.Errorf("Volume Data file %s.dat does not exist.", fileName)
  35. }
  36. }
  37. if e != nil {
  38. if !os.IsPermission(e) {
  39. return fmt.Errorf("cannot load Volume Data %s.dat: %v", fileName, e)
  40. }
  41. }
  42. if v.ReplicaPlacement == nil {
  43. e = v.readSuperBlock()
  44. } else {
  45. e = v.maybeWriteSuperBlock()
  46. }
  47. if e == nil && alsoLoadIndex {
  48. var indexFile *os.File
  49. if v.readOnly {
  50. glog.V(1).Infoln("open to read file", fileName+".idx")
  51. if indexFile, e = os.OpenFile(fileName+".idx", os.O_RDONLY, 0644); e != nil {
  52. return fmt.Errorf("cannot read Volume Index %s.idx: %v", fileName, e)
  53. }
  54. } else {
  55. glog.V(1).Infoln("open to write file", fileName+".idx")
  56. if indexFile, e = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644); e != nil {
  57. return fmt.Errorf("cannot write Volume Index %s.idx: %v", fileName, e)
  58. }
  59. }
  60. if e = CheckVolumeDataIntegrity(v, indexFile); e != nil {
  61. v.readOnly = true
  62. glog.V(0).Infof("volumeDataIntegrityChecking failed %v", e)
  63. }
  64. switch needleMapKind {
  65. case NeedleMapInMemory:
  66. glog.V(0).Infoln("loading index file", fileName+".idx", "readonly", v.readOnly)
  67. if v.nm, e = LoadNeedleMap(indexFile); e != nil {
  68. glog.V(0).Infof("loading index %s error: %v", fileName+".idx", e)
  69. }
  70. case NeedleMapLevelDb:
  71. glog.V(0).Infoln("loading leveldb file", fileName+".ldb")
  72. if v.nm, e = NewLevelDbNeedleMap(fileName+".ldb", indexFile); e != nil {
  73. glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", e)
  74. }
  75. case NeedleMapBoltDb:
  76. glog.V(0).Infoln("loading boltdb file", fileName+".bdb")
  77. if v.nm, e = NewBoltDbNeedleMap(fileName+".bdb", indexFile); e != nil {
  78. glog.V(0).Infof("loading boltdb %s error: %v", fileName+".bdb", e)
  79. }
  80. }
  81. }
  82. return e
  83. }
  84. func checkFile(filename string) (exists, canRead, canWrite bool, modTime time.Time) {
  85. exists = true
  86. fi, err := os.Stat(filename)
  87. if os.IsNotExist(err) {
  88. exists = false
  89. return
  90. }
  91. if fi.Mode()&0400 != 0 {
  92. canRead = true
  93. }
  94. if fi.Mode()&0200 != 0 {
  95. canWrite = true
  96. }
  97. modTime = fi.ModTime()
  98. return
  99. }