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.

167 lines
4.2 KiB

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