diff --git a/lfu.app b/lfu.app index 5c75137..2fca70c 100644 --- a/lfu.app +++ b/lfu.app @@ -1,6 +1,6 @@ {application,lfu,[ {description,"Least Frequently Used Algorithm"}, - {vsn,"2.3.0"}, + {vsn,"2.3.1"}, {modules,[ lfu_app,lfu_sup,lfu, lfu_score_sups_sup,lfu_protocol, diff --git a/priv/lfu.rel b/priv/lfu.rel index 2c3f07d..4585538 100644 --- a/priv/lfu.rel +++ b/priv/lfu.rel @@ -9,5 +9,5 @@ {crypto,"4.8"}, {public_key,"1.9"}, {asn1,"5.0.14"}, - {lfu, "2.3.0"}] + {lfu, "2.3.1"}] }. diff --git a/priv/lfu.tar.gz b/priv/lfu.tar.gz index ede4bb7..c44625b 100644 Binary files a/priv/lfu.tar.gz and b/priv/lfu.tar.gz differ diff --git a/src/lfu.erl b/src/lfu.erl index 6598f7b..eeeffe1 100644 --- a/src/lfu.erl +++ b/src/lfu.erl @@ -16,15 +16,15 @@ fetch/0, clean/0, fetch/1, - clean/1 + clean/1, + clean/2 ]). -export([ reset/1 ]). -export([ score/2, - fetch/2, - clean/2 + fetch/2 ]). -export([ init/1, diff --git a/src/lfu_protocol.erl b/src/lfu_protocol.erl index c445779..dbb51f8 100644 --- a/src/lfu_protocol.erl +++ b/src/lfu_protocol.erl @@ -32,9 +32,9 @@ callback_mode() -> state_functions. -common(info,{tcp,S,<<"POINT",_:1/binary,P/binary>>},[S,T]) -> +common(info,{tcp,S,<<"POINT:",P/binary>>},[S,T]) -> T:setopts(S,[{active,once}]), - K = break_binary_string(P), + K = break_binary_string(byte_size(P),P), case lfu:point(K) of ok -> T:send(S,<<"OK">>); @@ -46,7 +46,7 @@ common(info,{tcp,S,<<"POINT",_:1/binary,P/binary>>},[S,T]) -> T:send(S,<<"{","ERROR",":","UNKNOW_ERROR","}">>) end, keep_state_and_data; -common(info,{tcp,S,<<"CHEAT",_:1/binary,P/binary>>},[S,T]) -> +common(info,{tcp,S,<<"CHEAT:",P/binary>>},[S,T]) -> T:setopts(S,[{active,once}]), KVL = lists:filtermap( fun(KV) -> @@ -57,7 +57,7 @@ common(info,{tcp,S,<<"CHEAT",_:1/binary,P/binary>>},[S,T]) -> false end end, - binary:split(break_binary_string(P),<<";">>,[global])), + binary:split(break_binary_string(byte_size(P),P),<<";">>,[global])), case lfu:cheat(KVL) of ok -> T:send(S,<<"OK">>); @@ -69,9 +69,9 @@ common(info,{tcp,S,<<"CHEAT",_:1/binary,P/binary>>},[S,T]) -> T:send(S,<<"{","ERROR",":","UNKNOW_ERROR","}">>) end, keep_state_and_data; -common(info,{tcp,S,<<"COUNT",_:1/binary,P/binary>>},[S,T]) -> +common(info,{tcp,S,<<"COUNT:",P/binary>>},[S,T]) -> T:setopts(S,[{active,once}]), - K = break_binary_string(P), + K = break_binary_string(byte_size(P),P), case lfu:count(K) of "type_error" -> T:send(S,<<"{","ERROR",":","TYPE_ERROR","}">>); @@ -86,7 +86,7 @@ common(info,{tcp,S,<<"COUNT",_:1/binary,P/binary>>},[S,T]) -> T:send(S,<<"{","ERROR",":","UNKNOW_ERROR","}">>) end, keep_state_and_data; -common(info,{tcp,S,<<"STATE",_/binary>>},[S,T]) -> +common(info,{tcp,S,<<"STATE",E/binary>>},[S,T]) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> T:setopts(S,[{active,once}]), case lfu:state() of [O,Q] -> @@ -97,7 +97,7 @@ common(info,{tcp,S,<<"STATE",_/binary>>},[S,T]) -> T:send(S,<<"{","ERROR",":","UNKNOW_ERROR","}">>) end, keep_state_and_data; -common(info,{tcp,S,<<"STORE",_/binary>>},[S,T]) -> +common(info,{tcp,S,<<"STORE",E/binary>>},[S,T]) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> T:setopts(S,[{active,once}]), case lfu:store() of ok -> @@ -106,7 +106,7 @@ common(info,{tcp,S,<<"STORE",_/binary>>},[S,T]) -> T:send(S,<<"{","ERROR",":","UNKNOW_ERROR","}">>) end, keep_state_and_data; -common(info,{tcp,S,<<"SCORE",_/binary>>},[S,T]) -> +common(info,{tcp,S,<<"SCORE",E/binary>>},[S,T]) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> T:setopts(S,[{active,once}]), case lfu:score() of ready -> @@ -115,7 +115,7 @@ common(info,{tcp,S,<<"SCORE",_/binary>>},[S,T]) -> T:send(S,<<"{","ERROR",":","UNKNOW_ERROR","}">>) end, keep_state_and_data; -common(info,{tcp,S,<<"FETCH",_/binary>>},[S,T]) -> +common(info,{tcp,S,<<"FETCH",E/binary>>},[S,T]) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> T:setopts(S,[{active,once}]), case catch ets:info(lfu:fetch()) of I when is_list(I) -> @@ -136,7 +136,7 @@ common(info,{tcp,S,<<"FETCH",_/binary>>},[S,T]) -> T:send(S,<<"{","ERROR",":","UNKNOW_ERROR","}">>) end, keep_state_and_data; -common(info,{tcp,S,<<"CLEAN",":","SYNC",_/binary>>},[S,T]) -> +common(info,{tcp,S,<<"CLEAN",":","SYNC",E/binary>>},[S,T]) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> T:setopts(S,[{active,once}]), case lfu:clean(sync) of {TID,R} when is_reference(TID) andalso is_reference(R) -> @@ -159,7 +159,7 @@ common(info,{tcp,S,<<"CLEAN",":","SYNC",_/binary>>},[S,T]) -> T:send(S,<<"{","ERROR",":","UNKNOW_ERROR","}">>), keep_state_and_data end; -common(info,{tcp,S,<<"CLEAN",":","ASYNC",_/binary>>},[S,T]) -> +common(info,{tcp,S,<<"CLEAN",":","ASYNC",E/binary>>},[S,T]) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> T:setopts(S,[{active,once}]), case lfu:clean(async) of TID when is_reference(TID) -> @@ -185,7 +185,7 @@ common(info,{tcp,S,<<"CLEAN",":",_P/binary>>},[S,T]) -> T:setopts(S,[{active,once}]), T:send(S,<<"{","ERROR",":","EXPIRED_REF","}">>), keep_state_and_data; -common(info,{tcp,S,<<"CLEAN",_/binary>>},[S,T]) -> +common(info,{tcp,S,<<"CLEAN",E/binary>>},[S,T]) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> T:setopts(S,[{active,once}]), case lfu:clean(async) of TID when is_reference(TID) -> @@ -214,9 +214,13 @@ common(info,{tcp,S,_B},[S,T]) -> delete(state_timeout,BR,[S,T,#{ref := BR, tid := _T}]) -> {next_state,common,[S,T]}; +delete(info,{tcp,_S,<<"CLEAN:ASYNC",E/binary>>},_StateData) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> + {keep_state_and_data,[postpone]}; +delete(info,{tcp,_S,<<"CLEAN:SYNC",E/binary>>},_StateData) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> + {keep_state_and_data,[postpone]}; delete(info,{tcp,S,<<"CLEAN",":",P/binary>>},[S,T,#{ref := BR, tid := TID}]) -> T:setopts(S,[{active,once}]), - case break_binary_string(P) =:= BR of + case break_binary_string(byte_size(P),P) =:= BR of true -> case lfu:clean(list_to_ref(binary_to_list(BR)),TID) of ok -> @@ -230,21 +234,21 @@ delete(info,{tcp,S,<<"CLEAN",":",P/binary>>},[S,T,#{ref := BR, tid := TID}]) -> T:send(S,<<"{","ERROR",":","UNKNOW_REF","}">>), keep_state_and_data end; -delete(info,{tcp,_S,<<"POINT",_:1/binary,_P/binary>>},_StateData) -> +delete(info,{tcp,_S,<<"POINT:",_P/binary>>},_StateData) -> {keep_state_and_data,[postpone]}; -delete(info,{tcp,_S,<<"CHEAT",_:1/binary,_P/binary>>},_StateData) -> +delete(info,{tcp,_S,<<"CHEAT:",_P/binary>>},_StateData) -> {keep_state_and_data,[postpone]}; -delete(info,{tcp,_S,<<"COUNT",_:1/binary,_P/binary>>},_StateData) -> +delete(info,{tcp,_S,<<"COUNT:",_P/binary>>},_StateData) -> {keep_state_and_data,[postpone]}; -delete(info,{tcp,_S,<<"STATE",_/binary>>},_StateData) -> +delete(info,{tcp,_S,<<"STATE",E/binary>>},_StateData) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> {keep_state_and_data,[postpone]}; -delete(info,{tcp,_S,<<"STORE",_/binary>>},_StateData) -> +delete(info,{tcp,_S,<<"STORE",E/binary>>},_StateData) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> {keep_state_and_data,[postpone]}; -delete(info,{tcp,_S,<<"SCORE",_/binary>>},_StateData) -> +delete(info,{tcp,_S,<<"SCORE",E/binary>>},_StateData) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> {keep_state_and_data,[postpone]}; -delete(info,{tcp,_S,<<"FETCH",_/binary>>},_StateData) -> +delete(info,{tcp,_S,<<"FETCH",E/binary>>},_StateData) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> {keep_state_and_data,[postpone]}; -delete(info,{tcp,_S,<<"CLEAN",_/binary>>},_StateData) -> +delete(info,{tcp,_S,<<"CLEAN",E/binary>>},_StateData) when E =:= <<>> orelse E =:= <<"\r\n">> orelse E =:= <<"\n">> orelse E =:= <<"\r">> -> {keep_state_and_data,[postpone]}; delete(info,{tcp,S,_B},[S,T,_MD]) -> T:setopts(S,[{active,once}]), @@ -272,5 +276,14 @@ pack_list_to_binary([H|T],B) -> pack_list_to_binary(T,<>) end. -break_binary_string(B) -> - hd(binary:split(B,<<"\r\n">>,[global])). +break_binary_string(S,B) -> + case B of + <> -> + B1; + <> -> + B2; + <> -> + B3; + _ -> + B + end.