2021-12-22 09:28:55 +08:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filesys/page_writer"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PageWriter struct {
|
2022-01-18 05:53:30 +08:00
|
|
|
fh *FileHandle
|
2021-12-22 09:28:55 +08:00
|
|
|
collection string
|
|
|
|
replication string
|
|
|
|
chunkSize int64
|
|
|
|
writerPattern *WriterPattern
|
|
|
|
|
|
|
|
randomWriter page_writer.DirtyPages
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ = page_writer.DirtyPages(&PageWriter{})
|
|
|
|
)
|
|
|
|
|
2022-01-18 05:53:30 +08:00
|
|
|
func newPageWriter(fh *FileHandle, chunkSize int64) *PageWriter {
|
2021-12-22 09:28:55 +08:00
|
|
|
pw := &PageWriter{
|
2022-01-18 05:53:30 +08:00
|
|
|
fh: fh,
|
2021-12-22 09:28:55 +08:00
|
|
|
chunkSize: chunkSize,
|
2021-12-25 14:38:22 +08:00
|
|
|
writerPattern: NewWriterPattern(chunkSize),
|
2022-01-18 05:53:30 +08:00
|
|
|
randomWriter: newMemoryChunkPages(fh, chunkSize),
|
2022-01-18 15:23:49 +08:00
|
|
|
// randomWriter: newTempFileDirtyPages(fh.f, chunkSize),
|
2021-12-22 09:28:55 +08:00
|
|
|
}
|
|
|
|
return pw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) AddPage(offset int64, data []byte) {
|
|
|
|
|
2022-01-18 05:53:30 +08:00
|
|
|
glog.V(4).Infof("%v AddPage [%d, %d) streaming:%v", pw.fh.f.fullpath(), offset, offset+int64(len(data)), pw.writerPattern.IsStreamingMode())
|
2021-12-25 14:38:22 +08:00
|
|
|
|
2021-12-22 09:28:55 +08:00
|
|
|
chunkIndex := offset / pw.chunkSize
|
|
|
|
for i := chunkIndex; len(data) > 0; i++ {
|
|
|
|
writeSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
|
|
|
|
pw.addToOneChunk(i, offset, data[:writeSize])
|
|
|
|
offset += writeSize
|
|
|
|
data = data[writeSize:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) addToOneChunk(chunkIndex, offset int64, data []byte) {
|
|
|
|
pw.randomWriter.AddPage(offset, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) FlushData() error {
|
2021-12-25 14:38:22 +08:00
|
|
|
pw.writerPattern.Reset()
|
2021-12-22 09:28:55 +08:00
|
|
|
return pw.randomWriter.FlushData()
|
|
|
|
}
|
|
|
|
|
2021-12-24 09:17:32 +08:00
|
|
|
func (pw *PageWriter) ReadDirtyDataAt(data []byte, offset int64) (maxStop int64) {
|
2022-01-18 05:53:30 +08:00
|
|
|
glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.fh.f.fullpath(), offset, offset+int64(len(data)))
|
2021-12-24 09:17:32 +08:00
|
|
|
|
|
|
|
chunkIndex := offset / pw.chunkSize
|
|
|
|
for i := chunkIndex; len(data) > 0; i++ {
|
|
|
|
readSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
|
|
|
|
|
2022-01-18 14:24:44 +08:00
|
|
|
maxStop = pw.randomWriter.ReadDirtyDataAt(data[:readSize], offset)
|
2021-12-24 09:17:32 +08:00
|
|
|
|
|
|
|
offset += readSize
|
|
|
|
data = data[readSize:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2021-12-22 09:28:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) GetStorageOptions() (collection, replication string) {
|
|
|
|
return pw.randomWriter.GetStorageOptions()
|
|
|
|
}
|
2021-12-24 09:17:32 +08:00
|
|
|
|
2022-01-18 14:24:44 +08:00
|
|
|
func (pw *PageWriter) LockForRead(startOffset, stopOffset int64) {
|
|
|
|
pw.randomWriter.LockForRead(startOffset, stopOffset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) UnlockForRead(startOffset, stopOffset int64) {
|
|
|
|
pw.randomWriter.UnlockForRead(startOffset, stopOffset)
|
|
|
|
}
|
|
|
|
|
2021-12-24 09:17:32 +08:00
|
|
|
func (pw *PageWriter) Destroy() {
|
|
|
|
pw.randomWriter.Destroy()
|
|
|
|
}
|
2021-12-24 09:48:34 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|