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.

94 lines
2.4 KiB

  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. randomWriter: newTempFileDirtyPages(file, chunkSize),
  23. streamWriter: newContinuousDirtyPages(file),
  24. writerPattern: NewWriterPattern(file.Name, chunkSize),
  25. }
  26. return pw
  27. }
  28. func (pw *PageWriter) AddPage(offset int64, data []byte) {
  29. glog.V(4).Infof("AddPage %v [%d, %d) streaming:%v", pw.f.fullpath(), offset, offset+int64(len(data)), pw.writerPattern.IsStreamingMode())
  30. pw.writerPattern.MonitorWriteAt(offset, len(data))
  31. chunkIndex := offset / pw.chunkSize
  32. for i := chunkIndex; len(data) > 0; i++ {
  33. writeSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
  34. pw.addToOneChunk(i, offset, data[:writeSize])
  35. offset += writeSize
  36. data = data[writeSize:]
  37. }
  38. }
  39. func (pw *PageWriter) addToOneChunk(chunkIndex, offset int64, data []byte) {
  40. if chunkIndex > 0 {
  41. if pw.writerPattern.IsStreamingMode() {
  42. pw.streamWriter.AddPage(offset, data)
  43. return
  44. }
  45. }
  46. pw.randomWriter.AddPage(offset, data)
  47. }
  48. func (pw *PageWriter) FlushData() error {
  49. if err := pw.streamWriter.FlushData(); err != nil {
  50. return err
  51. }
  52. return pw.randomWriter.FlushData()
  53. }
  54. func (pw *PageWriter) ReadDirtyDataAt(data []byte, offset int64) (maxStop int64) {
  55. glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.f.fullpath(), offset, offset+int64(len(data)))
  56. chunkIndex := offset / pw.chunkSize
  57. for i := chunkIndex; len(data) > 0; i++ {
  58. readSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
  59. m1 := pw.streamWriter.ReadDirtyDataAt(data[:readSize], offset)
  60. m2 := pw.randomWriter.ReadDirtyDataAt(data[:readSize], offset)
  61. maxStop = max(maxStop, max(m1, m2))
  62. offset += readSize
  63. data = data[readSize:]
  64. }
  65. return
  66. }
  67. func (pw *PageWriter) GetStorageOptions() (collection, replication string) {
  68. if pw.writerPattern.IsStreamingMode() {
  69. return pw.streamWriter.GetStorageOptions()
  70. }
  71. return pw.randomWriter.GetStorageOptions()
  72. }
  73. func (pw *PageWriter) Destroy() {
  74. pw.randomWriter.Destroy()
  75. }