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