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 junit.framework.TestCase;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.fs.FileSystem;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.KeyValue;
31  import org.apache.hadoop.hbase.KeyValue.Type;
32  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
33  import org.apache.hadoop.hbase.io.hfile.HFileContext;
34  import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
35  import org.apache.hadoop.hbase.regionserver.BloomType;
36  import org.apache.hadoop.hbase.regionserver.StoreFile;
37  import org.apache.hadoop.hbase.regionserver.StoreFileScanner;
38  import org.apache.hadoop.hbase.testclassification.SmallTests;
39  import org.apache.hadoop.hbase.util.Bytes;
40  import org.apache.hadoop.hbase.util.FSUtils;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(SmallTests.class)
45  public class TestMobFile extends TestCase {
46    static final Log LOG = LogFactory.getLog(TestMobFile.class);
47    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
48    private Configuration conf = TEST_UTIL.getConfiguration();
49    private CacheConfig cacheConf =  new CacheConfig(conf);
50    private final String TABLE = "tableName";
51    private final String FAMILY = "familyName";
52  
53    @Test
54    public void testReadKeyValue() throws Exception {
55      FileSystem fs = FileSystem.get(conf);
56  	Path testDir = FSUtils.getRootDir(conf);
57      Path outputDir = new Path(new Path(testDir, TABLE), FAMILY);
58      HFileContext meta = new HFileContextBuilder().withBlockSize(8*1024).build();
59      StoreFile.Writer writer = new StoreFile.WriterBuilder(conf, cacheConf, fs)
60              .withOutputDir(outputDir)
61              .withFileContext(meta)
62              .build();
63      String caseName = getName();
64      MobTestUtil.writeStoreFile(writer, caseName);
65  
66      MobFile mobFile = new MobFile(new StoreFile(fs, writer.getPath(),
67          conf, cacheConf, BloomType.NONE));
68      byte[] family = Bytes.toBytes(caseName);
69      byte[] qualify = Bytes.toBytes(caseName);
70  
71      // Test the start key
72      byte[] startKey = Bytes.toBytes("aa");  // The start key bytes
73      KeyValue expectedKey =
74          new KeyValue(startKey, family, qualify, Long.MAX_VALUE, Type.Put, startKey);
75      KeyValue seekKey = expectedKey.createKeyOnly(false);
76      Cell cell = mobFile.readCell(seekKey, false);
77      MobTestUtil.assertCellEquals(expectedKey, cell);
78  
79      // Test the end key
80      byte[] endKey = Bytes.toBytes("zz");  // The end key bytes
81      expectedKey = new KeyValue(endKey, family, qualify, Long.MAX_VALUE, Type.Put, endKey);
82      seekKey = expectedKey.createKeyOnly(false);
83      cell = mobFile.readCell(seekKey, false);
84      MobTestUtil.assertCellEquals(expectedKey, cell);
85  
86      // Test the random key
87      byte[] randomKey = Bytes.toBytes(MobTestUtil.generateRandomString(2));
88      expectedKey = new KeyValue(randomKey, family, qualify, Long.MAX_VALUE, Type.Put, randomKey);
89      seekKey = expectedKey.createKeyOnly(false);
90      cell = mobFile.readCell(seekKey, false);
91      MobTestUtil.assertCellEquals(expectedKey, cell);
92  
93      // Test the key which is less than the start key
94      byte[] lowerKey = Bytes.toBytes("a1"); // Smaller than "aa"
95      expectedKey = new KeyValue(startKey, family, qualify, Long.MAX_VALUE, Type.Put, startKey);
96      seekKey = new KeyValue(lowerKey, family, qualify, Long.MAX_VALUE, Type.Put, lowerKey);
97      cell = mobFile.readCell(seekKey, false);
98      MobTestUtil.assertCellEquals(expectedKey, cell);
99  
100     // Test the key which is more than the end key
101     byte[] upperKey = Bytes.toBytes("z{"); // Bigger than "zz"
102     seekKey = new KeyValue(upperKey, family, qualify, Long.MAX_VALUE, Type.Put, upperKey);
103     cell = mobFile.readCell(seekKey, false);
104     assertNull(cell);
105   }
106 
107   @Test
108   public void testGetScanner() throws Exception {
109     FileSystem fs = FileSystem.get(conf);
110     Path testDir = FSUtils.getRootDir(conf);
111     Path outputDir = new Path(new Path(testDir, TABLE), FAMILY);
112     HFileContext meta = new HFileContextBuilder().withBlockSize(8*1024).build();
113     StoreFile.Writer writer = new StoreFile.WriterBuilder(conf, cacheConf, fs)
114             .withOutputDir(outputDir)
115             .withFileContext(meta)
116             .build();
117     MobTestUtil.writeStoreFile(writer, getName());
118 
119     MobFile mobFile = new MobFile(new StoreFile(fs, writer.getPath(),
120         conf, cacheConf, BloomType.NONE));
121     assertNotNull(mobFile.getScanner());
122     assertTrue(mobFile.getScanner() instanceof StoreFileScanner);
123   }
124 }