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.

96 lines
2.2 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/chrislusf/seaweedfs/weed/glog"
  4. "github.com/chrislusf/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) GetStorageOptions() (collection, replication string) {
  54. return pw.randomWriter.GetStorageOptions()
  55. }
  56. func (pw *PageWriter) LockForRead(startOffset, stopOffset int64) {
  57. pw.randomWriter.LockForRead(startOffset, stopOffset)
  58. }
  59. func (pw *PageWriter) UnlockForRead(startOffset, stopOffset int64) {
  60. pw.randomWriter.UnlockForRead(startOffset, stopOffset)
  61. }
  62. func (pw *PageWriter) Destroy() {
  63. pw.randomWriter.Destroy()
  64. }
  65. func max(x, y int64) int64 {
  66. if x > y {
  67. return x
  68. }
  69. return y
  70. }
  71. func min(x, y int64) int64 {
  72. if x < y {
  73. return x
  74. }
  75. return y
  76. }