1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.io.hfile;
19
20 import org.apache.hadoop.hbase.classification.InterfaceAudience;
21 import org.apache.hadoop.hbase.io.HeapSize;
22 import org.apache.hadoop.hbase.util.Bytes;
23 import org.apache.hadoop.hbase.util.ClassSize;
24
25
26
27
28 @InterfaceAudience.Private
29 public class BlockCacheKey implements HeapSize, java.io.Serializable {
30 private static final long serialVersionUID = -5199992013113130534L;
31 private final String hfileName;
32 private final long offset;
33 private final BlockType blockType;
34 private final boolean isPrimaryReplicaBlock;
35
36
37
38
39
40
41 public BlockCacheKey(String hfileName, long offset) {
42 this(hfileName, offset, true, BlockType.DATA);
43 }
44
45 public BlockCacheKey(String hfileName, long offset, boolean isPrimaryReplica, BlockType blockType) {
46 this.isPrimaryReplicaBlock = isPrimaryReplica;
47 this.hfileName = hfileName;
48 this.offset = offset;
49 this.blockType = blockType;
50 }
51
52 @Override
53 public int hashCode() {
54 return hfileName.hashCode() * 127 + (int) (offset ^ (offset >>> 32));
55 }
56
57 @Override
58 public boolean equals(Object o) {
59 if (o instanceof BlockCacheKey) {
60 BlockCacheKey k = (BlockCacheKey) o;
61 return offset == k.offset
62 && (hfileName == null ? k.hfileName == null : hfileName
63 .equals(k.hfileName));
64 } else {
65 return false;
66 }
67 }
68
69 @Override
70 public String toString() {
71 return String.format("%s_%d", hfileName, offset);
72 }
73
74 public static final long FIXED_OVERHEAD = ClassSize.align(
75 ClassSize.OBJECT +
76 Bytes.SIZEOF_BOOLEAN +
77 ClassSize.REFERENCE +
78 ClassSize.REFERENCE +
79 Bytes.SIZEOF_LONG);
80
81
82
83
84
85 @Override
86 public long heapSize() {
87 return ClassSize.align(FIXED_OVERHEAD + ClassSize.STRING +
88 2 * hfileName.length());
89 }
90
91
92
93
94
95 public String getHfileName() {
96 return hfileName;
97 }
98
99 public boolean isPrimary() {
100 return isPrimaryReplicaBlock;
101 }
102
103 public long getOffset() {
104 return offset;
105 }
106
107 public BlockType getBlockType() {
108 return blockType;
109 }
110 }