Skip to content

Commit c0009a3

Browse files
authored
Merge pull request #54 from Shpaky/v2.2.2
fix - 'resetting/2' function fixed and some few fixes
2 parents b0f725b + 6d4d2fd commit c0009a3

File tree

6 files changed

+104
-104
lines changed

6 files changed

+104
-104
lines changed

lfu.app

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{application,lfu,[
22
{description,"Least Frequently Used Algorithm"},
3-
{vsn,"2.2.1"},
3+
{vsn,"2.2.2"},
44
{modules,[
55
lfu_app,lfu_sup,lfu,
66
lfu_score_sups_sup,lfu_protocol,

priv/lfu.rel

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
{crypto,"4.8"},
1010
{public_key,"1.9"},
1111
{asn1,"5.0.14"},
12-
{lfu, "2.2.1"}]
12+
{lfu, "2.2.2"}]
1313
}.

priv/lfu.tar.gz

-83 Bytes
Binary file not shown.

src/lfu.erl

+85-81
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ common(cast,{point,K},[O,Q]) ->
202202
{keep_state,[O,Q]}
203203
end;
204204
common(cast,{cheat,KVL},[O,Q]) ->
205-
NQ = length(lists:filter(
206-
fun({K,V}) when V =< ?MAX_ORDER ->
205+
NQ = lists:foldl(
206+
fun({K,V},NQ) when V =< ?MAX_ORDER ->
207207
case get(K) of
208208
undefined -> skip;
209209
C ->
@@ -276,44 +276,40 @@ common(cast,{cheat,KVL},[O,Q]) ->
276276
end;
277277
true -> skip
278278
end,
279-
case get(clean) of
280-
undefined -> put(clean,0);
281-
_ -> skip
282-
end,
283279
case get(K) of
284280
undefined ->
285281
if
286282
V > 0 ->
287283
put(K,V),
288284
ets:insert(?ETS_KEYS_STORE_TABLE_NAME,{K,V}),
289285
if
290-
V > ?SCORE_OFFSET -> true;
291-
true -> false
286+
V > ?SCORE_OFFSET -> NQ + 1;
287+
true -> NQ
292288
end;
293-
true -> false
289+
true -> NQ
294290
end;
295291
OV ->
296292
if
297293
V > 0 ->
298294
put(K,V),
299295
ets:insert(?ETS_KEYS_STORE_TABLE_NAME,{K,V}),
300296
if
301-
OV =< ?SCORE_OFFSET andalso V > ?SCORE_OFFSET -> true;
302-
true -> false
297+
OV =< ?SCORE_OFFSET andalso V > ?SCORE_OFFSET -> NQ + 1;
298+
true -> NQ
303299
end;
304300
true ->
305301
if
306302
OV > ?SCORE_OFFSET ->
307-
erase(K),ets:delete(?ETS_KEYS_STORE_TABLE_NAME,K),put(clean,get(clean)+1),false;
303+
erase(K),ets:delete(?ETS_KEYS_STORE_TABLE_NAME,K),NQ - 1;
308304
true ->
309-
erase(K),ets:delete(?ETS_KEYS_STORE_TABLE_NAME,K),false
305+
erase(K),ets:delete(?ETS_KEYS_STORE_TABLE_NAME,K),NQ
310306
end
311307
end
312308
end;
313-
(_) ->
314-
false
309+
(_,NQ) ->
310+
NQ
315311
end,
316-
KVL))+Q-erase(clean),
312+
Q,KVL),
317313
{keep_state,[O,NQ]};
318314
common({call,From},{count,K},[O,Q]) ->
319315
{keep_state,[O,Q],[{reply,From,get(K)}]};
@@ -744,47 +740,15 @@ scoring(L,U,R) ->
744740
end.
745741

746742
resetting({_,KL},Q) ->
747-
put(reset,0),
748-
lists:foreach(
749-
fun(K) ->
750-
ets:delete(?ETS_KEYS_STORE_TABLE_NAME,K),
751-
C = erase(K),
752-
if
753-
(C-1) div ?MAX_LIMIT == 0 ->
754-
N = list_to_atom("o0" ++ integer_to_list((C-1) div ?MIN_LIMIT)),
755-
case whereis(N) of
756-
undefined ->
757-
lfu_exact_score_sup:start([(C-1) div ?MIN_LIMIT,0]),
758-
lfu_exact_score:reset(N,K);
759-
_ ->
760-
lfu_exact_score:reset(N,K)
761-
end;
762-
true ->
763-
N = list_to_atom("o" ++ integer_to_list((C-1) div ?MAX_LIMIT)),
764-
case whereis(N) of
765-
undefined ->
766-
lfu_quick_score_sup:start([(C-1) div ?MAX_LIMIT,0]),
767-
lfu_quick_score:reset(N,K);
768-
true ->
769-
lfu_quick_score:reset(N,K)
770-
end
771-
end,
772-
put(reset,get(reset)+1)
773-
end,
774-
KL),
775-
Q - erase(reset);
776-
resetting(T,Q) ->
777-
put(reset,0),
778-
ets:info(T) =/= undefined andalso
779-
ets:foldl(
780-
fun({_,KL},[]) ->
781-
lists:foreach(
782-
fun(K) ->
743+
lists:foldl(
744+
fun(K,NQ) ->
745+
case erase(K) of
746+
undefined -> NQ;
747+
C ->
783748
ets:delete(?ETS_KEYS_STORE_TABLE_NAME,K),
784-
C = erase(K),
785749
if
786750
(C-1) div ?MAX_LIMIT == 0 ->
787-
N = list_to_atom("o0" ++ integer_to_list((C-1) div ?MIN_LIMIT)),
751+
N = list_to_atom("o0" ++ integer_to_list((C-1) div ?MIN_LIMIT)),
788752
case whereis(N) of
789753
undefined ->
790754
lfu_exact_score_sup:start([(C-1) div ?MIN_LIMIT,0]),
@@ -798,38 +762,78 @@ resetting(T,Q) ->
798762
undefined ->
799763
lfu_quick_score_sup:start([(C-1) div ?MAX_LIMIT,0]),
800764
lfu_quick_score:reset(N,K);
801-
true ->
765+
_ ->
802766
lfu_quick_score:reset(N,K)
803767
end
804768
end,
805-
put(reset,get(reset)+1)
806-
end,
807-
KL),[]
769+
NQ - 1
770+
end
808771
end,
809-
[],T),
810-
Q - erase(reset).
772+
Q,KL);
773+
resetting(T,Q) ->
774+
case ets:info(T) of
775+
undefined -> Q;
776+
_ ->
777+
ets:foldl(
778+
fun({_,KL},NQ) ->
779+
lists:foldl(
780+
fun(K,NQ) ->
781+
case erase(K) of
782+
undefined -> NQ;
783+
C ->
784+
ets:delete(?ETS_KEYS_STORE_TABLE_NAME,K),
785+
if
786+
(C-1) div ?MAX_LIMIT == 0 ->
787+
N = list_to_atom("o0" ++ integer_to_list((C-1) div ?MIN_LIMIT)),
788+
case whereis(N) of
789+
undefined ->
790+
lfu_exact_score_sup:start([(C-1) div ?MIN_LIMIT,0]),
791+
lfu_exact_score:reset(N,K);
792+
_ ->
793+
lfu_exact_score:reset(N,K)
794+
end;
795+
true ->
796+
N = list_to_atom("o" ++ integer_to_list((C-1) div ?MAX_LIMIT)),
797+
case whereis(N) of
798+
undefined ->
799+
lfu_quick_score_sup:start([(C-1) div ?MAX_LIMIT,0]),
800+
lfu_quick_score:reset(N,K);
801+
_ ->
802+
lfu_quick_score:reset(N,K)
803+
end
804+
end,
805+
NQ - 1
806+
end
807+
end,
808+
NQ,KL)
809+
end,
810+
Q,T)
811+
end.
811812

812813
restorage(T) ->
813-
put(quantity,0),
814-
ets:whereis(T) =/= undefined andalso
815-
ets:foldl(
816-
fun({K,V},[]) ->
817-
if
818-
V =< ?MAX_ORDER ->
814+
case ets:whereis(T) of
815+
undefined -> 0;
816+
_ ->
817+
ets:foldl(
818+
fun({K,V},Q) ->
819819
if
820-
V div ?MAX_LIMIT =< 1 ->
821-
N = list_to_atom("o0" ++ integer_to_list((V -1)div ?MIN_LIMIT)),
822-
whereis(N) =:= undefined andalso lfu_exact_score_sup:start([(V-1) div ?MIN_LIMIT,0]);
820+
V =< ?MAX_ORDER ->
821+
if
822+
V div ?MAX_LIMIT =< 1 ->
823+
N = list_to_atom("o0" ++ integer_to_list((V -1)div ?MIN_LIMIT)),
824+
whereis(N) =:= undefined andalso lfu_exact_score_sup:start([(V-1) div ?MIN_LIMIT,0]);
825+
true ->
826+
N = list_to_atom("o" ++ integer_to_list((V-1) div ?MAX_LIMIT)),
827+
whereis(N) =:= undefined andalso lfu_quick_score_sup:start([(V-1) div ?MAX_LIMIT,0])
828+
end,
829+
put(K,V),
830+
if
831+
V > ?SCORE_OFFSET -> Q + 1;
832+
true -> Q
833+
end;
823834
true ->
824-
N = list_to_atom("o" ++ integer_to_list((V-1) div ?MAX_LIMIT)),
825-
whereis(N) =:= undefined andalso lfu_quick_score_sup:start([(V-1) div ?MAX_LIMIT,0])
826-
end,
827-
put(K,V),
828-
V > ?SCORE_OFFSET andalso put(quantity,get(quantity)+1),
829-
[];
830-
true ->
831-
[]
832-
end
833-
end,
834-
[],T),
835-
erase(quantity).
835+
Q
836+
end
837+
end,
838+
0,T)
839+
end.

src/lfu_exact_score.erl

+12-13
Original file line numberDiff line numberDiff line change
@@ -162,33 +162,32 @@ insert(L,U,T) ->
162162
).
163163

