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.

92 lines
2.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
  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) {
  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)
  32. offset += writeSize
  33. data = data[writeSize:]
  34. }
  35. }
  36. func (pw *PageWriter) addToOneChunk(chunkIndex, offset int64, data []byte, isSequential bool) {
  37. pw.randomWriter.AddPage(offset, data, isSequential)
  38. }
  39. func (pw *PageWriter) FlushData() error {
  40. return pw.randomWriter.FlushData()
  41. }
  42. func (pw *PageWriter) ReadDirtyDataAt(data []byte, offset 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)
  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. }