page_writer.go 2.2 KB

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