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.

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