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.

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