diff options
author | Magnus Ahltorp <map@kth.se> | 2015-04-07 22:15:22 +0200 |
---|---|---|
committer | Linus Nordberg <linus@nordberg.se> | 2015-04-08 18:20:57 +0200 |
commit | 950442da625b22e5e033f4955f61a2a4efbd487f (patch) | |
tree | 4324366d7fe866b7c5029dca904adf305eba2a2b /src | |
parent | 16520e4f8e9ac02d213455f213fe84ed0a705448 (diff) |
perm:ensurefile now only does fsync only when sync flag is set
Closes CATLFISH-35
Diffstat (limited to 'src')
-rw-r--r-- | src/db.erl | 2 | ||||
-rw-r--r-- | src/perm.erl | 57 |
2 files changed, 31 insertions, 28 deletions
@@ -59,7 +59,7 @@ add(LeafHash, Data) -> -spec add_entryhash(binary(), binary()) -> ok. add_entryhash(LeafHash, EntryHash) -> - ok = perm:ensurefile(entryhash_root_path(), EntryHash, LeafHash), + ok = perm:ensurefile_nosync(entryhash_root_path(), EntryHash, LeafHash), ok. -spec add_index_nosync(binary(), non_neg_integer()) -> ok. diff --git a/src/perm.erl b/src/perm.erl index 0cbe6cb..725cfb7 100644 --- a/src/perm.erl +++ b/src/perm.erl @@ -58,34 +58,37 @@ ensurefile_nosync(Rootdir, Key, Content) -> ensurefile(Rootdir, Key, Content, Syncflag) -> lager:debug("dir ~p key ~s", [Rootdir, mochihex:to_hex(Key)]), {Dirs, Path} = path_for_key(Rootdir, Key), - case readfile_and_verify(Path, Content) of - ok -> - lager:debug("key ~s existed, fsync", [mochihex:to_hex(Key)]), - ok = util:fsync([Path, Rootdir | Dirs]), + Result = + case readfile_and_verify(Path, Content) of + ok -> + lager:debug("key ~s existed, fsync", [mochihex:to_hex(Key)]), + ok = util:fsync([Path, Rootdir | Dirs]), + lager:debug("key ~s fsynced", [mochihex:to_hex(Key)]), + ok; + differ -> + lager:debug("key ~s existed, was different", + [mochihex:to_hex(Key)]), + differ; + {error, enoent} -> + lager:debug("key ~s didn't exist, add", [mochihex:to_hex(Key)]), + util:check_error(make_dirs([Rootdir, Rootdir ++ "nursery/"] + ++ Dirs), + makedir, "Error creating directory"), + NurseryName = Rootdir ++ "nursery/" ++ + util:tempfilename(hex:bin_to_hexstr(Key)), + util:write_tempfile_and_rename(Path, NurseryName, Content), + ok; + {error, Error} -> + util:exit_with_error(Error, readfile, "Error reading file") + end, + case Syncflag of + sync -> + lager:debug("key ~s added, fsync", [mochihex:to_hex(Key)]), + ok = util:fsync([Path, Rootdir | Dirs]), lager:debug("key ~s fsynced", [mochihex:to_hex(Key)]), - ok; - differ -> - lager:debug("key ~s existed, was different", [mochihex:to_hex(Key)]), - differ; - {error, enoent} -> - lager:debug("key ~s didn't exist, add", [mochihex:to_hex(Key)]), - util:check_error(make_dirs([Rootdir, Rootdir ++ "nursery/"] - ++ Dirs), - makedir, "Error creating directory"), - NurseryName = Rootdir ++ "nursery/" ++ - util:tempfilename(hex:bin_to_hexstr(Key)), - util:write_tempfile_and_rename(Path, NurseryName, Content), - case Syncflag of - sync -> - lager:debug("key ~s added, fsync", [mochihex:to_hex(Key)]), - ok = util:fsync([Path, Rootdir | Dirs]), - lager:debug("key ~s fsynced", [mochihex:to_hex(Key)]), - ok; - nosync -> - ok - end; - {error, Error} -> - util:exit_with_error(Error, readfile, "Error reading file") + Result; + nosync -> + Result end. -spec readfile(string(), binary()) -> binary() | noentry. |