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.

76 lines
2.2 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. if !oldOffset.IsZero() && oldSize != TombstoneFileSize {
  32. nm.DeletionCounter++
  33. nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(oldSize)
  34. }
  35. } else {
  36. oldSize := nm.m.Delete(NeedleId(key))
  37. nm.DeletionCounter++
  38. nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(oldSize)
  39. }
  40. return nil
  41. })
  42. glog.V(1).Infof("max file key: %d for file: %s", nm.MaxFileKey(), file.Name())
  43. return nm, e
  44. }
  45. func (nm *NeedleMap) Put(key NeedleId, offset Offset, size uint32) error {
  46. _, oldSize := nm.m.Set(NeedleId(key), offset, size)
  47. nm.logPut(key, oldSize, size)
  48. return nm.appendToIndexFile(key, offset, size)
  49. }
  50. func (nm *NeedleMap) Get(key NeedleId) (element *needle_map.NeedleValue, ok bool) {
  51. element, ok = nm.m.Get(NeedleId(key))
  52. return
  53. }
  54. func (nm *NeedleMap) Delete(key NeedleId, offset Offset) error {
  55. deletedBytes := nm.m.Delete(NeedleId(key))
  56. nm.logDelete(deletedBytes)
  57. return nm.appendToIndexFile(key, offset, TombstoneFileSize)
  58. }
  59. func (nm *NeedleMap) Close() {
  60. indexFileName := nm.indexFile.Name()
  61. if err := nm.indexFile.Sync(); err != nil {
  62. glog.Warningf("sync file %s failed, %v", indexFileName, err)
  63. }
  64. _ = nm.indexFile.Close()
  65. }
  66. func (nm *NeedleMap) Destroy() error {
  67. nm.Close()
  68. return os.Remove(nm.indexFile.Name())
  69. }