page_writer.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package mount
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/glog"
  4. "github.com/seaweedfs/seaweedfs/weed/mount/page_writer"
  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. }
  24. return pw
  25. }
  26. func (pw *PageWriter) AddPage(offset int64, data []byte, isSequential bool, tsNs int64) {
  27. glog.V(4).Infof("%v AddPage [%d, %d)", pw.fh.fh, offset, offset+int64(len(data)))
  28. chunkIndex := offset / pw.chunkSize
  29. for i := chunkIndex; len(data) > 0; i++ {
  30. writeSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
  31. pw.addToOneChunk(i, offset, data[:writeSize], isSequential, tsNs)
  32. offset += writeSize
  33. data = data[writeSize:]
  34. }
  35. }
  36. func (pw *PageWriter) addToOneChunk(chunkIndex, offset int64, data []byte, isSequential bool, tsNs int64) {
  37. pw.randomWriter.AddPage(offset, data, isSequential, tsNs)
  38. }
  39. func (pw *PageWriter) FlushData() error {
  40. return pw.randomWriter.FlushData()
  41. }
  42. func (pw *PageWriter) ReadDirtyDataAt(data []byte, offset int64, tsNs int64) (maxStop int64) {
  43. glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.fh.fh, offset, offset+int64(len(data)))
  44. chunkIndex := offset / pw.chunkSize
  45. for i := chunkIndex; len(data) > 0; i++ {
  46. readSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
  47. maxStop = pw.randomWriter.ReadDirtyDataAt(data[:readSize], offset, tsNs)
  48. offset += readSize
  49. data = data[readSize:]
  50. }
  51. return
  52. }
  53. func (pw *PageWriter) LockForRead(startOffset, stopOffset int64) {
  54. pw.randomWriter.LockForRead(startOffset, stopOffset)
  55. }
  56. func (pw *PageWriter) UnlockForRead(startOffset, stopOffset int64) {
  57. pw.randomWriter.UnlockForRead(startOffset, stopOffset)
  58. }
  59. func (pw *PageWriter) Destroy() {
  60. pw.randomWriter.Destroy()
  61. }
  62. func max(x, y int64) int64 {
  63. if x > y {
  64. return x
  65. }
  66. return y
  67. }
  68. func min(x, y int64) int64 {
  69. if x < y {
  70. return x
  71. }
  72. return y
  73. }