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 BlockCacheKey cacheKey;
56    private final Cacheable buf;
57    private volatile long accessTime;
58    private long size;
59    private BlockPriority priority;
60  
61    public CachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime) {
62      this(cacheKey, buf, accessTime, false);
63    }
64  
65    public CachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime,
66        boolean inMemory) {
67      this.cacheKey = cacheKey;
68      this.buf = buf;
69      this.accessTime = accessTime;
70      // We approximate the size of this class by the size of its name string
71      // plus the size of its byte buffer plus the overhead associated with all
72      // the base classes. We also include the base class
73      // sizes in the PER_BLOCK_OVERHEAD variable rather than align()ing them with
74      // their buffer lengths. This variable is used elsewhere in unit tests.
75      this.size = ClassSize.align(cacheKey.heapSize())
76          + ClassSize.align(buf.heapSize()) + PER_BLOCK_OVERHEAD;
77      if(inMemory) {
78        this.priority = BlockPriority.MEMORY;
79      } else {
80        this.priority = BlockPriority.SINGLE;
81      }
82    }
83  
84    /**
85     * Block has been accessed.  Update its local access time.
86     */
87    public void access(long accessTime) {
88      this.accessTime = accessTime;
89      if(this.priority == BlockPriority.SINGLE) {
90        this.priority = BlockPriority.MULTI;
91      }
92    }
93  
94    public long heapSize() {
95      return size;
96    }
97  
98    public int compareTo(CachedBlock that) {
99      if(this.accessTime == that.accessTime) return 0;
100     return this.accessTime < that.accessTime ? 1 : -1;
101   }
102 
103   public Cacheable getBuffer() {
104     return this.buf;
105   }
106 
107   public BlockCacheKey getCacheKey() {
108     return this.cacheKey;
109   }
110 
111   public BlockPriority getPriority() {
112     return this.priority;
113   }
114 }