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.

136 lines
3.8 KiB

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