Move checksum file related code to machi_csum_table #11
4 changed files with 28 additions and 24 deletions
|
@ -95,7 +95,7 @@ verify_file_checksums_common(Sock1, EpochID, File, ReadChunk) ->
|
||||||
try
|
try
|
||||||
case ?FLU_C:checksum_list(Sock1, EpochID, File) of
|
case ?FLU_C:checksum_list(Sock1, EpochID, File) of
|
||||||
{ok, InfoBin} ->
|
{ok, InfoBin} ->
|
||||||
{Info, _} = machi_checksums:split_checksum_list_blob_decode(InfoBin),
|
{Info, _} = machi_csum_table:split_checksum_list_blob_decode(InfoBin),
|
||||||
Res = lists:foldl(verify_chunk_checksum(File, ReadChunk),
|
Res = lists:foldl(verify_chunk_checksum(File, ReadChunk),
|
||||||
[], Info),
|
[], Info),
|
||||||
{ok, Res};
|
{ok, Res};
|
||||||
|
|
|
@ -59,11 +59,12 @@ open(CSumFilename, _Opts) ->
|
||||||
-spec find(table(), machi_dt:chunk_pos(), machi_dt:chunk_size()) ->
|
-spec find(table(), machi_dt:chunk_pos(), machi_dt:chunk_size()) ->
|
||||||
{ok, machi_dt:chunk_csum()} | {error, trimmed|notfound}.
|
{ok, machi_dt:chunk_csum()} | {error, trimmed|notfound}.
|
||||||
find(#machi_csum_table{table=T}, Offset, Size) ->
|
find(#machi_csum_table{table=T}, Offset, Size) ->
|
||||||
|
%% TODO: Check whether all bytes here are written or not
|
||||||
case ets:lookup(T, Offset) of
|
case ets:lookup(T, Offset) of
|
||||||
[{Offset, Size, trimmed}] -> {error, trimmed};
|
[{Offset, Size, trimmed}] -> {error, trimmed};
|
||||||
[{Offset, Size, Checksum}] -> {ok, Checksum};
|
[{Offset, Size, Checksum}] -> {ok, Checksum};
|
||||||
[{Offset, _, Checksum}] -> {ok, Checksum};
|
[{Offset, _, _}] -> {error, unknown_chunk};
|
||||||
[] -> {error, notfound}
|
[] -> {error, unknown_chunk}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec write(table(), machi_dt:chunk_pos(), machi_dt:chunk_size(),
|
-spec write(table(), machi_dt:chunk_pos(), machi_dt:chunk_size(),
|
||||||
|
|
|
@ -597,9 +597,10 @@ handle_write(FHd, CsumTable, Filename, TaggedCsum, Offset, Data, U) ->
|
||||||
case machi_csum_table:find(CsumTable, Offset, Size) of
|
case machi_csum_table:find(CsumTable, Offset, Size) of
|
||||||
{error, trimmed} = Error ->
|
{error, trimmed} = Error ->
|
||||||
Error;
|
Error;
|
||||||
{error, notfound} ->
|
{error, unknown_chunk} ->
|
||||||
%% Just unknown pair of {offset, size}
|
%% The specified has some bytes written, while
|
||||||
%% given. Trust U and return as it is used
|
%% it's not in the checksum table. Trust U and
|
||||||
|
%% return as it is used.
|
||||||
{error, written};
|
{error, written};
|
||||||
{ok, TaggedCsum} ->
|
{ok, TaggedCsum} ->
|
||||||
case do_read(FHd, Filename, TaggedCsum, Offset, Size) of
|
case do_read(FHd, Filename, TaggedCsum, Offset, Size) of
|
||||||
|
|
|
@ -3,27 +3,29 @@
|
||||||
|
|
||||||
smoke_test() ->
|
smoke_test() ->
|
||||||
Filename = "./temp-checksum-dumb-file",
|
Filename = "./temp-checksum-dumb-file",
|
||||||
{ok, MC} = machi_checksums:new(Filename),
|
_ = file:delete(Filename),
|
||||||
|
{ok, MC} = machi_csum_table:open(Filename, []),
|
||||||
{Offset, Size, Checksum} = {64, 34, <<"deadbeef">>},
|
{Offset, Size, Checksum} = {64, 34, <<"deadbeef">>},
|
||||||
{error, unwritten} = machi_checksums:find(MC, Offset, Size),
|
{error, unknown_chunk} = machi_csum_table:find(MC, Offset, Size),
|
||||||
ok = machi_checksums:write(MC, Offset, Size, Checksum),
|
ok = machi_csum_table:write(MC, Offset, Size, Checksum),
|
||||||
{ok, Checksum} = machi_checksums:find(MC, Offset, Size),
|
{ok, Checksum} = machi_csum_table:find(MC, Offset, Size),
|
||||||
ok = machi_checksums:trim(MC, Offset, Size),
|
ok = machi_csum_table:trim(MC, Offset, Size),
|
||||||
{error, trimmed} = machi_checksums:find(MC, Offset, Size),
|
{error, trimmed} = machi_csum_table:find(MC, Offset, Size),
|
||||||
ok = machi_checksums:destroy(MC),
|
ok = machi_csum_table:close(MC),
|
||||||
ok = machi_checksums:delete_file(MC).
|
ok = machi_csum_table:delete(MC).
|
||||||
|
|
||||||
close_test() ->
|
close_test() ->
|
||||||
Filename = "./temp-checksum-dumb-file-2",
|
Filename = "./temp-checksum-dumb-file-2",
|
||||||
{ok, MC} = machi_checksums:new(Filename),
|
_ = file:delete(Filename),
|
||||||
|
{ok, MC} = machi_csum_table:open(Filename, []),
|
||||||
{Offset, Size, Checksum} = {64, 34, <<"deadbeef">>},
|
{Offset, Size, Checksum} = {64, 34, <<"deadbeef">>},
|
||||||
{error, unwritten} = machi_checksums:find(MC, Offset, Size),
|
{error, unknown_chunk} = machi_csum_table:find(MC, Offset, Size),
|
||||||
ok = machi_checksums:write(MC, Offset, Size, Checksum),
|
ok = machi_csum_table:write(MC, Offset, Size, Checksum),
|
||||||
{ok, Checksum} = machi_checksums:find(MC, Offset, Size),
|
{ok, Checksum} = machi_csum_table:find(MC, Offset, Size),
|
||||||
ok = machi_checksums:destroy(MC),
|
ok = machi_csum_table:close(MC),
|
||||||
|
|
||||||
{ok, MC2} = machi_checksums:new(Filename),
|
{ok, MC2} = machi_csum_table:open(Filename, []),
|
||||||
{ok, Checksum} = machi_checksums:find(MC2, Offset, Size),
|
{ok, Checksum} = machi_csum_table:find(MC2, Offset, Size),
|
||||||
ok = machi_checksums:trim(MC2, Offset, Size),
|
ok = machi_csum_table:trim(MC2, Offset, Size),
|
||||||
{error, trimmed} = machi_checksums:find(MC2, Offset, Size),
|
{error, trimmed} = machi_csum_table:find(MC2, Offset, Size),
|
||||||
ok = machi_checksums:delete_file(MC2).
|
ok = machi_csum_table:delete(MC2).
|
||||||
|
|
Loading…
Reference in a new issue