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.mob;
019
020import java.util.Random;
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.HBaseTestingUtility;
023import org.apache.hadoop.hbase.HColumnDescriptor;
024import org.apache.hadoop.hbase.HTableDescriptor;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.client.ConnectionFactory;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.client.Scan;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.AfterClass;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040@Category(MediumTests.class)
041public class TestMobDataBlockEncoding {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045      HBaseClassTestRule.forClass(TestMobDataBlockEncoding.class);
046
047  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
048  private final static byte [] row1 = Bytes.toBytes("row1");
049  private final static byte [] family = Bytes.toBytes("family");
050  private final static byte [] qf1 = Bytes.toBytes("qualifier1");
051  private final static byte [] qf2 = Bytes.toBytes("qualifier2");
052  protected final byte[] qf3 = Bytes.toBytes("qualifier3");
053  private static Table table;
054  private static Admin admin;
055  private static HColumnDescriptor hcd;
056  private static HTableDescriptor desc;
057  private static Random random = new Random();
058  private static long defaultThreshold = 10;
059
060  @BeforeClass
061  public static void setUpBeforeClass() throws Exception {
062    TEST_UTIL.startMiniCluster(1);
063  }
064
065  @AfterClass
066  public static void tearDownAfterClass() throws Exception {
067    TEST_UTIL.shutdownMiniCluster();
068  }
069
070  public void setUp(long threshold, String TN, DataBlockEncoding encoding)
071      throws Exception {
072    desc = new HTableDescriptor(TableName.valueOf(TN));
073    hcd = new HColumnDescriptor(family);
074    hcd.setMobEnabled(true);
075    hcd.setMobThreshold(threshold);
076    hcd.setMaxVersions(4);
077    hcd.setDataBlockEncoding(encoding);
078    desc.addFamily(hcd);
079    admin = TEST_UTIL.getAdmin();
080    admin.createTable(desc);
081    table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())
082            .getTable(TableName.valueOf(TN));
083  }
084
085  /**
086   * Generate the mob value.
087   *
088   * @param size the size of the value
089   * @return the mob value generated
090   */
091  private static byte[] generateMobValue(int size) {
092    byte[] mobVal = new byte[size];
093    random.nextBytes(mobVal);
094    return mobVal;
095  }
096
097  @Test
098  public void testDataBlockEncoding() throws Exception {
099    for (DataBlockEncoding encoding : DataBlockEncoding.values()) {
100      testDataBlockEncoding(encoding);
101    }
102  }
103
104  public void testDataBlockEncoding(DataBlockEncoding encoding) throws Exception {
105    String TN = "testDataBlockEncoding" + encoding;
106    setUp(defaultThreshold, TN, encoding);
107    long ts1 = System.currentTimeMillis();
108    long ts2 = ts1 + 1;
109    long ts3 = ts1 + 2;
110    byte[] value = generateMobValue((int) defaultThreshold + 1);
111
112    Put put1 = new Put(row1);
113    put1.addColumn(family, qf1, ts3, value);
114    put1.addColumn(family, qf2, ts2, value);
115    put1.addColumn(family, qf3, ts1, value);
116    table.put(put1);
117    admin.flush(TableName.valueOf(TN));
118
119    Scan scan = new Scan();
120    scan.setMaxVersions(4);
121    MobTestUtil.assertCellsValue(table, scan, value, 3);
122  }
123}