@Test
  public void testSeekBefore() throws Exception {
    Path p = makeNewFile();
    FileSystem fs = TEST_UTIL.getTestFileSystem();
    Configuration conf = TEST_UTIL.getConfiguration();
    int[] bucketSizes = { 512, 2048, 4096, 64 * 1024, 128 * 1024 };
    BucketCache bucketcache =
      new BucketCache("offheap", 128 * 1024 * 1024, 64 * 1024, bucketSizes, 5, 64 * 100, null);

    HFile.Reader reader = HFile.createReader(fs, p, new CacheConfig(conf, bucketcache), true, conf);

    // warm cache
    HFileScanner scanner = reader.getScanner(conf, true, true);
    scanner.seekTo(toKV("i"));
    assertEquals("i", toRowStr(scanner.getCell()));
    scanner.close();

    while (bucketcache.getBlockCount() <= 0) {
      Thread.sleep(10);
    }

    // reopen again.
    scanner = reader.getScanner(conf, true, true);
    scanner.seekTo(toKV("i"));
    assertEquals("i", toRowStr(scanner.getCell()));
    scanner.seekBefore(toKV("i"));
    assertEquals("g", toRowStr(scanner.getCell()));
    scanner.close();

    for (CachedBlock cachedBlock : Lists.newArrayList(bucketcache)) {
      BlockCacheKey cacheKey =
        new BlockCacheKey(cachedBlock.getFilename(), cachedBlock.getOffset());
      int refCount = bucketcache.getRpcRefCount(cacheKey);
      assertEquals(0, refCount);
    }

    // case 2
    scanner = reader.getScanner(conf, true, true);
    scanner.seekTo(toKV("i"));
    assertEquals("i", toRowStr(scanner.getCell()));
    scanner.seekBefore(toKV("c"));
    scanner.close();
    for (CachedBlock cachedBlock : Lists.newArrayList(bucketcache)) {
      BlockCacheKey cacheKey =
        new BlockCacheKey(cachedBlock.getFilename(), cachedBlock.getOffset());
      int refCount = bucketcache.getRpcRefCount(cacheKey);
      assertEquals(0, refCount);
    }

    reader.close();

    // clear bucketcache
    for (CachedBlock cachedBlock : Lists.newArrayList(bucketcache)) {
      BlockCacheKey cacheKey =
        new BlockCacheKey(cachedBlock.getFilename(), cachedBlock.getOffset());
      bucketcache.evictBlock(cacheKey);
    }
    bucketcache.shutdown();

    deleteTestDir(fs);
  }
Test that we only count block size once per block while scanning
hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFileReaderImpl.java