rotate chunk if chunk quota exeeded

This commit is contained in:
Dmitry Kolesnikov 2013-04-16 21:35:56 +03:00
parent 0c290e12b7
commit 36ea69e896
2 changed files with 13 additions and 31 deletions

View File

@ -237,13 +237,6 @@ evict(#cache{}=S) ->
heap = cache_heap:alloc(cache_heap:free(Cell, S#cache.heap))
}.
drop(#cache{}=S) ->
Cell = cache_heap:last(S#cache.heap),
?DEBUG("cache ~p: free cell ~p~n", [S#cache.name, Cell]),
S#cache{
heap = cache_heap:free(Cell, S#cache.heap)
}.
%%
%%
quota(#cache{}=S) ->
@ -255,23 +248,23 @@ quota(#cache{}=S) ->
maybe_size_quota(#cache{quota_size=undefined}=S) ->
S;
maybe_size_quota(S) ->
case lists:sum(cache_heap:size(S#cache.heap)) of
Size when Size > S#cache.quota_size ->
maybe_size_quota(drop(S));
Quota = S#cache.quota_size div S#cache.n,
[Head | _] = cache_heap:cells(S#cache.heap),
case ets:info(Head, size) of
Val when Val >= Quota ->
evict(S);
_ ->
S#cache{
heap = cache_heap:talloc(S#cache.n, S#cache.heap)
}
S
end.
maybe_memory_quota(#cache{quota_memory=undefined}=S) ->
S;
maybe_memory_quota(S) ->
case lists:sum(cache_heap:memory(S#cache.heap)) of
Size when Size > S#cache.quota_memory ->
maybe_memory_quota(drop(S));
Quota = S#cache.quota_size div S#cache.n,
[Head | _] = cache_heap:cells(S#cache.heap),
case ets:info(Head, memory) of
Val when Val >= Quota ->
evict(S);
_ ->
S#cache{
heap = cache_heap:talloc(S#cache.n, S#cache.heap)
}
S
end.

View File

@ -21,7 +21,7 @@
-module(cache_heap).
-export([
new/1, alloc/1, talloc/2, free/1, free/2,
new/1, alloc/1, free/1, free/2,
cells/1, size/1, memory/1, last/1, first/1
]).
@ -45,17 +45,6 @@ alloc(#heap{}=H) ->
cells = [create() | H#heap.cells]
}.
% tail alloc
talloc(N, #heap{}=H)
when length(H#heap.cells) < N ->
talloc(N,
H#heap{
cells = H#heap.cells ++ [create()]
}
);
talloc(_, #heap{}=H) ->
H.
create() ->
ets:new(undefined, [set, protected]).