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.io.IOException;
021import java.util.Collection;
022import java.util.List;
023import java.util.Map;
024import java.util.NavigableMap;
025import java.util.TreeMap;
026import java.util.function.Function;
027import org.apache.hadoop.hbase.ExtendedCell;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * class for cell sink that separates the provided cells into multiple files for date tiered
032 * compaction.
033 */
034@InterfaceAudience.Private
035public class DateTieredMultiFileWriter extends AbstractMultiFileWriter {
036
037  protected final NavigableMap<Long, StoreFileWriter> lowerBoundary2Writer = new TreeMap<>();
038
039  private final boolean needEmptyFile;
040
041  private final Map<Long, String> lowerBoundariesPolicies;
042
043  protected Function<ExtendedCell, Long> tieringFunction;
044
045  /**
046   * @param lowerBoundariesPolicies each window to storage policy map.
047   * @param needEmptyFile           whether need to create an empty store file if we haven't written
048   *                                out anything.
049   */
050  public DateTieredMultiFileWriter(List<Long> lowerBoundaries,
051    Map<Long, String> lowerBoundariesPolicies, boolean needEmptyFile) {
052    this(lowerBoundaries, lowerBoundariesPolicies, needEmptyFile, c -> c.getTimestamp());
053  }
054
055  /**
056   * @param lowerBoundariesPolicies each window to storage policy map.
057   * @param needEmptyFile           whether need to create an empty store file if we haven't written
058   *                                out anything.
059   */
060  public DateTieredMultiFileWriter(List<Long> lowerBoundaries,
061    Map<Long, String> lowerBoundariesPolicies, boolean needEmptyFile,
062    Function<ExtendedCell, Long> tieringFunction) {
063    for (Long lowerBoundary : lowerBoundaries) {
064      lowerBoundary2Writer.put(lowerBoundary, null);
065    }
066    this.needEmptyFile = needEmptyFile;
067    this.lowerBoundariesPolicies = lowerBoundariesPolicies;
068    this.tieringFunction = tieringFunction;
069  }
070
071  @Override
072  public void append(ExtendedCell cell) throws IOException {
073    Map.Entry<Long, StoreFileWriter> entry =
074      lowerBoundary2Writer.floorEntry(tieringFunction.apply(cell));
075    StoreFileWriter writer = entry.getValue();
076    if (writer == null) {
077      String lowerBoundaryStoragePolicy = lowerBoundariesPolicies.get(entry.getKey());
078      if (lowerBoundaryStoragePolicy != null) {
079        writer = writerFactory.createWriterWithStoragePolicy(lowerBoundaryStoragePolicy);
080      } else {
081        writer = writerFactory.createWriter();
082      }
083      lowerBoundary2Writer.put(entry.getKey(), writer);
084    }
085    writer.append(cell);
086  }
087
088  @Override
089  protected Collection<StoreFileWriter> writers() {
090    return lowerBoundary2Writer.values();
091  }
092
093  @Override
094  protected void preCommitWriters() throws IOException {
095    if (!needEmptyFile) {
096      return;
097    }
098    for (StoreFileWriter writer : lowerBoundary2Writer.values()) {
099      if (writer != null) {
100        return;
101      }
102    }
103    // we haven't written out any data, create an empty file to retain metadata
104    lowerBoundary2Writer.put(lowerBoundary2Writer.firstKey(), writerFactory.createWriter());
105  }
106}