From 36ea69e896c68d7581b006934619d39db571dc3d Mon Sep 17 00:00:00 2001 From: Dmitry Kolesnikov Date: Tue, 16 Apr 2013 21:35:56 +0300 Subject: [PATCH] rotate chunk if chunk quota exeeded --- src/cache_bucket.erl | 31 ++++++++++++------------------- src/cache_heap.erl | 13 +------------ 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/cache_bucket.erl b/src/cache_bucket.erl index cd3f099..0d8c049 100644 --- a/src/cache_bucket.erl +++ b/src/cache_bucket.erl @@ -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. diff --git a/src/cache_heap.erl b/src/cache_heap.erl index 7b32198..ac78756 100644 --- a/src/cache_heap.erl +++ b/src/cache_heap.erl @@ -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]).