mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
volume: avoid file.stat(), file.seek() if possible during writes
This commit is contained in:
parent
29575dadc5
commit
c78409a598
|
@ -12,6 +12,8 @@ var (
|
|||
type DiskFile struct {
|
||||
File *os.File
|
||||
fullFilePath string
|
||||
fileSize int64
|
||||
modTime time.Time
|
||||
}
|
||||
|
||||
func NewDiskFile(f *os.File) *DiskFile {
|
||||
|
@ -26,11 +28,24 @@ func (df *DiskFile) ReadAt(p []byte, off int64) (n int, err error) {
|
|||
}
|
||||
|
||||
func (df *DiskFile) WriteAt(p []byte, off int64) (n int, err error) {
|
||||
return df.File.WriteAt(p, off)
|
||||
n, err = df.File.WriteAt(p, off)
|
||||
if err == nil {
|
||||
waterMark := off + int64(n)
|
||||
if waterMark > df.fileSize {
|
||||
df.fileSize = waterMark
|
||||
df.modTime = time.Now()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (df *DiskFile) Truncate(off int64) error {
|
||||
return df.File.Truncate(off)
|
||||
err := df.File.Truncate(off)
|
||||
if err == nil {
|
||||
df.fileSize = off
|
||||
df.modTime = time.Now()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (df *DiskFile) Close() error {
|
||||
|
@ -38,6 +53,9 @@ func (df *DiskFile) Close() error {
|
|||
}
|
||||
|
||||
func (df *DiskFile) GetStat() (datSize int64, modTime time.Time, err error) {
|
||||
if df.fileSize != 0 {
|
||||
return df.fileSize, df.modTime, nil
|
||||
}
|
||||
stat, e := df.File.Stat()
|
||||
if e == nil {
|
||||
return stat.Size(), stat.ModTime(), nil
|
||||
|
@ -50,5 +68,6 @@ func (df *DiskFile) Name() string {
|
|||
}
|
||||
|
||||
func (df *DiskFile) Sync() error {
|
||||
df.fileSize = 0
|
||||
return df.File.Sync()
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ type baseNeedleMapper struct {
|
|||
|
||||
indexFile *os.File
|
||||
indexFileAccessLock sync.Mutex
|
||||
indexFileOffset int64
|
||||
}
|
||||
|
||||
func (nm *baseNeedleMapper) IndexFileSize() uint64 {
|
||||
|
@ -56,11 +57,18 @@ func (nm *baseNeedleMapper) appendToIndexFile(key NeedleId, offset Offset, size
|
|||
|
||||
nm.indexFileAccessLock.Lock()
|
||||
defer nm.indexFileAccessLock.Unlock()
|
||||
if _, err := nm.indexFile.Seek(0, 2); err != nil {
|
||||
return fmt.Errorf("cannot seek end of indexfile %s: %v",
|
||||
nm.indexFile.Name(), err)
|
||||
if nm.indexFileOffset == 0 {
|
||||
if fileSize, err := nm.indexFile.Seek(0, 2); err != nil {
|
||||
return fmt.Errorf("cannot seek end of indexfile %s: %v",
|
||||
nm.indexFile.Name(), err)
|
||||
} else {
|
||||
nm.indexFileOffset = fileSize
|
||||
}
|
||||
}
|
||||
written, err := nm.indexFile.WriteAt(bytes, nm.indexFileOffset)
|
||||
if err == nil {
|
||||
nm.indexFileOffset += int64(written)
|
||||
}
|
||||
_, err := nm.indexFile.Write(bytes)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue