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