1   /*
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.io.hfile;
22  
23  import java.io.IOException;
24  import java.net.URL;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  import org.junit.Before;
33  import org.junit.Test;
34  
35  import static org.junit.Assert.*;
36  
37  public class TestHFileReaderV1 {
38  
39    private static final HBaseTestingUtility TEST_UTIL =
40      new HBaseTestingUtility();
41  
42    private Configuration conf;
43    private FileSystem fs;
44  
45    private static final int N = 1000;
46  
47    @Before
48    public void setUp() throws IOException {
49      conf = TEST_UTIL.getConfiguration();
50      fs = FileSystem.get(conf);
51    }
52  
53    @Test
54    public void testReadingExistingVersion1HFile() throws IOException {
55      URL url = TestHFileReaderV1.class.getResource(
56          "8e8ab58dcf39412da19833fcd8f687ac");
57      Path existingHFilePath = new Path(url.getPath());
58      HFile.Reader reader =
59        HFile.createReader(fs, existingHFilePath, new CacheConfig(conf));
60      reader.loadFileInfo();
61      FixedFileTrailer trailer = reader.getTrailer();
62  
63      assertEquals(N, reader.getEntries());
64      assertEquals(N, trailer.getEntryCount());
65      assertEquals(1, trailer.getVersion());
66      assertEquals(Compression.Algorithm.GZ, trailer.getCompressionCodec());
67  
68      for (boolean pread : new boolean[] { false, true }) {
69        int totalDataSize = 0;
70        int n = 0;
71  
72        HFileScanner scanner = reader.getScanner(false, pread);
73        assertTrue(scanner.seekTo());
74        do {
75          totalDataSize += scanner.getKey().limit() + scanner.getValue().limit()
76              + Bytes.SIZEOF_INT * 2;
77          ++n;
78        } while (scanner.next());
79  
80        // Add magic record sizes, one per data block.
81        totalDataSize += 8 * trailer.getDataIndexCount();
82  
83        assertEquals(N, n);
84        assertEquals(trailer.getTotalUncompressedBytes(), totalDataSize);
85      }
86      reader.close();
87    }
88  
89  }