1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.io.hfile;
20
21 import org.apache.hadoop.classification.InterfaceAudience;
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 @InterfaceAudience.Private
35 public class CachedBlock implements HeapSize, Comparable<CachedBlock> {
36
37 public final static long PER_BLOCK_OVERHEAD = ClassSize.align(
38 ClassSize.OBJECT + (3 * ClassSize.REFERENCE) + (2 * Bytes.SIZEOF_LONG) +
39 ClassSize.STRING + ClassSize.BYTE_BUFFER);
40
41 static enum BlockPriority {
42
43
44
45 SINGLE,
46
47
48
49 MULTI,
50
51
52
53 MEMORY
54 };
55
56 private final BlockCacheKey cacheKey;
57 private final Cacheable buf;
58 private volatile long accessTime;
59 private long size;
60 private BlockPriority priority;
61
62 public CachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime) {
63 this(cacheKey, buf, accessTime, false);
64 }
65
66 public CachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime,
67 boolean inMemory) {
68 this.cacheKey = cacheKey;
69 this.buf = buf;
70 this.accessTime = accessTime;
71
72
73
74
75
76 this.size = ClassSize.align(cacheKey.heapSize())
77 + ClassSize.align(buf.heapSize()) + PER_BLOCK_OVERHEAD;
78 if(inMemory) {
79 this.priority = BlockPriority.MEMORY;
80 } else {
81 this.priority = BlockPriority.SINGLE;
82 }
83 }
84
85
86
87
88 public void access(long accessTime) {
89 this.accessTime = accessTime;
90 if(this.priority == BlockPriority.SINGLE) {
91 this.priority = BlockPriority.MULTI;
92 }
93 }
94
95 public long heapSize() {
96 return size;
97 }
98
99 @Override
100 public int compareTo(CachedBlock that) {
101 if(this.accessTime == that.accessTime) return 0;
102 return this.accessTime < that.accessTime ? 1 : -1;
103 }
104
105 @Override
106 public boolean equals(Object obj) {
107 if (this == obj) {
108 return true;
109 }
110 if (obj == null || getClass() != obj.getClass()) {
111 return false;
112 }
113 CachedBlock other = (CachedBlock) obj;
114 return compareTo(other) == 0;
115 }
116
117 public Cacheable getBuffer() {
118 return this.buf;
119 }
120
121 public BlockCacheKey getCacheKey() {
122 return this.cacheKey;
123 }
124
125 public BlockPriority getPriority() {
126 return this.priority;
127 }
128 }