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.

174 lines
4.3 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. var offset, size uint32
  69. bytes := make([]byte, 8)
  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. if len(data) != 8 {
  78. glog.V(0).Infof("wrong data length: %d", len(data))
  79. return fmt.Errorf("wrong data length: %d", len(data))
  80. }
  81. offset = util.BytesToUint32(data[0:4])
  82. size = util.BytesToUint32(data[4:8])
  83. return nil
  84. })
  85. if err != nil {
  86. return nil, false
  87. }
  88. return &needle.NeedleValue{Key: needle.Key(key), Offset: offset, Size: size}, true
  89. }
  90. func (m *BoltDbNeedleMap) Put(key uint64, offset uint32, size uint32) error {
  91. var oldSize uint32
  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 boltDbWrite(m.db, key, offset, size)
  101. }
  102. func boltDbWrite(db *bolt.DB,
  103. key uint64, offset uint32, size uint32) error {
  104. bytes := make([]byte, 16)
  105. util.Uint64toBytes(bytes[0:8], key)
  106. util.Uint32toBytes(bytes[8:12], offset)
  107. util.Uint32toBytes(bytes[12:16], size)
  108. return db.Update(func(tx *bolt.Tx) error {
  109. bucket, err := tx.CreateBucketIfNotExists(boltdbBucket)
  110. if err != nil {
  111. return err
  112. }
  113. err = bucket.Put(bytes[0:8], bytes[8:16])
  114. if err != nil {
  115. return err
  116. }
  117. return nil
  118. })
  119. }
  120. func boltDbDelete(db *bolt.DB, key uint64) error {
  121. bytes := make([]byte, 8)
  122. util.Uint64toBytes(bytes, key)
  123. return db.Update(func(tx *bolt.Tx) error {
  124. bucket, err := tx.CreateBucketIfNotExists(boltdbBucket)
  125. if err != nil {
  126. return err
  127. }
  128. err = bucket.Delete(bytes)
  129. if err != nil {
  130. return err
  131. }
  132. return nil
  133. })
  134. }
  135. func (m *BoltDbNeedleMap) Delete(key uint64, offset uint32) error {
  136. if oldNeedle, ok := m.Get(key); ok {
  137. m.logDelete(oldNeedle.Size)
  138. }
  139. // write to index file first
  140. if err := m.appendToIndexFile(key, offset, TombstoneFileSize); err != nil {
  141. return err
  142. }
  143. return boltDbDelete(m.db, key)
  144. }
  145. func (m *BoltDbNeedleMap) Close() {
  146. m.db.Close()
  147. }
  148. func (m *BoltDbNeedleMap) Destroy() error {
  149. m.Close()
  150. os.Remove(m.indexFile.Name())
  151. return os.Remove(m.dbFileName)
  152. }