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