WIP: checksum_list incomplete implementation....

This commit is contained in:
Scott Lystig Fritchie 2015-06-23 16:53:06 +09:00
parent 6e77a4ea74
commit 6722b3c0f1
3 changed files with 52 additions and 2 deletions

View file

@ -161,6 +161,17 @@ message Mpb_ReadChunkResp {
optional Mpb_ChunkCSum csum = 3; optional Mpb_ChunkCSum csum = 3;
} }
// read_chunk() request & response
message Mpb_ChecksumListReq {
required string file = 1;
}
message Mpb_ChecksumListResp {
required Mpb_GeneralStatusCode status = 1;
optional bytes chunk = 2;
}
////////////////////////////////////////// //////////////////////////////////////////
// //
// request & response wrapper // request & response wrapper
@ -183,6 +194,7 @@ message Mpb_Request {
optional Mpb_AppendChunkReq append_chunk = 12; optional Mpb_AppendChunkReq append_chunk = 12;
optional Mpb_WriteChunkReq write_chunk = 13; optional Mpb_WriteChunkReq write_chunk = 13;
optional Mpb_ReadChunkReq read_chunk = 14; optional Mpb_ReadChunkReq read_chunk = 14;
optional Mpb_ChecksumListReq checksum_list = 15;
} }
message Mpb_Response { message Mpb_Response {
@ -204,4 +216,5 @@ message Mpb_Response {
optional Mpb_AppendChunkResp append_chunk = 12; optional Mpb_AppendChunkResp append_chunk = 12;
optional Mpb_WriteChunkResp write_chunk = 13; optional Mpb_WriteChunkResp write_chunk = 13;
optional Mpb_ReadChunkResp read_chunk = 14; optional Mpb_ReadChunkResp read_chunk = 14;
optional Mpb_ChecksumListResp checksum_list = 15;
} }

View file

@ -40,7 +40,8 @@
auth/3, auth/4, auth/3, auth/4,
append_chunk/6, append_chunk/7, append_chunk/6, append_chunk/7,
write_chunk/5, write_chunk/6, write_chunk/5, write_chunk/6,
read_chunk/4, read_chunk/5 read_chunk/4, read_chunk/5,
checksum_list/2, checksum_list/3
]). ]).
%% gen_server callbacks %% gen_server callbacks
@ -96,6 +97,12 @@ read_chunk(PidSpec, File, Offset, Size) ->
read_chunk(PidSpec, File, Offset, Size, Timeout) -> read_chunk(PidSpec, File, Offset, Size, Timeout) ->
send_sync(PidSpec, {read_chunk, File, Offset, Size}, Timeout). send_sync(PidSpec, {read_chunk, File, Offset, Size}, Timeout).
checksum_list(PidSpec, File) ->
checksum_list(PidSpec, File, ?DEFAULT_TIMEOUT).
checksum_list(PidSpec, File, Timeout) ->
send_sync(PidSpec, {checksum_list, File}, Timeout).
send_sync(PidSpec, Cmd, Timeout) -> send_sync(PidSpec, Cmd, Timeout) ->
gen_server:call(PidSpec, {send_sync, Cmd}, Timeout). gen_server:call(PidSpec, {send_sync, Cmd}, Timeout).
@ -281,6 +288,28 @@ do_send_sync({read_chunk, File, Offset, Size},
catch X:Y -> catch X:Y ->
Res = {bummer, {X, Y, erlang:get_stacktrace()}}, Res = {bummer, {X, Y, erlang:get_stacktrace()}},
{Res, S#state{count=Count+1}} {Res, S#state{count=Count+1}}
end;
do_send_sync({checksum_list, File},
#state{sock=Sock, sock_id=Index, count=Count}=S) ->
try
ReqID = <<Index:64/big, Count:64/big>>,
Req = #mpb_checksumlistreq{file=File},
R1a = #mpb_request{req_id=ReqID,
checksum_list=Req},
Bin1a = machi_pb:encode_mpb_request(R1a),
ok = gen_tcp:send(Sock, Bin1a),
{ok, Bin1B} = gen_tcp:recv(Sock, 0),
case (catch machi_pb:decode_mpb_response(Bin1B)) of
#mpb_response{req_id=ReqID, checksum_list=R} when R /= undefined ->
Result = convert_checksum_list_resp(R),
{Result, S#state{count=Count+1}};
#mpb_response{req_id=ReqID, generic=G} when G /= undefined ->
#mpb_errorresp{code=Code, msg=Msg, extra=Extra} = G,
{{error, {Code, Msg, Extra}}, S#state{count=Count+1}}
end
catch X:Y ->
Res = {bummer, {X, Y, erlang:get_stacktrace()}},
{Res, S#state{count=Count+1}}
end. end.
convert_csum_req(none) -> convert_csum_req(none) ->
@ -316,3 +345,8 @@ convert_read_chunk_resp(#mpb_readchunkresp{status='OK', chunk=Chunk}) ->
{ok, Chunk}; {ok, Chunk};
convert_read_chunk_resp(#mpb_readchunkresp{status=Status}) -> convert_read_chunk_resp(#mpb_readchunkresp{status=Status}) ->
convert_general_status_code(Status). convert_general_status_code(Status).
convert_checksum_list_resp(#mpb_checksumlistresp{status='OK', chunk=Chunk}) ->
{ok, Chunk};
convert_checksum_list_resp(#mpb_checksumlistresp{status=Status}) ->
convert_general_status_code(Status).

View file

@ -70,16 +70,19 @@ smoke_test2() ->
{ok, {Off2, Size2, File2}} = {ok, {Off2, Size2, File2}} =
?C:append_chunk(Clnt, PK, Prefix, Chunk2, CSum2, 1024), ?C:append_chunk(Clnt, PK, Prefix, Chunk2, CSum2, 1024),
Chunk3 = ["This is a ", <<"test,">>, 32, [["Hello, world!"]]], Chunk3 = ["This is a ", <<"test,">>, 32, [["Hello, world!"]]],
File3 = File2,
Off3 = Off2 + iolist_size(Chunk2), Off3 = Off2 + iolist_size(Chunk2),
Size3 = iolist_size(Chunk3), Size3 = iolist_size(Chunk3),
ok = ?C:write_chunk(Clnt, File2, Off3, Chunk3, none), ok = ?C:write_chunk(Clnt, File2, Off3, Chunk3, none),
Reads = [{iolist_to_binary(Chunk1), File1, Off1, Size1}, Reads = [{iolist_to_binary(Chunk1), File1, Off1, Size1},
{iolist_to_binary(Chunk2), File2, Off2, Size2}, {iolist_to_binary(Chunk2), File2, Off2, Size2},
{iolist_to_binary(Chunk3), File2, Off3, Size3}], {iolist_to_binary(Chunk3), File3, Off3, Size3}],
[{ok, Ch} = ?C:read_chunk(Clnt, Fl, Off, Sz) || [{ok, Ch} = ?C:read_chunk(Clnt, Fl, Off, Sz) ||
{Ch, Fl, Off, Sz} <- Reads], {Ch, Fl, Off, Sz} <- Reads],
{ok, _} = ?C:checksum_list(Clnt, File1),
ok ok
after after
(catch ?C:quit(Clnt)) (catch ?C:quit(Clnt))