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.

74 lines
2.3 KiB

6 years ago
6 years ago
6 years ago
  1. package storage
  2. import (
  3. "os"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/storage/idx"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle_map"
  7. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  8. )
  9. type NeedleMap struct {
  10. baseNeedleMapper
  11. m needle_map.NeedleValueMap
  12. }
  13. func NewCompactNeedleMap(file *os.File) *NeedleMap {
  14. nm := &NeedleMap{
  15. m: needle_map.NewCompactMap(),
  16. }
  17. nm.indexFile = file
  18. return nm
  19. }
  20. func LoadCompactNeedleMap(file *os.File) (*NeedleMap, error) {
  21. nm := NewCompactNeedleMap(file)
  22. return doLoading(file, nm)
  23. }
  24. func doLoading(file *os.File, nm *NeedleMap) (*NeedleMap, error) {
  25. e := idx.WalkIndexFile(file, func(key NeedleId, offset Offset, size uint32) error {
  26. nm.MaybeSetMaxFileKey(key)
  27. if !offset.IsZero() && size != TombstoneFileSize {
  28. nm.FileCounter++
  29. nm.FileByteCounter = nm.FileByteCounter + uint64(size)
  30. oldOffset, oldSize := nm.m.Set(NeedleId(key), offset, size)
  31. // glog.V(3).Infoln("reading key", key, "offset", offset*NeedlePaddingSize, "size", size, "oldSize", oldSize)
  32. if !oldOffset.IsZero() && oldSize != TombstoneFileSize {
  33. nm.DeletionCounter++
  34. nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(oldSize)
  35. }
  36. } else {
  37. oldSize := nm.m.Delete(NeedleId(key))
  38. // glog.V(3).Infoln("removing key", key, "offset", offset*NeedlePaddingSize, "size", size, "oldSize", oldSize)
  39. nm.DeletionCounter++
  40. nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(oldSize)
  41. }
  42. return nil
  43. })
  44. glog.V(1).Infof("max file key: %d for file: %s", nm.MaxFileKey(), file.Name())
  45. return nm, e
  46. }
  47. func (nm *NeedleMap) Put(key NeedleId, offset Offset, size uint32) error {
  48. _, oldSize := nm.m.Set(NeedleId(key), offset, size)
  49. nm.logPut(key, oldSize, size)
  50. return nm.appendToIndexFile(key, offset, size)
  51. }
  52. func (nm *NeedleMap) Get(key NeedleId) (element *needle_map.NeedleValue, ok bool) {
  53. element, ok = nm.m.Get(NeedleId(key))
  54. return
  55. }
  56. func (nm *NeedleMap) Delete(key NeedleId, offset Offset) error {
  57. deletedBytes := nm.m.Delete(NeedleId(key))
  58. nm.logDelete(deletedBytes)
  59. return nm.appendToIndexFile(key, offset, TombstoneFileSize)
  60. }
  61. func (nm *NeedleMap) Close() {
  62. _ = nm.indexFile.Close()
  63. }
  64. func (nm *NeedleMap) Destroy() error {
  65. nm.Close()
  66. return os.Remove(nm.indexFile.Name())
  67. }