View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.*;
27  import org.apache.hadoop.hbase.regionserver.ScanQueryMatcher.MatchCode;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.junit.experimental.categories.Category;
30  
31  @Category(SmallTests.class)
32  public class TestScanWildcardColumnTracker extends HBaseTestCase {
33  
34    final static int VERSIONS = 2;
35  
36    public void testCheckColumn_Ok() throws IOException {
37      ScanWildcardColumnTracker tracker =
38        new ScanWildcardColumnTracker(0, VERSIONS, Long.MIN_VALUE);
39  
40      //Create list of qualifiers
41      List<byte[]> qualifiers = new ArrayList<byte[]>();
42      qualifiers.add(Bytes.toBytes("qualifer1"));
43      qualifiers.add(Bytes.toBytes("qualifer2"));
44      qualifiers.add(Bytes.toBytes("qualifer3"));
45      qualifiers.add(Bytes.toBytes("qualifer4"));
46  
47      //Setting up expected result
48      List<MatchCode> expected = new ArrayList<MatchCode>();
49      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
50      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
51      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
52      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
53  
54      List<ScanQueryMatcher.MatchCode> actual = new ArrayList<MatchCode>();
55  
56      for(byte [] qualifier : qualifiers) {
57        ScanQueryMatcher.MatchCode mc = tracker.checkColumn(qualifier, 0,
58            qualifier.length, 1, KeyValue.Type.Put.getCode(), false);
59        actual.add(mc);
60      }
61  
62      //Compare actual with expected
63      for(int i=0; i<expected.size(); i++) {
64        assertEquals(expected.get(i), actual.get(i));
65      }
66    }
67  
68    public void testCheckColumn_EnforceVersions() throws IOException {
69      ScanWildcardColumnTracker tracker =
70        new ScanWildcardColumnTracker(0, VERSIONS, Long.MIN_VALUE);
71  
72      //Create list of qualifiers
73      List<byte[]> qualifiers = new ArrayList<byte[]>();
74      qualifiers.add(Bytes.toBytes("qualifer1"));
75      qualifiers.add(Bytes.toBytes("qualifer1"));
76      qualifiers.add(Bytes.toBytes("qualifer1"));
77      qualifiers.add(Bytes.toBytes("qualifer2"));
78  
79      //Setting up expected result
80      List<ScanQueryMatcher.MatchCode> expected = new ArrayList<MatchCode>();
81      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
82      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
83      expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
84      expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
85  
86      List<MatchCode> actual = new ArrayList<ScanQueryMatcher.MatchCode>();
87  
88      long timestamp = 0;
89      for(byte [] qualifier : qualifiers) {
90        MatchCode mc = tracker.checkColumn(qualifier, 0, qualifier.length,
91            ++timestamp, KeyValue.Type.Put.getCode(), false);
92        actual.add(mc);
93      }
94  
95      //Compare actual with expected
96      for(int i=0; i<expected.size(); i++) {
97        assertEquals(expected.get(i), actual.get(i));
98      }
99    }
100 
101   public void DisabledTestCheckColumn_WrongOrder() {
102     ScanWildcardColumnTracker tracker =
103       new ScanWildcardColumnTracker(0, VERSIONS, Long.MIN_VALUE);
104 
105     //Create list of qualifiers
106     List<byte[]> qualifiers = new ArrayList<byte[]>();
107     qualifiers.add(Bytes.toBytes("qualifer2"));
108     qualifiers.add(Bytes.toBytes("qualifer1"));
109 
110     boolean ok = false;
111 
112     try {
113       for(byte [] qualifier : qualifiers) {
114         tracker.checkColumn(qualifier, 0, qualifier.length, 1,
115             KeyValue.Type.Put.getCode(), false);
116       }
117     } catch (Exception e) {
118       ok = true;
119     }
120 
121     assertEquals(true, ok);
122   }
123 
124 
125 }
126