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.

89 lines
2.8 KiB

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