Skip to content

Commit 7332f0c

Browse files
committed
Add sync flag to flatfs
Enabled for the paranoid
1 parent c050ef7 commit 7332f0c

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

flatfs/flatfs.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ type Datastore struct {
3434
path string
3535
// length of the dir splay prefix, in bytes of hex digits
3636
hexPrefixLen int
37+
sync bool
3738
}
3839

3940
var _ datastore.Datastore = (*Datastore)(nil)
4041

41-
func New(path string, prefixLen int) (*Datastore, error) {
42+
func New(path string, prefixLen int, sync bool) (*Datastore, error) {
4243
if prefixLen <= 0 || prefixLen > maxPrefixLen {
4344
return nil, ErrBadPrefixLen
4445
}
4546
fs := &Datastore{
4647
path: path,
4748
// convert from binary bytes to bytes of hex encoding
4849
hexPrefixLen: prefixLen * hex.EncodedLen(1),
50+
sync: sync,
4951
}
5052
return fs, nil
5153
}
@@ -77,12 +79,12 @@ func (fs *Datastore) makePrefixDir(dir string) error {
7779
return err
7880
}
7981

80-
// In theory, if we create a new prefix dir and add a file to
81-
// it, the creation of the prefix dir itself might not be
82-
// durable yet. Sync the root dir after a successful mkdir of
83-
// a prefix dir, just to be paranoid.
84-
if err := syncDir(fs.path); err != nil {
85-
return err
82+
if fs.sync {
83+
// In theory, if we create a new prefix dir and add a file to
84+
// it, the creation of the prefix dir itself might not be
85+
// durable yet. Sync the root dir after a successful mkdir of
86+
// a prefix dir, just to be paranoid.
87+
return syncDir(fs.path)
8688
}
8789
return nil
8890
}
@@ -149,8 +151,10 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
149151
if _, err := tmp.Write(val); err != nil {
150152
return err
151153
}
152-
if err := tmp.Sync(); err != nil {
153-
return err
154+
if fs.sync {
155+
if err := tmp.Sync(); err != nil {
156+
return err
157+
}
154158
}
155159
if err := tmp.Close(); err != nil {
156160
return err
@@ -163,8 +167,8 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
163167
}
164168
removed = true
165169

166-
if err := syncDir(dir); err != nil {
167-
return err
170+
if fs.sync {
171+
return syncDir(dir)
168172
}
169173
return nil
170174
}
@@ -214,8 +218,10 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
214218
// Now we sync everything
215219
// sync and close files
216220
for fi, _ := range files {
217-
if err := fi.Sync(); err != nil {
218-
return err
221+
if fs.sync {
222+
if err := fi.Sync(); err != nil {
223+
return err
224+
}
219225
}
220226

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

239245
// now sync the dirs for those files
240-
for _, dir := range dirsToSync {
241-
if err := syncDir(dir); err != nil {
242-
return err
246+
if fs.sync {
247+
for _, dir := range dirsToSync {
248+
if err := syncDir(dir); err != nil {
249+
return err
250+
}
243251
}
244-
}
245252

246-
// sync top flatfs dir
247-
if err := syncDir(fs.path); err != nil {
248-
return err
253+
// sync top flatfs dir
254+
return syncDir(fs.path)
249255
}
250256

251257
return nil

0 commit comments

Comments
 (0)