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.

159 lines
4.5 KiB

6 years ago
6 years ago
6 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/syndtr/goleveldb/leveldb/errors"
  7. "github.com/syndtr/goleveldb/leveldb/opt"
  8. "github.com/chrislusf/seaweedfs/weed/storage/idx"
  9. "github.com/syndtr/goleveldb/leveldb"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle_map"
  12. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  13. )
  14. type LevelDbNeedleMap struct {
  15. baseNeedleMapper
  16. dbFileName string
  17. db *leveldb.DB
  18. }
  19. func NewLevelDbNeedleMap(dbFileName string, indexFile *os.File, opts *opt.Options) (m *LevelDbNeedleMap, err error) {
  20. m = &LevelDbNeedleMap{dbFileName: dbFileName}
  21. m.indexFile = indexFile
  22. if !isLevelDbFresh(dbFileName, indexFile) {
  23. glog.V(1).Infof("Start to Generate %s from %s", dbFileName, indexFile.Name())
  24. generateLevelDbFile(dbFileName, indexFile)
  25. glog.V(1).Infof("Finished Generating %s from %s", dbFileName, indexFile.Name())
  26. }
  27. glog.V(1).Infof("Opening %s...", dbFileName)
  28. if m.db, err = leveldb.OpenFile(dbFileName, opts); err != nil {
  29. if errors.IsCorrupted(err) {
  30. m.db, err = leveldb.RecoverFile(dbFileName, opts)
  31. }
  32. if err != nil {
  33. return
  34. }
  35. }
  36. glog.V(1).Infof("Loading %s...", indexFile.Name())
  37. mm, indexLoadError := newNeedleMapMetricFromIndexFile(indexFile)
  38. if indexLoadError != nil {
  39. return nil, indexLoadError
  40. }
  41. m.mapMetric = *mm
  42. return
  43. }
  44. func isLevelDbFresh(dbFileName string, indexFile *os.File) bool {
  45. // normally we always write to index file first
  46. dbLogFile, err := os.Open(filepath.Join(dbFileName, "LOG"))
  47. if err != nil {
  48. return false
  49. }
  50. defer dbLogFile.Close()
  51. dbStat, dbStatErr := dbLogFile.Stat()
  52. indexStat, indexStatErr := indexFile.Stat()
  53. if dbStatErr != nil || indexStatErr != nil {
  54. glog.V(0).Infof("Can not stat file: %v and %v", dbStatErr, indexStatErr)
  55. return false
  56. }
  57. return dbStat.ModTime().After(indexStat.ModTime())
  58. }
  59. func generateLevelDbFile(dbFileName string, indexFile *os.File) error {
  60. db, err := leveldb.OpenFile(dbFileName, nil)
  61. if err != nil {
  62. return err
  63. }
  64. defer db.Close()
  65. return idx.WalkIndexFile(indexFile, func(key NeedleId, offset Offset, size Size) error {
  66. if !offset.IsZero() && size.IsValid() {
  67. levelDbWrite(db, key, offset, size)
  68. } else {
  69. levelDbDelete(db, key)
  70. }
  71. return nil
  72. })
  73. }
  74. func (m *LevelDbNeedleMap) Get(key NeedleId) (element *needle_map.NeedleValue, ok bool) {
  75. bytes := make([]byte, NeedleIdSize)
  76. NeedleIdToBytes(bytes[0:NeedleIdSize], key)
  77. data, err := m.db.Get(bytes, nil)
  78. if err != nil || len(data) != OffsetSize+SizeSize {
  79. return nil, false
  80. }
  81. offset := BytesToOffset(data[0:OffsetSize])
  82. size := BytesToSize(data[OffsetSize : OffsetSize+SizeSize])
  83. return &needle_map.NeedleValue{Key: key, Offset: offset, Size: size}, true
  84. }
  85. func (m *LevelDbNeedleMap) Put(key NeedleId, offset Offset, size Size) error {
  86. var oldSize Size
  87. if oldNeedle, ok := m.Get(key); ok {
  88. oldSize = oldNeedle.Size
  89. }
  90. m.logPut(key, oldSize, size)
  91. // write to index file first
  92. if err := m.appendToIndexFile(key, offset, size); err != nil {
  93. return fmt.Errorf("cannot write to indexfile %s: %v", m.indexFile.Name(), err)
  94. }
  95. return levelDbWrite(m.db, key, offset, size)
  96. }
  97. func levelDbWrite(db *leveldb.DB, key NeedleId, offset Offset, size Size) error {
  98. bytes := needle_map.ToBytes(key, offset, size)
  99. if err := db.Put(bytes[0:NeedleIdSize], bytes[NeedleIdSize:NeedleIdSize+OffsetSize+SizeSize], nil); err != nil {
  100. return fmt.Errorf("failed to write leveldb: %v", err)
  101. }
  102. return nil
  103. }
  104. func levelDbDelete(db *leveldb.DB, key NeedleId) error {
  105. bytes := make([]byte, NeedleIdSize)
  106. NeedleIdToBytes(bytes, key)
  107. return db.Delete(bytes, nil)
  108. }
  109. func (m *LevelDbNeedleMap) Delete(key NeedleId, offset Offset) error {
  110. oldNeedle, found := m.Get(key)
  111. if !found || oldNeedle.Size.IsDeleted() {
  112. return nil
  113. }
  114. m.logDelete(oldNeedle.Size)
  115. // write to index file first
  116. if err := m.appendToIndexFile(key, offset, TombstoneFileSize); err != nil {
  117. return err
  118. }
  119. return levelDbWrite(m.db, key, oldNeedle.Offset, -oldNeedle.Size)
  120. }
  121. func (m *LevelDbNeedleMap) Close() {
  122. indexFileName := m.indexFile.Name()
  123. if err := m.indexFile.Sync(); err != nil {
  124. glog.Warningf("sync file %s failed: %v", indexFileName, err)
  125. }
  126. if err := m.indexFile.Close(); err != nil {
  127. glog.Warningf("close index file %s failed: %v", indexFileName, err)
  128. }
  129. if err := m.db.Close(); err != nil {
  130. glog.Warningf("close levelDB failed: %v", err)
  131. }
  132. }
  133. func (m *LevelDbNeedleMap) Destroy() error {
  134. m.Close()
  135. os.Remove(m.indexFile.Name())
  136. return os.RemoveAll(m.dbFileName)
  137. }