001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.io.hfile;
020
021import java.util.Iterator;
022
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.hadoop.hbase.io.hfile.Cacheable.MemoryType;
025
026/**
027 * Block cache interface. Anything that implements the {@link Cacheable}
028 * interface can be put in the cache.
029 */
030@InterfaceAudience.Private
031public interface BlockCache extends Iterable<CachedBlock> {
032  /**
033   * Add block to cache.
034   * @param cacheKey The block's cache key.
035   * @param buf The block contents wrapped in a ByteBuffer.
036   * @param inMemory Whether block should be treated as in-memory
037   */
038  void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory);
039
040  /**
041   * Add block to cache (defaults to not in-memory).
042   * @param cacheKey The block's cache key.
043   * @param buf The object to cache.
044   */
045  void cacheBlock(BlockCacheKey cacheKey, Cacheable buf);
046
047  /**
048   * Fetch block from cache.
049   * @param cacheKey Block to fetch.
050   * @param caching Whether this request has caching enabled (used for stats)
051   * @param repeat Whether this is a repeat lookup for the same block
052   *        (used to avoid double counting cache misses when doing double-check locking)
053   * @param updateCacheMetrics Whether to update cache metrics or not
054   * @return Block or null if block is not in 2 cache.
055   */
056  Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat,
057    boolean updateCacheMetrics);
058
059  /**
060   * Evict block from cache.
061   * @param cacheKey Block to evict
062   * @return true if block existed and was evicted, false if not
063   */
064  boolean evictBlock(BlockCacheKey cacheKey);
065
066  /**
067   * Evicts all blocks for the given HFile.
068   *
069   * @return the number of blocks evicted
070   */
071  int evictBlocksByHfileName(String hfileName);
072
073  /**
074   * Get the statistics for this block cache.
075   * @return Stats
076   */
077  CacheStats getStats();
078
079  /**
080   * Shutdown the cache.
081   */
082  void shutdown();
083
084  /**
085   * Returns the total size of the block cache, in bytes.
086   * @return size of cache, in bytes
087   */
088  long size();
089
090  /**
091   * Returns the Max size of the block cache, in bytes.
092   * @return size of cache, in bytes
093   */
094  long getMaxSize();
095
096  /**
097   * Returns the free size of the block cache, in bytes.
098   * @return free space in cache, in bytes
099   */
100  long getFreeSize();
101
102  /**
103   * Returns the occupied size of the block cache, in bytes.
104   * @return occupied space in cache, in bytes
105   */
106  long getCurrentSize();
107
108  /**
109   * Returns the occupied size of data blocks, in bytes.
110   * @return occupied space in cache, in bytes
111   */
112  long getCurrentDataSize();
113
114  /**
115   * Returns the number of blocks currently cached in the block cache.
116   * @return number of blocks in the cache
117   */
118  long getBlockCount();
119
120 /**
121  * Returns the number of data blocks currently cached in the block cache.
122  * @return number of blocks in the cache
123  */
124 long getDataBlockCount();
125
126  /**
127   * @return Iterator over the blocks in the cache.
128   */
129  @Override
130  Iterator<CachedBlock> iterator();
131
132  /**
133   * @return The list of sub blockcaches that make up this one; returns null if no sub caches.
134   */
135  BlockCache [] getBlockCaches();
136
137  /**
138   * Called when the scanner using the block decides to return the block once its usage
139   * is over.
140   * This API should be called after the block is used, failing to do so may have adverse effects
141   * by preventing the blocks from being evicted because of which it will prevent new hot blocks
142   * from getting added to the block cache.  The implementation of the BlockCache will decide
143   * on what to be done with the block based on the memory type of the block's {@link MemoryType}.
144   * @param cacheKey the cache key of the block
145   * @param block the hfileblock to be returned
146   */
147  default void returnBlock(BlockCacheKey cacheKey, Cacheable block){};
148}