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 java.nio.ByteBuffer;
23  
24  import org.apache.hadoop.hbase.io.HeapSize;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.apache.hadoop.hbase.util.ClassSize;
27  
28  /**
29   * Represents an entry in the {@link LruBlockCache}.
30   *
31   * <p>Makes the block memory-aware with {@link HeapSize} and Comparable
32   * to sort by access time for the LRU.  It also takes care of priority by
33   * either instantiating as in-memory or handling the transition from single
34   * to multiple access.
35   */
36  public class CachedBlock implements HeapSize, Comparable<CachedBlock> {
37  
38    public final static long PER_BLOCK_OVERHEAD = ClassSize.align(
39      ClassSize.OBJECT + (3 * ClassSize.REFERENCE) + (2 * Bytes.SIZEOF_LONG) +
40      ClassSize.STRING + ClassSize.BYTE_BUFFER);
41  
42    static enum BlockPriority {
43      /**
44       * Accessed a single time (used for scan-resistance)
45       */
46      SINGLE,
47      /**
48       * Accessed multiple times
49       */
50      MULTI,
51      /**
52       * Block from in-memory store
53       */
54      MEMORY
55    };
56  
57    private final String blockName;
58    private final ByteBuffer buf;
59    private volatile long accessTime;
60    private long size;
61    private BlockPriority priority;
62  
63    public CachedBlock(String blockName, ByteBuffer buf, long accessTime) {
64      this(blockName, buf, accessTime, false);
65    }
66  
67    public CachedBlock(String blockName, ByteBuffer buf, long accessTime,
68        boolean inMemory) {
69      this.blockName = blockName;
70      this.buf = buf;
71      this.accessTime = accessTime;
72      this.size = ClassSize.align(blockName.length()) +
73      ClassSize.align(buf.capacity()) + PER_BLOCK_OVERHEAD;
74      if(inMemory) {
75        this.priority = BlockPriority.MEMORY;
76      } else {
77        this.priority = BlockPriority.SINGLE;
78      }
79    }
80  
81    /**
82     * Block has been accessed.  Update its local access time.
83     */
84    public void access(long accessTime) {
85      this.accessTime = accessTime;
86      if(this.priority == BlockPriority.SINGLE) {
87        this.priority = BlockPriority.MULTI;
88      }
89    }
90  
91    public long heapSize() {
92      return size;
93    }
94  
95    public int compareTo(CachedBlock that) {
96      if(this.accessTime == that.accessTime) return 0;
97      return this.accessTime < that.accessTime ? 1 : -1;
98    }
99  
100   public ByteBuffer getBuffer() {
101     return this.buf;
102   }
103 
104   public String getName() {
105     return this.blockName;
106   }
107 
108   public BlockPriority getPriority() {
109     return this.priority;
110   }
111 }