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.coprocessor.example;
019
020import static org.junit.jupiter.api.Assertions.assertArrayEquals;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.junit.jupiter.api.Assertions.assertNull;
024
025import java.io.IOException;
026import java.util.ArrayList;
027import java.util.List;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.Result;
034import org.apache.hadoop.hbase.client.ResultScanner;
035import org.apache.hadoop.hbase.client.Scan;
036import org.apache.hadoop.hbase.client.Table;
037import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
038import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
039import org.apache.hadoop.hbase.testclassification.MediumTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.junit.jupiter.api.AfterAll;
042import org.junit.jupiter.api.BeforeAll;
043import org.junit.jupiter.api.Tag;
044import org.junit.jupiter.api.Test;
045
046@Tag(CoprocessorTests.TAG)
047@Tag(MediumTests.TAG)
048public class TestValueReplacingCompaction {
049
050  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
051  private static final TableName NAME = TableName.valueOf("TestValueReplacement");
052  private static final byte[] FAMILY = Bytes.toBytes("f");
053  private static final byte[] QUALIFIER = Bytes.toBytes("q");
054  private static final ColumnFamilyDescriptor CFD =
055    ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build();
056  private static final int NUM_ROWS = 5;
057  private static final String value = "foo";
058  private static final String replacedValue = "bar";
059
060  @BeforeAll
061  public static void setUp() throws Exception {
062    UTIL.startMiniCluster(1);
063    UTIL.getAdmin()
064      .createTable(TableDescriptorBuilder.newBuilder(NAME)
065        .setCoprocessor(ValueRewritingObserver.class.getName())
066        .setValue(ValueRewritingObserver.ORIGINAL_VALUE_KEY, value)
067        .setValue(ValueRewritingObserver.REPLACED_VALUE_KEY, replacedValue).setColumnFamily(CFD)
068        .build());
069  }
070
071  @AfterAll
072  public static void tearDown() throws Exception {
073    UTIL.shutdownMiniCluster();
074  }
075
076  private void writeData(Table t) throws IOException {
077    List<Put> puts = new ArrayList<>(NUM_ROWS);
078    for (int i = 0; i < NUM_ROWS; i++) {
079      Put p = new Put(Bytes.toBytes(i + 1));
080      p.addColumn(FAMILY, QUALIFIER, Bytes.toBytes(value));
081      puts.add(p);
082    }
083    t.put(puts);
084  }
085
086  @Test
087  public void test() throws IOException, InterruptedException {
088    try (Table t = UTIL.getConnection().getTable(NAME)) {
089      writeData(t);
090
091      // Flush the data
092      UTIL.flush(NAME);
093      // Issue a compaction
094      UTIL.compact(NAME, true);
095
096      Scan s = new Scan();
097      s.addColumn(FAMILY, QUALIFIER);
098
099      try (ResultScanner scanner = t.getScanner(s)) {
100        for (int i = 0; i < NUM_ROWS; i++) {
101          Result result = scanner.next();
102          assertNotNull(result, "The " + (i + 1) + "th result was unexpectedly null");
103          assertEquals(1, result.getFamilyMap(FAMILY).size());
104          assertArrayEquals(Bytes.toBytes(i + 1), result.getRow());
105          assertArrayEquals(Bytes.toBytes(replacedValue), result.getValue(FAMILY, QUALIFIER));
106        }
107        assertNull(scanner.next());
108      }
109    }
110  }
111}