001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.mob;
020
021import java.io.IOException;
022import java.util.concurrent.atomic.AtomicLong;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.io.hfile.CacheConfig;
028import org.apache.hadoop.hbase.regionserver.BloomType;
029import org.apache.hadoop.hbase.regionserver.HStoreFile;
030import org.apache.yetus.audience.InterfaceAudience;
031
032/**
033 * Cached mob file.
034 */
035@InterfaceAudience.Private
036public class CachedMobFile extends MobFile implements Comparable<CachedMobFile> {
037
038  private long accessCount;
039  private AtomicLong referenceCount = new AtomicLong(0);
040
041  public CachedMobFile(HStoreFile sf) {
042    super(sf);
043  }
044
045  public static CachedMobFile create(FileSystem fs, Path path, Configuration conf,
046      CacheConfig cacheConf) throws IOException {
047    // XXX: primaryReplica is only used for constructing the key of block cache so it is not a
048    // critical problem if we pass the wrong value, so here we always pass true. Need to fix later.
049    HStoreFile sf = new HStoreFile(fs, path, conf, cacheConf, BloomType.NONE, true);
050    return new CachedMobFile(sf);
051  }
052
053  public void access(long accessCount) {
054    this.accessCount = accessCount;
055  }
056
057  @Override
058  public int compareTo(CachedMobFile that) {
059    if (this.accessCount == that.accessCount) return 0;
060    return this.accessCount < that.accessCount ? 1 : -1;
061  }
062
063  @Override
064  public boolean equals(Object obj) {
065    if (this == obj) {
066      return true;
067    }
068    if (obj == null) {
069      return false;
070    }
071    if (!(obj instanceof CachedMobFile)) {
072      return false;
073    }
074    return compareTo((CachedMobFile) obj) == 0;
075  }
076
077  @Override
078  public int hashCode() {
079    return (int)(accessCount ^ (accessCount >>> 32));
080  }
081
082  /**
083   * Opens the mob file if it's not opened yet and increases the reference.
084   * It's not thread-safe. Use MobFileCache.openFile() instead.
085   * The reader of the mob file is just opened when it's not opened no matter how many times
086   * this open() method is invoked.
087   * The reference is a counter that how many times this reader is referenced. When the
088   * reference is 0, this reader is closed.
089   */
090  @Override
091  public void open() throws IOException {
092    super.open();
093    referenceCount.incrementAndGet();
094  }
095
096  /**
097   * Decreases the reference of the underlying reader for the mob file.
098   * It's not thread-safe. Use MobFileCache.closeFile() instead.
099   * This underlying reader isn't closed until the reference is 0.
100   */
101  @Override
102  public void close() throws IOException {
103    long refs = referenceCount.decrementAndGet();
104    if (refs == 0) {
105      super.close();
106    }
107  }
108
109  /**
110   * Gets the reference of the current mob file.
111   * Internal usage, currently it's for testing.
112   * @return The reference of the current mob file.
113   */
114  public long getReferenceCount() {
115    return this.referenceCount.longValue();
116  }
117}