Skip to content

Commit

Permalink
add option to disable syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Nov 9, 2015
1 parent c050ef7 commit 9a9fcb4
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions flatfs/flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ type Datastore struct {
path string
// length of the dir splay prefix, in bytes of hex digits
hexPrefixLen int

// sychronize all writes and directory changes for added safety
sync bool
}

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 {
return nil, ErrBadPrefixLen
}
Expand Down Expand Up @@ -81,8 +84,10 @@ func (fs *Datastore) makePrefixDir(dir string) error {
// it, the creation of the prefix dir itself might not be
// durable yet. Sync the root dir after a successful mkdir of
// a prefix dir, just to be paranoid.
if err := syncDir(fs.path); err != nil {
return err
if fs.sync {
if err := syncDir(fs.path); err != nil {
return err
}
}
return nil
}
Expand Down Expand Up @@ -149,8 +154,10 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
if _, err := tmp.Write(val); err != nil {
return err
}
if err := tmp.Sync(); err != nil {
return err
if fs.sync {
if err := tmp.Sync(); err != nil {
return err
}
}
if err := tmp.Close(); err != nil {
return err
Expand All @@ -163,8 +170,10 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
}
removed = true

if err := syncDir(dir); err != nil {
return err
if fs.sync {
if err := syncDir(dir); err != nil {
return err
}
}
return nil
}
Expand Down Expand Up @@ -214,8 +223,10 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
// Now we sync everything
// sync and close files
for fi, _ := range files {
if err := fi.Sync(); err != nil {
return err
if fs.sync {
if err := fi.Sync(); err != nil {
return err
}
}

if err := fi.Close(); err != nil {
Expand All @@ -237,15 +248,17 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
}

// now sync the dirs for those files
for _, dir := range dirsToSync {
if err := syncDir(dir); err != nil {
return err
if fs.sync {
for _, dir := range dirsToSync {
if err := syncDir(dir); err != nil {
return err
}
}
}

// sync top flatfs dir
if err := syncDir(fs.path); err != nil {
return err
// sync top flatfs dir
if err := syncDir(fs.path); err != nil {
return err
}
}

return nil
Expand Down

0 comments on commit 9a9fcb4

Please sign in to comment.