From 08a4b1ab4faa22630aee32acf34a62f006ef0726 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Wed, 28 Jan 2015 22:41:15 +0100 Subject: Delay fsync for index writes --- src/db.erl | 2 +- src/index.erl | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/db.erl b/src/db.erl index 0541678..0361aaf 100644 --- a/src/db.erl +++ b/src/db.erl @@ -181,7 +181,7 @@ handle_call(stop, _From, State) -> handle_call({add_index_nosync, {LeafHash, Index}}, _From, State) -> ok = perm:ensurefile_nosync(indexforhash_root_path(), LeafHash, integer_to_binary(Index)), - ok = index:add(index_path(), Index, LeafHash), + ok = index:add_nosync(index_path(), Index, LeafHash), {reply, ok, State}. indexforhash_sync(LeafHash, Index) -> 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") -- cgit v1.1