Browse Source

atomic file write fix

pull/67/head
Jae 10 years ago
parent
commit
626a68e670
2 changed files with 8 additions and 4 deletions
  1. +6
    -4
      common/os.go
  2. +2
    -0
      merkle/iavl_test.go

+ 6
- 4
common/os.go 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 {


+ 2
- 0
merkle/iavl_test.go 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(), "")
}


Loading…
Cancel
Save