add spec
This commit is contained in:
parent
3adb2d33ec
commit
1ad849c058
@ -12,7 +12,7 @@
|
|||||||
-include_lib("stdlib/include/qlc.hrl").
|
-include_lib("stdlib/include/qlc.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([get_host/1, get_hosts/2, add_host/1, change_status/2, delete/1, table_size/0, filter_hosts/3]).
|
-export([get_host/1, get_hosts/2, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3]).
|
||||||
|
|
||||||
get_host(HostId) when is_binary(HostId) ->
|
get_host(HostId) when is_binary(HostId) ->
|
||||||
case mnesia:dirty_read(host, HostId) of
|
case mnesia:dirty_read(host, HostId) of
|
||||||
@ -23,6 +23,9 @@ get_host(HostId) when is_binary(HostId) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
%% 获取app信息
|
%% 获取app信息
|
||||||
|
-spec get_hosts(Start :: integer(), Limit :: integer()) ->
|
||||||
|
{ok, Items :: list(), Stat :: maps()} |
|
||||||
|
{error, Reason :: any()}.
|
||||||
get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
|
get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
|
||||||
Fun = fun() ->
|
Fun = fun() ->
|
||||||
Q = qlc:q([E || E <- mnesia:table(host)]),
|
Q = qlc:q([E || E <- mnesia:table(host)]),
|
||||||
@ -46,7 +49,10 @@ get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, L
|
|||||||
Error
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
filter_hosts(Pred, Start, Limit) when is_function(Pred, 1), is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
|
-spec find_hosts(Pred :: fun((#host{}) -> boolean()), Start :: integer(), Limit :: integer()) ->
|
||||||
|
{ok, Items :: [#host{}], Num :: integer()} |
|
||||||
|
{error, Reason :: any()}.
|
||||||
|
find_hosts(Pred, Start, Limit) when is_function(Pred, 1), is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
|
||||||
Fun = fun() ->
|
Fun = fun() ->
|
||||||
Q = qlc:q([E || E <- mnesia:table(host)]),
|
Q = qlc:q([E || E <- mnesia:table(host)]),
|
||||||
qlc:fold(fun(Host, Acc) ->
|
qlc:fold(fun(Host, Acc) ->
|
||||||
@ -60,11 +66,12 @@ filter_hosts(Pred, Start, Limit) when is_function(Pred, 1), is_integer(Limit), i
|
|||||||
case mnesia:transaction(Fun) of
|
case mnesia:transaction(Fun) of
|
||||||
{atomic, Items} when is_list(Items) ->
|
{atomic, Items} when is_list(Items) ->
|
||||||
Items1 = lists:reverse(Items),
|
Items1 = lists:reverse(Items),
|
||||||
{ok, lists:sublist(Items1, Start + 1, Limit)};
|
{ok, lists:sublist(Items1, Start + 1, Limit), length(Items1)};
|
||||||
{aborted, Error} ->
|
{aborted, Error} ->
|
||||||
Error
|
{error, Error}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
-spec add_host(Host :: #host{}) -> ok | {error, Reason :: any()}.
|
||||||
add_host(Host = #host{}) ->
|
add_host(Host = #host{}) ->
|
||||||
case mnesia:transaction(fun() -> mnesia:write(host, Host, write) end) of
|
case mnesia:transaction(fun() -> mnesia:write(host, Host, write) end) of
|
||||||
{atomic, _} ->
|
{atomic, _} ->
|
||||||
@ -73,6 +80,7 @@ add_host(Host = #host{}) ->
|
|||||||
{error, Error}
|
{error, Error}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
-spec change_status(HostId :: binary(), Status :: integer()) -> ok | {error, Reason :: any()}.
|
||||||
change_status(HostId, Status) when is_binary(HostId), is_integer(Status) ->
|
change_status(HostId, Status) when is_binary(HostId), is_integer(Status) ->
|
||||||
Fun = fun() ->
|
Fun = fun() ->
|
||||||
case mnesia:read(host, HostId) of
|
case mnesia:read(host, HostId) of
|
||||||
@ -90,6 +98,7 @@ change_status(HostId, Status) when is_binary(HostId), is_integer(Status) ->
|
|||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
-spec delete(HostId :: binary()) -> ok | {error, Reason :: any()}.
|
||||||
delete(HostId) when is_binary(HostId) ->
|
delete(HostId) when is_binary(HostId) ->
|
||||||
case mnesia:transaction(fun() -> mnesia:delete(host, HostId, write) end) of
|
case mnesia:transaction(fun() -> mnesia:delete(host, HostId, write) end) of
|
||||||
{atomic, ok} ->
|
{atomic, ok} ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user