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.

185 lines
4.7 KiB

7 years ago
7 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/boltdb/bolt"
  6. "errors"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. type BoltDbNeedleMap struct {
  13. dbFileName string
  14. db *bolt.DB
  15. baseNeedleMapper
  16. }
  17. var boltdbBucket = []byte("weed")
  18. var NotFound = errors.New("not found")
  19. func NewBoltDbNeedleMap(dbFileName string, indexFile *os.File) (m *BoltDbNeedleMap, err error) {
  20. m = &BoltDbNeedleMap{dbFileName: dbFileName}
  21. m.indexFile = indexFile
  22. if !isBoltDbFresh(dbFileName, indexFile) {
  23. glog.V(0).Infof("Start to Generate %s from %s", dbFileName, indexFile.Name())
  24. generateBoltDbFile(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 = bolt.Open(dbFileName, 0644, nil); 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 isBoltDbFresh(dbFileName string, indexFile *os.File) bool {
  40. // normally we always write to index file first
  41. dbLogFile, err := os.Open(dbFileName)
  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 generateBoltDbFile(dbFileName string, indexFile *os.File) error {
  55. db, err := bolt.Open(dbFileName, 0644, nil)
  56. if err != nil {
  57. return err
  58. }
  59. defer db.Close()
  60. return WalkIndexFile(indexFile, func(key NeedleId, offset Offset, size uint32) error {
  61. if offset > 0 && size != TombstoneFileSize {
  62. boltDbWrite(db, key, offset, size)
  63. } else {
  64. boltDbDelete(db, key)
  65. }
  66. return nil
  67. })
  68. }
  69. func (m *BoltDbNeedleMap) Get(key NeedleId) (element *needle.NeedleValue, ok bool) {
  70. var offset Offset
  71. var size uint32
  72. bytes := make([]byte, NeedleIdSize)
  73. NeedleIdToBytes(bytes, key)
  74. err := m.db.View(func(tx *bolt.Tx) error {
  75. bucket := tx.Bucket(boltdbBucket)
  76. if bucket == nil {
  77. return fmt.Errorf("Bucket %q not found!", boltdbBucket)
  78. }
  79. data := bucket.Get(bytes)
  80. if len(data) == 0 {
  81. return NotFound
  82. }
  83. if len(data) != OffsetSize+SizeSize {
  84. glog.V(0).Infof("key:%v has wrong data length: %d", key, len(data))
  85. return fmt.Errorf("key:%v has wrong data length: %d", key, len(data))
  86. }
  87. offset = BytesToOffset(data[0:OffsetSize])
  88. size = util.BytesToUint32(data[OffsetSize : OffsetSize+SizeSize])
  89. return nil
  90. })
  91. if err != nil {
  92. return nil, false
  93. }
  94. return &needle.NeedleValue{Key: key, Offset: offset, Size: size}, true
  95. }
  96. func (m *BoltDbNeedleMap) Put(key NeedleId, offset Offset, size uint32) error {
  97. var oldSize uint32
  98. if oldNeedle, ok := m.Get(key); ok {
  99. oldSize = oldNeedle.Size
  100. }
  101. m.logPut(key, oldSize, size)
  102. // write to index file first
  103. if err := m.appendToIndexFile(key, offset, size); err != nil {
  104. return fmt.Errorf("cannot write to indexfile %s: %v", m.indexFile.Name(), err)
  105. }
  106. return boltDbWrite(m.db, key, offset, size)
  107. }
  108. func boltDbWrite(db *bolt.DB,
  109. key NeedleId, offset Offset, size uint32) error {
  110. bytes := make([]byte, NeedleIdSize+OffsetSize+SizeSize)
  111. NeedleIdToBytes(bytes[0:NeedleIdSize], key)
  112. OffsetToBytes(bytes[NeedleIdSize:NeedleIdSize+OffsetSize], offset)
  113. util.Uint32toBytes(bytes[NeedleIdSize+OffsetSize:NeedleIdSize+OffsetSize+SizeSize], size)
  114. return db.Update(func(tx *bolt.Tx) error {
  115. bucket, err := tx.CreateBucketIfNotExists(boltdbBucket)
  116. if err != nil {
  117. return err
  118. }
  119. err = bucket.Put(bytes[0:NeedleIdSize], bytes[NeedleIdSize:NeedleIdSize+OffsetSize+SizeSize])
  120. if err != nil {
  121. return err
  122. }
  123. return nil
  124. })
  125. }
  126. func boltDbDelete(db *bolt.DB, key NeedleId) error {
  127. bytes := make([]byte, NeedleIdSize)
  128. NeedleIdToBytes(bytes, key)
  129. return db.Update(func(tx *bolt.Tx) error {
  130. bucket, err := tx.CreateBucketIfNotExists(boltdbBucket)
  131. if err != nil {
  132. return err
  133. }
  134. err = bucket.Delete(bytes)
  135. if err != nil {
  136. return err
  137. }
  138. return nil
  139. })
  140. }
  141. func (m *BoltDbNeedleMap) Delete(key NeedleId, offset Offset) error {
  142. if oldNeedle, ok := m.Get(key); ok {
  143. m.logDelete(oldNeedle.Size)
  144. }
  145. // write to index file first
  146. if err := m.appendToIndexFile(key, offset, TombstoneFileSize); err != nil {
  147. return err
  148. }
  149. return boltDbDelete(m.db, key)
  150. }
  151. func (m *BoltDbNeedleMap) Close() {
  152. m.indexFile.Close()
  153. m.db.Close()
  154. }
  155. func (m *BoltDbNeedleMap) Destroy() error {
  156. m.Close()
  157. os.Remove(m.indexFile.Name())
  158. return os.Remove(m.dbFileName)
  159. }