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.

139 lines
4.1 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/storage/types"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/syndtr/goleveldb/leveldb"
  11. )
  12. type LevelDbNeedleMap struct {
  13. dbFileName string
  14. db *leveldb.DB
  15. baseNeedleMapper
  16. }
  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(0).Infof("Start to Generate %s from %s", dbFileName, indexFile.Name())
  22. generateLevelDbFile(dbFileName, indexFile)
  23. glog.V(0).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. mm, indexLoadError := newNeedleMapMetricFromIndexFile(indexFile)
  31. if indexLoadError != nil {
  32. return nil, indexLoadError
  33. }
  34. m.mapMetric = *mm
  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 NeedleId, offset 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 NeedleId) (element *needle.NeedleValue, ok bool) {
  68. bytes := make([]byte, NeedleIdSize)
  69. NeedleIdToBytes(bytes[0:NeedleIdSize], key)
  70. data, err := m.db.Get(bytes, nil)
  71. if err != nil || len(data) != OffsetSize+SizeSize {
  72. return nil, false
  73. }
  74. offset := BytesToOffset(data[0:OffsetSize])
  75. size := util.BytesToUint32(data[OffsetSize : OffsetSize+SizeSize])
  76. return &needle.NeedleValue{Key: NeedleId(key), Offset: offset, Size: size}, true
  77. }
  78. func (m *LevelDbNeedleMap) Put(key NeedleId, offset Offset, 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 NeedleId, offset Offset, size uint32) error {
  92. bytes := make([]byte, NeedleIdSize+OffsetSize+SizeSize)
  93. NeedleIdToBytes(bytes[0:NeedleIdSize], key)
  94. OffsetToBytes(bytes[NeedleIdSize:NeedleIdSize+OffsetSize], offset)
  95. util.Uint32toBytes(bytes[NeedleIdSize+OffsetSize:NeedleIdSize+OffsetSize+SizeSize], size)
  96. if err := db.Put(bytes[0:NeedleIdSize], bytes[NeedleIdSize:NeedleIdSize+OffsetSize+SizeSize], 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 NeedleId) error {
  102. bytes := make([]byte, NeedleIdSize)
  103. NeedleIdToBytes(bytes, key)
  104. return db.Delete(bytes, nil)
  105. }
  106. func (m *LevelDbNeedleMap) Delete(key NeedleId, offset Offset) 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.indexFile.Close()
  118. m.db.Close()
  119. }
  120. func (m *LevelDbNeedleMap) Destroy() error {
  121. m.Close()
  122. os.Remove(m.indexFile.Name())
  123. return os.RemoveAll(m.dbFileName)
  124. }