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.hbase.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     * @param cacheDataInL1 If multi-tier block cache deploy -- i.e. has an L1 and L2 tier -- then
37     * if this flag is true, cache data blocks up in the L1 tier (meta blocks are probably being
38     * cached in L1 already).
39     */
40    void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory, boolean cacheDataInL1);
41  
42    /**
43     * Add block to cache (defaults to not in-memory).
44     * @param cacheKey The block's cache key.
45     * @param buf The object to cache.
46     */
47    void cacheBlock(BlockCacheKey cacheKey, Cacheable buf);
48  
49    /**
50     * Fetch block from cache.
51     * @param cacheKey Block to fetch.
52     * @param caching Whether this request has caching enabled (used for stats)
53     * @param repeat Whether this is a repeat lookup for the same block
54     *        (used to avoid double counting cache misses when doing double-check locking)
55     * @param updateCacheMetrics Whether to update cache metrics or not
56     * @return Block or null if block is not in 2 cache.
57     */
58    Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat,
59      boolean updateCacheMetrics);
60  
61    /**
62     * Evict block from cache.
63     * @param cacheKey Block to evict
64     * @return true if block existed and was evicted, false if not
65     */
66    boolean evictBlock(BlockCacheKey cacheKey);
67  
68    /**
69     * Evicts all blocks for the given HFile.
70     *
71     * @return the number of blocks evicted
72     */
73    int evictBlocksByHfileName(String hfileName);
74  
75    /**
76     * Get the statistics for this block cache.
77     * @return Stats
78     */
79    CacheStats getStats();
80  
81    /**
82     * Shutdown the cache.
83     */
84    void shutdown();
85  
86    /**
87     * Returns the total size of the block cache, in bytes.
88     * @return size of cache, in bytes
89     */
90    long size();
91  
92    /**
93     * Returns the free size of the block cache, in bytes.
94     * @return free space in cache, in bytes
95     */
96    long getFreeSize();
97  
98    /**
99     * Returns the occupied size of the block cache, in bytes.
100    * @return occupied space in cache, in bytes
101    */
102   long getCurrentSize();
103 
104   /**
105    * Returns the number of blocks currently cached in the block cache.
106    * @return number of blocks in the cache
107    */
108   long getBlockCount();
109 
110   /**
111    * @return Iterator over the blocks in the cache.
112    */
113   Iterator<CachedBlock> iterator();
114 
115   /**
116    * @return The list of sub blockcaches that make up this one; returns null if no sub caches.
117    */
118   BlockCache [] getBlockCaches();
119 }