forked from basho-labs/luwak
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Cliff Moon
committed
May 31, 2010
1 parent
c55d8dc
commit 0338f25
Showing
6 changed files
with
177 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
-module(luwak_get_stream). | ||
|
||
-export([get_stream/4, recv/2]). | ||
|
||
-record(map, {riak,blocksize,ref,pid,offset,endoffset}). | ||
|
||
-include_lib("luwak/include/luwak.hrl"). | ||
|
||
|
||
%% @spec get_stream(Riak :: riak(), File :: luwak_file(), Start :: int(), Length :: length()) -> | ||
%% get_stream() | {error, Reason} | ||
get_stream(Riak, File, Start, Length) -> | ||
Ref = make_ref(), | ||
BlockSize = luwak_file:get_property(File, block_size), | ||
Root = luwak_file:get_property(File, root), | ||
MapStart = [{{?N_BUCKET, Root}, 0}], | ||
Map = #map{riak=Riak,blocksize=BlockSize,ref=Ref,offset=Start,endoffset=Start+Length}, | ||
Receiver = proc_lib:spawn_link(receiver_fun(MapStart, self(), Map)), | ||
{get_stream, Ref, Receiver}. | ||
|
||
recv({get_stream, Ref, Pid}, Timeout) -> | ||
receive | ||
{get, Ref, Data, Offset} -> {Data, Offset}; | ||
{get, Ref, eos} -> eos; | ||
{get, Ref, closed} -> closed | ||
after Timeout -> | ||
{error, timeout} | ||
end. | ||
|
||
nonblock_mr(Riak,Query,MapInput) -> | ||
case Riak:mapred_stream(Query,self(),30000) of | ||
{ok, {ReqId, FlowPid}} -> | ||
luke_flow:add_inputs(FlowPid, MapInput), | ||
luke_flow:finish_inputs(FlowPid); | ||
Error -> | ||
exit(Error) | ||
end. | ||
|
||
receiver_fun(MapInput, Parent, Map=#map{riak=Riak,offset=Offset,endoffset=EndOffset,ref=Ref}) -> | ||
fun() -> | ||
Query = [{map,{qfun,map_fun()},Map#map{pid=self()}, false}], | ||
nonblock_mr(Riak, Query, MapInput), | ||
receive_loop(Ref, Offset, EndOffset, Parent) | ||
end. | ||
|
||
receive_loop(Ref, Offset, EndOffset, Parent) when Offset >= EndOffset -> | ||
?debugMsg("receive_loop sending eos~n"), | ||
Parent ! {get, Ref, eos}, | ||
ok; | ||
receive_loop(Ref, Offset, EndOffset, Parent) -> | ||
receive | ||
{get, Ref, Data, Offset} -> | ||
?debugFmt("receive_loop got ~p~n", [{get, Ref, Data, Offset}]), | ||
Parent ! {get, Ref, Data, Offset}, | ||
receive_loop(Ref, Offset+byte_size(Data), EndOffset, Parent); | ||
{close, Ref} -> | ||
?debugFmt("receive_loop got ~p~n", [{close, Ref}]), | ||
Parent ! {get, Ref, closed}, | ||
ok | ||
end. | ||
|
||
map_fun() -> | ||
fun(Obj, TreeOffset, Map) -> | ||
case (catch map(riak_object:get_value(Obj), TreeOffset, Map)) of | ||
{'EXIT', Error} -> | ||
error_logger:error_msg("map failed with: ~p~n", [Error]), | ||
throw(Error); | ||
Result -> Result | ||
end | ||
end. | ||
|
||
map(Parent=#n{}, TreeOffset, Map=#map{riak=Riak,offset=Offset,endoffset=EndOffset,blocksize=BlockSize}) -> | ||
?debugFmt("A map(~p, ~p, ~p)~n", [Parent, TreeOffset, Map]), | ||
Fun = fun({Name,Length},AccOffset) -> | ||
{[{{?N_BUCKET, Name}, AccOffset}], AccOffset+Length} | ||
end, | ||
Blocks = luwak_tree:get_range(Riak, Fun, Parent, BlockSize, TreeOffset, Offset, EndOffset), | ||
Query = [{map,{qfun,map_fun()},Map,false}], | ||
spawn(fun() -> | ||
?debugFmt("launching MR on ~p~n", [Blocks]), | ||
nonblock_mr(Riak, Query, Blocks) | ||
end), | ||
[]; | ||
map(Block, TreeOffset, Map=#map{riak=Riak,offset=Offset,endoffset=EndOffset,ref=Ref,pid=Pid,blocksize=BlockSize}) when TreeOffset < Offset -> | ||
?debugFmt("B map(~p, ~p, ~p)~n", [Block, TreeOffset, Map]), | ||
PartialSize = Offset - TreeOffset, | ||
<<_:PartialSize/binary, Tail/binary>> = luwak_block:data(Block), | ||
?debugFmt("sending ~p~n", [{get, Ref, Tail, Offset}]), | ||
Pid ! {get, Ref, Tail, Offset}, | ||
[]; | ||
map(Block, TreeOffset, Map=#map{riak=Riak,offset=Offset,endoffset=EndOffset,ref=Ref,pid=Pid,blocksize=BlockSize}) when BlockSize >= EndOffset - TreeOffset -> | ||
?debugFmt("C map(~p, ~p, ~p)~n", [Block, TreeOffset, Map]), | ||
PartialSize = EndOffset - TreeOffset, | ||
<<PartialData:PartialSize/binary, _/binary>> = luwak_block:data(Block), | ||
?debugFmt("sending ~p~n", [{get, Ref, PartialData, TreeOffset}]), | ||
Pid ! {get, Ref, PartialData, TreeOffset}, | ||
[]; | ||
map(Block, TreeOffset, Map=#map{riak=Riak,offset=Offset,endoffset=EndOffset,ref=Ref,pid=Pid,blocksize=BlockSize}) -> | ||
?debugFmt("D map(~p, ~p, ~p)~n", [Block, TreeOffset, Map]), | ||
Data = luwak_block:data(Block), | ||
?debugFmt("sending ~p~n", [{get, Ref, Data, TreeOffset}]), | ||
Pid ! {get, Ref, Data, TreeOffset}, | ||
[]. | ||
|
||
ident_fun() -> | ||
fun({Name,Length}, NodeOffset) -> | ||
{[{Name,Length}], NodeOffset} | ||
end. | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-module(luwak_get_stream_tests). | ||
|
||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
simple_get_stream_test() -> | ||
test_helper:riak_test(fun(Riak) -> | ||
{ok, File} = luwak_file:create(Riak, <<"file1">>, [{block_size,3},{tree_order,3}], dict:new()), | ||
{ok, Written, File1} = luwak_io:put_range(Riak, File, 0, <<"wontyoupleasetouchmymonkey">>), | ||
GetStream = luwak_get_stream:get_stream(Riak, File1, 0, 26), | ||
timer:sleep(100), | ||
?assertEqual({<<"won">>, 0}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"tyo">>, 3}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"upl">>, 6}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"eas">>, 9}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"eto">>, 12}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"uch">>, 15}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"mym">>, 18}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"onk">>, 21}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"ey">>, 24}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual(eos, luwak_get_stream:recv(GetStream, 1000)) | ||
end). | ||
|
||
three_level_tree_stream_test_() -> | ||
Test = fun() -> | ||
test_helper:riak_test(fun(Riak) -> | ||
{ok, File} = luwak_file:create(Riak, <<"file1">>, [{block_size,2},{tree_order,2}], dict:new()), | ||
{ok, Written, File1} = luwak_io:put_range(Riak, File, 0, <<"wontyoupleasetouchmymonkey">>), | ||
GetStream = luwak_get_stream:get_stream(Riak, File1, 3, 10), | ||
?assertEqual({<<"t">>, 3}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"yo">>, 4}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"up">>, 6}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"le">>, 8}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"as">>, 10}, luwak_get_stream:recv(GetStream, 1000)), | ||
?assertEqual({<<"e">>, 12}, luwak_get_stream:recv(GetStream, 1000)) | ||
end) | ||
end, | ||
{timeout, 30000, Test}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters