/*
 * Copyright 2015 Gilga Einziger. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.benmanes.caffeine.cache.simulator.policy.sketch.tinycache;

import java.util.Set;

import com.github.benmanes.caffeine.cache.simulator.BasicSettings;
import com.github.benmanes.caffeine.cache.simulator.admission.tinycache.TinyCache;
import com.github.benmanes.caffeine.cache.simulator.admission.tinycache.TinyCacheWithGhostCache;
import com.github.benmanes.caffeine.cache.simulator.policy.Policy;
import com.github.benmanes.caffeine.cache.simulator.policy.PolicyStats;
import com.google.common.collect.ImmutableSet;
import com.typesafe.config.Config;

/**
 * @author gilga1983@gmail.com (Gil Einziger)
 */
public final class WindowTinyCachePolicy implements Policy {
  private final TinyCache window;
  private final PolicyStats policyStats;
  private final TinyCacheWithGhostCache tinyCache;

  public WindowTinyCachePolicy(Config config) {
    BasicSettings settings = new BasicSettings(config);
    this.policyStats = new PolicyStats("sketch.WindowTinyCache");
    int maxSize = settings.maximumSize();
    window = new TinyCache(1, 64, 0);
    maxSize -= 64;
    tinyCache = new TinyCacheWithGhostCache((int) Math.ceil(maxSize / 64.0),
        64, settings.randomSeed());
  }

  /** Returns all variations of this policy based on the configuration parameters. */
  public static Set<Policy> policies(Config config) {
    return ImmutableSet.of(new WindowTinyCachePolicy(config));
  }

  @Override
  public void record(long key) {
    if (tinyCache.contains(key) || window.contains(key)) {
      tinyCache.recordItem(key);
      policyStats.recordHit();
    } else {
      boolean evicted = tinyCache.addItem(key);
      if (!evicted) {
        evicted = window.addItem(key);
      }
      tinyCache.recordItem(key);
      policyStats.recordMiss();
      if (evicted) {
        policyStats.recordEviction();
      }
    }
  }

  @Override
  public PolicyStats stats() {
    return policyStats;
  }
}

@@ -1634,24 +1634,106 @@
 imumSize();%0A
+    if (maxSize %3C= 64) %7B%0A      window = null;%0A    %7D else %7B%0A      maxSize -= 64;%0A  
     window =
@@ -1758,38 +1758,25 @@
 64, 0);%0A    
-maxSize -= 64;
+%7D
 %0A    tinyCac
@@ -2169,16 +2169,37 @@
 (key) %7C%7C
+ ((window != null) &&
  window.
@@ -2212,16 +2212,17 @@
 ns(key))
+)
  %7B%0A     
@@ -2355,24 +2355,44 @@
 if (!evicted
+ && (window != null)
 ) %7B%0A        

Support small caches in WindowTinyCachePolicy