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.

135 lines
3.7 KiB

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