Commit b72b00c3 authored by Jeromy's avatar Jeromy

add option to disable syncing

parent 3488e4dd
...@@ -34,11 +34,14 @@ type Datastore struct { ...@@ -34,11 +34,14 @@ type Datastore struct {
path string path string
// length of the dir splay prefix, in bytes of hex digits // length of the dir splay prefix, in bytes of hex digits
hexPrefixLen int hexPrefixLen int
// sychronize all writes and directory changes for added safety
sync bool
} }
var _ datastore.Datastore = (*Datastore)(nil) var _ datastore.Datastore = (*Datastore)(nil)
func New(path string, prefixLen int) (*Datastore, error) { func New(path string, prefixLen int, sync bool) (*Datastore, error) {
if prefixLen <= 0 || prefixLen > maxPrefixLen { if prefixLen <= 0 || prefixLen > maxPrefixLen {
return nil, ErrBadPrefixLen return nil, ErrBadPrefixLen
} }
...@@ -81,8 +84,10 @@ func (fs *Datastore) makePrefixDir(dir string) error { ...@@ -81,8 +84,10 @@ func (fs *Datastore) makePrefixDir(dir string) error {
// it, the creation of the prefix dir itself might not be // it, the creation of the prefix dir itself might not be
// durable yet. Sync the root dir after a successful mkdir of // durable yet. Sync the root dir after a successful mkdir of
// a prefix dir, just to be paranoid. // a prefix dir, just to be paranoid.
if err := syncDir(fs.path); err != nil { if fs.sync {
return err if err := syncDir(fs.path); err != nil {
return err
}
} }
return nil return nil
} }
...@@ -149,8 +154,10 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error { ...@@ -149,8 +154,10 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
if _, err := tmp.Write(val); err != nil { if _, err := tmp.Write(val); err != nil {
return err return err
} }
if err := tmp.Sync(); err != nil { if fs.sync {
return err if err := tmp.Sync(); err != nil {
return err
}
} }
if err := tmp.Close(); err != nil { if err := tmp.Close(); err != nil {
return err return err
...@@ -163,8 +170,10 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error { ...@@ -163,8 +170,10 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
} }
removed = true removed = true
if err := syncDir(dir); err != nil { if fs.sync {
return err if err := syncDir(dir); err != nil {
return err
}
} }
return nil return nil
} }
...@@ -214,8 +223,10 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error { ...@@ -214,8 +223,10 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
// Now we sync everything // Now we sync everything
// sync and close files // sync and close files
for fi, _ := range files { for fi, _ := range files {
if err := fi.Sync(); err != nil { if fs.sync {
return err if err := fi.Sync(); err != nil {
return err
}
} }
if err := fi.Close(); err != nil { if err := fi.Close(); err != nil {
...@@ -237,15 +248,17 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error { ...@@ -237,15 +248,17 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
} }
// now sync the dirs for those files // now sync the dirs for those files
for _, dir := range dirsToSync { if fs.sync {
if err := syncDir(dir); err != nil { for _, dir := range dirsToSync {
return err if err := syncDir(dir); err != nil {
return err
}
} }
}
// sync top flatfs dir // sync top flatfs dir
if err := syncDir(fs.path); err != nil { if err := syncDir(fs.path); err != nil {
return err return err
}
} }
return nil return nil
......
...@@ -35,7 +35,7 @@ func TestBadPrefixLen(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestBadPrefixLen(t *testing.T) {
defer cleanup() defer cleanup()
for i := 0; i > -3; i-- { for i := 0; i > -3; i-- {
_, err := flatfs.New(temp, 0) _, err := flatfs.New(temp, i, false)
if g, e := err, flatfs.ErrBadPrefixLen; g != e { if g, e := err, flatfs.ErrBadPrefixLen; g != e {
t.Errorf("expected ErrBadPrefixLen, got: %v", g) t.Errorf("expected ErrBadPrefixLen, got: %v", g)
} }
...@@ -46,7 +46,7 @@ func TestPutBadValueType(t *testing.T) { ...@@ -46,7 +46,7 @@ func TestPutBadValueType(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -61,7 +61,7 @@ func TestPut(t *testing.T) { ...@@ -61,7 +61,7 @@ func TestPut(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -76,7 +76,7 @@ func TestGet(t *testing.T) { ...@@ -76,7 +76,7 @@ func TestGet(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -104,7 +104,7 @@ func TestPutOverwrite(t *testing.T) { ...@@ -104,7 +104,7 @@ func TestPutOverwrite(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -136,7 +136,7 @@ func TestGetNotFoundError(t *testing.T) { ...@@ -136,7 +136,7 @@ func TestGetNotFoundError(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -154,7 +154,7 @@ func TestStorage(t *testing.T) { ...@@ -154,7 +154,7 @@ func TestStorage(t *testing.T) {
const prefixLen = 2 const prefixLen = 2
const prefix = "7175" const prefix = "7175"
const target = prefix + string(os.PathSeparator) + "71757578.data" const target = prefix + string(os.PathSeparator) + "71757578.data"
fs, err := flatfs.New(temp, prefixLen) fs, err := flatfs.New(temp, prefixLen, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -209,7 +209,7 @@ func TestHasNotFound(t *testing.T) { ...@@ -209,7 +209,7 @@ func TestHasNotFound(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -227,7 +227,7 @@ func TestHasFound(t *testing.T) { ...@@ -227,7 +227,7 @@ func TestHasFound(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -249,7 +249,7 @@ func TestDeleteNotFound(t *testing.T) { ...@@ -249,7 +249,7 @@ func TestDeleteNotFound(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -264,7 +264,7 @@ func TestDeleteFound(t *testing.T) { ...@@ -264,7 +264,7 @@ func TestDeleteFound(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -289,7 +289,7 @@ func TestQuerySimple(t *testing.T) { ...@@ -289,7 +289,7 @@ func TestQuerySimple(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -325,7 +325,7 @@ func TestBatchPut(t *testing.T) { ...@@ -325,7 +325,7 @@ func TestBatchPut(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -337,7 +337,7 @@ func TestBatchDelete(t *testing.T) { ...@@ -337,7 +337,7 @@ func TestBatchDelete(t *testing.T) {
temp, cleanup := tempdir(t) temp, cleanup := tempdir(t)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
t.Fatalf("New fail: %v\n", err) t.Fatalf("New fail: %v\n", err)
} }
...@@ -360,7 +360,7 @@ func BenchmarkConsecutivePut(b *testing.B) { ...@@ -360,7 +360,7 @@ func BenchmarkConsecutivePut(b *testing.B) {
temp, cleanup := tempdir(b) temp, cleanup := tempdir(b)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
b.Fatalf("New fail: %v\n", err) b.Fatalf("New fail: %v\n", err)
} }
...@@ -390,7 +390,7 @@ func BenchmarkBatchedPut(b *testing.B) { ...@@ -390,7 +390,7 @@ func BenchmarkBatchedPut(b *testing.B) {
temp, cleanup := tempdir(b) temp, cleanup := tempdir(b)
defer cleanup() defer cleanup()
fs, err := flatfs.New(temp, 2) fs, err := flatfs.New(temp, 2, false)
if err != nil { if err != nil {
b.Fatalf("New fail: %v\n", err) b.Fatalf("New fail: %v\n", 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