Skip to content

Commit

Permalink
fix windows compile gpb failed
Browse files Browse the repository at this point in the history
  • Loading branch information
U-JOHNLIU\jonhl committed May 28, 2021
1 parent 9306ffb commit b6264c0
Show file tree
Hide file tree
Showing 21 changed files with 2,144 additions and 74 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ test-data/
!deps/.placeholder
*.o
*.plt
*.iml
*.iws
erl_crash.dump
!ebin/.placeholder
.concrete/DEV_MODE
Expand Down
31 changes: 31 additions & 0 deletions apps/dgiot/src/storage/dgiot_cache.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------


-module(dgiot_cache).
-author("johnliu").
%% API
-export([set/3, get/1, delete/1]).


set(Key, Value, TTLInSeconds) ->
dgiot_cache_worker:set(Key, Value, TTLInSeconds).

get(Key) ->
dgiot_cache_worker:get(Key).

delete(Key) ->
dgiot_cache_worker:delete(Key).
134 changes: 134 additions & 0 deletions apps/dgiot/src/storage/dgiot_cache_check_worker.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------

-module(dgiot_cache_check_worker).
-author("johnliu").
-behaviour(gen_server).
-include_lib("dgiot/include/logger.hrl").


%% API
-export([lookup/1, delete/1, start_link/0, start_link/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3]).

-record(checkstate, {weight, checkets}).

%%%===================================================================
%%% API
%%%===================================================================

delete(Key) ->
delete(?MODULE, Key).
delete(ETS, Key) ->
ets:delete(ETS, Key).

lookup(Key) ->
lookup(?MODULE, Key).
lookup(CheckEts, Key) ->
case ets:lookup(CheckEts, Key) of
[] ->
notfound;
[Head | _Tail] ->
{_, {Accesses, StartTime, TTL}} = Head,
Now = calendar:local_time(),
CurrentTime = calendar:datetime_to_gregorian_seconds(Now),
NewTTL = TTL + StartTime - CurrentTime,
if
NewTTL < 0 ->
ets:delete(CheckEts, Key),
expired;
true ->
ets:insert(CheckEts, {Key, {Accesses + 1, CurrentTime, NewTTL}}),
ok
end
end.

start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

start_link(Arg) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, Arg, []).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

init(Opts) ->
Weight = proplists:get_value(ets_weight, Opts, 30),
CheckEts = ets:new(?MODULE, [public, named_table, {write_concurrency, true}, {read_concurrency, true}]),
{ok, #checkstate{
weight = Weight, checkets = CheckEts}
}.

handle_call({lookup, Key}, _From, #checkstate{
checkets = CheckEts} = State) ->
Reply = lookup(CheckEts, Key),
{reply, Reply, State};
handle_call({set, Key, TTL}, _From, #checkstate{
checkets = CheckEts} = State) ->
Now = calendar:local_time(),
StartTime = calendar:datetime_to_gregorian_seconds(Now),
ets:insert(CheckEts, {Key, {0, StartTime, TTL}}),
%dgiot_mcache:insert({{Key, cache}, {0, StartTime, TTL}}),
{reply, ok, State};
handle_call(sortkey, _From, #checkstate{
weight = Weight,
checkets = CheckEts} = State) ->
Wgts = ets:foldl(
fun({K, {Accesses, Start, TTL}}, Acc) ->
[{K, Accesses * Weight + Start + TTL} | Acc]
end, [], CheckEts),
CmpFun = fun(A, B) ->
{_, Time1} = A,
{_, Time2} = B,
if
Time1 < Time2 -> true;
true -> false
end
end,
SortedKeys = lists:sort(CmpFun, Wgts),
{reply, SortedKeys, State};
handle_call({shrink, CurrentTime}, _From, #checkstate{checkets = CheckEts} = State) ->
ExpireCond = [{{'$1', {'_', '$2', '$3'}}, [{'=<', {'+', '$2', '$3'}, {const, CurrentTime}}], ['$1']}],
ExpiredKeys = ets:select(CheckEts, ExpireCond),
lists:foreach(fun(K) ->
ets:delete(CheckEts, K)
end, ExpiredKeys),
{reply, ExpiredKeys, State};
handle_call(_Msg, _From, State) ->
{reply, ok, State}.

handle_cast({delete, Key}, #checkstate{checkets = CheckEts} = State) ->
ets:delete(CheckEts, Key),
{noreply, State};
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast(_Msg, State) ->
{noreply, State}.

handle_info(_Msg, State) ->
{noreply, State}.

terminate(_Reason, #checkstate{checkets = CheckEts}) ->
?LOG(info,"check terminates", []),
ets:delete(CheckEts),
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.
48 changes: 48 additions & 0 deletions apps/dgiot/src/storage/dgiot_cache_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------


-module(dgiot_cache_sup).
-author("johnliu").
-behaviour(supervisor).

%% API
-export([start_link/0, start_link/1]).

%% Supervisor callbacks
-export([init/1]).

-define(SERVER, ?MODULE).

start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).

start_link(Opts) ->
supervisor:start_link({local, ?SERVER}, ?MODULE, Opts).

init(Opts) ->
MaxSize = proplists:get_value(ets_maxsize, Opts, 32 * 1024 * 1024),
Threshold = proplists:get_value(ets_threshold, Opts, 0.85),
Weight = proplists:get_value(ets_weight, Opts, 30),
ValOpts = [{ets_maxsize, MaxSize}, {ets_threshold, Threshold}, {checkpid, dgiot_cache_check_worker}],
ChkOpt = [{ets_weight, Weight}],
ValServ = {dgiot_cache_worker, {dgiot_cache_worker, start_link, [ValOpts]},
permanent, 2000, worker, [dgiot_cache_worker]},
ChkServ = {dgiot_cache_check_worker, {dgiot_cache_check_worker, start_link, [ChkOpt]},
permanent, 2000, worker, [dgiot_cache_check_worker]},
Children = [ValServ, ChkServ],
RestartStrategy = {one_for_one, 0, 1},
{ok, {RestartStrategy, Children}}.
Loading

0 comments on commit b6264c0

Please sign in to comment.