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 static org.junit.Assert.assertNotNull; 021import static org.junit.Assert.assertNull; 022import static org.junit.Assert.assertTrue; 023 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.FileSystem; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.Cell; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseTestingUtility; 030import org.apache.hadoop.hbase.KeyValue; 031import org.apache.hadoop.hbase.KeyValue.Type; 032import org.apache.hadoop.hbase.io.hfile.CacheConfig; 033import org.apache.hadoop.hbase.io.hfile.HFileContext; 034import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder; 035import org.apache.hadoop.hbase.regionserver.BloomType; 036import org.apache.hadoop.hbase.regionserver.HStoreFile; 037import org.apache.hadoop.hbase.regionserver.StoreFileScanner; 038import org.apache.hadoop.hbase.regionserver.StoreFileWriter; 039import org.apache.hadoop.hbase.testclassification.SmallTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.junit.ClassRule; 042import org.junit.Rule; 043import org.junit.Test; 044import org.junit.experimental.categories.Category; 045import org.junit.rules.TestName; 046 047@Category(SmallTests.class) 048public class TestMobFile { 049 050 @ClassRule 051 public static final HBaseClassTestRule CLASS_RULE = 052 HBaseClassTestRule.forClass(TestMobFile.class); 053 054 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 055 private Configuration conf = TEST_UTIL.getConfiguration(); 056 private CacheConfig cacheConf = new CacheConfig(conf); 057 @Rule 058 public TestName testName = new TestName(); 059 060 @Test 061 public void testReadKeyValue() throws Exception { 062 Path testDir = TEST_UTIL.getDataTestDir(); 063 FileSystem fs = testDir.getFileSystem(conf); 064 HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).build(); 065 StoreFileWriter writer = new StoreFileWriter.Builder(conf, cacheConf, fs).withOutputDir(testDir) 066 .withFileContext(meta).build(); 067 String caseName = testName.getMethodName(); 068 MobTestUtil.writeStoreFile(writer, caseName); 069 070 MobFile mobFile = 071 new MobFile(new HStoreFile(fs, writer.getPath(), conf, cacheConf, BloomType.NONE, true)); 072 byte[] family = Bytes.toBytes(caseName); 073 byte[] qualify = Bytes.toBytes(caseName); 074 075 // Test the start key 076 byte[] startKey = Bytes.toBytes("aa"); // The start key bytes 077 KeyValue expectedKey = 078 new KeyValue(startKey, family, qualify, Long.MAX_VALUE, Type.Put, startKey); 079 KeyValue seekKey = expectedKey.createKeyOnly(false); 080 Cell cell = mobFile.readCell(seekKey, false).getCell(); 081 MobTestUtil.assertCellEquals(expectedKey, cell); 082 083 // Test the end key 084 byte[] endKey = Bytes.toBytes("zz"); // The end key bytes 085 expectedKey = new KeyValue(endKey, family, qualify, Long.MAX_VALUE, Type.Put, endKey); 086 seekKey = expectedKey.createKeyOnly(false); 087 cell = mobFile.readCell(seekKey, false).getCell(); 088 MobTestUtil.assertCellEquals(expectedKey, cell); 089 090 // Test the random key 091 byte[] randomKey = Bytes.toBytes(MobTestUtil.generateRandomString(2)); 092 expectedKey = new KeyValue(randomKey, family, qualify, Long.MAX_VALUE, Type.Put, randomKey); 093 seekKey = expectedKey.createKeyOnly(false); 094 cell = mobFile.readCell(seekKey, false).getCell(); 095 MobTestUtil.assertCellEquals(expectedKey, cell); 096 097 // Test the key which is less than the start key 098 byte[] lowerKey = Bytes.toBytes("a1"); // Smaller than "aa" 099 expectedKey = new KeyValue(startKey, family, qualify, Long.MAX_VALUE, Type.Put, startKey); 100 seekKey = new KeyValue(lowerKey, family, qualify, Long.MAX_VALUE, Type.Put, lowerKey); 101 cell = mobFile.readCell(seekKey, false).getCell(); 102 MobTestUtil.assertCellEquals(expectedKey, cell); 103 104 // Test the key which is more than the end key 105 byte[] upperKey = Bytes.toBytes("z{"); // Bigger than "zz" 106 seekKey = new KeyValue(upperKey, family, qualify, Long.MAX_VALUE, Type.Put, upperKey); 107 assertNull(mobFile.readCell(seekKey, false)); 108 } 109 110 @Test 111 public void testGetScanner() throws Exception { 112 Path testDir = TEST_UTIL.getDataTestDir(); 113 FileSystem fs = testDir.getFileSystem(conf); 114 HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).build(); 115 StoreFileWriter writer = new StoreFileWriter.Builder(conf, cacheConf, fs).withOutputDir(testDir) 116 .withFileContext(meta).build(); 117 MobTestUtil.writeStoreFile(writer, testName.getMethodName()); 118 119 MobFile mobFile = 120 new MobFile(new HStoreFile(fs, writer.getPath(), conf, cacheConf, BloomType.NONE, true)); 121 assertNotNull(mobFile.getScanner()); 122 assertTrue(mobFile.getScanner() instanceof StoreFileScanner); 123 } 124}