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.

78 lines
2.1 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),
  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, startOffset int64) (maxStop int64) {
  55. glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.f.fullpath(), startOffset, startOffset+int64(len(data)))
  56. m1 := pw.streamWriter.ReadDirtyDataAt(data, startOffset)
  57. m2 := pw.randomWriter.ReadDirtyDataAt(data, startOffset)
  58. return max(m1, m2)
  59. }
  60. func (pw *PageWriter) GetStorageOptions() (collection, replication string) {
  61. if pw.writerPattern.IsStreamingMode() {
  62. return pw.streamWriter.GetStorageOptions()
  63. }
  64. return pw.randomWriter.GetStorageOptions()
  65. }