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.

113 lines
3.4 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. }
  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", fileName+".idx", "to memory readonly", v.readOnly)
  67. if v.nm, e = LoadCompactNeedleMap(indexFile); e != nil {
  68. glog.V(0).Infof("loading index %s to memory error: %v", fileName+".idx", e)
  69. }
  70. case NeedleMapLevelDb:
  71. glog.V(0).Infoln("loading leveldb", 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", 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. case NeedleMapBtree:
  81. glog.V(0).Infoln("loading index", fileName+".idx", "to btree readonly", v.readOnly)
  82. if v.nm, e = LoadBtreeNeedleMap(indexFile); e != nil {
  83. glog.V(0).Infof("loading index %s to btree error: %v", fileName+".idx", e)
  84. }
  85. }
  86. }
  87. return e
  88. }
  89. func checkFile(filename string) (exists, canRead, canWrite bool, modTime time.Time) {
  90. exists = true
  91. fi, err := os.Stat(filename)
  92. if os.IsNotExist(err) {
  93. exists = false
  94. return
  95. }
  96. if fi.Mode()&0400 != 0 {
  97. canRead = true
  98. }
  99. if fi.Mode()&0200 != 0 {
  100. canWrite = true
  101. }
  102. modTime = fi.ModTime()
  103. return
  104. }