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.

141 lines
4.1 KiB

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