/*
 * 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.querymatcher;

import static org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode.INCLUDE;
import static org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode.SEEK_NEXT_COL;
import static org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode.SKIP;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.KeepDeletedCells;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.KeyValue.Type;
import org.apache.hadoop.hbase.KeyValueUtil;
import org.apache.hadoop.hbase.regionserver.ScanInfo;
import org.apache.hadoop.hbase.regionserver.ScanType;
import org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode;
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Tag(RegionServerTests.TAG)
@Tag(SmallTests.TAG)
public class TestCompactionScanQueryMatcher extends AbstractTestScanQueryMatcher {

  private static final Logger LOG = LoggerFactory.getLogger(TestCompactionScanQueryMatcher.class);

  @Test
  public void testMatch_PartialRangeDropDeletes() throws Exception {
    // Some ranges.
    testDropDeletes(row2, row3, new byte[][] { row1, row2, row2, row3 }, INCLUDE, SKIP, SKIP,
      INCLUDE);
    testDropDeletes(row2, row3, new byte[][] { row1, row1, row2 }, INCLUDE, INCLUDE, SKIP);
    testDropDeletes(row2, row3, new byte[][] { row2, row3, row3 }, SKIP, INCLUDE, INCLUDE);
    testDropDeletes(row1, row3, new byte[][] { row1, row2, row3 }, SKIP, SKIP, INCLUDE);
    // Open ranges.
    testDropDeletes(HConstants.EMPTY_START_ROW, row3, new byte[][] { row1, row2, row3 }, SKIP, SKIP,
      INCLUDE);
    testDropDeletes(row2, HConstants.EMPTY_END_ROW, new byte[][] { row1, row2, row3 }, INCLUDE,
      SKIP, SKIP);
    testDropDeletes(HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW,
      new byte[][] { row1, row2, row3, row3 }, SKIP, SKIP, SKIP, SKIP);

    // No KVs in range.
    testDropDeletes(row2, row3, new byte[][] { row1, row1, row3 }, INCLUDE, INCLUDE, INCLUDE);
    testDropDeletes(row2, row3, new byte[][] { row3, row3 }, INCLUDE, INCLUDE);
    testDropDeletes(row2, row3, new byte[][] { row1, row1 }, INCLUDE, INCLUDE);
  }

  /**
   * Test redundant delete marker handling with COMPACT_RETAIN_DELETES. Cells are auto-generated
   * from the given types with decrementing timestamps.
   */
  @Test
  public void testSkipsRedundantDeleteMarkers() throws IOException {
    // Interleaved DeleteColumn + Put. First DC included, put triggers SEEK_NEXT_COL.
    assertRetainDeletes(new Type[] { Type.DeleteColumn, Type.Put, Type.DeleteColumn }, INCLUDE,
      SEEK_NEXT_COL);

    // Contiguous DeleteColumn. First included, rest redundant.
    assertRetainDeletes(new Type[] { Type.DeleteColumn, Type.DeleteColumn, Type.DeleteColumn },
      INCLUDE, SEEK_NEXT_COL, SEEK_NEXT_COL);

    // Contiguous DeleteFamily. First included, rest redundant.
    assertRetainDeletes(new Type[] { Type.DeleteFamily, Type.DeleteFamily, Type.DeleteFamily },
      INCLUDE, SEEK_NEXT_COL, SEEK_NEXT_COL);

    // DF + DFV interleaved. DF included, DFV redundant (SKIP because empty qualifier),
    // older DF redundant (SEEK_NEXT_COL), older DFV redundant (SKIP).
    assertRetainDeletes(new Type[] { Type.DeleteFamily, Type.DeleteFamilyVersion, Type.DeleteFamily,
      Type.DeleteFamilyVersion }, INCLUDE, SKIP, SEEK_NEXT_COL, SKIP);

    // Delete (version) covered by DeleteColumn.
    assertRetainDeletes(new Type[] { Type.DeleteColumn, Type.Delete, Type.Delete, Type.Delete },
      INCLUDE, SEEK_NEXT_COL, SEEK_NEXT_COL, SEEK_NEXT_COL);

    // KEEP_DELETED_CELLS=TRUE: all markers retained.
    assertRetainDeletes(KeepDeletedCells.TRUE,
      new Type[] { Type.DeleteColumn, Type.DeleteColumn, Type.DeleteColumn }, INCLUDE, INCLUDE,
      INCLUDE);
  }

  /**
   * Redundant column-level deletes with empty qualifier must not seek past a subsequent
   * DeleteFamily. getKeyForNextColumn treats empty qualifier as "no column" and returns
   * SEEK_NEXT_ROW, which would skip the DF and all remaining cells in the row.
   */
  @Test
  public void testEmptyQualifierDeleteDoesNotSkipDeleteFamily() throws IOException {
    byte[] emptyQualifier = HConstants.EMPTY_BYTE_ARRAY;

    // DC(empty) + DC(empty) redundant + DF must still be reachable.
    assertRetainDeletes(emptyQualifier,
      new Type[] { Type.DeleteColumn, Type.DeleteColumn, Type.DeleteFamily }, INCLUDE, SKIP,
      INCLUDE);

    // DC(empty) + Delete(empty) redundant + DF must still be reachable.
    assertRetainDeletes(emptyQualifier,
      new Type[] { Type.DeleteColumn, Type.Delete, Type.DeleteFamily }, INCLUDE, SKIP, INCLUDE);
  }

  private void assertRetainDeletes(Type[] types, MatchCode... expected) throws IOException {
    assertRetainDeletes(KeepDeletedCells.FALSE, types, expected);
  }

  private void assertRetainDeletes(byte[] qualifier, Type[] types, MatchCode... expected)
    throws IOException {
    assertRetainDeletes(KeepDeletedCells.FALSE, qualifier, types, expected);
  }

  /**
   * Build cells from the given types with decrementing timestamps (same ts for adjacent
   * family-level and column-level types at the same position). Family-level types (DeleteFamily,
   * DeleteFamilyVersion) use empty qualifier; others use col1.
   */
  private void assertRetainDeletes(KeepDeletedCells keepDeletedCells, Type[] types,
    MatchCode... expected) throws IOException {
    assertRetainDeletes(keepDeletedCells, null, types, expected);
  }

  /**
   * Build cells from the given types with decrementing timestamps. If qualifier is null,
   * family-level types use empty qualifier and others use col1. If qualifier is specified, all
   * types use that qualifier.
   */
  private void assertRetainDeletes(KeepDeletedCells keepDeletedCells, byte[] qualifier,
    Type[] types, MatchCode... expected) throws IOException {
    long now = EnvironmentEdgeManager.currentTime();
    ScanInfo scanInfo = new ScanInfo(this.conf, fam1, 0, 1, ttl, keepDeletedCells,
      HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false);
    CompactionScanQueryMatcher qm =
      CompactionScanQueryMatcher.create(scanInfo, ScanType.COMPACT_RETAIN_DELETES, 0L,
        HConstants.OLDEST_TIMESTAMP, HConstants.OLDEST_TIMESTAMP, now, null, null, null);
    qm.setToNewRow(KeyValueUtil.createFirstOnRow(row1));

    long ts = now;
    List<MatchCode> actual = new ArrayList<>(expected.length);
    for (int i = 0; i < types.length; i++) {
      byte[] qual;
      if (qualifier != null) {
        qual = qualifier;
      } else {
        boolean familyLevel = types[i] == Type.DeleteFamily || types[i] == Type.DeleteFamilyVersion;
        qual = familyLevel ? HConstants.EMPTY_BYTE_ARRAY : col1;
      }
      KeyValue kv = types[i] == Type.Put
        ? new KeyValue(row1, fam1, qual, ts, types[i], data)
        : new KeyValue(row1, fam1, qual, ts, types[i]);
      actual.add(qm.match(kv));
      if (actual.size() >= expected.length) {
        break;
      }
      // Decrement ts for next cell, but keep same ts when the next type has lower type code
      // at the same logical position (e.g. DF then DFV at the same timestamp).
      if (i + 1 < types.length && types[i + 1].getCode() < types[i].getCode()) {
        continue;
      }
      ts--;
    }
    for (int i = 0; i < expected.length; i++) {
      assertEquals(expected[i], actual.get(i), "Mismatch at index " + i);
    }
  }

  private void testDropDeletes(byte[] from, byte[] to, byte[][] rows, MatchCode... expected)
    throws IOException {
    long now = EnvironmentEdgeManager.currentTime();
    // Set time to purge deletes to negative value to avoid it ever happening.
    ScanInfo scanInfo = new ScanInfo(this.conf, fam2, 0, 1, ttl, KeepDeletedCells.FALSE,
      HConstants.DEFAULT_BLOCKSIZE, -1L, rowComparator, false);

    CompactionScanQueryMatcher qm =
      CompactionScanQueryMatcher.create(scanInfo, ScanType.COMPACT_RETAIN_DELETES, Long.MAX_VALUE,
        HConstants.OLDEST_TIMESTAMP, HConstants.OLDEST_TIMESTAMP, now, from, to, null);
    List<ScanQueryMatcher.MatchCode> actual = new ArrayList<>(rows.length);
    byte[] prevRow = null;
    for (byte[] row : rows) {
      if (prevRow == null || !Bytes.equals(prevRow, row)) {
        qm.setToNewRow(KeyValueUtil.createFirstOnRow(row));
        prevRow = row;
      }
      actual.add(qm.match(new KeyValue(row, fam2, null, now, Type.Delete)));
    }

    assertEquals(expected.length, actual.size());
    for (int i = 0; i < expected.length; i++) {
      LOG.debug("expected " + expected[i] + ", actual " + actual.get(i));
      assertEquals(expected[i], actual.get(i));
    }
  }
}
