Package org.apache.hadoop.hbase.coprocessor

Source Code of org.apache.hadoop.hbase.coprocessor.TestHTableWrapper

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Coprocessor;
import org.apache.hadoop.hbase.CoprocessorEnvironment;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.MediumTests;
import org.apache.hadoop.hbase.client.Append;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Increment;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Row;
import org.apache.hadoop.hbase.client.RowMutations;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.VersionInfo;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import static org.junit.Assert.*;

/**
* Tests class {@link org.apache.hadoop.hbase.coprocessor.CoprocessorHost.Environment.HTableWrapper}
* by invoking its methods and briefly asserting the result is reasonable.
*/
@Category(MediumTests.class)
public class TestHTableWrapper {

  private static final HBaseTestingUtility util = new HBaseTestingUtility();

  private static final byte[] TEST_TABLE = Bytes.toBytes("test");
  private static final byte[] TEST_FAMILY = Bytes.toBytes("f1");

  private static final byte[] ROW_A = Bytes.toBytes("aaa");
  private static final byte[] ROW_B = Bytes.toBytes("bbb");
  private static final byte[] ROW_C = Bytes.toBytes("ccc");
  private static final byte[] ROW_D = Bytes.toBytes("ddd");
  private static final byte[] ROW_E = Bytes.toBytes("eee");

  private static final byte[] qualifierCol1 = Bytes.toBytes("col1");

  private static final byte[] bytes1 = Bytes.toBytes(1);
  private static final byte[] bytes2 = Bytes.toBytes(2);
  private static final byte[] bytes3 = Bytes.toBytes(3);
  private static final byte[] bytes4 = Bytes.toBytes(4);
  private static final byte[] bytes5 = Bytes.toBytes(5);

  static class DummyRegionObserver extends BaseRegionObserver {
  }

  private HTableInterface hTableInterface;
  private HTable table;

  @BeforeClass
  public static void setupBeforeClass() throws Exception {
    util.startMiniCluster();
  }

  @AfterClass
  public static void tearDownAfterClass() throws Exception {
    util.shutdownMiniCluster();
  }

  @Before
  public void before() throws Exception {
    table = util.createTable(TEST_TABLE, TEST_FAMILY);

    Put puta = new Put(ROW_A);
    puta.add(TEST_FAMILY, qualifierCol1, bytes1);
    table.put(puta);

    Put putb = new Put(ROW_B);
    putb.add(TEST_FAMILY, qualifierCol1, bytes2);
    table.put(putb);

    Put putc = new Put(ROW_C);
    putc.add(TEST_FAMILY, qualifierCol1, bytes3);
    table.put(putc);
  }

  @After
  public void after() throws Exception {
    try {
      if (table != null) {
        table.close();
      }
    } finally {
      util.deleteTable(TEST_TABLE);
    }
  }

  @Test
  public void testHTableInterfaceMethods() throws Exception {
    Configuration conf = util.getConfiguration();
    MasterCoprocessorHost cpHost = util.getMiniHBaseCluster().getMaster().getCoprocessorHost();
    Class<?> implClazz = DummyRegionObserver.class;
    cpHost.load(implClazz, Coprocessor.PRIORITY_HIGHEST, conf);
    CoprocessorEnvironment env = cpHost.findCoprocessorEnvironment(implClazz.getName());
    assertEquals(Coprocessor.VERSION, env.getVersion());
    assertEquals(VersionInfo.getVersion(), env.getHBaseVersion());
    hTableInterface = env.getTable(TEST_TABLE);
    checkHTableInterfaceMethods();
    cpHost.shutdown(env);
  }

  private void checkHTableInterfaceMethods() throws Exception {
    checkConf();
    checkNameAndDescriptor();
    checkAutoFlush();
    checkBufferSize();
    checkExists();
    checkGetRowOrBefore();
    checkAppend();
    checkPutsAndDeletes();
    checkCheckAndPut();
    checkCheckAndDelete();
    checkIncrementColumnValue();
    checkIncrement();
    checkBatch();
    checkMutateRow();
    checkResultScanner();

    hTableInterface.flushCommits();
    hTableInterface.close();
  }

  private void checkConf() {
    Configuration confExpected = util.getConfiguration();
    Configuration confActual = hTableInterface.getConfiguration();
    assertTrue(confExpected == confActual);
  }

  private void checkNameAndDescriptor() throws IOException {
    assertArrayEquals(TEST_TABLE, hTableInterface.getTableName());
    assertEquals(table.getTableDescriptor(), hTableInterface.getTableDescriptor());
  }

  private void checkAutoFlush() {
    boolean initialAutoFlush = hTableInterface.isAutoFlush();
    hTableInterface.setAutoFlush(false);
    assertFalse(hTableInterface.isAutoFlush());
    hTableInterface.setAutoFlush(true, true);
    assertTrue(hTableInterface.isAutoFlush());
    hTableInterface.setAutoFlush(initialAutoFlush);
  }

  private void checkBufferSize() throws IOException {
    long initialWriteBufferSize = hTableInterface.getWriteBufferSize();
    hTableInterface.setWriteBufferSize(12345L);
    assertEquals(12345L, hTableInterface.getWriteBufferSize());
    hTableInterface.setWriteBufferSize(initialWriteBufferSize);
  }

  private void checkExists() throws IOException {
    boolean ex = hTableInterface.exists(new Get(ROW_A).addColumn(TEST_FAMILY, qualifierCol1));
    assertTrue(ex);
  }

  @SuppressWarnings("deprecation")
  private void checkGetRowOrBefore() throws IOException {
    Result rowOrBeforeResult = hTableInterface.getRowOrBefore(ROW_A, TEST_FAMILY);
    assertArrayEquals(ROW_A, rowOrBeforeResult.getRow());
  }

  private void checkAppend() throws IOException {
    final byte[] appendValue = Bytes.toBytes("append");
    Append append = new Append(qualifierCol1).add(TEST_FAMILY, qualifierCol1, appendValue);
    Result appendResult = hTableInterface.append(append);
    byte[] appendedRow = appendResult.getRow();
    checkRowValue(appendedRow, appendValue);
  }

  private void checkPutsAndDeletes() throws IOException {
    // put:
    Put putD = new Put(ROW_D).add(TEST_FAMILY, qualifierCol1, bytes2);
    hTableInterface.put(putD);
    checkRowValue(ROW_D, bytes2);

    // delete:
    Delete delete = new Delete(ROW_D);
    hTableInterface.delete(delete);
    checkRowValue(ROW_D, null);

    // multiple puts:
    Put[] puts = new Put[] { new Put(ROW_D).add(TEST_FAMILY, qualifierCol1, bytes2),
        new Put(ROW_E).add(TEST_FAMILY, qualifierCol1, bytes3) };
    hTableInterface.put(Arrays.asList(puts));
    checkRowsValues(new byte[][] { ROW_D, ROW_E }, new byte[][] { bytes2, bytes3 });

    // multiple deletes:
    Delete[] deletes = new Delete[] { new Delete(ROW_D), new Delete(ROW_E) };
    hTableInterface.delete(new ArrayList<Delete>(Arrays.asList(deletes)));
    checkRowsValues(new byte[][] { ROW_D, ROW_E }, new byte[][] { null, null });
  }

  private void checkCheckAndPut() throws IOException {
    Put putC = new Put(ROW_C).add(TEST_FAMILY, qualifierCol1, bytes5);
  
TOP

Related Classes of org.apache.hadoop.hbase.coprocessor.TestHTableWrapper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.