1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mob;
20
21 import java.io.IOException;
22 import java.util.concurrent.atomic.AtomicLong;
23
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
29 import org.apache.hadoop.hbase.regionserver.BloomType;
30 import org.apache.hadoop.hbase.regionserver.StoreFile;
31
32
33
34
35 @InterfaceAudience.Private
36 public class CachedMobFile extends MobFile implements Comparable<CachedMobFile> {
37
38 private long accessCount;
39 private AtomicLong referenceCount = new AtomicLong(0);
40
41 public CachedMobFile(StoreFile sf) {
42 super(sf);
43 }
44
45 public static CachedMobFile create(FileSystem fs, Path path, Configuration conf,
46 CacheConfig cacheConf) throws IOException {
47 StoreFile sf = new StoreFile(fs, path, conf, cacheConf, BloomType.NONE);
48 return new CachedMobFile(sf);
49 }
50
51 public void access(long accessCount) {
52 this.accessCount = accessCount;
53 }
54
55 public int compareTo(CachedMobFile that) {
56 if (this.accessCount == that.accessCount) return 0;
57 return this.accessCount < that.accessCount ? 1 : -1;
58 }
59
60 @Override
61 public boolean equals(Object obj) {
62 if (this == obj) {
63 return true;
64 }
65 if (obj == null) {
66 return false;
67 }
68 if (!(obj instanceof CachedMobFile)) {
69 return false;
70 }
71 return compareTo((CachedMobFile) obj) == 0;
72 }
73
74 @Override
75 public int hashCode() {
76 return (int)(accessCount ^ (accessCount >>> 32));
77 }
78
79
80
81
82
83
84
85
86
87 @Override
88 public void open() throws IOException {
89 super.open();
90 referenceCount.incrementAndGet();
91 }
92
93
94
95
96
97
98 @Override
99 public void close() throws IOException {
100 long refs = referenceCount.decrementAndGet();
101 if (refs == 0) {
102 super.close();
103 }
104 }
105
106
107
108
109
110
111 public long getReferenceCount() {
112 return this.referenceCount.longValue();
113 }
114 }