needle_map_sorted_file.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package storage
  2. import (
  3. "os"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle_map"
  7. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  8. )
  9. type SortedFileNeedleMap struct {
  10. baseNeedleMapper
  11. baseFileName string
  12. dbFile *os.File
  13. dbFileSize int64
  14. }
  15. func NewSortedFileNeedleMap(indexBaseFileName string, indexFile *os.File) (m *SortedFileNeedleMap, err error) {
  16. m = &SortedFileNeedleMap{baseFileName: indexBaseFileName}
  17. m.indexFile = indexFile
  18. fileName := indexBaseFileName + ".sdx"
  19. if !isSortedFileFresh(fileName, indexFile) {
  20. glog.V(0).Infof("Start to Generate %s from %s", fileName, indexFile.Name())
  21. erasure_coding.WriteSortedFileFromIdx(indexBaseFileName, ".sdx")
  22. glog.V(0).Infof("Finished Generating %s from %s", fileName, indexFile.Name())
  23. }
  24. glog.V(1).Infof("Opening %s...", fileName)
  25. if m.dbFile, err = os.Open(indexBaseFileName + ".sdx"); err != nil {
  26. return
  27. }
  28. dbStat, _ := m.dbFile.Stat()
  29. m.dbFileSize = dbStat.Size()
  30. glog.V(1).Infof("Loading %s...", indexFile.Name())
  31. mm, indexLoadError := newNeedleMapMetricFromIndexFile(indexFile)
  32. if indexLoadError != nil {
  33. return nil, indexLoadError
  34. }
  35. m.mapMetric = *mm
  36. return
  37. }
  38. func isSortedFileFresh(dbFileName string, indexFile *os.File) bool {
  39. // normally we always write to index file first
  40. dbFile, err := os.Open(dbFileName)
  41. if err != nil {
  42. return false
  43. }
  44. defer dbFile.Close()
  45. dbStat, dbStatErr := dbFile.Stat()
  46. indexStat, indexStatErr := indexFile.Stat()
  47. if dbStatErr != nil || indexStatErr != nil {
  48. glog.V(0).Infof("Can not stat file: %v and %v", dbStatErr, indexStatErr)
  49. return false
  50. }
  51. return dbStat.ModTime().After(indexStat.ModTime())
  52. }
  53. func (m *SortedFileNeedleMap) Get(key NeedleId) (element *needle_map.NeedleValue, ok bool) {
  54. offset, size, err := erasure_coding.SearchNeedleFromSortedIndex(m.dbFile, m.dbFileSize, key, nil)
  55. ok = err == nil
  56. return &needle_map.NeedleValue{Key: key, Offset: offset, Size: size}, ok
  57. }
  58. func (m *SortedFileNeedleMap) Put(key NeedleId, offset Offset, size Size) error {
  59. return os.ErrInvalid
  60. }
  61. func (m *SortedFileNeedleMap) Delete(key NeedleId, offset Offset) error {
  62. _, size, err := erasure_coding.SearchNeedleFromSortedIndex(m.dbFile, m.dbFileSize, key, nil)
  63. if err != nil {
  64. if err == erasure_coding.NotFoundError {
  65. return nil
  66. }
  67. return err
  68. }
  69. if size.IsDeleted() {
  70. return nil
  71. }
  72. // write to index file first
  73. if err := m.appendToIndexFile(key, offset, TombstoneFileSize); err != nil {
  74. return err
  75. }
  76. _, _, err = erasure_coding.SearchNeedleFromSortedIndex(m.dbFile, m.dbFileSize, key, erasure_coding.MarkNeedleDeleted)
  77. return err
  78. }
  79. func (m *SortedFileNeedleMap) Close() {
  80. m.indexFile.Close()
  81. m.dbFile.Close()
  82. }
  83. func (m *SortedFileNeedleMap) Destroy() error {
  84. m.Close()
  85. os.Remove(m.indexFile.Name())
  86. return os.Remove(m.baseFileName + ".sdx")
  87. }