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.

115 lines
3.5 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, 0)
  13. return
  14. }
  15. func (v *Volume) load(alsoLoadIndex bool, createDatIfMissing bool, needleMapKind NeedleMapType, preallocate int64) 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 = createVolumeFile(fileName+".dat", preallocate)
  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. } else {
  41. return fmt.Errorf("load data file %s.dat: %v", fileName, e)
  42. }
  43. }
  44. if v.ReplicaPlacement == nil {
  45. e = v.readSuperBlock()
  46. } else {
  47. e = v.maybeWriteSuperBlock()
  48. }
  49. if e == nil && alsoLoadIndex {
  50. var indexFile *os.File
  51. if v.readOnly {
  52. glog.V(1).Infoln("open to read file", fileName+".idx")
  53. if indexFile, e = os.OpenFile(fileName+".idx", os.O_RDONLY, 0644); e != nil {
  54. return fmt.Errorf("cannot read Volume Index %s.idx: %v", fileName, e)
  55. }
  56. } else {
  57. glog.V(1).Infoln("open to write file", fileName+".idx")
  58. if indexFile, e = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644); e != nil {
  59. return fmt.Errorf("cannot write Volume Index %s.idx: %v", fileName, e)
  60. }
  61. }
  62. if e = CheckVolumeDataIntegrity(v, indexFile); e != nil {
  63. v.readOnly = true
  64. glog.V(0).Infof("volumeDataIntegrityChecking failed %v", e)
  65. }
  66. switch needleMapKind {
  67. case NeedleMapInMemory:
  68. glog.V(0).Infoln("loading index", fileName+".idx", "to memory readonly", v.readOnly)
  69. if v.nm, e = LoadCompactNeedleMap(indexFile); e != nil {
  70. glog.V(0).Infof("loading index %s to memory error: %v", fileName+".idx", e)
  71. }
  72. case NeedleMapLevelDb:
  73. glog.V(0).Infoln("loading leveldb", fileName+".ldb")
  74. if v.nm, e = NewLevelDbNeedleMap(fileName+".ldb", indexFile); e != nil {
  75. glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", e)
  76. }
  77. case NeedleMapBoltDb:
  78. glog.V(0).Infoln("loading boltdb", fileName+".bdb")
  79. if v.nm, e = NewBoltDbNeedleMap(fileName+".bdb", indexFile); e != nil {
  80. glog.V(0).Infof("loading boltdb %s error: %v", fileName+".bdb", e)
  81. }
  82. case NeedleMapBtree:
  83. glog.V(0).Infoln("loading index", fileName+".idx", "to btree readonly", v.readOnly)
  84. if v.nm, e = LoadBtreeNeedleMap(indexFile); e != nil {
  85. glog.V(0).Infof("loading index %s to btree error: %v", fileName+".idx", e)
  86. }
  87. }
  88. }
  89. return e
  90. }
  91. func checkFile(filename string) (exists, canRead, canWrite bool, modTime time.Time) {
  92. exists = true
  93. fi, err := os.Stat(filename)
  94. if os.IsNotExist(err) {
  95. exists = false
  96. return
  97. }
  98. if fi.Mode()&0400 != 0 {
  99. canRead = true
  100. }
  101. if fi.Mode()&0200 != 0 {
  102. canWrite = true
  103. }
  104. modTime = fi.ModTime()
  105. return
  106. }