1%% ------------------------------------------------------------------- 2%% 3%% Copyright (c) 2011 Basho Technologies, Inc. 4%% 5%% This file is provided to you under the Apache License, 6%% Version 2.0 (the "License"); you may not use this file 7%% except in compliance with the License. You may obtain 8%% a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, 13%% software distributed under the License is distributed on an 14%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15%% KIND, either express or implied. See the License for the 16%% specific language governing permissions and limitations 17%% under the License. 18%% 19%% ------------------------------------------------------------------- 20%%%------------------------------------------------------------------- 21%%% File: folsom_sample_slide_server.erl 22%%% @author Russell Brown <russelldb@basho.com> 23%%% @doc 24%%% Serialization point for folsom_sample_slide. Handles 25%%% pruning of older smaples. One started per histogram. 26%%% See folsom.hrl, folsom_sample_slide, folsom_sample_slide_sup 27%%% @end 28%%%----------------------------------------------------------------- 29-module(folsom_sample_slide_server). 30 31-behaviour(gen_server). 32 33%% API 34-export([start_link/3, stop/1, resize/2]). 35 36-record(state, {sample_mod, reservoir, window}). 37 38%% gen_server callbacks 39-export([init/1, handle_call/3, handle_cast/2, handle_info/2, 40 terminate/2, code_change/3]). 41 42start_link(SampleMod, Reservoir, Window) -> 43 gen_server:start_link(?MODULE, [SampleMod, Reservoir, Window], []). 44 45stop(Pid) -> 46 gen_server:cast(Pid, stop). 47 48init([SampleMod, Reservoir, Window]) -> 49 {ok, #state{sample_mod = SampleMod, reservoir = Reservoir, window = Window}, timeout(Window)}. 50 51resize(Pid, NewWindow) -> 52 gen_server:call(Pid, {resize, NewWindow}). 53 54handle_call({resize, NewWindow}, _From, State) -> 55 NewState = State#state{window=NewWindow}, 56 Reply = ok, 57 {reply, Reply, NewState, timeout(NewWindow)}; 58handle_call(_Request, _From, State) -> 59 Reply = ok, 60 {reply, Reply, State}. 61 62handle_cast(stop, State) -> 63 {stop, normal, State}; 64handle_cast(_Msg, State) -> 65 {noreply, State}. 66 67handle_info(timeout, State=#state{sample_mod = SampleMod, reservoir = Reservoir, window = Window}) -> 68 SampleMod:trim(Reservoir, Window), 69 {noreply, State, timeout(Window)}; 70handle_info(_Info, State) -> 71 {noreply, State}. 72 73terminate(_Reason, _State) -> 74 ok. 75 76code_change(_OldVsn, State, _Extra) -> 77 {ok, State}. 78 79timeout(Window) -> 80 timer:seconds(Window) div 2. 81