1   /*
2    * Copyright 2009 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.regionserver;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.hadoop.hbase.*;
28  import org.apache.hadoop.hbase.regionserver.ScanQueryMatcher.MatchCode;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(SmallTests.class)
33  public class TestScanWildcardColumnTracker extends HBaseTestCase {
34  
35    final static int VERSIONS = 2;
36  
37    public void testCheckColumn_Ok() throws IOException {
38      ScanWildcardColumnTracker tracker =
39        new ScanWildcardColumnTracker(0, VERSIONS, Long.MIN_VALUE);
40  
41      //Create list of qualifiers
42      List<byte[]> qualifiers = new ArrayList<byte[]>();
43      qualifiers.add(Bytes.toBytes("qualifer1"));
44      qualifiers.add(Bytes.toBytes("qualifer2"));
45      qualifiers.add(Bytes.toBytes("qualifer3"));
46      qualifiers.add(Bytes.toBytes("qualifer4"));
47  
48      //Setting up expected result
49      List<MatchCode> expected = new ArrayList<MatchCode>();
50      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
51      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
52      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
53      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
54  
55      List<ScanQueryMatcher.MatchCode> actual = new ArrayList<MatchCode>();
56  
57      for(byte [] qualifier : qualifiers) {
58        ScanQueryMatcher.MatchCode mc = ScanQueryMatcher.checkColumn(tracker, qualifier, 0,
59            qualifier.length, 1, KeyValue.Type.Put.getCode(), false);
60        actual.add(mc);
61      }
62  
63      //Compare actual with expected
64      for(int i=0; i<expected.size(); i++) {
65        assertEquals(expected.get(i), actual.get(i));
66      }
67    }
68  
69    public void testCheckColumn_EnforceVersions() throws IOException {
70      ScanWildcardColumnTracker tracker =
71        new ScanWildcardColumnTracker(0, VERSIONS, Long.MIN_VALUE);
72  
73      //Create list of qualifiers
74      List<byte[]> qualifiers = new ArrayList<byte[]>();
75      qualifiers.add(Bytes.toBytes("qualifer1"));
76      qualifiers.add(Bytes.toBytes("qualifer1"));
77      qualifiers.add(Bytes.toBytes("qualifer1"));
78      qualifiers.add(Bytes.toBytes("qualifer2"));
79  
80      //Setting up expected result
81      List<ScanQueryMatcher.MatchCode> expected = new ArrayList<MatchCode>();
82      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
83      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
84      expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
85      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
86  
87      List<MatchCode> actual = new ArrayList<ScanQueryMatcher.MatchCode>();
88  
89      long timestamp = 0;
90      for(byte [] qualifier : qualifiers) {
91        MatchCode mc = ScanQueryMatcher.checkColumn(tracker, qualifier, 0, qualifier.length,
92            ++timestamp, KeyValue.Type.Put.getCode(), false);
93        actual.add(mc);
94      }
95  
96      //Compare actual with expected
97      for(int i=0; i<expected.size(); i++) {
98        assertEquals(expected.get(i), actual.get(i));
99      }
100   }
101 
102   public void DisabledTestCheckColumn_WrongOrder() {
103     ScanWildcardColumnTracker tracker =
104       new ScanWildcardColumnTracker(0, VERSIONS, Long.MIN_VALUE);
105 
106     //Create list of qualifiers
107     List<byte[]> qualifiers = new ArrayList<byte[]>();
108     qualifiers.add(Bytes.toBytes("qualifer2"));
109     qualifiers.add(Bytes.toBytes("qualifer1"));
110 
111     boolean ok = false;
112 
113     try {
114       for(byte [] qualifier : qualifiers) {
115         ScanQueryMatcher.checkColumn(tracker, qualifier, 0, qualifier.length, 1,
116             KeyValue.Type.Put.getCode(), false);
117       }
118     } catch (Exception e) {
119       ok = true;
120     }
121 
122     assertEquals(true, ok);
123   }
124 
125 
126   @org.junit.Rule
127   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
128     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
129 }
130