View Javadoc

1   /**
2   *
3   * Licensed to the Apache Software Foundation (ASF) under one
4   * or more contributor license agreements.  See the NOTICE file
5   * distributed with this work for additional information
6   * regarding copyright ownership.  The ASF licenses this file
7   * to you under the Apache License, Version 2.0 (the
8   * "License"); you may not use this file except in compliance
9   * with the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19  package org.apache.hadoop.hbase.mob;
20  
21  import java.util.List;
22  import java.util.Random;
23  
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.CellUtil;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HColumnDescriptor;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.client.*;
31  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
32  import org.apache.hadoop.hbase.testclassification.MediumTests;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.junit.AfterClass;
35  import org.junit.Assert;
36  import org.junit.BeforeClass;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  @Category(MediumTests.class)
41  public class TestMobDataBlockEncoding {
42  
43    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
44    private final static byte [] row1 = Bytes.toBytes("row1");
45    private final static byte [] family = Bytes.toBytes("family");
46    private final static byte [] qf1 = Bytes.toBytes("qualifier1");
47    private final static byte [] qf2 = Bytes.toBytes("qualifier2");
48    protected final byte[] qf3 = Bytes.toBytes("qualifier3");
49    private static Table table;
50    private static HBaseAdmin admin;
51    private static HColumnDescriptor hcd;
52    private static HTableDescriptor desc;
53    private static Random random = new Random();
54    private static long defaultThreshold = 10;
55  
56    @BeforeClass
57    public static void setUpBeforeClass() throws Exception {
58      TEST_UTIL.getConfiguration().setInt("hbase.master.info.port", 0);
59      TEST_UTIL.getConfiguration().setBoolean("hbase.regionserver.info.port.auto", true);
60  
61      TEST_UTIL.startMiniCluster(1);
62    }
63  
64    @AfterClass
65    public static void tearDownAfterClass() throws Exception {
66      TEST_UTIL.shutdownMiniCluster();
67    }
68  
69    public void setUp(long threshold, String TN, DataBlockEncoding encoding)
70        throws Exception {
71      desc = new HTableDescriptor(TableName.valueOf(TN));
72      hcd = new HColumnDescriptor(family);
73      hcd.setMobEnabled(true);
74      hcd.setMobThreshold(threshold);
75      hcd.setMaxVersions(4);
76      hcd.setDataBlockEncoding(encoding);
77      desc.addFamily(hcd);
78      admin = TEST_UTIL.getHBaseAdmin();
79      admin.createTable(desc);
80      table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())
81              .getTable(TableName.valueOf(TN));
82    }
83  
84    /**
85     * Generate the mob value.
86     *
87     * @param size the size of the value
88     * @return the mob value generated
89     */
90    private static byte[] generateMobValue(int size) {
91      byte[] mobVal = new byte[size];
92      random.nextBytes(mobVal);
93      return mobVal;
94    }
95  
96    @Test
97    public void testDataBlockEncoding() throws Exception {
98      for (DataBlockEncoding encoding : DataBlockEncoding.values()) {
99        testDataBlockEncoding(encoding);
100     }
101   }
102 
103   public void testDataBlockEncoding(DataBlockEncoding encoding) throws Exception {
104     String TN = "testDataBlockEncoding" + encoding;
105     setUp(defaultThreshold, TN, encoding);
106     long ts1 = System.currentTimeMillis();
107     long ts2 = ts1 + 1;
108     long ts3 = ts1 + 2;
109     byte[] value = generateMobValue((int) defaultThreshold + 1);
110 
111     Put put1 = new Put(row1);
112     put1.addColumn(family, qf1, ts3, value);
113     put1.addColumn(family, qf2, ts2, value);
114     put1.addColumn(family, qf3, ts1, value);
115     table.put(put1);
116     admin.flush(TableName.valueOf(TN));
117 
118     Scan scan = new Scan();
119     scan.setMaxVersions(4);
120 
121     ResultScanner results = table.getScanner(scan);
122     int count = 0;
123     for (Result res : results) {
124       List<Cell> cells = res.listCells();
125       for(Cell cell : cells) {
126         // Verify the value
127         Assert.assertEquals(Bytes.toString(value),
128             Bytes.toString(CellUtil.cloneValue(cell)));
129         count++;
130       }
131     }
132     results.close();
133     Assert.assertEquals(3, count);
134   }
135 }