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