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.hbase.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 LruCachedBlock implements HeapSize, Comparable<LruCachedBlock> {
36
37 public final static long PER_BLOCK_OVERHEAD = ClassSize.align(
38 ClassSize.OBJECT + (3 * ClassSize.REFERENCE) + (3 * Bytes.SIZEOF_LONG) +
39 ClassSize.STRING + ClassSize.BYTE_BUFFER);
40
41 private final BlockCacheKey cacheKey;
42 private final Cacheable buf;
43 private volatile long accessTime;
44 private long size;
45 private BlockPriority priority;
46
47
48
49 private final long cachedTime = System.nanoTime();
50
51 public LruCachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime) {
52 this(cacheKey, buf, accessTime, false);
53 }
54
55 public LruCachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime,
56 boolean inMemory) {
57 this.cacheKey = cacheKey;
58 this.buf = buf;
59 this.accessTime = accessTime;
60
61
62
63
64
65 this.size = ClassSize.align(cacheKey.heapSize())
66 + ClassSize.align(buf.heapSize()) + PER_BLOCK_OVERHEAD;
67 if(inMemory) {
68 this.priority = BlockPriority.MEMORY;
69 } else {
70 this.priority = BlockPriority.SINGLE;
71 }
72 }
73
74
75
76
77
78
79 public void access(long accessTime) {
80 this.accessTime = accessTime;
81 if(this.priority == BlockPriority.SINGLE) {
82 this.priority = BlockPriority.MULTI;
83 }
84 }
85
86
87
88
89 public long getCachedTime() {
90 return this.cachedTime;
91 }
92
93 public long heapSize() {
94 return size;
95 }
96
97 @Override
98 public int compareTo(LruCachedBlock that) {
99
100 if (this.accessTime == that.accessTime) return 0;
101 return this.accessTime < that.accessTime ? 1 : -1;
102 }
103
104 @Override
105 public int hashCode() {
106 return (int)(accessTime ^ (accessTime >>> 32));
107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj) {
112 return true;
113 }
114 if (obj == null || getClass() != obj.getClass()) {
115 return false;
116 }
117 LruCachedBlock other = (LruCachedBlock) obj;
118 return compareTo(other) == 0;
119 }
120
121 public Cacheable getBuffer() {
122 return this.buf;
123 }
124
125 public BlockCacheKey getCacheKey() {
126 return this.cacheKey;
127 }
128
129 public BlockPriority getPriority() {
130 return this.priority;
131 }
132 }