1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
72 byte[] startKey = Bytes.toBytes("aa");
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
80 byte[] endKey = Bytes.toBytes("zz");
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
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
94 byte[] lowerKey = Bytes.toBytes("a1");
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
101 byte[] upperKey = Bytes.toBytes("z{");
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 }