Commit e644777b authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 4a57fffd
...@@ -21,6 +21,7 @@ package fs1 ...@@ -21,6 +21,7 @@ package fs1
// index for quickly finding oid -> latest data record // index for quickly finding oid -> latest data record
import ( import (
"bufio"
"bytes" "bytes"
"context" "context"
"encoding/binary" "encoding/binary"
...@@ -182,14 +183,17 @@ func (fsi *Index) SaveFile(path string) error { ...@@ -182,14 +183,17 @@ func (fsi *Index) SaveFile(path string) error {
return &IndexSaveError{err} // XXX needed? return &IndexSaveError{err} // XXX needed?
} }
// TODO use buffering for f (ogórek does not buffer itself on encoding) // use buffering for f (ogórek does not buffer itself on encoding)
err1 := fsi.Save(f) fb := bufio.NewWriter(f)
err2 := f.Close()
if err1 != nil || err2 != nil { err1 := fsi.Save(fb)
err2 := fb.Flush()
err3 := f.Close()
if err1 != nil || err2 != nil || err3 != nil {
os.Remove(f.Name()) os.Remove(f.Name())
err = err1 err = err1
if err == nil { if err == nil {
err = &IndexSaveError{err2} // XXX needed? err = &IndexSaveError{xerr.First(err2, err3)} // XXX needed?
} }
return err return err
} }
......
...@@ -221,7 +221,7 @@ func TestIndexSaveToPy(t *testing.T) { ...@@ -221,7 +221,7 @@ func TestIndexSaveToPy(t *testing.T) {
} }
func TestIndexBuildVerify(t *testing.T) { func TestIndexBuildVerify(t *testing.T) {
index, err := BuildIndexForFile(context.Background(), "testdata/1.fs") index, err := BuildIndexForFile(context.Background(), "testdata/1.fs", nil)
if err != nil { if err != nil {
t.Fatalf("index build: %v", err) t.Fatalf("index build: %v", err)
} }
...@@ -230,14 +230,14 @@ func TestIndexBuildVerify(t *testing.T) { ...@@ -230,14 +230,14 @@ func TestIndexBuildVerify(t *testing.T) {
t.Fatal("computed index differ from expected") t.Fatal("computed index differ from expected")
} }
_, err = index.VerifyForFile(context.Background(), "testdata/1.fs", -1) _, err = index.VerifyForFile(context.Background(), "testdata/1.fs", -1, nil)
if err != nil { if err != nil {
t.Fatalf("index verify: %v", err) t.Fatalf("index verify: %v", err)
} }
pos0, _ := index.Get(0) pos0, _ := index.Get(0)
index.Set(0, pos0 + 1) index.Set(0, pos0 + 1)
_, err = index.VerifyForFile(context.Background(), "testdata/1.fs", -1) _, err = index.VerifyForFile(context.Background(), "testdata/1.fs", -1, nil)
if err == nil { if err == nil {
t.Fatalf("index verify: expected error after tweak") t.Fatalf("index verify: expected error after tweak")
} }
...@@ -254,6 +254,24 @@ func BenchmarkIndexLoad(b *testing.B) { ...@@ -254,6 +254,24 @@ func BenchmarkIndexLoad(b *testing.B) {
} }
} }
func BenchmarkIndexSave(b *testing.B) {
// FIXME small testdata/1.fs is not representative for benchmarks
index, err := LoadIndexFile("testdata/1.fs.index")
if err != nil {
b.Fatal(err)
}
workdir := xworkdir(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = index.SaveFile(workdir + "/1.fs.index")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkIndexGet(b *testing.B) { func BenchmarkIndexGet(b *testing.B) {
// FIXME small testdata/1.fs is not representative for benchmarks // FIXME small testdata/1.fs is not representative for benchmarks
fsi, err := LoadIndexFile("testdata/1.fs.index") fsi, err := LoadIndexFile("testdata/1.fs.index")
...@@ -304,7 +322,7 @@ func needZODBPy(t *testing.T) { ...@@ -304,7 +322,7 @@ func needZODBPy(t *testing.T) {
} }
// create temp dir inside workRoot // create temp dir inside workRoot
func xworkdir(t *testing.T) string { func xworkdir(t testing.TB) string {
work, err := ioutil.TempDir(workRoot, "") work, err := ioutil.TempDir(workRoot, "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment