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