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.regionserver;
019
020import java.util.Comparator;
021import java.util.function.Function;
022import java.util.function.ToLongFunction;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * Useful comparators for comparing store files.
027 */
028@InterfaceAudience.Private
029final class StoreFileComparators {
030  /**
031   * Comparator that compares based on the Sequence Ids of the the store files. Bulk loads that did
032   * not request a seq ID are given a seq id of -1; thus, they are placed before all non- bulk
033   * loads, and bulk loads with sequence Id. Among these files, the size is used to determine the
034   * ordering, then bulkLoadTime. If there are ties, the path name is used as a tie-breaker.
035   */
036  public static final Comparator<HStoreFile> SEQ_ID =
037    Comparator.comparingLong(HStoreFile::getMaxSequenceId)
038      .thenComparing(Comparator.comparingLong(new GetFileSize()).reversed())
039      .thenComparingLong(new GetBulkTime()).thenComparing(new GetPathName());
040
041  /**
042   * Comparator for time-aware compaction. SeqId is still the first ordering criterion to maintain
043   * MVCC.
044   */
045  public static final Comparator<HStoreFile> SEQ_ID_MAX_TIMESTAMP =
046    Comparator.comparingLong(HStoreFile::getMaxSequenceId).thenComparingLong(new GetMaxTimestamp())
047      .thenComparing(Comparator.comparingLong(new GetFileSize()).reversed())
048      .thenComparingLong(new GetBulkTime()).thenComparing(new GetPathName());
049
050  private static class GetFileSize implements ToLongFunction<HStoreFile> {
051
052    @Override
053    public long applyAsLong(HStoreFile sf) {
054      if (sf.getReader() != null) {
055        return sf.getReader().length();
056      } else {
057        // the reader may be null for the compacted files and if the archiving
058        // had failed.
059        return -1L;
060      }
061    }
062  }
063
064  private static class GetBulkTime implements ToLongFunction<HStoreFile> {
065
066    @Override
067    public long applyAsLong(HStoreFile sf) {
068      return sf.getBulkLoadTimestamp().orElse(Long.MAX_VALUE);
069    }
070  }
071
072  private static class GetPathName implements Function<HStoreFile, String> {
073
074    @Override
075    public String apply(HStoreFile sf) {
076      return sf.getPath().getName();
077    }
078  }
079
080  private static class GetMaxTimestamp implements ToLongFunction<HStoreFile> {
081
082    @Override
083    public long applyAsLong(HStoreFile sf) {
084      return sf.getMaximumTimestamp().orElse(Long.MAX_VALUE);
085    }
086  }
087}