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 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
71
72
73
74
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
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 }