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.io.encoding.DataBlockEncoding;
23 import org.apache.hadoop.hbase.util.Bytes;
24 import org.apache.hadoop.hbase.util.ClassSize;
25
26
27
28
29 @InterfaceAudience.Private
30 public class BlockCacheKey implements HeapSize, java.io.Serializable {
31 private static final long serialVersionUID = -5199992013113130534L;
32 private final String hfileName;
33 private final long offset;
34 private final DataBlockEncoding encoding;
35 private final BlockType blockType;
36
37 public BlockCacheKey(String file, long offset, DataBlockEncoding encoding,
38 BlockType blockType) {
39 this.hfileName = file;
40 this.offset = offset;
41
42
43
44 this.encoding = (encoding != null && (blockType == null
45 || blockType.isData())) ? encoding : DataBlockEncoding.NONE;
46 this.blockType = blockType;
47 }
48
49
50
51
52
53
54 public BlockCacheKey(String hfileName, long offset) {
55 this(hfileName, offset, DataBlockEncoding.NONE, BlockType.DATA);
56 }
57
58 @Override
59 public int hashCode() {
60 return hfileName.hashCode() * 127 + (int) (offset ^ (offset >>> 32)) +
61 encoding.ordinal() * 17;
62 }
63
64 @Override
65 public boolean equals(Object o) {
66 if (o instanceof BlockCacheKey) {
67 BlockCacheKey k = (BlockCacheKey) o;
68 return offset == k.offset && encoding == k.encoding
69 && (hfileName == null ? k.hfileName == null : hfileName
70 .equals(k.hfileName));
71 } else {
72 return false;
73 }
74 }
75
76 @Override
77 public String toString() {
78 return hfileName + "_" + offset
79 + (encoding == DataBlockEncoding.NONE ? "" : "_" + encoding);
80 }
81
82 public static final long FIXED_OVERHEAD = ClassSize.align(
83 ClassSize.OBJECT +
84 ClassSize.REFERENCE +
85 ClassSize.REFERENCE +
86 ClassSize.REFERENCE +
87 Bytes.SIZEOF_LONG);
88
89
90
91
92
93 @Override
94 public long heapSize() {
95 return ClassSize.align(FIXED_OVERHEAD + 2 * hfileName.length());
96 }
97
98
99
100
101
102 public String getHfileName() {
103 return hfileName;
104 }
105
106 public DataBlockEncoding getDataBlockEncoding() {
107 return encoding;
108 }
109
110 public long getOffset() {
111 return offset;
112 }
113
114 public BlockType getBlockType() {
115 return blockType;
116 }
117 }