anchor.diff:  
       CheckInterval = get_check_interval(ns_config:get()),
   
  -    Self ! compact,
  -    {ok, idle, #state{buckets_to_compact=[],
  -                      compactor_pid=undefined,
  -                      compactor_config=undefined,
  -                      scheduler = compaction_scheduler:init(CheckInterval, compact),
  -                      running_forced_compactions=dict:new(),
  -                      forced_compaction_pids=dict:new()}}.
  -
  -handle_event(Event, StateName, State) ->
  -    ?log_warning("Got unexpected event ~p (when in ~p):~n~p",
  -                 [Event, StateName, State]),
  -    {next_state, StateName, State}.
  -
  -handle_sync_event({force_compact_bucket, Bucket}, _From, StateName, State) ->
  +    {ok, #state{kv_compaction =
  +                    init_scheduled_compaction(CheckInterval, compact_kv,
  +                                              fun spawn_scheduled_kv_compactor/2),
  +                views_compaction =
  +                    init_scheduled_compaction(CheckInterval, compact_views,
  +                                              fun spawn_scheduled_views_compactor/2),
  +                running_forced_compactions=dict:new(),
  +                forced_compaction_pids=dict:new()}}.
  +
  +handle_cast(Message, State) ->
  +    ?log_warning("Got unexpected cast ~p:~n~p", [Message, State]),
  +    {noreply, State}.
  +
  +handle_call({force_compact_bucket, Bucket}, _From, State) ->
       Compaction = #forced_compaction{type=bucket, name=Bucket},
   
       NewState =
  
positive.code:                 State;
              false ->
                  Configs = compaction_config(Bucket),
                  Pid = spawn_bucket_compactor(Bucket, Configs, true),
                  register_forced_compaction(Pid, Compaction, State)
          end,
      {reply, ok, StateName, NewState};
  handle_sync_event({force_purge_compact_bucket, Bucket}, _From, StateName, State) ->
      Compaction = #forced_compaction{type=bucket_purge, name=Bucket},
  
      NewState =
  
negative.code:               end
        end).
  
  init_scheduled_compaction(CheckInterval, Message, Fun) ->
      self() ! Message,
      #compaction_state{buckets_to_compact=[],
                        compactor_pid=undefined,
                        scheduler=compaction_scheduler:init(CheckInterval, Message),
                        compactor_fun = Fun}.
  
  process_scheduler_message(#compaction_state{buckets_to_compact=Buckets0,
                                              scheduler=Scheduler,
                                              compactor_pid=undefined} = State) ->
      Buckets =
          case Buckets0 of
              [] ->
                  lists:map(fun list_to_binary/1,
                            ns_bucket:node_bucket_names_of_type(node(), membase));
              _ ->
                  Buckets0
          end,
  
      case Buckets of
          [] ->
              ?log_debug("No buckets to compact. Rescheduling compaction."),
              State#compaction_state{scheduler=compaction_scheduler:schedule_next(Scheduler)};
          _ ->
              ?log_debug("Starting compaction for the following buckets: ~n~p",
                         [Buckets]),
              NewState1 = State#compaction_state{buckets_to_compact=Buckets,
                                                 scheduler=
                                                     compaction_scheduler:start_now(Scheduler)},
              compact_next_bucket(NewState1)
      end.
  
  process_compactors_exit(Reason, #compaction_state{compactor_pid=Compactor,
                                                    buckets_to_compact=Buckets,
                                                    scheduler=Scheduler} = State) ->
      true = Buckets =/= [],
  
      case Reason of
          normal ->
              ok;
          shutdown ->
              ?log_debug("Compactor ~p exited with reason ~p", [Compactor, Reason]);
          _ ->
              ?log_error("Compactor ~p exited unexpectedly: ~p. "
                         "Moving to the next bucket.", [Compactor, Reason])
      end,
  
      NewBuckets =
          case Reason of
              shutdown ->
                  %% this can happen either on config change or if compaction
                  %% has been stopped due to end of allowed time period; in the
                  %% latter case there's no need to compact bucket again; but it
                  %% won't hurt: compaction shall be terminated rightaway with
                  %% reason normal
                  Buckets;
              _ ->
                  tl(Buckets)
          end,
  
      NewState = State#compaction_state{compactor_pid=undefined,
                                        compactor_config=undefined,
                                        buckets_to_compact=NewBuckets},
      case NewBuckets of
          [] ->
              ?log_debug("Finished compaction iteration."),
              NewState#compaction_state{scheduler =
                                            compaction_scheduler:schedule_next(Scheduler)};
          _ ->
              compact_next_bucket(NewState)
      end.
  
  process_config_change(Config, #compaction_state{scheduler = Scheduler} = State) ->
      CheckInterval = get_check_interval(Config),
      NewScheduler = compaction_scheduler:set_interval(CheckInterval, Scheduler),
  
      maybe_restart_compactor(Config, State),
      State#compaction_state{scheduler = NewScheduler}.
  
  maybe_restart_compactor(_NewConfig, #compaction_state{compactor_pid=undefined}) ->
      ok;
  maybe_restart_compactor(_NewConfig, #compaction_state{buckets_to_compact=[]}) ->
      ok;
  maybe_restart_compactor(NewConfig, #compaction_state{compactor_pid=Compactor,
                                                       buckets_to_compact=[CompactedBucket|_],
                                                       compactor_config=Config}) ->
      {MaybeNewConfig, _} = compaction_config(NewConfig, CompactedBucket),
      case MaybeNewConfig =:= Config of
          true ->
              ok;
          false ->
              ?log_info("Restarting compactor ~p since "
                        "autocompaction config has changed", [Compactor]),
              exit(Compactor, shutdown)
      end.
  
  -spec compact_next_bucket(#compaction_state{}) -> #compaction_state{}.
  compact_next_bucket(#compaction_state{buckets_to_compact=Buckets,
                                        compactor_pid=undefined,
                                        compactor_fun=Fun} = State) ->
      true = [] =/= Buckets,
  
      NextBucket = hd(Buckets),
  
      {InitialConfig, _ConfigProps} = Configs = compaction_config(NextBucket),
  
      Compactor = Fun(NextBucket, Configs),
      State#compaction_state{compactor_pid = Compactor,
                             compactor_config = InitialConfig}.
  
  db_name(BucketName, SubName) when is_list(SubName) ->
      db_name(BucketName, list_to_binary(SubName));
  