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.querymatcher;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.fail;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026import org.apache.hadoop.hbase.CellComparatorImpl;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.KeyValue;
029import org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode;
030import org.apache.hadoop.hbase.testclassification.RegionServerTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category({ RegionServerTests.class, SmallTests.class })
038public class TestScanWildcardColumnTracker {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042      HBaseClassTestRule.forClass(TestScanWildcardColumnTracker.class);
043
044  final static int VERSIONS = 2;
045
046  @Test
047  public void testCheckColumnOk() throws IOException {
048    ScanWildcardColumnTracker tracker = new ScanWildcardColumnTracker(
049        0, VERSIONS, Long.MIN_VALUE, CellComparatorImpl.COMPARATOR);
050
051    // Create list of qualifiers
052    List<byte[]> qualifiers = new ArrayList<>(4);
053    qualifiers.add(Bytes.toBytes("qualifier1"));
054    qualifiers.add(Bytes.toBytes("qualifier2"));
055    qualifiers.add(Bytes.toBytes("qualifier3"));
056    qualifiers.add(Bytes.toBytes("qualifier4"));
057
058    // Setting up expected result
059    List<MatchCode> expected = new ArrayList<>(4);
060    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
061    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
062    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
063    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
064
065    List<ScanQueryMatcher.MatchCode> actual = new ArrayList<>(qualifiers.size());
066
067    for (byte[] qualifier : qualifiers) {
068      ScanQueryMatcher.MatchCode mc = ScanQueryMatcher.checkColumn(tracker, qualifier, 0,
069        qualifier.length, 1, KeyValue.Type.Put.getCode(), false);
070      actual.add(mc);
071    }
072
073    // Compare actual with expected
074    for (int i = 0; i < expected.size(); i++) {
075      assertEquals(expected.get(i), actual.get(i));
076    }
077  }
078
079  @Test
080  public void testCheckColumnEnforceVersions() throws IOException {
081    ScanWildcardColumnTracker tracker = new ScanWildcardColumnTracker(
082        0, VERSIONS, Long.MIN_VALUE, CellComparatorImpl.COMPARATOR);
083
084    // Create list of qualifiers
085    List<byte[]> qualifiers = new ArrayList<>(4);
086    qualifiers.add(Bytes.toBytes("qualifier1"));
087    qualifiers.add(Bytes.toBytes("qualifier1"));
088    qualifiers.add(Bytes.toBytes("qualifier1"));
089    qualifiers.add(Bytes.toBytes("qualifier2"));
090
091    // Setting up expected result
092    List<ScanQueryMatcher.MatchCode> expected = new ArrayList<>(4);
093    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
094    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
095    expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
096    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
097
098    List<MatchCode> actual = new ArrayList<>(qualifiers.size());
099
100    long timestamp = 0;
101    for (byte[] qualifier : qualifiers) {
102      MatchCode mc = ScanQueryMatcher.checkColumn(tracker, qualifier, 0, qualifier.length,
103        ++timestamp, KeyValue.Type.Put.getCode(), false);
104      actual.add(mc);
105    }
106
107    // Compare actual with expected
108    for (int i = 0; i < expected.size(); i++) {
109      assertEquals(expected.get(i), actual.get(i));
110    }
111  }
112
113  @Test
114  public void DisabledTestCheckColumnWrongOrder() {
115    ScanWildcardColumnTracker tracker = new ScanWildcardColumnTracker(
116        0, VERSIONS, Long.MIN_VALUE, CellComparatorImpl.COMPARATOR);
117
118    // Create list of qualifiers
119    List<byte[]> qualifiers = new ArrayList<>(2);
120    qualifiers.add(Bytes.toBytes("qualifier2"));
121    qualifiers.add(Bytes.toBytes("qualifier1"));
122
123    try {
124      for (byte[] qualifier : qualifiers) {
125        ScanQueryMatcher.checkColumn(tracker, qualifier, 0, qualifier.length, 1,
126          KeyValue.Type.Put.getCode(), false);
127      }
128      fail();
129    } catch (IOException e) {
130      // expected
131    }
132  }
133}