2021-12-22 01:28:55 +00:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filesys/page_writer"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PageWriter struct {
|
|
|
|
f *File
|
|
|
|
collection string
|
|
|
|
replication string
|
|
|
|
chunkSize int64
|
|
|
|
writerPattern *WriterPattern
|
|
|
|
|
|
|
|
randomWriter page_writer.DirtyPages
|
|
|
|
streamWriter page_writer.DirtyPages
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ = page_writer.DirtyPages(&PageWriter{})
|
|
|
|
)
|
|
|
|
|
|
|
|
func newPageWriter(file *File, chunkSize int64) *PageWriter {
|
|
|
|
pw := &PageWriter{
|
|
|
|
f: file,
|
|
|
|
chunkSize: chunkSize,
|
2021-12-25 06:38:22 +00:00
|
|
|
writerPattern: NewWriterPattern(chunkSize),
|
2022-01-17 09:53:56 +00:00
|
|
|
randomWriter: newMemoryChunkPages(file, chunkSize),
|
2021-12-22 01:28:55 +00:00
|
|
|
}
|
|
|
|
return pw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) AddPage(offset int64, data []byte) {
|
|
|
|
|
2021-12-25 06:38:22 +00:00
|
|
|
glog.V(4).Infof("%v AddPage [%d, %d) streaming:%v", pw.f.fullpath(), offset, offset+int64(len(data)), pw.writerPattern.IsStreamingMode())
|
|
|
|
|
2021-12-22 01:28:55 +00: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) {
|
|
|
|
if chunkIndex > 0 {
|
2021-12-25 06:38:22 +00:00
|
|
|
if pw.writerPattern.IsStreamingMode() && pw.streamWriter != nil {
|
2021-12-22 01:28:55 +00:00
|
|
|
pw.streamWriter.AddPage(offset, data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pw.randomWriter.AddPage(offset, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) FlushData() error {
|
2021-12-25 06:38:22 +00:00
|
|
|
pw.writerPattern.Reset()
|
|
|
|
if pw.streamWriter != nil {
|
|
|
|
if err := pw.streamWriter.FlushData(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-22 01:28:55 +00:00
|
|
|
}
|
|
|
|
return pw.randomWriter.FlushData()
|
|
|
|
}
|
|
|
|
|
2021-12-24 01:17:32 +00:00
|
|
|
func (pw *PageWriter) ReadDirtyDataAt(data []byte, offset int64) (maxStop int64) {
|
|
|
|
glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.f.fullpath(), offset, offset+int64(len(data)))
|
|
|
|
|
|
|
|
chunkIndex := offset / pw.chunkSize
|
|
|
|
for i := chunkIndex; len(data) > 0; i++ {
|
|
|
|
readSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)
|
|
|
|
|
2021-12-25 06:38:22 +00:00
|
|
|
if pw.streamWriter != nil {
|
|
|
|
m1 := pw.streamWriter.ReadDirtyDataAt(data[:readSize], offset)
|
|
|
|
maxStop = max(maxStop, m1)
|
|
|
|
}
|
2021-12-24 01:17:32 +00:00
|
|
|
m2 := pw.randomWriter.ReadDirtyDataAt(data[:readSize], offset)
|
2021-12-25 06:38:22 +00:00
|
|
|
maxStop = max(maxStop, m2)
|
2021-12-24 01:17:32 +00:00
|
|
|
|
|
|
|
offset += readSize
|
|
|
|
data = data[readSize:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2021-12-22 01:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *PageWriter) GetStorageOptions() (collection, replication string) {
|
2021-12-25 06:38:22 +00:00
|
|
|
if pw.writerPattern.IsStreamingMode() && pw.streamWriter != nil {
|
2021-12-22 01:28:55 +00:00
|
|
|
return pw.streamWriter.GetStorageOptions()
|
|
|
|
}
|
|
|
|
return pw.randomWriter.GetStorageOptions()
|
|
|
|
}
|
2021-12-24 01:17:32 +00:00
|
|
|
|
|
|
|
func (pw *PageWriter) Destroy() {
|
2021-12-25 06:38:22 +00:00
|
|
|
if pw.streamWriter != nil {
|
|
|
|
pw.streamWriter.Destroy()
|
|
|
|
}
|
2021-12-24 01:17:32 +00:00
|
|
|
pw.randomWriter.Destroy()
|
|
|
|
}
|
2021-12-24 01:48:34 +00: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
|
|
|
|
}
|