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 org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.HBaseTestingUtility;
022import org.apache.hadoop.hbase.HColumnDescriptor;
023import org.apache.hadoop.hbase.HTableDescriptor;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Admin;
026import org.apache.hadoop.hbase.client.ConnectionFactory;
027import org.apache.hadoop.hbase.client.Put;
028import org.apache.hadoop.hbase.client.Scan;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
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 long defaultThreshold = 10;
058
059  @BeforeClass
060  public static void setUpBeforeClass() throws Exception {
061    TEST_UTIL.startMiniCluster(1);
062  }
063
064  @AfterClass
065  public static void tearDownAfterClass() throws Exception {
066    TEST_UTIL.shutdownMiniCluster();
067  }
068
069  public void setUp(long threshold, String TN, DataBlockEncoding encoding) throws Exception {
070    desc = new HTableDescriptor(TableName.valueOf(TN));
071    hcd = new HColumnDescriptor(family);
072    hcd.setMobEnabled(true);
073    hcd.setMobThreshold(threshold);
074    hcd.setMaxVersions(4);
075    hcd.setDataBlockEncoding(encoding);
076    desc.addFamily(hcd);
077    admin = TEST_UTIL.getAdmin();
078    admin.createTable(desc);
079    table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())
080      .getTable(TableName.valueOf(TN));
081  }
082
083  /**
084   * Generate the mob value.
085   * @param size the size of the value
086   * @return the mob value generated
087   */
088  private static byte[] generateMobValue(int size) {
089    byte[] mobVal = new byte[size];
090    Bytes.random(mobVal);
091    return mobVal;
092  }
093
094  @Test
095  public void testDataBlockEncoding() throws Exception {
096    for (DataBlockEncoding encoding : DataBlockEncoding.values()) {
097      testDataBlockEncoding(encoding);
098    }
099  }
100
101  public void testDataBlockEncoding(DataBlockEncoding encoding) throws Exception {
102    String TN = "testDataBlockEncoding" + encoding;
103    setUp(defaultThreshold, TN, encoding);
104    long ts1 = EnvironmentEdgeManager.currentTime();
105    long ts2 = ts1 + 1;
106    long ts3 = ts1 + 2;
107    byte[] value = generateMobValue((int) defaultThreshold + 1);
108
109    Put put1 = new Put(row1);
110    put1.addColumn(family, qf1, ts3, value);
111    put1.addColumn(family, qf2, ts2, value);
112    put1.addColumn(family, qf3, ts1, value);
113    table.put(put1);
114    admin.flush(TableName.valueOf(TN));
115
116    Scan scan = new Scan();
117    scan.setMaxVersions(4);
118    MobTestUtil.assertCellsValue(table, scan, value, 3);
119  }
120}