Skip to content

Instantly share code, notes, and snippets.

@binarytemple
Created July 19, 2014 00:00
Show Gist options
  • Select an option

  • Save binarytemple/af1921c0b16e836b12a7 to your computer and use it in GitHub Desktop.

Select an option

Save binarytemple/af1921c0b16e836b12a7 to your computer and use it in GitHub Desktop.
ListCask = fun(Vnode,OutFile) ->
Fold_data = fun(Handle) ->
FoldFun = fun(Key, _Ts, _PosInfo, {ok,Acc}) -> io:format(OutFile, "~p~n",[binary_to_term(Key)]),{ok,Acc + 1} end,
try bitcask_fileops:fold_keys(Handle, FoldFun, {ok,0}, datafile) of
{ok,A} -> A;
{error,E} -> {error, E}
catch
_Error ->
{error, io_lib:format("~s~s~n",["Corrupted datafile: ",bitcask_fileops:datafile_name(Handle)])}
end
end,
Test_data = fun(Datafile) ->
case catch bitcask_fileops:open_file(Datafile) of
{ok, Handle} ->
KeyCount = Fold_data(Handle),
bitcask_fileops:close(Handle),
{ok, KeyCount};
{error, Reason} ->
{error, io_lib:format("Couldn't open bitcask data file ~s with reason ~p~n", [Datafile, Reason])};
_Error ->
{error, io_lib:format("Couldn't open bitcask data file ~s~n", [Datafile])}
end
end,
List_bitcask = fun(Dir) ->
case file:list_dir(Dir) of
{ok, Files} ->
[ Test_data(filename:absname(F, Dir)) || F <- Files,
filename:extension(F) == ".data"];
{error, Reason} ->
{error, io_lib:format("Error listing directory ~s: ~s",[Dir, Reason])};
Dirlist -> {error, Dirlist}
end
end,
{ok, Dpath} = application:get_env(bitcask,data_root),
case List_bitcask(filename:absname(Vnode,Dpath)) of
{error, Reason} ->
io:format("Error: ~s~n",[Reason]);
Rawlist ->
try lists:flatten(Rawlist) of
A -> A
catch
A -> {error, A}
end
end
end.
ListAll = fun(OutFileName) ->
{ok,Fhandle} = file:open(OutFileName,[write]),
Rawcounts = [ ListCask( io_lib:format("~B",[P]), Fhandle ) || {riak_kv_vnode, P, _Pid} <- riak_core_vnode_manager:all_vnodes() ],
file:close(Fhandle),
{KeyCount, Errors} = lists:foldl(fun(Count,{Total,Errors}) -> case Count of {ok, C} -> {Total + C, Errors}; _ -> {Total, [ Count | Errors]} end end,{0,[]}, lists:flatten(Rawcounts)),
io:format("Listed ~B keys to file ~s~n",[KeyCount,OutFileName]),
[ io:format("Error: ~p~n",[Err]) || Err <- Errors ],
ok
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment