001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile;
019
020import org.apache.yetus.audience.InterfaceAudience;
021
022@InterfaceAudience.Private
023public class InclusiveCombinedBlockCache extends CombinedBlockCache {
024  public InclusiveCombinedBlockCache(FirstLevelBlockCache l1, BlockCache l2) {
025    super(l1, l2);
026    l1.setVictimCache(l2);
027  }
028
029  @Override
030  public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat,
031    boolean updateCacheMetrics) {
032    // On all external cache set ups the lru should have the l2 cache set as the victimHandler
033    // Because of that all requests that miss inside of the lru block cache will be
034    // tried in the l2 block cache.
035    return l1Cache.getBlock(cacheKey, caching, repeat, updateCacheMetrics);
036  }
037
038  /**
039   * @param cacheKey The block's cache key.
040   * @param buf      The block contents wrapped in a ByteBuffer.
041   * @param inMemory Whether block should be treated as in-memory. This parameter is only useful for
042   *                 the L1 lru cache.
043   */
044  @Override
045  public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory) {
046    // This is the inclusive part of the combined block cache.
047    // Every block is placed into both block caches.
048    l1Cache.cacheBlock(cacheKey, buf, inMemory);
049
050    // This assumes that insertion into the L2 block cache is either async or very fast.
051    l2Cache.cacheBlock(cacheKey, buf, inMemory);
052  }
053
054  @Override
055  public boolean evictBlock(BlockCacheKey cacheKey) {
056    boolean l1Result = this.l1Cache.evictBlock(cacheKey);
057    boolean l2Result = this.l2Cache.evictBlock(cacheKey);
058    return l1Result || l2Result;
059  }
060}