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