View Javadoc

1   /**
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.io.hfile;
21  
22  import org.apache.hadoop.hbase.io.HeapSize;
23  import org.apache.hadoop.hbase.util.Bytes;
24  import org.apache.hadoop.hbase.util.ClassSize;
25  
26  /**
27   * Represents an entry in the {@link LruBlockCache}.
28   *
29   * <p>Makes the block memory-aware with {@link HeapSize} and Comparable
30   * to sort by access time for the LRU.  It also takes care of priority by
31   * either instantiating as in-memory or handling the transition from single
32   * to multiple access.
33   */
34  public class CachedBlock implements HeapSize, Comparable<CachedBlock> {
35  
36    public final static long PER_BLOCK_OVERHEAD = ClassSize.align(
37      ClassSize.OBJECT + (3 * ClassSize.REFERENCE) + (2 * Bytes.SIZEOF_LONG) +
38      ClassSize.STRING + ClassSize.BYTE_BUFFER);
39  
40    static enum BlockPriority {
41      /**
42       * Accessed a single time (used for scan-resistance)
43       */
44      SINGLE,
45      /**
46       * Accessed multiple times
47       */
48      MULTI,
49      /**
50       * Block from in-memory store
51       */
52      MEMORY
53    };
54  
55    private final String blockName;
56    private final Cacheable buf;
57    private volatile long accessTime;
58    private long size;
59    private BlockPriority priority;
60  
61    public CachedBlock(String blockName, Cacheable buf, long accessTime) {
62      this(blockName, buf, accessTime, false);
63    }
64  
65    public CachedBlock(String blockName, Cacheable buf, long accessTime,
66        boolean inMemory) {
67      this.blockName = blockName;
68      this.buf = buf;
69      this.accessTime = accessTime;
70      this.size = ClassSize.align(blockName.length())
71          + ClassSize.align(buf.heapSize()) + PER_BLOCK_OVERHEAD;
72      if(inMemory) {
73        this.priority = BlockPriority.MEMORY;
74      } else {
75        this.priority = BlockPriority.SINGLE;
76      }
77    }
78  
79    /**
80     * Block has been accessed.  Update its local access time.
81     */
82    public void access(long accessTime) {
83      this.accessTime = accessTime;
84      if(this.priority == BlockPriority.SINGLE) {
85        this.priority = BlockPriority.MULTI;
86      }
87    }
88  
89    public long heapSize() {
90      return size;
91    }
92  
93    public int compareTo(CachedBlock that) {
94      if(this.accessTime == that.accessTime) return 0;
95      return this.accessTime < that.accessTime ? 1 : -1;
96    }
97  
98    public Cacheable getBuffer() {
99      return this.buf;
100   }
101 
102   public String getName() {
103     return this.blockName;
104   }
105 
106   public BlockPriority getPriority() {
107     return this.priority;
108   }
109 }