View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import java.io.IOException;
21  import java.util.Collection;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.NavigableMap;
25  import java.util.TreeMap;
26  
27  import org.apache.hadoop.hbase.Cell;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.regionserver.StoreFile.Writer;
30  
31  /**
32   * class for cell sink that separates the provided cells into multiple files for date tiered
33   * compaction.
34   */
35  @InterfaceAudience.Private
36  public class DateTieredMultiFileWriter extends AbstractMultiFileWriter {
37  
38    private final NavigableMap<Long, StoreFile.Writer> lowerBoundary2Writer
39      = new TreeMap<Long, StoreFile.Writer>();
40  
41    private final boolean needEmptyFile;
42  
43    /**
44     * @param needEmptyFile whether need to create an empty store file if we haven't written out
45     *          anything.
46     */
47    public DateTieredMultiFileWriter(List<Long> lowerBoundaries, boolean needEmptyFile) {
48      for (Long lowerBoundary : lowerBoundaries) {
49        lowerBoundary2Writer.put(lowerBoundary, null);
50      }
51      this.needEmptyFile = needEmptyFile;
52    }
53  
54    @Override
55    public void append(Cell cell) throws IOException {
56      Map.Entry<Long, StoreFile.Writer> entry = lowerBoundary2Writer.floorEntry(cell.getTimestamp());
57      StoreFile.Writer writer = entry.getValue();
58      if (writer == null) {
59        writer = writerFactory.createWriter();
60        lowerBoundary2Writer.put(entry.getKey(), writer);
61      }
62      writer.append(cell);
63    }
64  
65    @Override
66    protected Collection<Writer> writers() {
67      return lowerBoundary2Writer.values();
68    }
69  
70    @Override
71    protected void preCommitWriters() throws IOException {
72      if (!needEmptyFile) {
73        return;
74      }
75      for (StoreFile.Writer writer : lowerBoundary2Writer.values()) {
76        if (writer != null) {
77          return;
78        }
79      }
80      // we haven't written out any data, create an empty file to retain metadata
81      lowerBoundary2Writer.put(lowerBoundary2Writer.firstKey(), writerFactory.createWriter());
82    }
83  }