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.

124 lines
3.4 KiB

7 years ago
  1. package storage
  2. import (
  3. "io"
  4. "os"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  8. )
  9. type NeedleMap struct {
  10. m needle.NeedleValueMap
  11. baseNeedleMapper
  12. }
  13. func NewCompactNeedleMap(file *os.File) *NeedleMap {
  14. nm := &NeedleMap{
  15. m: needle.NewCompactMap(),
  16. }
  17. nm.indexFile = file
  18. return nm
  19. }
  20. func NewBtreeNeedleMap(file *os.File) *NeedleMap {
  21. nm := &NeedleMap{
  22. m: needle.NewBtreeMap(),
  23. }
  24. nm.indexFile = file
  25. return nm
  26. }
  27. const (
  28. RowsToRead = 1024
  29. )
  30. func LoadCompactNeedleMap(file *os.File) (*NeedleMap, error) {
  31. nm := NewCompactNeedleMap(file)
  32. return doLoading(file, nm)
  33. }
  34. func LoadBtreeNeedleMap(file *os.File) (*NeedleMap, error) {
  35. nm := NewBtreeNeedleMap(file)
  36. return doLoading(file, nm)
  37. }
  38. func doLoading(file *os.File, nm *NeedleMap) (*NeedleMap, error) {
  39. e := WalkIndexFile(file, func(key NeedleId, offset Offset, size uint32) error {
  40. nm.MaybeSetMaxFileKey(key)
  41. if !offset.IsZero() && size != TombstoneFileSize {
  42. nm.FileCounter++
  43. nm.FileByteCounter = nm.FileByteCounter + uint64(size)
  44. oldOffset, oldSize := nm.m.Set(NeedleId(key), offset, size)
  45. // glog.V(3).Infoln("reading key", key, "offset", offset*NeedlePaddingSize, "size", size, "oldSize", oldSize)
  46. if !oldOffset.IsZero() && oldSize != TombstoneFileSize {
  47. nm.DeletionCounter++
  48. nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(oldSize)
  49. }
  50. } else {
  51. oldSize := nm.m.Delete(NeedleId(key))
  52. // glog.V(3).Infoln("removing key", key, "offset", offset*NeedlePaddingSize, "size", size, "oldSize", oldSize)
  53. nm.DeletionCounter++
  54. nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(oldSize)
  55. }
  56. return nil
  57. })
  58. glog.V(1).Infof("max file key: %d for file: %s", nm.MaxFileKey(), file.Name())
  59. return nm, e
  60. }
  61. // walks through the index file, calls fn function with each key, offset, size
  62. // stops with the error returned by the fn function
  63. func WalkIndexFile(r *os.File, fn func(key NeedleId, offset Offset, size uint32) error) error {
  64. var readerOffset int64
  65. bytes := make([]byte, NeedleEntrySize*RowsToRead)
  66. count, e := r.ReadAt(bytes, readerOffset)
  67. glog.V(3).Infoln("file", r.Name(), "readerOffset", readerOffset, "count", count, "e", e)
  68. readerOffset += int64(count)
  69. var (
  70. key NeedleId
  71. offset Offset
  72. size uint32
  73. i int
  74. )
  75. for count > 0 && e == nil || e == io.EOF {
  76. for i = 0; i+NeedleEntrySize <= count; i += NeedleEntrySize {
  77. key, offset, size = IdxFileEntry(bytes[i : i+NeedleEntrySize])
  78. if e = fn(key, offset, size); e != nil {
  79. return e
  80. }
  81. }
  82. if e == io.EOF {
  83. return nil
  84. }
  85. count, e = r.ReadAt(bytes, readerOffset)
  86. glog.V(3).Infoln("file", r.Name(), "readerOffset", readerOffset, "count", count, "e", e)
  87. readerOffset += int64(count)
  88. }
  89. return e
  90. }
  91. func (nm *NeedleMap) Put(key NeedleId, offset Offset, size uint32) error {
  92. _, oldSize := nm.m.Set(NeedleId(key), offset, size)
  93. nm.logPut(key, oldSize, size)
  94. return nm.appendToIndexFile(key, offset, size)
  95. }
  96. func (nm *NeedleMap) Get(key NeedleId) (element *needle.NeedleValue, ok bool) {
  97. element, ok = nm.m.Get(NeedleId(key))
  98. return
  99. }
  100. func (nm *NeedleMap) Delete(key NeedleId, offset Offset) error {
  101. deletedBytes := nm.m.Delete(NeedleId(key))
  102. nm.logDelete(deletedBytes)
  103. return nm.appendToIndexFile(key, offset, TombstoneFileSize)
  104. }
  105. func (nm *NeedleMap) Close() {
  106. _ = nm.indexFile.Close()
  107. }
  108. func (nm *NeedleMap) Destroy() error {
  109. nm.Close()
  110. return os.Remove(nm.indexFile.Name())
  111. }