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;
026
027import org.apache.hadoop.hbase.Cell;
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  private final NavigableMap<Long, StoreFileWriter> lowerBoundary2Writer = new TreeMap<>();
038
039  private final boolean needEmptyFile;
040
041  /**
042   * @param needEmptyFile whether need to create an empty store file if we haven't written out
043   *          anything.
044   */
045  public DateTieredMultiFileWriter(List<Long> lowerBoundaries, boolean needEmptyFile) {
046    for (Long lowerBoundary : lowerBoundaries) {
047      lowerBoundary2Writer.put(lowerBoundary, null);
048    }
049    this.needEmptyFile = needEmptyFile;
050  }
051
052  @Override
053  public void append(Cell cell) throws IOException {
054    Map.Entry<Long, StoreFileWriter> entry = lowerBoundary2Writer.floorEntry(cell.getTimestamp());
055    StoreFileWriter writer = entry.getValue();
056    if (writer == null) {
057      writer = writerFactory.createWriter();
058      lowerBoundary2Writer.put(entry.getKey(), writer);
059    }
060    writer.append(cell);
061  }
062
063  @Override
064  protected Collection<StoreFileWriter> writers() {
065    return lowerBoundary2Writer.values();
066  }
067
068  @Override
069  protected void preCommitWriters() throws IOException {
070    if (!needEmptyFile) {
071      return;
072    }
073    for (StoreFileWriter writer : lowerBoundary2Writer.values()) {
074      if (writer != null) {
075        return;
076      }
077    }
078    // we haven't written out any data, create an empty file to retain metadata
079    lowerBoundary2Writer.put(lowerBoundary2Writer.firstKey(), writerFactory.createWriter());
080  }
081}