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.regionserver;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import org.apache.commons.lang3.RandomStringUtils;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.Get;
029import org.apache.hadoop.hbase.client.Put;
030import org.apache.hadoop.hbase.filter.TimestampsFilter;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.testclassification.RegionServerTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
040
041@Category({RegionServerTests.class, LargeTests.class})
042public class TestTimestampFilterSeekHint {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046      HBaseClassTestRule.forClass(TestTimestampFilterSeekHint.class);
047
048  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
049  private final static String RK = "myRK";
050  private final static byte[] RK_BYTES = Bytes.toBytes(RK);
051
052  private final static String FAMILY = "D";
053  private final static byte[] FAMILY_BYTES = Bytes.toBytes(FAMILY);
054
055  private final static String QUAL = "0";
056  private final static byte[] QUAL_BYTES = Bytes.toBytes(QUAL);
057
058  public static final int MAX_VERSIONS = 50000;
059  private HRegion region;
060  private int regionCount = 0;
061
062  @Test
063  public void testGetSeek() throws IOException {
064    StoreFileScanner.instrument();
065    prepareRegion();
066
067    Get g = new Get(RK_BYTES);
068    final TimestampsFilter timestampsFilter = new TimestampsFilter(ImmutableList.of(5L), true);
069    g.setFilter(timestampsFilter);
070    final long initialSeekCount = StoreFileScanner.getSeekCount();
071    region.get(g);
072    final long finalSeekCount = StoreFileScanner.getSeekCount();
073
074    /*
075      Make sure there's more than one.
076      Aka one seek to get to the row, and one to get to the time.
077    */
078    assertTrue(finalSeekCount >= initialSeekCount + 3 );
079  }
080
081  @Test
082  public void testGetDoesntSeekWithNoHint() throws IOException {
083    StoreFileScanner.instrument();
084    prepareRegion();
085
086    Get g = new Get(RK_BYTES);
087    g.setFilter(new TimestampsFilter(ImmutableList.of(5L)));
088    final long initialSeekCount = StoreFileScanner.getSeekCount();
089    region.get(g);
090    final long finalSeekCount = StoreFileScanner.getSeekCount();
091
092    assertTrue(finalSeekCount >= initialSeekCount );
093    assertTrue(finalSeekCount < initialSeekCount + 3);
094  }
095
096  @Before
097  public void prepareRegion() throws IOException {
098    ColumnFamilyDescriptor columnFamilyDescriptor =
099      ColumnFamilyDescriptorBuilder
100        .newBuilder(Bytes.toBytes(FAMILY))
101        .setBlocksize(1024)
102        .setMaxVersions(MAX_VERSIONS)
103        .build();
104    region = TEST_UTIL
105      .createTestRegion("TestTimestampFilterSeekHint" + regionCount++, columnFamilyDescriptor);
106
107    for (long i = 0; i <MAX_VERSIONS - 2; i++) {
108      Put p = new Put(RK_BYTES, i);
109      p.addColumn(FAMILY_BYTES, QUAL_BYTES, Bytes.toBytes(RandomStringUtils.randomAlphabetic(255)));
110      region.put(p);
111    }
112    region.flush(true);
113  }
114}