View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.io.hfile;
20  
21  import java.util.Iterator;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  
25  /**
26   * Block cache interface. Anything that implements the {@link Cacheable}
27   * interface can be put in the cache.
28   */
29  @InterfaceAudience.Private
30  public interface BlockCache extends Iterable<CachedBlock> {
31    /**
32     * Add block to cache.
33     * @param cacheKey The block's cache key.
34     * @param buf The block contents wrapped in a ByteBuffer.
35     * @param inMemory Whether block should be treated as in-memory
36     */
37    void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory);
38  
39    /**
40     * Add block to cache (defaults to not in-memory).
41     * @param cacheKey The block's cache key.
42     * @param buf The object to cache.
43     */
44    void cacheBlock(BlockCacheKey cacheKey, Cacheable buf);
45  
46    /**
47     * Fetch block from cache.
48     * @param cacheKey Block to fetch.
49     * @param caching Whether this request has caching enabled (used for stats)
50     * @param repeat Whether this is a repeat lookup for the same block
51     *        (used to avoid double counting cache misses when doing double-check locking)
52     * @param updateCacheMetrics Whether to update cache metrics or not
53     * @return Block or null if block is not in 2 cache.
54     */
55    Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat,
56      boolean updateCacheMetrics);
57  
58    /**
59     * Evict block from cache.
60     * @param cacheKey Block to evict
61     * @return true if block existed and was evicted, false if not
62     */
63    boolean evictBlock(BlockCacheKey cacheKey);
64  
65    /**
66     * Evicts all blocks for the given HFile.
67     *
68     * @return the number of blocks evicted
69     */
70    int evictBlocksByHfileName(String hfileName);
71  
72    /**
73     * Get the statistics for this block cache.
74     * @return Stats
75     */
76    CacheStats getStats();
77  
78    /**
79     * Shutdown the cache.
80     */
81    void shutdown();
82  
83    /**
84     * Returns the total size of the block cache, in bytes.
85     * @return size of cache, in bytes
86     */
87    long size();
88  
89    /**
90     * Returns the free size of the block cache, in bytes.
91     * @return free space in cache, in bytes
92     */
93    long getFreeSize();
94  
95    /**
96     * Returns the occupied size of the block cache, in bytes.
97     * @return occupied space in cache, in bytes
98     */
99    long getCurrentSize();
100 
101   /**
102    * Returns the number of blocks currently cached in the block cache.
103    * @return number of blocks in the cache
104    */
105   long getBlockCount();
106 
107   /**
108    * @return Iterator over the blocks in the cache.
109    */
110   Iterator<CachedBlock> iterator();
111 
112   /**
113    * @return The list of sub blockcaches that make up this one; returns null if no sub caches.
114    */
115   BlockCache [] getBlockCaches();
116 }