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.

112 lines
2.6 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. fh *FileHandle
  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(fh *FileHandle, chunkSize int64) *PageWriter {
  19. pw := &PageWriter{
  20. fh: fh,
  21. chunkSize: chunkSize,
  22. writerPattern: NewWriterPattern(chunkSize),
  23. randomWriter: newMemoryChunkPages(fh, chunkSize),
  24. }
  25. return pw
  26. }
  27. func (pw *PageWriter) AddPage(offset int64, data []byte) {
  28. glog.V(4).Infof("%v AddPage [%d, %d) streaming:%v", pw.fh.f.fullpath(), offset, offset+int64(len(data)), pw.writerPattern.IsStreamingMode())
  29. chunkIndex := offset / pw.chunkSize
  30. for i := chunkIndex; len(data) > 0; i++ {
  31. writeSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
  32. pw.addToOneChunk(i, offset, data[:writeSize])
  33. offset += writeSize
  34. data = data[writeSize:]
  35. }
  36. }
  37. func (pw *PageWriter) addToOneChunk(chunkIndex, offset int64, data []byte) {
  38. if chunkIndex > 0 {
  39. if pw.writerPattern.IsStreamingMode() && pw.streamWriter != nil {
  40. pw.streamWriter.AddPage(offset, data)
  41. return
  42. }
  43. }
  44. pw.randomWriter.AddPage(offset, data)
  45. }
  46. func (pw *PageWriter) FlushData() error {
  47. pw.writerPattern.Reset()
  48. if pw.streamWriter != nil {
  49. if err := pw.streamWriter.FlushData(); err != nil {
  50. return err
  51. }
  52. }
  53. return pw.randomWriter.FlushData()
  54. }
  55. func (pw *PageWriter) ReadDirtyDataAt(data []byte, offset int64) (maxStop int64) {
  56. glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.fh.f.fullpath(), offset, offset+int64(len(data)))
  57. chunkIndex := offset / pw.chunkSize
  58. for i := chunkIndex; len(data) > 0; i++ {
  59. readSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
  60. if pw.streamWriter != nil {
  61. m1 := pw.streamWriter.ReadDirtyDataAt(data[:readSize], offset)
  62. maxStop = max(maxStop, m1)
  63. }
  64. m2 := pw.randomWriter.ReadDirtyDataAt(data[:readSize], offset)
  65. maxStop = max(maxStop, m2)
  66. offset += readSize
  67. data = data[readSize:]
  68. }
  69. return
  70. }
  71. func (pw *PageWriter) GetStorageOptions() (collection, replication string) {
  72. if pw.writerPattern.IsStreamingMode() && pw.streamWriter != nil {
  73. return pw.streamWriter.GetStorageOptions()
  74. }
  75. return pw.randomWriter.GetStorageOptions()
  76. }
  77. func (pw *PageWriter) Destroy() {
  78. if pw.streamWriter != nil {
  79. pw.streamWriter.Destroy()
  80. }
  81. pw.randomWriter.Destroy()
  82. }
  83. func max(x, y int64) int64 {
  84. if x > y {
  85. return x
  86. }
  87. return y
  88. }
  89. func min(x, y int64) int64 {
  90. if x < y {
  91. return x
  92. }
  93. return y
  94. }