1%%% 2%%% Copyright 2011, Boundary 3%%% 4%%% Licensed under the Apache License, Version 2.0 (the "License"); 5%%% you may not use this file except in compliance with the License. 6%%% You may obtain a copy of the License at 7%%% 8%%% http://www.apache.org/licenses/LICENSE-2.0 9%%% 10%%% Unless required by applicable law or agreed to in writing, software 11%%% distributed under the License is distributed on an "AS IS" BASIS, 12%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13%%% See the License for the specific language governing permissions and 14%%% limitations under the License. 15%%% 16 17 18%%%------------------------------------------------------------------- 19%%% File: folsom_sample_slide_sorted.erl 20%%% @author Ramon Lastres <ramon.lastres@erlang-solutions.com> 21%%% @doc 22%%% simple sliding window histogram. 23%%% @end 24%%%----------------------------------------------------------------- 25 26-module(folsom_sample_slide_sorted). 27 28-export([ 29 new/1, 30 update/2, 31 get_values/1 32 ]). 33 34-include("folsom.hrl"). 35 36new(Size) -> 37 #slide_sorted{size = Size}. 38 39update(#slide_sorted{size = Size, reservoir = Reservoir, n = N} = Sample, Value) 40 when N < Size -> 41 ets:insert(Reservoir, {os:timestamp(), Value}), 42 Sample#slide_sorted{n = N + 1}; 43update(#slide_sorted{reservoir = Reservoir, n = N, size = Size} = Sample, Value) 44 when N == Size -> 45 Oldest = ets:first(Reservoir), 46 ets:delete(Reservoir, Oldest), 47 ets:insert(Reservoir, {os:timestamp(), Value}), 48 Sample. 49 50get_values(#slide_sorted{reservoir = Reservoir}) -> 51 {_, Values} = lists:unzip(ets:tab2list(Reservoir)), 52 Values. 53