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.

117 lines
2.8 KiB

3 years ago
  1. package filesys
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/filesys/page_writer"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. )
  6. type PageWriter struct {
  7. f *File
  8. collection string
  9. replication string
  10. chunkSize int64
  11. writerPattern *WriterPattern
  12. randomWriter page_writer.DirtyPages
  13. streamWriter page_writer.DirtyPages
  14. }
  15. var (
  16. _ = page_writer.DirtyPages(&PageWriter{})
  17. )
  18. func newPageWriter(file *File, chunkSize int64) *PageWriter {
  19. pw := &PageWriter{
  20. f: file,
  21. chunkSize: chunkSize,
  22. writerPattern: NewWriterPattern(chunkSize),
  23. randomWriter: newTempFileDirtyPages(file, chunkSize),
  24. streamWriter: newStreamDirtyPages(file, chunkSize),
  25. //streamWriter: newContinuousDirtyPages(file),
  26. //streamWriter: nil,
  27. }
  28. return pw
  29. }
  30. func (pw *PageWriter) AddPage(offset int64, data []byte) {
  31. pw.writerPattern.MonitorWriteAt(offset, len(data))
  32. glog.V(4).Infof("%v AddPage [%d, %d) streaming:%v", pw.f.fullpath(), offset, offset+int64(len(data)), pw.writerPattern.IsStreamingMode())
  33. chunkIndex := offset / pw.chunkSize
  34. for i := chunkIndex; len(data) > 0; i++ {
  35. writeSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
  36. pw.addToOneChunk(i, offset, data[:writeSize])
  37. offset += writeSize
  38. data = data[writeSize:]
  39. }
  40. }
  41. func (pw *PageWriter) addToOneChunk(chunkIndex, offset int64, data []byte) {
  42. if chunkIndex > 0 {
  43. if pw.writerPattern.IsStreamingMode() && pw.streamWriter != nil {
  44. pw.streamWriter.AddPage(offset, data)
  45. return
  46. }
  47. }
  48. pw.randomWriter.AddPage(offset, data)
  49. }
  50. func (pw *PageWriter) FlushData() error {
  51. pw.writerPattern.Reset()
  52. if pw.streamWriter != nil {
  53. if err := pw.streamWriter.FlushData(); err != nil {
  54. return err
  55. }
  56. }
  57. return pw.randomWriter.FlushData()
  58. }
  59. func (pw *PageWriter) ReadDirtyDataAt(data []byte, offset int64) (maxStop int64) {
  60. glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.f.fullpath(), offset, offset+int64(len(data)))
  61. chunkIndex := offset / pw.chunkSize
  62. for i := chunkIndex; len(data) > 0; i++ {
  63. readSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
  64. if pw.streamWriter != nil {
  65. m1 := pw.streamWriter.ReadDirtyDataAt(data[:readSize], offset)
  66. maxStop = max(maxStop, m1)
  67. }
  68. m2 := pw.randomWriter.ReadDirtyDataAt(data[:readSize], offset)
  69. maxStop = max(maxStop, m2)
  70. offset += readSize
  71. data = data[readSize:]
  72. }
  73. return
  74. }
  75. func (pw *PageWriter) GetStorageOptions() (collection, replication string) {
  76. if pw.writerPattern.IsStreamingMode() && pw.streamWriter != nil {
  77. return pw.streamWriter.GetStorageOptions()
  78. }
  79. return pw.randomWriter.GetStorageOptions()
  80. }
  81. func (pw *PageWriter) Destroy() {
  82. if pw.streamWriter != nil {
  83. pw.streamWriter.Destroy()
  84. }
  85. pw.randomWriter.Destroy()
  86. }
  87. func max(x, y int64) int64 {
  88. if x > y {
  89. return x
  90. }
  91. return y
  92. }
  93. func min(x, y int64) int64 {
  94. if x < y {
  95. return x
  96. }
  97. return y
  98. }