atomic file write fix

This commit is contained in:
Jae 2015-05-10 15:22:42 -07:00
parent 6020223e85
commit 626a68e670
2 changed files with 8 additions and 4 deletions

View File

@ -43,12 +43,14 @@ func AtomicWriteFile(filePath string, newBytes []byte) error {
return fmt.Errorf("Failed to write file %v. %v", filePath+".bak", err)
}
}
// Write newBytes to filePath.
err := ioutil.WriteFile(filePath, newBytes, 0600)
// Write newBytes to filePath.new
err := ioutil.WriteFile(filePath+".new", newBytes, 0600)
if err != nil {
return fmt.Errorf("Failed to write file %v. %v", filePath, err)
return fmt.Errorf("Failed to write file %v. %v", filePath+".new", err)
}
return nil
// Move filePath.new to filePath
err = os.Rename(filePath+".new", filePath)
return err
}
func EnsureDir(dir string) error {

View File

@ -241,6 +241,8 @@ func BenchmarkImmutableAvlTree(b *testing.B) {
b.StopTimer()
t := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, nil)
// 23000ns/op, 43000ops/s
// for i := 0; i < 10000000; i++ {
for i := 0; i < 1000000; i++ {
t.Set(RandUint64(), "")
}