diff options
author | Magnus Ahltorp <map@kth.se> | 2015-01-28 22:41:15 +0100 |
---|---|---|
committer | Magnus Ahltorp <map@kth.se> | 2015-01-28 22:41:15 +0100 |
commit | 7d8d0e05da0d8d221254fd6c85145d327157845e (patch) | |
tree | fcd0abfd8eb09394038f8b524a04ca9e38c3470f /src/index.erl | |
parent | 1f2c976ea9924589fc3dc6c6b0f6d231e724a45f (diff) |
Delay fsync for index writes
Diffstat (limited to 'src/index.erl')
-rw-r--r-- | src/index.erl | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/index.erl b/src/index.erl index 96195e3..c0e344a 100644 --- a/src/index.erl +++ b/src/index.erl @@ -12,13 +12,20 @@ %% TODO: Checksums -module(index). --export([get/2, getrange/3, add/3, addlast/2, indexsize/1]). +-export([get/2, getrange/3, add/3, add_nosync/3, addlast/2, indexsize/1]). -define(ENTRYSIZE, 32). -define(ENTRYSIZEINFILE, (?ENTRYSIZE*2+1)). -spec add(string(), integer() | last, binary()) -> ok. -add(Basepath, Index, Entry) when is_binary(Entry), size(Entry) == ?ENTRYSIZE -> +add(Basepath, Index, Entry) -> + add(Basepath, Index, Entry, sync). + +-spec add_nosync(string(), integer() | last, binary()) -> ok. +add_nosync(Basepath, Index, Entry) -> + add(Basepath, Index, Entry, nosync). + +add(Basepath, Index, Entry, Syncflag) when is_binary(Entry), size(Entry) == ?ENTRYSIZE -> case file:open(Basepath, [read, write, binary]) of {ok, File} -> {ok, Position} = file:position(File, eof), @@ -55,7 +62,12 @@ add(Basepath, Index, Entry) when is_binary(Entry), size(Entry) == ?ENTRYSIZE -> end end, ok = file:close(File), - util:fsync([Basepath, filename:dirname(Basepath)]); + case Syncflag of + sync -> + util:fsync([Basepath, filename:dirname(Basepath)]); + nosync -> + ok + end; {error, Error} -> util:exit_with_error(Error, writefile, "Error opening file for writing") |