disk file avoid file.Stat()

This commit is contained in:
Chris Lu 2021-02-20 20:06:06 -08:00
parent a2383b3b12
commit 7635f6b9fa
3 changed files with 12 additions and 3 deletions

View file

@ -92,7 +92,7 @@ func main() {
header := superBlock.Bytes()
if n, e := datFile.WriteAt(header, 0); n == 0 || e != nil {
if n, e := datBackend.WriteAt(header, 0); n == 0 || e != nil {
glog.Fatalf("cannot write super block: %v", e)
}

View file

@ -2,6 +2,7 @@ package backend
import (
"github.com/chrislusf/seaweedfs/weed/glog"
. "github.com/chrislusf/seaweedfs/weed/storage/types"
"os"
"time"
)
@ -22,11 +23,15 @@ func NewDiskFile(f *os.File) *DiskFile {
if err != nil {
glog.Fatalf("stat file %s: %v", f.Name(), err)
}
offset := stat.Size()
if offset%NeedlePaddingSize != 0 {
offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
}
return &DiskFile{
fullFilePath: f.Name(),
File: f,
fileSize: stat.Size(),
fileSize: offset,
modTime: stat.ModTime(),
}
}
@ -47,6 +52,10 @@ func (df *DiskFile) WriteAt(p []byte, off int64) (n int, err error) {
return
}
func (df *DiskFile) Append(p []byte) (n int, err error) {
return df.WriteAt(p, df.fileSize)
}
func (df *DiskFile) Truncate(off int64) error {
err := df.File.Truncate(off)
if err == nil {

View file

@ -286,7 +286,7 @@ func (v *Volume) makeupDiff(newDatFileName, newIdxFileName, oldDatFileName, oldI
if err != nil {
return fmt.Errorf("ReadNeedleBlob %s key %d offset %d size %d failed: %v", oldDatFile.Name(), key, increIdxEntry.offset.ToActualOffset(), increIdxEntry.size, err)
}
dst.Write(needleBytes)
dstDatBackend.Append(needleBytes)
util.Uint32toBytes(idxEntryBytes[8:12], uint32(offset/NeedlePaddingSize))
} else { //deleted needle
//fakeDelNeedle 's default Data field is nil