Revert "volume: avoid file.stat(), file.seek() if possible during writes"

This reverts commit c78409a598.
This commit is contained in:
Chris Lu 2021-02-19 03:56:27 -08:00
parent b961cd6208
commit a1210d1e8d
2 changed files with 6 additions and 33 deletions

View file

@ -12,8 +12,6 @@ var (
type DiskFile struct {
File *os.File
fullFilePath string
fileSize int64
modTime time.Time
}
func NewDiskFile(f *os.File) *DiskFile {
@ -28,24 +26,11 @@ func (df *DiskFile) ReadAt(p []byte, off int64) (n int, err error) {
}
func (df *DiskFile) WriteAt(p []byte, off int64) (n int, err error) {
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
return df.File.WriteAt(p, off)
}
func (df *DiskFile) Truncate(off int64) error {
err := df.File.Truncate(off)
if err == nil {
df.fileSize = off
df.modTime = time.Now()
}
return err
return df.File.Truncate(off)
}
func (df *DiskFile) Close() error {
@ -53,9 +38,6 @@ 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
@ -68,6 +50,5 @@ func (df *DiskFile) Name() string {
}
func (df *DiskFile) Sync() error {
df.fileSize = 0
return df.File.Sync()
}

View file

@ -41,7 +41,6 @@ type baseNeedleMapper struct {
indexFile *os.File
indexFileAccessLock sync.Mutex
indexFileOffset int64
}
func (nm *baseNeedleMapper) IndexFileSize() uint64 {
@ -57,18 +56,11 @@ func (nm *baseNeedleMapper) appendToIndexFile(key NeedleId, offset Offset, size
nm.indexFileAccessLock.Lock()
defer nm.indexFileAccessLock.Unlock()
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)
if _, err := nm.indexFile.Seek(0, 2); err != nil {
return fmt.Errorf("cannot seek end of indexfile %s: %v",
nm.indexFile.Name(), err)
}
_, err := nm.indexFile.Write(bytes)
return err
}