View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * Cached mob file.
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     * Opens the mob file if it's not opened yet and increases the reference.
81     * It's not thread-safe. Use MobFileCache.openFile() instead.
82     * The reader of the mob file is just opened when it's not opened no matter how many times
83     * this open() method is invoked.
84     * The reference is a counter that how many times this reader is referenced. When the
85     * reference is 0, this reader is closed.
86     */
87    @Override
88    public void open() throws IOException {
89      super.open();
90      referenceCount.incrementAndGet();
91    }
92  
93    /**
94     * Decreases the reference of the underlying reader for the mob file.
95     * It's not thread-safe. Use MobFileCache.closeFile() instead.
96     * This underlying reader isn't closed until the reference is 0.
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    * Gets the reference of the current mob file.
108    * Internal usage, currently it's for testing.
109    * @return The reference of the current mob file.
110    */
111   public long getReferenceCount() {
112     return this.referenceCount.longValue();
113   }
114 }