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