virajjasani commented on code in PR #5545: URL: https://github.com/apache/hbase/pull/5545#discussion_r1443550627
########## hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/DualFileWriter.java: ########## @@ -0,0 +1,253 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.regionserver; + +import static org.apache.hadoop.hbase.regionserver.HStoreFile.HAS_LIVE_VERSIONS_KEY; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.CellComparator; +import org.apache.hadoop.hbase.CellUtil; +import org.apache.hadoop.hbase.io.hfile.HFile; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * Separates the provided cells into two files, one file for the live cells and the other for the + * rest of the cells (historical cells). The live cells includes the live put cells, delete all and + * version delete markers that are not masked by other delete all markers. + */ [email protected] +public class DualFileWriter extends AbstractMultiFileWriter { + + private final CellComparator comparator; + private StoreFileWriter liveVersionWriter; + private StoreFileWriter historicalVersionWriter; + + private final List<StoreFileWriter> writers; + // The last cell of the current row + private Cell lastCell; + // The first (latest) delete family marker of the current row + private Cell deleteFamily; + // The list of delete family version markers of the current row + private List<Cell> deleteFamilyVersionList = new ArrayList<>(); + // The first (latest) delete column marker of the current column + private Cell deleteColumn; + // The list of delete column version markers of the current column + private List<Cell> deleteColumnVersionList = new ArrayList<>(); + // The live put cell count for the current column + private int livePutCellCount; + private final boolean dualWriterEnabled; + private final int maxVersions; + private final boolean newVersionBehavior; + + public DualFileWriter(CellComparator comparator, int maxVersions, boolean dualWriterEnabled, + boolean newVersionBehavior) { + this.comparator = comparator; + this.maxVersions = maxVersions; + this.dualWriterEnabled = dualWriterEnabled; + this.newVersionBehavior = newVersionBehavior; + writers = new ArrayList<>(2); + initRowState(); + } + + private void initRowState() { + deleteFamily = null; + deleteFamilyVersionList.clear(); + lastCell = null; + } + + private void initColumnState() { + livePutCellCount = 0; + deleteColumn = null; + deleteColumnVersionList.clear(); + + } + + private void addLiveVersion(Cell cell) throws IOException { + if (liveVersionWriter == null) { + liveVersionWriter = writerFactory.createWriter(); + writers.add(liveVersionWriter); + } + liveVersionWriter.append(cell); + } + + private void addHistoricalVersion(Cell cell) throws IOException { + if (historicalVersionWriter == null) { + historicalVersionWriter = writerFactory.createWriter(); + writers.add(historicalVersionWriter); + } + historicalVersionWriter.append(cell); + } + + private boolean isDeletedByDeleteFamily(Cell cell) { + return deleteFamily != null && (deleteFamily.getTimestamp() > cell.getTimestamp() + || (deleteFamily.getTimestamp() == cell.getTimestamp() + && (!newVersionBehavior || cell.getSequenceId() < deleteFamily.getSequenceId()))); + } + + private boolean isDeletedByDeleteFamilyVersion(Cell cell) { + for (Cell deleteFamilyVersion : deleteFamilyVersionList) { + if ( + deleteFamilyVersion.getTimestamp() == cell.getTimestamp() + && (!newVersionBehavior || cell.getSequenceId() < deleteFamilyVersion.getSequenceId()) + ) return true; + } + return false; + } + + private boolean isDeletedByDeleteColumn(Cell cell) { + return deleteColumn != null && (deleteColumn.getTimestamp() > cell.getTimestamp() + || (deleteColumn.getTimestamp() == cell.getTimestamp() + && (!newVersionBehavior || cell.getSequenceId() < deleteColumn.getSequenceId()))); + } + + private boolean isDeletedByDeleteColumnVersion(Cell cell) { + for (Cell deleteColumnVersion : deleteColumnVersionList) { + if ( + deleteColumnVersion.getTimestamp() == cell.getTimestamp() + && (!newVersionBehavior || cell.getSequenceId() < deleteColumnVersion.getSequenceId()) + ) return true; + } + return false; + } + + private boolean isDeleted(Cell cell) { + return isDeletedByDeleteFamily(cell) || isDeletedByDeleteColumn(cell) + || isDeletedByDeleteFamilyVersion(cell) || isDeletedByDeleteColumnVersion(cell); + } + + private void appendCell(Cell cell) throws IOException { + if ((lastCell == null || !CellUtil.matchingColumn(lastCell, cell))) { + initColumnState(); + } + if (cell.getType() == Cell.Type.DeleteFamily) { + if (deleteFamily == null) { + deleteFamily = cell; + addLiveVersion(cell); + } else { + addHistoricalVersion(cell); + } + } else if (cell.getType() == Cell.Type.DeleteFamilyVersion) { + if (!isDeletedByDeleteFamily(cell)) { Review Comment: Just in case, if DeleteFamily and DeleteFamilyVersion both have same timestamp (if at all possible), maybe this could be better handled with `if (!isDeletedByDeleteFamily(cell) && deleteFamily == null)`? Although from data correctness, this is likely not an issue. It's just to protect DeleteFamilyVersion from going to live version file if DeleteFamily is already present in live version file with same timestamp. Maybe it's just a corner case. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
