2022-02-14 14:50:44 +08:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mount/page_writer"
|
2022-02-14 14:50:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type PageWriter struct {
|
2022-03-14 09:17:35 +08:00
|
|
|
fh *FileHandle
|
|
|
|
collection string
|
|
|
|
replication string
|
|
|
|
chunkSize int64
|
|
|
|
writerPattern *WriterPattern
|
2022-02-14 14:50:44 +08:00
|
|
|
|
|
|
|
randomWriter page_writer.DirtyPages
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ = page_writer.DirtyPages(&PageWriter{})
|
|
|
|
)
|
|
|
|
|
|
|
|
func newPageWriter(fh *FileHandle, chunkSize int64) *PageWriter {
|
|
|
|
pw := &PageWriter{
|
2022-03-14 09:17:35 +08:00
|
|
|
fh: fh,
|
|
|
|
chunkSize: chunkSize,
|
|
|
|
writerPattern: NewWriterPattern(chunkSize),
|
|
|
|
randomWriter: newMemoryChunkPages(fh, chunkSize),
|
2022-02-14 14:50:44 +08:00
|
|
|
}
|
|
|
|
return pw
|
|
|
|
}
|
|
|
|
|
2023-01-03 15:20:45 +08:00
|
|
|
func (pw *PageWriter) AddPage(offset int64, data []byte, isSequential bool, tsNs int64) {
|
2022-02-14 14:50:44 +08:00
|
|
|
|
2022-02-14 17:36:10 +08:00
|
|
|
glog.V(4).Infof("%v AddPage [%d, %d)", pw.fh.fh, offset, offset+int64(len(data)))
|
2022-02-14 14:50:44 +08:00
|
|
|
|
|
|
|
chunkIndex := offset / pw.chunkSize
|
|
|
|
for i := chunkIndex; len(data) > 0; i++ {
|
|
|
|
writeSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
|
2023-01-03 15:20:45 +08:00
|
|
|
pw.addToOneChunk(i, offset, data[:writeSize], isSequential, tsNs)
|
2022-02-14 14:50:44 +08:00
|
|
|
offset += writeSize
|
|
|
|
data = data[writeSize:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 15:20:45 +08:00
|
|
|
func (pw *PageWriter) addToOneChunk(chunkIndex, offset int64, data []byte, isSequential bool, tsNs int64) {
|
|
|
|
pw.randomWriter.AddPage(offset, data, isSequential, tsNs)
|
2022-02-14 14:50:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) FlushData() error {
|
|
|
|
return pw.randomWriter.FlushData()
|
|
|
|
}
|
|
|
|
|
2023-01-03 15:20:45 +08:00
|
|
|
func (pw *PageWriter) ReadDirtyDataAt(data []byte, offset int64, tsNs int64) (maxStop int64) {
|
2024-11-08 05:00:12 +08:00
|
|
|
glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.fh.inode, offset, offset+int64(len(data)))
|
2022-02-14 14:50:44 +08:00
|
|
|
|
|
|
|
chunkIndex := offset / pw.chunkSize
|
|
|
|
for i := chunkIndex; len(data) > 0; i++ {
|
|
|
|
readSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
|
|
|
|
|
2023-01-03 15:20:45 +08:00
|
|
|
maxStop = pw.randomWriter.ReadDirtyDataAt(data[:readSize], offset, tsNs)
|
2022-02-14 14:50:44 +08:00
|
|
|
|
|
|
|
offset += readSize
|
|
|
|
data = data[readSize:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) LockForRead(startOffset, stopOffset int64) {
|
|
|
|
pw.randomWriter.LockForRead(startOffset, stopOffset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) UnlockForRead(startOffset, stopOffset int64) {
|
|
|
|
pw.randomWriter.UnlockForRead(startOffset, stopOffset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) Destroy() {
|
|
|
|
pw.randomWriter.Destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
func max(x, y int64) int64 {
|
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
func min(x, y int64) int64 {
|
|
|
|
if x < y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|