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.

126 lines
3.5 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. if key > nm.MaximumFileKey {
  41. nm.MaximumFileKey = key
  42. }
  43. if offset > 0 && size != TombstoneFileSize {
  44. nm.FileCounter++
  45. nm.FileByteCounter = nm.FileByteCounter + uint64(size)
  46. oldOffset, oldSize := nm.m.Set(NeedleId(key), offset, size)
  47. // glog.V(3).Infoln("reading key", key, "offset", offset*NeedlePaddingSize, "size", size, "oldSize", oldSize)
  48. if oldOffset > 0 && oldSize != TombstoneFileSize {
  49. nm.DeletionCounter++
  50. nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(oldSize)
  51. }
  52. } else {
  53. oldSize := nm.m.Delete(NeedleId(key))
  54. // glog.V(3).Infoln("removing key", key, "offset", offset*NeedlePaddingSize, "size", size, "oldSize", oldSize)
  55. nm.DeletionCounter++
  56. nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(oldSize)
  57. }
  58. return nil
  59. })
  60. glog.V(1).Infof("max file key: %d for file: %s", nm.MaximumFileKey, file.Name())
  61. return nm, e
  62. }
  63. // walks through the index file, calls fn function with each key, offset, size
  64. // stops with the error returned by the fn function
  65. func WalkIndexFile(r *os.File, fn func(key NeedleId, offset Offset, size uint32) error) error {
  66. var readerOffset int64
  67. bytes := make([]byte, NeedleEntrySize*RowsToRead)
  68. count, e := r.ReadAt(bytes, readerOffset)
  69. glog.V(3).Infoln("file", r.Name(), "readerOffset", readerOffset, "count", count, "e", e)
  70. readerOffset += int64(count)
  71. var (
  72. key NeedleId
  73. offset Offset
  74. size uint32
  75. i int
  76. )
  77. for count > 0 && e == nil || e == io.EOF {
  78. for i = 0; i+NeedleEntrySize <= count; i += NeedleEntrySize {
  79. key, offset, size = IdxFileEntry(bytes[i : i+NeedleEntrySize])
  80. if e = fn(key, offset, size); e != nil {
  81. return e
  82. }
  83. }
  84. if e == io.EOF {
  85. return nil
  86. }
  87. count, e = r.ReadAt(bytes, readerOffset)
  88. glog.V(3).Infoln("file", r.Name(), "readerOffset", readerOffset, "count", count, "e", e)
  89. readerOffset += int64(count)
  90. }
  91. return e
  92. }
  93. func (nm *NeedleMap) Put(key NeedleId, offset Offset, size uint32) error {
  94. _, oldSize := nm.m.Set(NeedleId(key), offset, size)
  95. nm.logPut(key, oldSize, size)
  96. return nm.appendToIndexFile(key, offset, size)
  97. }
  98. func (nm *NeedleMap) Get(key NeedleId) (element *needle.NeedleValue, ok bool) {
  99. element, ok = nm.m.Get(NeedleId(key))
  100. return
  101. }
  102. func (nm *NeedleMap) Delete(key NeedleId, offset Offset) error {
  103. deletedBytes := nm.m.Delete(NeedleId(key))
  104. nm.logDelete(deletedBytes)
  105. return nm.appendToIndexFile(key, offset, TombstoneFileSize)
  106. }
  107. func (nm *NeedleMap) Close() {
  108. _ = nm.indexFile.Close()
  109. }
  110. func (nm *NeedleMap) Destroy() error {
  111. nm.Close()
  112. return os.Remove(nm.indexFile.Name())
  113. }