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