needle_map_leveldb.go 4.3 KB

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