1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.io;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.fs.FileSystem;
32 import org.apache.hadoop.fs.Path;
33 import org.apache.hadoop.hbase.HBaseTestingUtility;
34 import org.apache.hadoop.hbase.KeyValue;
35 import org.apache.hadoop.hbase.SmallTests;
36 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
37 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
38 import org.apache.hadoop.hbase.io.hfile.HFile;
39 import org.apache.hadoop.hbase.io.hfile.HFileScanner;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.junit.AfterClass;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.junit.experimental.categories.Category;
45
46 @Category(SmallTests.class)
47 public class TestHalfStoreFileReader {
48 private static HBaseTestingUtility TEST_UTIL;
49
50 @BeforeClass
51 public static void setupBeforeClass() throws Exception {
52 TEST_UTIL = new HBaseTestingUtility();
53 }
54
55 @AfterClass
56 public static void tearDownAfterClass() throws Exception {
57 TEST_UTIL.cleanupTestDir();
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 @Test
77 public void testHalfScanAndReseek() throws IOException {
78 String root_dir = TEST_UTIL.getDataTestDir().toString();
79 Path p = new Path(root_dir, "test");
80
81 Configuration conf = TEST_UTIL.getConfiguration();
82 FileSystem fs = FileSystem.get(conf);
83 CacheConfig cacheConf = new CacheConfig(conf);
84
85 HFile.Writer w = HFile.getWriterFactory(conf, cacheConf)
86 .withPath(fs, p)
87 .withBlockSize(1024)
88 .create();
89
90
91 List<KeyValue> items = genSomeKeys();
92 for (KeyValue kv : items) {
93 w.append(kv);
94 }
95 w.close();
96
97 HFile.Reader r = HFile.createReader(fs, p, cacheConf);
98 r.loadFileInfo();
99 byte [] midkey = r.midkey();
100 KeyValue midKV = KeyValue.createKeyValueFromKey(midkey);
101 midkey = midKV.getRow();
102
103
104
105 Reference bottom = new Reference(midkey, Reference.Range.bottom);
106 doTestOfScanAndReseek(p, fs, bottom, cacheConf);
107
108 Reference top = new Reference(midkey, Reference.Range.top);
109 doTestOfScanAndReseek(p, fs, top, cacheConf);
110
111 r.close();
112 }
113
114 private void doTestOfScanAndReseek(Path p, FileSystem fs, Reference bottom,
115 CacheConfig cacheConf)
116 throws IOException {
117 final HalfStoreFileReader halfreader = new HalfStoreFileReader(fs, p,
118 cacheConf, bottom);
119 halfreader.loadFileInfo();
120 final HFileScanner scanner = halfreader.getScanner(false, false);
121
122 scanner.seekTo();
123 KeyValue curr;
124 do {
125 curr = scanner.getKeyValue();
126 KeyValue reseekKv =
127 getLastOnCol(curr);
128 int ret = scanner.reseekTo(reseekKv.getKey());
129 assertTrue("reseek to returned: " + ret, ret > 0);
130
131 } while (scanner.next());
132
133 int ret = scanner.reseekTo(getLastOnCol(curr).getKey());
134
135 assertTrue( ret > 0 );
136
137 halfreader.close(true);
138 }
139
140
141
142 @Test
143 public void testHalfScanner() throws IOException {
144 String root_dir = TEST_UTIL.getDataTestDir().toString();
145 Path p = new Path(root_dir, "test");
146 Configuration conf = TEST_UTIL.getConfiguration();
147 FileSystem fs = FileSystem.get(conf);
148 CacheConfig cacheConf = new CacheConfig(conf);
149
150 HFile.Writer w = HFile.getWriterFactory(conf, cacheConf)
151 .withPath(fs, p)
152 .withBlockSize(1024)
153 .create();
154
155
156 List<KeyValue> items = genSomeKeys();
157 for (KeyValue kv : items) {
158 w.append(kv);
159 }
160 w.close();
161
162
163 HFile.Reader r = HFile.createReader(fs, p, cacheConf);
164 r.loadFileInfo();
165 byte[] midkey = r.midkey();
166 KeyValue midKV = KeyValue.createKeyValueFromKey(midkey);
167 midkey = midKV.getRow();
168
169 Reference bottom = new Reference(midkey, Reference.Range.bottom);
170 Reference top = new Reference(midkey, Reference.Range.top);
171
172
173 KeyValue beforeMidKey = null;
174 for (KeyValue item : items) {
175 if (item.equals(midKV)) {
176 break;
177 }
178 beforeMidKey = item;
179 }
180
181
182
183 KeyValue foundKeyValue = doTestOfSeekBefore(p, fs, bottom, midKV, cacheConf);
184 assertEquals(beforeMidKey, foundKeyValue);
185
186
187 foundKeyValue = doTestOfSeekBefore(p, fs, top, items.get(items.size() - 1), cacheConf);
188 assertEquals(items.get(items.size() - 2), foundKeyValue);
189
190 foundKeyValue = doTestOfSeekBefore(p, fs, bottom, items.get(items.size() - 1), cacheConf);
191 assertEquals(beforeMidKey, foundKeyValue);
192
193
194 foundKeyValue = doTestOfSeekBefore(p, fs, top, items.get(0), cacheConf);
195 assertNull(foundKeyValue);
196
197
198 foundKeyValue = doTestOfSeekBefore(p, fs, bottom, items.get(0), cacheConf);
199 assertNull(foundKeyValue);
200
201
202 foundKeyValue = doTestOfSeekBefore(p, fs, top, items.get(1), cacheConf);
203 assertNull(foundKeyValue);
204
205 foundKeyValue = doTestOfSeekBefore(p, fs, bottom, items.get(1), cacheConf);
206 assertEquals(items.get(0), foundKeyValue);
207
208
209 foundKeyValue = doTestOfSeekBefore(p, fs, top, midKV, cacheConf);
210 assertNull(foundKeyValue);
211 }
212
213 private KeyValue doTestOfSeekBefore(Path p, FileSystem fs, Reference bottom, KeyValue seekBefore,
214 CacheConfig cacheConfig)
215 throws IOException {
216 final HalfStoreFileReader halfreader = new HalfStoreFileReader(fs, p,
217 cacheConfig, bottom);
218 halfreader.loadFileInfo();
219 final HFileScanner scanner = halfreader.getScanner(false, false);
220 scanner.seekBefore(seekBefore.getKey());
221 return scanner.getKeyValue();
222 }
223
224 private KeyValue getLastOnCol(KeyValue curr) {
225 return KeyValue.createLastOnRow(
226 curr.getBuffer(), curr.getRowOffset(), curr.getRowLength(),
227 curr.getBuffer(), curr.getFamilyOffset(), curr.getFamilyLength(),
228 curr.getBuffer(), curr.getQualifierOffset(), curr.getQualifierLength());
229 }
230
231 static final int SIZE = 1000;
232
233 static byte[] _b(String s) {
234 return Bytes.toBytes(s);
235 }
236
237 List<KeyValue> genSomeKeys() {
238 List<KeyValue> ret = new ArrayList<KeyValue>(SIZE);
239 for (int i = 0; i < SIZE; i++) {
240 KeyValue kv =
241 new KeyValue(
242 _b(String.format("row_%04d", i)),
243 _b("family"),
244 _b("qualifier"),
245 1000,
246 _b("value"));
247 ret.add(kv);
248 }
249 return ret;
250 }
251
252
253
254 }
255