Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/derflow.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
-module(derflow).
-behaviour(application).
-export([start/2,
stop/1,
bind/2,
bind/3,
read/1,
declare/0,
thread/3,
get_stream/1,
byNeed/2,
byNeed/3,
waitNeeded/1,
async_print_stream/1]).
stop/1,
bind/2,
bind/3,
read/1,
declare/0,
thread/3,
get_stream/1,
byNeed/2,
byNeed/3,
waitNeeded/1,
async_print_stream/1]).
start(normal, _Args) ->
derflow_sup:start_link().

Expand Down Expand Up @@ -49,17 +49,17 @@ get_stream(Stream)->
async_print_stream(Stream)->
io:format("Stream: ~w~n", [Stream]),
case read(Stream) of
{nil, _} -> {ok, stream_read};
{Value, Next} ->
io:format("Value of stream: ~w~n",[Value]),
async_print_stream(Next)
{nil, _} -> {ok, stream_read};
{Value, Next} ->
io:format("Value of stream: ~w~n",[Value]),
async_print_stream(Next)
end.

%Internal functions

internal_get_stream(Head, Output) ->
case read(Head) of
{nil, _} -> Output;
{Value, Next} ->
internal_get_stream(Next, lists:append(Output, [Value]))
{nil, _} -> Output;
{Value, Next} ->
internal_get_stream(Next, lists:append(Output, [Value]))
end.
116 changes: 58 additions & 58 deletions src/derflow_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init(_Args) ->
io:format("Init called~n"),
ets:new(dvstore, [set, named_table, public, {write_concurrency, true}]),
{ok, #state{clock=0}}.
io:format("Init called~n"),
ets:new(dvstore, [set, named_table, public, {write_concurrency, true}]),
{ok, #state{clock=0}}.

declare() ->
gen_server:call(?MODULE, {declare}).
Expand All @@ -30,18 +30,18 @@ bind(Id, Value) ->
waitNeeded(Id) ->
gen_server:call(?MODULE, {waitNeeded, Id}).

wait(X) ->
gen_server:call(?MODULE, {wait, X}).

read(X) ->
gen_server:call(?MODULE, {read, X}).

handle_call({declare}, _From, State) ->
Clock = State#state.clock +1,
V = #dv{value=empty, next=empty},
ets:insert(dvstore, {Clock, V}),
{reply, {id, Clock}, State#state{clock=Clock}};
Clock = State#state.clock +1,
V = #dv{value=empty, next=empty},
ets:insert(dvstore, {Clock, V}),
{reply, {id, Clock}, State#state{clock=Clock}};

handle_call({next, Id}, _From, State)->
io:format("Requesting for next~w~n",[Id]),
Expand Down Expand Up @@ -70,34 +70,34 @@ handle_call({waitNeeded, Id}, From, State) ->
case V#dv.waitingThreads of [_H|_T] ->
{reply, ok, State};
_ ->
ets:insert(dvstore, {Id, V#dv{lazy=true, creator=From}}),
ets:insert(dvstore, {Id, V#dv{lazy=true, creator=From}}),
{noreply, State}
end;

%%%What if the Key does not exist in the map?%%%
handle_call({read,X}, From, State) ->
[{_Key,V}] = ets:lookup(dvstore, X),
[{_Key,V}] = ets:lookup(dvstore, X),
Value = V#dv.value,
Bounded = V#dv.bounded,
Creator = V#dv.creator,
Lazy = V#dv.lazy,
%%%Need to distinguish that value is not calculated or is the end of a list%%%
if Bounded == true ->
{reply, {Value, V#dv.next}, State};
true ->
if Lazy == true ->
WT = lists:append(V#dv.waitingThreads, [From]),
Bounded = V#dv.bounded,
Creator = V#dv.creator,
Lazy = V#dv.lazy,
%%%Need to distinguish that value is not calculated or is the end of a list%%%
if Bounded == true ->
{reply, {Value, V#dv.next}, State};
true ->
if Lazy == true ->
WT = lists:append(V#dv.waitingThreads, [From]),
V1 = V#dv{waitingThreads=WT},
ets:insert(dvstore, {X, V1}),
gen_server:reply(Creator, ok),
{noreply, State};
true ->
WT = lists:append(V#dv.waitingThreads, [From]),
V1 = V#dv{waitingThreads=WT},
ets:insert(dvstore, {X, V1}),
{noreply, State}
end
end;
gen_server:reply(Creator, ok),
{noreply, State};
true ->
WT = lists:append(V#dv.waitingThreads, [From]),
V1 = V#dv{waitingThreads=WT},
ets:insert(dvstore, {X, V1}),
{noreply, State}
end
end;

handle_call({wait, _X}, _From, State) ->
%_V = wait_for_value(X, State#state.kv),
Expand All @@ -108,43 +108,43 @@ handle_cast({_}, State) ->


%putLazy(Value, Next, Key, From) ->
% %io:format("Put lazy ~w~n",[Key]),
% V1 = #dv{value= Value, next =Next, bounded= true, lazy = true, creator = From},
% ets:insert(dvstore, {Key, V1}).
% %io:format("Put lazy ~w~n",[Key]),
% V1 = #dv{value= Value, next =Next, bounded= true, lazy = true, creator = From},
% ets:insert(dvstore, {Key, V1}).

%Bind the key with the result (either by assignment or calculating)
%executeLazy(Key, V) ->
% %io:format("Execute lazy ~w~n",[Key]),
% Value= V#dv.value,
% case Value of {F, Arg} ->
% Result = F(Arg);
% _ ->
% Result = Value
% end,
% V1 = V#dv{value= Result, lazy = false, waitingThreads= []},
% ets:insert(dvstore, {Key, V1}),
% Result.
% %io:format("Execute lazy ~w~n",[Key]),
% Value= V#dv.value,
% case Value of {F, Arg} ->
% Result = F(Arg);
% _ ->
% Result = Value
% end,
% V1 = V#dv{value= Result, lazy = false, waitingThreads= []},
% ets:insert(dvstore, {Key, V1}),
% Result.

put(Value, Next, Key) ->
[{_Key,V}] = ets:lookup(dvstore, Key),
Threads = V#dv.waitingThreads,
V1 = #dv{value= Value, next =Next, bounded= true,lazy=false},
ets:insert(dvstore, {Key, V1}),
replyToAll(Threads, Value, Next).
[{_Key,V}] = ets:lookup(dvstore, Key),
Threads = V#dv.waitingThreads,
V1 = #dv{value= Value, next =Next, bounded= true,lazy=false},
ets:insert(dvstore, {Key, V1}),
replyToAll(Threads, Value, Next).

execute_and_put(F, Arg, Next, Key) ->
[{_Key,V}] = ets:lookup(dvstore, Key),
Threads = V#dv.waitingThreads,
Value = F(Arg),
V1 = #dv{value= Value, next =Next, bounded= true, lazy=false},
ets:insert(dvstore, {Key, V1}),
replyToAll(Threads, Value, Next).
[{_Key,V}] = ets:lookup(dvstore, Key),
Threads = V#dv.waitingThreads,
Value = F(Arg),
V1 = #dv{value= Value, next =Next, bounded= true, lazy=false},
ets:insert(dvstore, {Key, V1}),
replyToAll(Threads, Value, Next).

replyToAll([], _Value, _Next) ->
ok;
ok;
replyToAll([H|T], Value, Next) ->
gen_server:reply(H,{Value,Next}),
replyToAll(T, Value, Next).
gen_server:reply(H,{Value,Next}),
replyToAll(T, Value, Next).

code_change(_, State, _) ->
{ok, State}.
Expand All @@ -158,9 +158,9 @@ terminate(normal, _State) ->
%wait_for_value(X, KV)->
% IsKey = derflow_map:is_key(X, KV),
% if IsKey == true ->
% derflow_map:get(X, KV);
% true ->
% waitTime(500),
% derflow_map:get(X, KV);
% true ->
% waitTime(500),
% wait_for_value(X, KV)
% end.
%
Expand Down
26 changes: 13 additions & 13 deletions test/bounded_buffer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ test2() ->
producer(Init, N, Output) ->
if (N>0) ->
derflow:waitNeeded(Output),
{id,Next} = derflow:bind(Output,Init),
io:format("Prod:Bound for ~w next ~w Produced~w ~n",[Output, Next, 11-N]),
{id,Next} = derflow:bind(Output,Init),
io:format("Prod:Bound for ~w next ~w Produced~w ~n",[Output, Next, 11-N]),
producer(Init + 1, N-1, Next);
true ->
derflow:bind(Output, nil)
Expand All @@ -29,8 +29,8 @@ loop(S1, S2, End) ->
{id, S2Next} = derflow:bind(S2, Value1),
io:format("Buff:Bound for consumer ~w-> ~w ~w~n",[S1,S2,Value1]),
case derflow:read(End) of {nil, _} ->
loop(S1Next, S2Next, End);
{_V,EndNext} ->
loop(S1Next, S2Next, End);
{_V,EndNext} ->
loop(S1Next, S2Next, EndNext)
end.

Expand All @@ -41,20 +41,20 @@ buffer(S1, Size, S2) ->

drop_list(S, Size) ->
if Size == 0 ->
S;
S;
true ->
{_Value,Next}=derflow:read(S),
drop_list(Next, Size-1)
{_Value,Next}=derflow:read(S),
drop_list(Next, Size-1)
end.

consumer(S2,F) ->
case derflow:read(S2) of
{nil, _} ->
io:format("Cons:Reading end~n");
{Value, Next} ->
io:format("Cons:Id ~w Consume ~w, Get ~w, Next~w ~n",[S2,Value, F(Value),Next]),
%timer:sleep(1000),
consumer(Next, F)
{nil, _} ->
io:format("Cons:Reading end~n");
{Value, Next} ->
io:format("Cons:Id ~w Consume ~w, Get ~w, Next~w ~n",[S2,Value, F(Value),Next]),
%timer:sleep(1000),
consumer(Next, F)
end.


18 changes: 9 additions & 9 deletions test/prod_cons.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ test1() ->

producer(Init, N, Output) ->
if (N>0) ->
timer:sleep(1000),
{id, Next} = derflow:bind(Output, Init),
producer(Init + 1, N-1, Next);
timer:sleep(1000),
{id, Next} = derflow:bind(Output, Init),
producer(Init + 1, N-1, Next);
true ->
derflow:bind(Output, nil)
derflow:bind(Output, nil)
end.

consumer(S1, F, S2) ->
case derflow:read(S1) of
{nil, _} ->
derflow:bind(S2, nil);
{Value, Next} ->
{id, NextOutput} = derflow:bind(S2, F, Value),
consumer(Next, F, NextOutput)
{nil, _} ->
derflow:bind(S2, nil);
{Value, Next} ->
{id, NextOutput} = derflow:bind(S2, F, Value),
consumer(Next, F, NextOutput)
end.
18 changes: 9 additions & 9 deletions test/prod_cons_lazy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ test1() ->

producer(Init, N, Output) ->
if (N>0) ->
timer:sleep(1000),
{id, Next} = derflow:byNeed(Output, Init),
producer(Init + 1, N-1, Next);
timer:sleep(1000),
{id, Next} = derflow:byNeed(Output, Init),
producer(Init + 1, N-1, Next);
true ->
derflow:bind(Output, nil)
derflow:bind(Output, nil)
end.

consumer(S1, F, S2) ->
case derflow:read(S1) of
{nil, _} ->
derflow:bind(S2, nil);
{Value, Next} ->
{id, NextOutput} = derflow:byNeed(S2, F, Value),
consumer(Next, F, NextOutput)
{nil, _} ->
derflow:bind(S2, nil);
{Value, Next} ->
{id, NextOutput} = derflow:byNeed(S2, F, Value),
consumer(Next, F, NextOutput)
end.