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.

166 lines
4.1 KiB

  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/boltdb/bolt"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. type BoltDbNeedleMap struct {
  11. dbFileName string
  12. db *bolt.DB
  13. baseNeedleMapper
  14. }
  15. var boltdbBucket = []byte("weed")
  16. func NewBoltDbNeedleMap(dbFileName string, indexFile *os.File) (m *BoltDbNeedleMap, err error) {
  17. m = &BoltDbNeedleMap{dbFileName: dbFileName}
  18. m.indexFile = indexFile
  19. if !isBoltDbFresh(dbFileName, indexFile) {
  20. glog.V(1).Infof("Start to Generate %s from %s", dbFileName, indexFile.Name())
  21. generateBoltDbFile(dbFileName, indexFile)
  22. glog.V(1).Infof("Finished Generating %s from %s", dbFileName, indexFile.Name())
  23. }
  24. glog.V(1).Infof("Opening %s...", dbFileName)
  25. if m.db, err = bolt.Open(dbFileName, 0644, nil); err != nil {
  26. return
  27. }
  28. glog.V(1).Infof("Loading %s...", indexFile.Name())
  29. nm, indexLoadError := LoadBtreeNeedleMap(indexFile)
  30. if indexLoadError != nil {
  31. return nil, indexLoadError
  32. }
  33. m.mapMetric = nm.mapMetric
  34. return
  35. }
  36. func isBoltDbFresh(dbFileName string, indexFile *os.File) bool {
  37. // normally we always write to index file first
  38. dbLogFile, err := os.Open(dbFileName)
  39. if err != nil {
  40. return false
  41. }
  42. defer dbLogFile.Close()
  43. dbStat, dbStatErr := dbLogFile.Stat()
  44. indexStat, indexStatErr := indexFile.Stat()
  45. if dbStatErr != nil || indexStatErr != nil {
  46. glog.V(0).Infof("Can not stat file: %v and %v", dbStatErr, indexStatErr)
  47. return false
  48. }
  49. return dbStat.ModTime().After(indexStat.ModTime())
  50. }
  51. func generateBoltDbFile(dbFileName string, indexFile *os.File) error {
  52. db, err := bolt.Open(dbFileName, 0644, nil)
  53. if err != nil {
  54. return err
  55. }
  56. defer db.Close()
  57. return WalkIndexFile(indexFile, func(key uint64, offset, size uint32) error {
  58. if offset > 0 && size != TombstoneFileSize {
  59. boltDbWrite(db, key, offset, size)
  60. } else {
  61. boltDbDelete(db, key)
  62. }
  63. return nil
  64. })
  65. }
  66. func (m *BoltDbNeedleMap) Get(key uint64) (element *needle.NeedleValue, ok bool) {
  67. bytes := make([]byte, 8)
  68. var data []byte
  69. util.Uint64toBytes(bytes, key)
  70. err := m.db.View(func(tx *bolt.Tx) error {
  71. bucket := tx.Bucket(boltdbBucket)
  72. if bucket == nil {
  73. return fmt.Errorf("Bucket %q not found!", boltdbBucket)
  74. }
  75. data = bucket.Get(bytes)
  76. return nil
  77. })
  78. if err != nil || len(data) != 8 {
  79. return nil, false
  80. }
  81. offset := util.BytesToUint32(data[0:4])
  82. size := util.BytesToUint32(data[4:8])
  83. return &needle.NeedleValue{Key: needle.Key(key), Offset: offset, Size: size}, true
  84. }
  85. func (m *BoltDbNeedleMap) Put(key uint64, offset uint32, size uint32) error {
  86. var oldSize uint32
  87. if oldNeedle, ok := m.Get(key); ok {
  88. oldSize = oldNeedle.Size
  89. }
  90. m.logPut(key, oldSize, size)
  91. // write to index file first
  92. if err := m.appendToIndexFile(key, offset, size); err != nil {
  93. return fmt.Errorf("cannot write to indexfile %s: %v", m.indexFile.Name(), err)
  94. }
  95. return boltDbWrite(m.db, key, offset, size)
  96. }
  97. func boltDbWrite(db *bolt.DB,
  98. key uint64, offset uint32, size uint32) error {
  99. bytes := make([]byte, 16)
  100. util.Uint64toBytes(bytes[0:8], key)
  101. util.Uint32toBytes(bytes[8:12], offset)
  102. util.Uint32toBytes(bytes[12:16], size)
  103. return db.Update(func(tx *bolt.Tx) error {
  104. bucket, err := tx.CreateBucketIfNotExists(boltdbBucket)
  105. if err != nil {
  106. return err
  107. }
  108. err = bucket.Put(bytes[0:8], bytes[8:16])
  109. if err != nil {
  110. return err
  111. }
  112. return nil
  113. })
  114. }
  115. func boltDbDelete(db *bolt.DB, key uint64) error {
  116. bytes := make([]byte, 8)
  117. util.Uint64toBytes(bytes, key)
  118. return db.Update(func(tx *bolt.Tx) error {
  119. bucket, err := tx.CreateBucketIfNotExists(boltdbBucket)
  120. if err != nil {
  121. return err
  122. }
  123. err = bucket.Delete(bytes)
  124. if err != nil {
  125. return err
  126. }
  127. return nil
  128. })
  129. }
  130. func (m *BoltDbNeedleMap) Delete(key uint64, offset uint32) error {
  131. if oldNeedle, ok := m.Get(key); ok {
  132. m.logDelete(oldNeedle.Size)
  133. }
  134. // write to index file first
  135. if err := m.appendToIndexFile(key, offset, TombstoneFileSize); err != nil {
  136. return err
  137. }
  138. return boltDbDelete(m.db, key)
  139. }
  140. func (m *BoltDbNeedleMap) Close() {
  141. m.db.Close()
  142. }
  143. func (m *BoltDbNeedleMap) Destroy() error {
  144. m.Close()
  145. os.Remove(m.indexFile.Name())
  146. return os.Remove(m.dbFileName)
  147. }