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