1 // Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 
6 package org.rocksdb;
7 
8 /**
9  * Similar to {@link LRUCache}, but based on the CLOCK algorithm with
10  * better concurrent performance in some cases
11  */
12 public class ClockCache extends Cache {
13 
14   /**
15    * Create a new cache with a fixed size capacity.
16    *
17    * @param capacity The fixed size capacity of the cache
18    */
ClockCache(final long capacity)19   public ClockCache(final long capacity) {
20     super(newClockCache(capacity, -1, false));
21   }
22 
23   /**
24    * Create a new cache with a fixed size capacity. The cache is sharded
25    * to 2^numShardBits shards, by hash of the key. The total capacity
26    * is divided and evenly assigned to each shard.
27    * numShardBits = -1 means it is automatically determined: every shard
28    * will be at least 512KB and number of shard bits will not exceed 6.
29    *
30    * @param capacity The fixed size capacity of the cache
31    * @param numShardBits The cache is sharded to 2^numShardBits shards,
32    *     by hash of the key
33    */
ClockCache(final long capacity, final int numShardBits)34   public ClockCache(final long capacity, final int numShardBits) {
35     super(newClockCache(capacity, numShardBits, false));
36   }
37 
38   /**
39    * Create a new cache with a fixed size capacity. The cache is sharded
40    * to 2^numShardBits shards, by hash of the key. The total capacity
41    * is divided and evenly assigned to each shard. If strictCapacityLimit
42    * is set, insert to the cache will fail when cache is full.
43    * numShardBits = -1 means it is automatically determined: every shard
44    * will be at least 512KB and number of shard bits will not exceed 6.
45    *
46    * @param capacity The fixed size capacity of the cache
47    * @param numShardBits The cache is sharded to 2^numShardBits shards,
48    *     by hash of the key
49    * @param strictCapacityLimit insert to the cache will fail when cache is full
50    */
ClockCache(final long capacity, final int numShardBits, final boolean strictCapacityLimit)51   public ClockCache(final long capacity, final int numShardBits,
52       final boolean strictCapacityLimit) {
53     super(newClockCache(capacity, numShardBits, strictCapacityLimit));
54   }
55 
newClockCache(final long capacity, final int numShardBits, final boolean strictCapacityLimit)56   private native static long newClockCache(final long capacity,
57       final int numShardBits, final boolean strictCapacityLimit);
disposeInternal(final long handle)58   @Override protected final native void disposeInternal(final long handle);
59 }
60