diff options
author | Magnus Ahltorp <map@kth.se> | 2015-01-28 17:29:58 +0100 |
---|---|---|
committer | Magnus Ahltorp <map@kth.se> | 2015-01-28 17:29:58 +0100 |
commit | 1f2c976ea9924589fc3dc6c6b0f6d231e724a45f (patch) | |
tree | c4eeb4276b17ffa8b84865374376c42995e7b690 /src/perm.erl | |
parent | 80e68e390f3a999c25fb3a153388d6d6d9ec64d7 (diff) |
Perform frontend index writes in two phases
Diffstat (limited to 'src/perm.erl')
-rw-r--r-- | src/perm.erl | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/perm.erl b/src/perm.erl index 9f02b55..614656a 100644 --- a/src/perm.erl +++ b/src/perm.erl @@ -2,7 +2,7 @@ %%% See LICENSE for licensing information. -module(perm). --export([ensurefile/3, readfile/2]). +-export([ensurefile/3, ensurefile_nosync/3, readfile/2]). -spec readfile_and_verify(string(), binary()) -> ok | differ | {error, atom()}. readfile_and_verify(Name, Content) -> @@ -49,13 +49,21 @@ path_for_key(Rootdir, Key) -> -spec ensurefile(string(), binary(), binary()) -> ok | differ. ensurefile(Rootdir, Key, Content) -> + ensurefile(Rootdir, Key, Content, sync). + +-spec ensurefile_nosync(string(), binary(), binary()) -> ok | differ. +ensurefile_nosync(Rootdir, Key, Content) -> + ensurefile(Rootdir, Key, Content, nosync). + +ensurefile(Rootdir, Key, Content, Syncflag) -> lager:debug("dir ~p key ~p", [Rootdir, Key]), {Dirs, Path} = path_for_key(Rootdir, Key), case readfile_and_verify(Path, Content) of ok -> lager:debug("key ~p existed, fsync", [Key]), - util:fsync([Path, Rootdir | Dirs]), - lager:debug("key ~p fsynced", [Key]); + ok = util:fsync([Path, Rootdir | Dirs]), + lager:debug("key ~p fsynced", [Key]), + ok; differ -> lager:debug("key ~p existed, was different", [Key]), differ; @@ -67,9 +75,15 @@ ensurefile(Rootdir, Key, Content) -> NurseryName = Rootdir ++ "nursery/" ++ util:tempfilename(hex:bin_to_hexstr(Key)), util:write_tempfile_and_rename(Path, NurseryName, Content), - lager:debug("key ~p added, fsync", [Key]), - util:fsync([Path, Rootdir | Dirs]), - lager:debug("key ~p fsynced", [Key]); + case Syncflag of + sync -> + lager:debug("key ~p added, fsync", [Key]), + ok = util:fsync([Path, Rootdir | Dirs]), + lager:debug("key ~p fsynced", [Key]), + ok; + nosync -> + ok + end; {error, Error} -> util:exit_with_error(Error, readfile, "Error reading file") end. |