Skip to content

Commit

Permalink
don't assume that a block has more data than what is being written to…
Browse files Browse the repository at this point in the history
… that block bz://497

Otherwise the binary-match fails, and the put_stream process dies, losing the new data.
  • Loading branch information
beerriot committed Jul 28, 2010
1 parent 172a8c6 commit 4cfe989
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/luwak_io.erl
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,14 @@ write_blocks(Riak, File, undefined, Start, Data, BlockSize, Written)
byte_size(Data)}|
Written])};
{ok, Block} ->
<<_:DataSize/binary, Tail/binary>> = luwak_block:data(Block),
BlockData = <<Data/binary, Tail/binary>>,
BlockData = case luwak_block:data(Block) of
<<_:DataSize/binary, Tail/binary>> ->
<<Data/binary, Tail/binary>>;
_ ->
%% block usage is less than the new
%% data for the block
Data
end,
{ok, NewBlock} = luwak_block:create(Riak, BlockData),
{ok, lists:reverse([{luwak_block:name(NewBlock),
byte_size(BlockData)}|
Expand Down
27 changes: 26 additions & 1 deletion test/luwak_io_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,29 @@ sub_block_partial_write_test() ->
Blocks = luwak_io:get_range(Riak, File3, 0, 66),
?debugFmt("blocks ~p~n", [Blocks]),
?assertEqual(<<B1/binary, B2/binary>>, iolist_to_binary(Blocks))
end).
end).

%% overwrite a file with more data than the file already had, but not
%% enough to touch the next block boundary
overwrite_nonfull_block_test() ->
test_helper:riak_test(fun(Riak) ->
{ok, F0} = luwak_file:create(Riak,
<<"basho1">>,
[{block_size, 4}],
dict:new()),
S0 = luwak_put_stream:start_link(Riak, F0, 0, 1000),
luwak_put_stream:send(S0, <<"a">>),
luwak_put_stream:close(S0),
timer:sleep(100),

{ok, F1} = luwak_file:get(Riak, <<"basho1">>),
S1 = luwak_put_stream:start_link(Riak, F1, 0, 1000),
luwak_put_stream:send(S1, <<"aa">>),
luwak_put_sream:close(S1),
timer:sleep(100),

{ok, F2} = luwak_file:get(Riak, <<"basho1">>),
Blocks = luwak_io:get_range(Riak, F2, 0, 4),
?debugFmt("blocks ~p~n", [Blocks]),
?assertEqual(<<"aa">>, iolist_to_binary(Blocks))
end).

0 comments on commit 4cfe989

Please sign in to comment.