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.

95 lines
2.2 KiB

3 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. 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. }