Skip to content

Instantly share code, notes, and snippets.

@RJ
Created April 15, 2011 12:40
Show Gist options
  • Save RJ/921625 to your computer and use it in GitHub Desktop.
Save RJ/921625 to your computer and use it in GitHub Desktop.
%% use sync log so we can check if it caused a wrap event
%% and record the timestamp/num entries for the new wrapfile
%% (ie, collect metadata about each wrapfile.N, for faster seeking)
disk_log:blog(State#state.log, Line),
%% Fun used to write index marker, if we need to this insertion:
WriteIndexFun = fun(FileOffset) ->
Info = disk_log:info(State#state.log),
NoItems = proplists:get_value(no_items, Info),
CurrFile = proplists:get_value(current_file, Info),
%% store an index marker:
IndexMarker = #indexmarker{ log=State#state.log,
n=CurrFile,
numitems=NoItems,
timestamp=Ts,
byteoffset=FileOffset },
?INFO("Writing index marker, NewI:~p NoItems:~p", [NewInsertionCounter, NoItems]),
disk_log:log(?INDEXMARKERNAME(State#state.uid), IndexMarker),
array:set(array:size(State#state.indexmarkers), IndexMarker, State#state.indexmarkers)
end,
%% Check for wrap msg
receive
%% always write indexmarker if we wrap:
{disk_log, _Node, _Log, {wrap, _NoLostItems}} ->
NewIndexMarkers = WriteIndexFun(0),
?INFO("Writing index marker due to wrap. I:~p Marker:~p",
[NewInsertionCounter, hd(NewIndexMarkers)])
after 0 ->
%% otherwise, write it if we are at an Nth boundary
case WasN of
true ->
NewIndexMarkers = WriteIndexFun(Offset),
?INFO("Writing index marker due to Nth. I:~p Marker:~p",
[NewInsertionCounter, hd(NewIndexMarkers)]);
%% Otherwise only write if this was the first ever entry to the log
false ->
case NewInsertionCounter =:= 1 of
true ->
NewIndexMarkers = WriteIndexFun(0),
?INFO("Writing index marker due to very first entry. I:~p Marker:~p",
[NewInsertionCounter, hd(NewIndexMarkers)]);
false ->
NewIndexMarkers = State#state.indexmarkers
end
end
end,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment