1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
30
31
32
33
34
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
45
46 SINGLE,
47
48
49
50 MULTI,
51
52
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
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 }