page_writer.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  14. var (
  15. _ = page_writer.DirtyPages(&PageWriter{})
  16. )
  17. func newPageWriter(fh *FileHandle, chunkSize int64) *PageWriter {
  18. pw := &PageWriter{
  19. fh: fh,
  20. chunkSize: chunkSize,
  21. writerPattern: NewWriterPattern(chunkSize),
  22. randomWriter: newMemoryChunkPages(fh, chunkSize),
  23. // randomWriter: newTempFileDirtyPages(fh.f, 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. pw.randomWriter.AddPage(offset, data)
  39. }
  40. func (pw *PageWriter) FlushData() error {
  41. pw.writerPattern.Reset()
  42. return pw.randomWriter.FlushData()
  43. }
  44. func (pw *PageWriter) ReadDirtyDataAt(data []byte, offset int64) (maxStop int64) {
  45. glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.fh.f.fullpath(), offset, offset+int64(len(data)))
  46. chunkIndex := offset / pw.chunkSize
  47. for i := chunkIndex; len(data) > 0; i++ {
  48. readSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
  49. maxStop = pw.randomWriter.ReadDirtyDataAt(data[:readSize], offset)
  50. offset += readSize
  51. data = data[readSize:]
  52. }
  53. return
  54. }
  55. func (pw *PageWriter) GetStorageOptions() (collection, replication string) {
  56. return pw.randomWriter.GetStorageOptions()
  57. }
  58. func (pw *PageWriter) LockForRead(startOffset, stopOffset int64) {
  59. pw.randomWriter.LockForRead(startOffset, stopOffset)
  60. }
  61. func (pw *PageWriter) UnlockForRead(startOffset, stopOffset int64) {
  62. pw.randomWriter.UnlockForRead(startOffset, stopOffset)
  63. }
  64. func (pw *PageWriter) Destroy() {
  65. pw.randomWriter.Destroy()
  66. }
  67. func max(x, y int64) int64 {
  68. if x > y {
  69. return x
  70. }
  71. return y
  72. }
  73. func min(x, y int64) int64 {
  74. if x < y {
  75. return x
  76. }
  77. return y
  78. }