164164
restorage(T,L,U,O) ->
165-
put(quantity,0),
166165
if
167166
O == 0 ->
168167
ets:foldl(
169-
fun({K,V},[]) ->
168+
fun({K,V},Q) ->
170169
if
171170
V >= L andalso V =< U ->
172171
put(K,V),
173-
V > ?SCORE_OFFSET andalso put(quantity,get(quantity)+1),
174-
[];
172+
if
173+
V > ?SCORE_OFFSET -> Q + 1;
174+
true -> Q
175+
end;
175176
true ->
176-
[]
177+
Q
177178
end
178179
end,
179-
[],T);
180+
0,T);
180181
true ->
181182
ets:foldl(
182-
fun({K,V},[]) ->
183+
fun({K,V},Q) ->
183184
if
184185
V >= L andalso V =< U ->
185186
put(K,V),
186-
put(quantity,get(quantity)+1),
187-
[];
187+
Q + 1;
188188
true ->
189-
[]
189+
Q
190190
end
191191
end,
192-
[],T)
193-
end,
194-
erase(quantity).
192+
0,T)
193+
end.

src/lfu_quick_score.erl

+5-8
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ point_handler(K,Q) ->
9494
undefined ->
9595
put(K,1),
9696
Q+1;
97-
C ->
97+
_ ->
9898
Q
9999
end.
100100
cheat_handler(K,V,Q) ->
@@ -120,17 +120,14 @@ insert(I,T) ->
120120
KL =/= [] andalso ets:insert(T,{I*?MAX_LIMIT,KL}).
121121

122122
restorage(T,L,U) ->
123-
put(quantity,0),
124123
ets:foldl(
125-
fun({K,V},[]) ->
124+
fun({K,V},Q) ->
126125
if
127126
V >= L andalso V =< U ->
128127
put(K,1),
129-
put(quantity,get(quantity)+1),
130-
[];
128+
Q + 1;
131129
true ->
132-
[]
130+
Q
133131
end
134132
end,
135-
[],T),
136-
erase(quantity).
133+
0,T).

0 commit comments

Comments
 (0)