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 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
28
29
30
31
32
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
43
44 SINGLE,
45
46
47
48 MULTI,
49
50
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
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 }