needle_map_leveldb.go 4.6 KB

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